Skip to content
Snippets Groups Projects

Issue #3506366 by mably: Get rid of appendXml

Merged Frank Mably requested to merge issue/term_glossary-3506366:3506366-get-rid-of into 4.2.x
1 file
+ 16
25
Compare changes
  • Side-by-side
  • Inline
@@ -178,7 +178,8 @@ class TermGlossaryManager implements TermGlossaryManagerInterface {
}
}
$total_tags = 0;
$ctx->tags = [];
$ctx->tagIndex = 0;
$html_dom = Html::load($input);
$xpath = new \DOMXPath($html_dom);
@@ -199,8 +200,7 @@ class TermGlossaryManager implements TermGlossaryManagerInterface {
$ctx->currentText = $current_node_text;
$ctx->tags = [];
$ctx->tagIndex = 0;
$last_tag_index = $ctx->tagIndex;
foreach ($term_list as $ctx->termId => $ctx->termArray) {
if ($ctx->isSingleMatch && $this->isTermAlreadySeen($ctx)) {
@@ -214,36 +214,27 @@ class TermGlossaryManager implements TermGlossaryManagerInterface {
}
// Perform replacements only if one or more matches are found.
if ($ctx->tagIndex > 0) {
if ($ctx->tagIndex > $last_tag_index) {
// $originalNode->nodeValue have unescaped HTML entities,
// the following line escapes them back.
$escaped_text = Html::escape($ctx->currentText);
$text_with_tags = preg_replace_callback(
'/\{\{(\d+)}}/',
function ($matches) use (&$ctx) {
$index = (int) $matches[1];
return $ctx->tags[$index] ?? '{{' . $index . '}}';
},
$escaped_text,
);
$replacement_node = $html_dom->createDocumentFragment();
$replacement_node->appendXML($text_with_tags);
$current_text_node->parentNode->replaceChild(
$replacement_node, $current_text_node);
$total_tags += $ctx->tagIndex;
$current_text_node->nodeValue = Html::escape($ctx->currentText);
}
}
if ($total_tags === 0) {
// No replacement has occurred, no need to serialize.
if ($ctx->tagIndex === 0) {
// No replacement has occurred, nothing to do.
return $input;
}
else {
return Html::serialize($html_dom);
// Replace all placeholders by their corresponding tags.
return preg_replace_callback(
'/\{\{(\d+)}}/',
function ($matches) use (&$ctx) {
$index = (int) $matches[1];
return $ctx->tags[$index] ?? '{{' . $index . '}}';
},
Html::serialize($html_dom),
);
}
}
}
Loading