From a15998b6e2dc749a5f8c0bd0c711d548738d6468 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 12 Apr 2021 22:01:30 +0200 Subject: [PATCH] Use `XPath::query()` for its superior performance --- .../node/HtmlOutputNodeProcessor.class.php | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeProcessor.class.php b/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeProcessor.class.php index 866ba51c1f..d2e54f1740 100644 --- a/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeProcessor.class.php +++ b/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeProcessor.class.php @@ -91,22 +91,15 @@ class HtmlOutputNodeProcessor extends AbstractHtmlNodeProcessor { $this->invokeNodeHandlers('wcf\system\html\output\node\HtmlOutputNode', ['woltlab-metacode']); if ($this->getHtmlProcessor()->removeLinks) { - $links = $this->getDocument()->getElementsByTagName('a'); - while ($links->length) { - DOMUtil::removeNode($links->item(0), true); + foreach ($this->getXPath()->query('//a') as $link) { + DOMUtil::removeNode($link, true); } } if ($this->outputType !== 'text/html') { // convert `

...

` into `...

` $paragraphs = []; - // The DOMNodeList returned by `getElementsByTagName()` is live, causing a performance - // penalty when modifying it a lot. - foreach ($this->getDocument()->getElementsByTagName('p') as $node) { - $paragraphs[] = $node; - } - - foreach ($paragraphs as $paragraph) { + foreach ($this->getXPath()->query('//p') as $paragraph) { $isLastNode = true; $sibling = $paragraph; while ($sibling = $sibling->nextSibling) { @@ -147,7 +140,6 @@ class HtmlOutputNodeProcessor extends AbstractHtmlNodeProcessor { DOMUtil::removeNode($paragraph, true); } - unset($paragraphs); if ($this->outputType === 'text/plain') { // remove all `\n` first @@ -166,10 +158,7 @@ class HtmlOutputNodeProcessor extends AbstractHtmlNodeProcessor { // insert a trailing newline for certain elements, such as `
` or `
  • ` foreach (self::$plainTextNewlineTags as $tagName) { - $elements = $this->getDocument()->getElementsByTagName($tagName); - while ($elements->length) { - $element = $elements->item(0); - + foreach ($this->getXPath()->query("//{$tagName}") as $element) { $newline = $this->getDocument()->createTextNode("\n"); $element->parentNode->insertBefore($newline, $element->nextSibling); DOMUtil::removeNode($element, true); @@ -177,9 +166,8 @@ class HtmlOutputNodeProcessor extends AbstractHtmlNodeProcessor { } // remove all other elements - $elements = $this->getDocument()->getElementsByTagName('*'); - while ($elements->length) { - DOMUtil::removeNode($elements->item(0), true); + foreach ($this->getXPath()->query('//*') as $element) { + DOMUtil::removeNode($element, true); } } } -- 2.20.1