Stripping superfluos whitespace from paragraphs
authorAlexander Ebert <ebert@woltlab.com>
Thu, 6 Oct 2016 09:49:41 +0000 (11:49 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 6 Oct 2016 09:49:41 +0000 (11:49 +0200)
wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeProcessor.class.php
wcfsetup/install/files/lib/util/DOMUtil.class.php

index dd17ca263c1f03d228a6aa238da8949a72598176..e13fc468164c5e1f17725fdc6367ba7d24815e30 100644 (file)
@@ -242,6 +242,28 @@ class HtmlInputNodeProcessor extends AbstractHtmlNodeProcessor {
                                }
                        }
                }
+               
+               // trim <p>...</p>
+               /** @var \DOMElement $paragraph */
+               foreach ($this->getDocument()->getElementsByTagName('p') as $paragraph) {
+                       DOMUtil::normalize($paragraph);
+                       
+                       if ($paragraph->firstChild && $paragraph->firstChild->nodeType === XML_TEXT_NODE) {
+                               $oldNode = $paragraph->firstChild;
+                               $newNode = $paragraph->ownerDocument->createTextNode(preg_replace('/^(\s|'.chr(226).chr(128).chr(175).'|'.chr(194).chr(160).')+/', '', $oldNode->textContent));
+                               $paragraph->insertBefore($newNode, $oldNode);
+                               $paragraph->removeChild($oldNode);
+                               
+                       }
+                       
+                       if ($paragraph->lastChild && $paragraph->lastChild->nodeType === XML_TEXT_NODE) {
+                               $oldNode = $paragraph->lastChild;
+                               $newNode = $paragraph->ownerDocument->createTextNode(preg_replace('/(\s|'.chr(226).chr(128).chr(175).'|'.chr(194).chr(160).')+$/', '', $oldNode->textContent));
+                               $paragraph->insertBefore($newNode, $oldNode);
+                               $paragraph->removeChild($oldNode);
+                               
+                       }
+               }
        }
        
        /**
index 17d434a27f5b87be74fb5ce3e241ae766312c8c8..d55ed3c8ca2668ceed074ab872b8527438fadbbd 100644 (file)
@@ -382,6 +382,37 @@ final class DOMUtil {
                while ($element !== $commonAncestor);
        }
        
+       /**
+        * Normalizes an element by joining adjacent text nodes.
+        * 
+        * @param       \DOMElement     $element        target element
+        */
+       public static function normalize(\DOMElement $element) {
+               $childNodes = DOMUtil::getChildNodes($element);
+               /** @var \DOMNode $lastTextNode */
+               $lastTextNode = null;
+               foreach ($childNodes as $childNode) {
+                       if ($childNode->nodeType !== XML_TEXT_NODE) {
+                               $lastTextNode = null;
+                               continue;
+                       }
+                       
+                       if ($lastTextNode === null) {
+                               $lastTextNode = $childNode;
+                       }
+                       else {
+                               // merge with last text node
+                               $newTextNode = $childNode->ownerDocument->createTextNode($lastTextNode->textContent . $childNode->textContent);
+                               $element->insertBefore($newTextNode, $lastTextNode);
+                               
+                               $element->removeChild($lastTextNode);
+                               $element->removeChild($childNode);
+                               
+                               $lastTextNode = $newTextNode;
+                       }
+               }
+       }
+       
        /**
         * Prepends a node to provided element.
         *