Make the normalizer reusable by passing in the `\DOMXPath` for `normalize()`
authorAlexander Ebert <ebert@woltlab.com>
Mon, 8 May 2023 10:00:04 +0000 (12:00 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 8 May 2023 16:34:04 +0000 (18:34 +0200)
wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeNormalizer.class.php
wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeProcessor.class.php

index 4bf2fdafab9790d34fc98be24b74c0d95f682eb1..85dc4295557cd1a850ff777beab7d0e46d8799f4 100644 (file)
@@ -14,26 +14,22 @@ use wcf\util\DOMUtil;
  */
 final class HtmlOutputNodeNormalizer
 {
-    public function __construct(private readonly \DOMXPath $xpath)
+    public function normalize(\DOMXPath $xpath): void
     {
-    }
-
-    public function normalize(): void
-    {
-        $this->normalizeBr();
+        $this->normalizeBr($xpath);
 
-        $candidates = $this->getPossibleSpacerParagraphs();
+        $candidates = $this->getPossibleSpacerParagraphs($xpath);
         $this->reduceSpacerParagraphs($candidates);
     }
 
     /**
      * @return list<\DOMElement>
      */
-    private function getPossibleSpacerParagraphs(): array
+    private function getPossibleSpacerParagraphs(\DOMXpath $xpath): array
     {
         $paragraphs = [];
 
-        foreach ($this->xpath->query('//p') as $p) {
+        foreach ($xpath->query('//p') as $p) {
             \assert($p instanceof \DOMElement);
 
             if ($p->childNodes->length === 1) {
@@ -96,9 +92,9 @@ final class HtmlOutputNodeNormalizer
         }
     }
 
-    private function normalizeBr(): void
+    private function normalizeBr(\DOMXpath $xpath): void
     {
-        foreach ($this->xpath->query('//br') as $br) {
+        foreach ($xpath->query('//br') as $br) {
             \assert($br instanceof \DOMElement);
 
             $this->unwrapBr($br);
index bf2b41a81e2f373011ac7cdee3548eeca3cf311e..2a6a109505cd1241a6034ca87428f126ff80e7ee 100644 (file)
@@ -97,7 +97,7 @@ class HtmlOutputNodeProcessor extends AbstractHtmlNodeProcessor
             $this->invokeHtmlNode(new HtmlOutputUnfurlUrlNode());
         }
 
-        (new HtmlOutputNodeNormalizer($this->getXPath()))->normalize();
+        (new HtmlOutputNodeNormalizer())->normalize($this->getXPath());
 
         // dynamic node handlers
         $this->invokeNodeHandlers('wcf\system\html\output\node\HtmlOutputNode', ['woltlab-metacode']);