Strip de fact empty paragraphs in messages
authorAlexander Ebert <ebert@woltlab.com>
Fri, 6 Aug 2021 14:44:42 +0000 (16:44 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 6 Aug 2021 14:44:42 +0000 (16:44 +0200)
wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeProcessor.class.php

index 14c910f22fb9c4d62de9245be0090c5ce9115894..db60a8cbc54e034106c8096470c8d335b58bd516 100644 (file)
@@ -103,6 +103,10 @@ class HtmlInputNodeProcessor extends AbstractHtmlNodeProcessor
         'pre',
         'sub',
         'sup',
+        'strong',
+        'del',
+        'em',
+        'u',
     ];
 
     /**
@@ -490,6 +494,33 @@ class HtmlInputNodeProcessor extends AbstractHtmlNodeProcessor
                 $quote->removeChild($removeElement);
             }
         }
+
+        // Strip de facto empty text nodes that are the result from quirky formatting, for example:
+        // <p><span style="font-size: 12px"> </span></p>
+        /** @var \DOMElement $paragraph */
+        foreach ($this->getXPath()->query('//p') as $paragraph) {
+            $textContent = StringUtil::trim($paragraph->textContent);
+            if ($textContent !== '') {
+                continue;
+            }
+
+            /** @var \DOMElement $element */
+            foreach ($paragraph->getElementsByTagName('*') as $element) {
+                if (!\in_array($element->nodeName, self::$emptyTags)) {
+                    continue 2;
+                }
+            }
+
+            // Do not strip content that contains non empty spans, such as icons.
+            /** @var \DOMElement $element */
+            foreach ($paragraph->getElementsByTagName('span') as $element) {
+                if ($element->getAttribute('class')) {
+                    continue 2;
+                }
+            }
+
+            $paragraph->parentNode->removeChild($paragraph);
+        }
     }
 
     /**