From 1f5d609337c75c7282997a0b6eb58017c3c0b2ff Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 6 Oct 2016 11:49:41 +0200 Subject: [PATCH] Stripping superfluos whitespace from paragraphs --- .../node/HtmlInputNodeProcessor.class.php | 22 +++++++++++++ .../install/files/lib/util/DOMUtil.class.php | 31 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeProcessor.class.php b/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeProcessor.class.php index dd17ca263c..e13fc46816 100644 --- a/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeProcessor.class.php +++ b/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeProcessor.class.php @@ -242,6 +242,28 @@ class HtmlInputNodeProcessor extends AbstractHtmlNodeProcessor { } } } + + // trim

...

+ /** @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); + + } + } } /** diff --git a/wcfsetup/install/files/lib/util/DOMUtil.class.php b/wcfsetup/install/files/lib/util/DOMUtil.class.php index 17d434a27f..d55ed3c8ca 100644 --- a/wcfsetup/install/files/lib/util/DOMUtil.class.php +++ b/wcfsetup/install/files/lib/util/DOMUtil.class.php @@ -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. * -- 2.20.1