<commit_before><?php

/*
 * HTML Server Components Compiler
 * http://ivopetkov.com/b/html-server-components/
 * Copyright 2015-2016, <NAME>
 * Free to use under the MIT license.
 */

/**
 * The class that processes components
 */
class HTMLServerComponentsCompiler
{

    /**
     * 
     */
    const VERSION = '0.2.0';

    /**
     *
     * @var array 
     */
    private $aliases = [];

    /**
     * Registers an alias
     * @param string $alias
     * @param string $original
     */
    function addAlias($alias, $original)
    {
        $this->aliases[$alias] = $original;
    }

    /**
     * Process (merge) components
     * @param string $html
     * @return string
     */
    public function process($html)
    {
        $domDocument = $this->getDOMDocument($html);
        $components = $domDocument->getElementsByTagName('component');
        $componentsCount = $components->length;
        if ($componentsCount > 0) {
            $domBodyElement = $domDocument->getElementsByTagName('body')->item(0);
            $domHeadElement = $domDocument->getElementsByTagName('head')->item(0);
            for ($i = 0; $i < $componentsCount; $i++) {
                $component = $components->item(0); // the component is removed later
                if (isset($component->parentNode)) {

                    $attributes = $this->getDOMElementAttributes($component);

                    if (isset($attributes['src'])) {
                        $srcAttributeValue = $attributes['src'];
                        if (isset($this->aliases[$srcAttributeValue])) {
                            $sourceParts = explode(':', $this->aliases[$srcAttributeValue], 2);
                        } else {
                            $sourceParts = explode(':', $srcAttributeValue, 2);
                        }
                        if (sizeof($sourceParts) === 2) {
                            $scheme = $sourceParts[0];
                            if ($scheme === 'data') {
                                $componentHTML = $this->processData($sourceParts[1]);
                            } elseif ($scheme === 'file') {
                                $componentHTML = $this->processFile($sourceParts[1], $attributes, $this->getInnerHTML($component));
                            } else {
                                throw new \Exception('URI scheme not valid!' . $domDocument->saveHTML($component));
                            }
                        } else {
                            throw new \Exception('URI scheme not found!' . $domDocument->saveHTML($component));
                        }
                    } else {
                        throw new \Exception('Component src attribute missing! ' . $domDocument->saveHTML($component));
                    }

                    $componentDOMDocument = $this->getDOMDocument($componentHTML);

                    $componentBodyElement = $componentDOMDocument->getElementsByTagName('body')->item(0);
                    $this->trimDOMElement($componentBodyElement);
                    $componentBodyChildrenCount = $componentBodyElement->childNodes->length;

                    // keep scripts at the bottom
                    $scriptElementsMovedIndexes = [];
                    for ($j = $componentBodyChildrenCount - 1; $j >= 0; $j--) {
                        $componentChild = $componentBodyElement->childNodes->item($j);
                        if ($componentChild instanceof DOMText) {
                            continue;
                        }
                        if ($componentChild instanceof DOMElement && $componentChild->tagName === 'script') {
                            $componentChildAttributes = $this->getDOMElementAttributes($componentChild);
                            if (!isset($componentChildAttributes['type']) || $componentChildAttributes['type'] === '' || $componentChildAttributes['type'] === 'text/javascript') {
                                $domBodyElement->appendChild($domDocument->importNode($componentChild, true));
                                $scriptElementsMovedIndexes[$j] = 1;
                            }
                        } else {
                            break;
                        }
                    }

                    for ($j = 0; $j < $componentBodyChildrenCount; $j++) {
                        if (!isset($scriptElementsMovedIndexes[$j])) {
                            $component->parentNode->insertBefore($domDocument->importNode($componentBodyElement->childNodes->item($j), true), $component);
                        }
                    }

                    // add head elements
                    $componentHeadElement = $componentDOMDocument->getElementsByTagName('head')->item(0);
                    $componentHeadElementChildrenCount = $componentHeadElement->childNodes->length;
                    for ($j = 0; $j < $componentHeadElementChildrenCount; $j++) {
                        $domHeadElement->appendChild($domDocument->importNode($componentHeadElement->childNodes->item($j), true));
                    }

                    // remove title tags if more than one - the last one stays
                    $titleElements = $domHeadElement->getElementsByTagName('title');
                    $titleElementsCount = $titleElements->length;
                    if ($titleElementsCount > 1) {
                        for ($j = $titleElementsCount - 2; $j >= 0; $j--) {
                            $domHeadElement->removeChild($titleElements->item($j));
                        }
                    }

                    // remove the component tag
                    $component->parentNode->removeChild($component);
                }
            }
        }

        $domDocument2 = new DOMDocument;
        $domDocument2->formatOutput = false;
        set_error_handler([$this, "handleInvalidHTMLErrors"]);
        $domDocument2->loadHTML($domDocument->saveHTML());<commit_msg>Removed auto move scripts to the bottom. It will be an option in the future.<commit_after><?php

/*
 * HTML Server Components Compiler
 * http://ivopetkov.com/b/html-server-components/
 * Copyright 2015-2016, <NAME>
 * Free to use under the MIT license.
 */

/**
 * The class that processes components
 */
class HTMLServerComponentsCompiler
{

    /**
     * 
     */
    const VERSION = '0.2.0';

    /**
     *
     * @var array 
     */
    private $aliases = [];

    /**
     * Registers an alias
     * @param string $alias
     * @param string $original
     */
    function addAlias($alias, $original)
    {
        $this->aliases[$alias] = $original;
    }

    /**
     * Process (merge) components
     * @param string $html
     * @return string
     */
    public function process($html)
    {
        $domDocument = $this->getDOMDocument($html);
        $components = $domDocument->getElementsByTagName('component');
        $componentsCount = $components->length;
        if ($componentsCount > 0) {
            $domBodyElement = $domDocument->getElementsByTagName('body')->item(0);
            $domHeadElement = $domDocument->getElementsByTagName('head')->item(0);
            for ($i = 0; $i < $componentsCount; $i++) {
                $component = $components->item(0); // the component is removed later
                if (isset($component->parentNode)) {

                    $attributes = $this->getDOMElementAttributes($component);

                    if (isset($attributes['src'])) {
                        $srcAttributeValue = $attributes['src'];
                        if (isset($this->aliases[$srcAttributeValue])) {
                            $sourceParts = explode(':', $this->aliases[$srcAttributeValue], 2);
                        } else {
                            $sourceParts = explode(':', $srcAttributeValue, 2);
                        }
                        if (sizeof($sourceParts) === 2) {
                            $scheme = $sourceParts[0];
                            if ($scheme === 'data') {
                                $componentHTML = $this->processData($sourceParts[1]);
                            } elseif ($scheme === 'file') {
                                $componentHTML = $this->processFile($sourceParts[1], $attributes, $this->getInnerHTML($component));
                            } else {
                                throw new \Exception('URI scheme not valid!' . $domDocument->saveHTML($component));
                            }
                        } else {
                            throw new \Exception('URI scheme not found!' . $domDocument->saveHTML($component));
                        }
                    } else {
                        throw new \Exception('Component src attribute missing! ' . $domDocument->saveHTML($component));
                    }

                    $componentDOMDocument = $this->getDOMDocument($componentHTML);

                    $componentBodyElement = $componentDOMDocument->getElementsByTagName('body')->item(0);
                    $this->trimDOMElement($componentBodyElement);
                    $componentBodyChildrenCount = $componentBodyElement->childNodes->length;

                    // keep scripts at the bottom
//                    $scriptElementsMovedIndexes = [];
//                    for ($j = $componentBodyChildrenCount - 1; $j >= 0; $j--) {
//                        $componentChild = $componentBodyElement->childNodes->item($j);
//                        if ($componentChild instanceof DOMText) {
//                            continue;
//                        }
//                        if ($componentChild instanceof DOMElement && $componentChild->tagName === 'script') {
//                            $componentChildAttributes = $this->getDOMElementAttributes($componentChild);
//                            if (!isset($componentChildAttributes['type']) || $componentChildAttributes['type'] === '' || $componentChildAttributes['type'] === 'text/javascript') {
//                                $domBodyElement->appendChild($domDocument->importNode($componentChild, true));
//                                $scriptElementsMovedIndexes[$j] = 1;
//                            }
//                        } else {
//                            break;
//                        }
//                    }

                    for ($j = 0; $j < $componentBodyChildrenCount; $j++) {
                        //if (!isset($scriptElementsMovedIndexes[$j])) {
                        $component->parentNode->insertBefore($domDocument->importNode($componentBodyElement->childNodes->item($j), true), $component);
                        //}
                    }

                    // add head elements
                    $componentHeadElement = $componentDOMDocument->getElementsByTagName('head')->item(0);
                    $componentHeadElementChildrenCount = $componentHeadElement->childNodes->length;
                    for ($j = 0; $j < $componentHeadElementChildrenCount; $j++) {
                        $domHeadElement->appendChild($domDocument->importNode($componentHeadElement->childNodes->item($j), true));
                    }

                    // remove title tags if more than one - the last one stays
                    $titleElements = $domHeadElement->getElementsByTagName('title');
                    $titleElementsCount = $titleElements->length;
                    if ($titleElementsCount > 1) {
                        for ($j = $titleElementsCount - 2; $j >= 0; $j--) {
                            $domHeadElement->removeChild($titleElements->item($j));
                        }
                    }

                    // remove the component tag
                    $component->parentNode->removeChild($component);
                }
            }
        }

        $domDocument2 = new DOMDocument;
        $domDocument2->formatOutput = false;
        set_error_handler([$this, "handleInvalidHTMLErrors"]);
        $domDocument2->loadHTML($domDocument->saveHTML());
{'fraction_non_alphanumeric': 0.1265105425452121, 'fraction_numerical': 0.005833819484957079, 'mean_word_length': 1.5194205332773463, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2631877', 'n_tokens_mistral': 2863, 'n_tokens_neox': 2693, 'n_words': 517}