<commit_before><?php
/*
Copyright 2015 Lcf.vs
 -
Released under the MIT license
 -
https://github.com/Lcfvs/DOMArch
*/
namespace DOMArch\View;

use DOMArch\Request;
use Exception;

use PHPDOM;
use PHPDOM\HTML\NodeList;
use PHPDOM\HTML\SelectorCache;

abstract class HTML
    extends PHPDOM\HTML\Document
{
    protected $_nodeClasses = [
        '\\DOMNode' => HTML\Node::class,
        '\\DOMElement' => HTML\Element::class,
        '\\DOMText' => HTML\Text::class,
        '\\DOMComment' => '\\PHPDOM\\HTML\\Comment',
        '\\DOMDocumentFragment' => '\\PHPDOM\\HTML\\DocumentFragment'
    ];

    public function __construct()
    {
        parent::__construct();

        SelectorCache::load();
    }

    abstract public function getTranslator();

    abstract public function getUrlTranslator();

    abstract protected function url(
        array $params = [],
        string $fragment = ''
    );

    public function translate(
        $translatable,
        ...$params
    )
    {
        $locale = $this->lang;
        $translator = $this->getTranslator();

        if (is_string($translatable)) {
            $translatable = $this->createTextNode($translatable);
        }

        switch ($translatable->nodeType) {
            case 1: {
                $element = null;
                $locale = $translatable->getAttribute('lang') ?: $locale;
                $children = $translatable->query('.//text()[normalize-space()]')
                    ->toArray();

                foreach ($children as $child) {
                    $text = trim($child->nodeValue);

                    if (!empty($text)) {
                        $element = $child;
                        break;
                    }
                }

                $listener = function(
                    $translation,
                    $id,
                    $is_translated
                ) use ($element) {
                    if (!$is_translated) {
                        $parent = $element->parentNode;
                        $parent->classList->add('untranslated');
                        $parent->dataset->translationId = $id;
                    }

                    $element->nodeValue = $translation;<commit_msg>translation fix<commit_after><?php
/*
Copyright 2015 Lcf.vs
 -
Released under the MIT license
 -
https://github.com/Lcfvs/DOMArch
*/
namespace DOMArch\View;

use DOMArch\Request;
use Exception;

use PHPDOM;
use PHPDOM\HTML\NodeList;
use PHPDOM\HTML\SelectorCache;

abstract class HTML
    extends PHPDOM\HTML\Document
{
    protected $_nodeClasses = [
        '\\DOMNode' => HTML\Node::class,
        '\\DOMElement' => HTML\Element::class,
        '\\DOMText' => HTML\Text::class,
        '\\DOMComment' => '\\PHPDOM\\HTML\\Comment',
        '\\DOMDocumentFragment' => '\\PHPDOM\\HTML\\DocumentFragment'
    ];

    public function __construct()
    {
        parent::__construct();

        SelectorCache::load();
    }

    abstract public function getTranslator();

    abstract public function getUrlTranslator();

    abstract protected function url(
        array $params = [],
        string $fragment = ''
    );

    public function translate(
        $translatable,
        ...$params
    )
    {
        $locale = $this->lang;
        $translator = $this->getTranslator();

        if (is_string($translatable)) {
            $translatable = $this->createTextNode($translatable);
        }

        switch ($translatable->nodeType) {
            case 1: {
                $element = null;
                $locale = $translatable->getAttribute('lang') ?: $locale;
                $children = $translatable->query('.//text()[normalize-space()]')
                    ->toArray();

                foreach ($children as $child) {
                    $text = trim($child->nodeValue);

                    if (!empty($text)) {
                        $element = $child;
                        break;
                    }
                }

                $listener = function(
                    $translation,
                    $id,
                    $is_translated
                ) use ($element) {
                    $parent = $element->parentNode;
                    $document = $parent->ownerDocument;
                    $text_node = $document->createTextNode($translation);

                    if (!$is_translated) {
                        $parent->classList->add('untranslated');
                        $parent->dataset->translationId = $id;
                    }

                    if ($parent->nodeName === 'title') {
                        $element->nodeValue = $translation;

                        return;
                    }

                    $parent->insert($text_node, $element);
                    //$element->remove();
{'fraction_non_alphanumeric': 0.1315624346371052, 'fraction_numerical': 0.002091612633340305, 'mean_word_length': 1.4880332986472424, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, 'https://': 2, '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': '3062560', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1209, 'n_words': 235}