This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace Esun\Translation\Model\Html;
use DOMDocument;
use DOMElement;
use DOMText;
class HtmlFragment
{
/**
* @param array<string, DOMText> $textNodes
* @param array<string, string> $texts
*/
public function __construct(
private readonly DOMDocument $document,
private readonly DOMElement $root,
private readonly array $textNodes,
private readonly array $texts,
private readonly array $directivePlaceholders = []
) {
}
/**
* @return array<string, string>
*/
public function getTexts(): array
{
return $this->texts;
}
/**
* @param array<string, string> $translations
*/
public function applyTranslations(array $translations): string
{
foreach ($this->textNodes as $id => $node) {
if (array_key_exists($id, $translations)) {
$node->nodeValue = $translations[$id];
}
}
$html = '';
foreach ($this->root->childNodes as $childNode) {
$html .= $this->document->saveHTML($childNode);
}
$restored = $this->directivePlaceholders === [] ? $html : strtr($html, $this->directivePlaceholders);
return $this->normalizeMagentoDirectiveQuotes($restored);
}
private function normalizeMagentoDirectiveQuotes(string $html): string
{
return preg_replace_callback(
'/\{\{.*?\}\}/s',
static function (array $matches): string {
return str_ireplace(
['&quot;', '&#34;', '&#x22;', '&apos;', '&#39;', '&#x27;'],
['"', '"', '"', "'", "'", "'"],
$matches[0]
);
},
$html
) ?? $html;
}
}
@@ -0,0 +1,122 @@
<?php
declare(strict_types=1);
namespace Esun\Translation\Model\Html;
use DOMDocument;
use DOMNode;
use DOMText;
use DOMXPath;
class HtmlTextExtractor
{
private const ROOT_ID = 'esun-translation-root';
private const DIRECTIVE_TOKEN_PREFIX = '__ESUN_HTML_DIRECTIVE_';
public function extract(string $html): HtmlFragment
{
[$protectedHtml, $directivePlaceholders] = $this->protectMagentoDirectives($html);
$document = $this->createDocument($protectedHtml);
$xpath = new DOMXPath($document);
$query = sprintf(
'//*[@id="%s"]//text()[normalize-space(.) != "" and not(ancestor::script) and not(ancestor::style) and not(ancestor::code) and not(ancestor::pre)]',
self::ROOT_ID
);
$nodes = $xpath->query($query);
$textNodes = [];
$texts = [];
$index = 0;
if ($nodes !== false) {
foreach ($nodes as $node) {
if (!$node instanceof DOMText) {
continue;
}
$text = $this->restoreMagentoDirectives($node->nodeValue ?? '', $directivePlaceholders);
if (trim($text) === '') {
continue;
}
$id = 'html_' . $index++;
$textNodes[$id] = $node;
$texts[$id] = $text;
}
}
$root = $document->getElementById(self::ROOT_ID);
if (!$root) {
throw new \RuntimeException('Unable to parse HTML fragment.');
}
return new HtmlFragment($document, $root, $textNodes, $texts, $directivePlaceholders);
}
private function createDocument(string $html): DOMDocument
{
$document = new DOMDocument('1.0', 'UTF-8');
$wrappedHtml = sprintf(
'<?xml encoding="UTF-8"><div id="%s">%s</div>',
self::ROOT_ID,
$html
);
$previous = libxml_use_internal_errors(true);
$loaded = $document->loadHTML($wrappedHtml, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
libxml_clear_errors();
libxml_use_internal_errors($previous);
if (!$loaded) {
throw new \RuntimeException('Unable to parse HTML fragment.');
}
return $document;
}
/**
* @return array{0: string, 1: array<string, string>}
*/
private function protectMagentoDirectives(string $html): array
{
$placeholders = [];
$index = 0;
$protected = preg_replace_callback(
'/\{\{.*?\}\}/s',
function (array $matches) use (&$placeholders, &$index): string {
$token = self::DIRECTIVE_TOKEN_PREFIX . $index++ . '__';
$placeholders[$token] = $this->normalizeMagentoDirectiveQuotes($matches[0]);
return $token;
},
$html
);
return [$protected ?? $html, $placeholders];
}
/**
* @param array<string, string> $placeholders
*/
private function restoreMagentoDirectives(string $html, array $placeholders): string
{
$restored = $placeholders === [] ? $html : strtr($html, $placeholders);
return $this->normalizeMagentoDirectiveQuotes($restored);
}
private function normalizeMagentoDirectiveQuotes(string $html): string
{
return preg_replace_callback(
'/\{\{.*?\}\}/s',
static function (array $matches): string {
return str_ireplace(
['&quot;', '&#34;', '&#x22;', '&apos;', '&#39;', '&#x27;'],
['"', '"', '"', "'", "'", "'"],
$matches[0]
);
},
$html
) ?? $html;
}
}
@@ -0,0 +1,109 @@
<?php
declare(strict_types=1);
namespace Esun\Translation\Model\Html;
use DOMDocument;
use DOMElement;
use DOMXPath;
class HtmlTranslationValidator
{
/**
* DOMDocument::loadHTML uses an HTML4 parser and reports safe HTML5/Page Builder tags as invalid.
*/
private const HTML5_ALLOWED_INVALID_TAGS = [
'article',
'aside',
'audio',
'canvas',
'figcaption',
'figure',
'footer',
'header',
'main',
'nav',
'picture',
'section',
'source',
'summary',
'template',
'time',
'video',
];
/**
* @return string[]
*/
public function validate(string $html): array
{
$errors = [];
if (substr_count($html, '{{') !== substr_count($html, '}}')) {
$errors[] = 'Magento directive braces are unbalanced.';
}
$document = new DOMDocument('1.0', 'UTF-8');
$previous = libxml_use_internal_errors(true);
$loaded = $document->loadHTML(
'<?xml encoding="UTF-8"><div>' . $html . '</div>',
LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED
);
$libxmlErrors = libxml_get_errors();
libxml_clear_errors();
libxml_use_internal_errors($previous);
if (!$loaded) {
return ['HTML cannot be parsed.'];
}
foreach ($libxmlErrors as $error) {
if ($error->level >= LIBXML_ERR_ERROR) {
if ($this->isAllowedHtml5InvalidTagMessage(trim($error->message))) {
continue;
}
$errors[] = trim($error->message);
}
}
$xpath = new DOMXPath($document);
foreach (['script', 'iframe', 'object', 'embed'] as $tag) {
$nodes = $xpath->query('//' . $tag);
if ($nodes !== false && $nodes->length > 0) {
$errors[] = sprintf('Disallowed <%s> tag found.', $tag);
}
}
$allNodes = $xpath->query('//*');
if ($allNodes !== false) {
foreach ($allNodes as $node) {
if (!$node instanceof DOMElement || !$node->hasAttributes()) {
continue;
}
foreach ($node->attributes as $attribute) {
$name = strtolower($attribute->nodeName);
$value = strtolower(trim($attribute->nodeValue ?? ''));
if (str_starts_with($name, 'on')) {
$errors[] = sprintf('Disallowed event attribute "%s" found.', $name);
}
if (in_array($name, ['href', 'src'], true) && str_starts_with($value, 'javascript:')) {
$errors[] = sprintf('Disallowed javascript URL in "%s" found.', $name);
}
}
}
}
return array_values(array_unique(array_filter($errors)));
}
private function isAllowedHtml5InvalidTagMessage(string $message): bool
{
if (!preg_match('/^Tag ([a-z0-9:-]+) invalid$/i', $message, $matches)) {
return false;
}
return in_array(strtolower($matches[1]), self::HTML5_ALLOWED_INVALID_TAGS, true);
}
}