Strip extra paragraph elements that were created in earlier versions
authorAlexander Ebert <ebert@woltlab.com>
Thu, 4 May 2023 14:06:55 +0000 (16:06 +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/HtmlOutputNodeP.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeP.class.php b/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeP.class.php
new file mode 100644 (file)
index 0000000..8f33381
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+namespace wcf\system\html\output\node;
+
+use wcf\system\html\node\AbstractHtmlNodeProcessor;
+use wcf\util\StringUtil;
+
+/**
+ * Removes empty paragraphs that were used to emulate paragraphs in earlier versions.
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2023 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.0
+ */
+final class HtmlOutputNodeP extends AbstractHtmlOutputNode
+{
+    /**
+     * @inheritDoc
+     */
+    protected $tagName = 'p';
+
+    /**
+     * @inheritDoc
+     */
+    public function process(array $elements, AbstractHtmlNodeProcessor $htmlNodeProcessor)
+    {
+        /** @var \DOMElement $element */
+        foreach ($elements as $element) {
+            if ($element->childElementCount === 1 && $element->firstElementChild) {
+                $child = $element->firstElementChild;
+                if ($child->tagName === 'br' && $child->getAttribute('data-cke-filler') !== 'true') {
+                    // This is most likely a legacy paragraph that was inserted
+                    // in earlier versions and is not longer required. We need
+                    // to verify that there is no other text inside the node
+                    // before removing it.
+                    if (StringUtil::trim($element->textContent) === '') {
+                        $element->remove();
+                    }
+                }
+            }
+        }
+    }
+}