From: Alexander Ebert Date: Tue, 23 Aug 2016 19:59:39 +0000 (+0200) Subject: Fix for poorly nested HTML X-Git-Tag: 3.0.0_Beta_1~542 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=9eeca5e093af62abb5420c980d6ddbcfb3857b1c;p=GitHub%2FWoltLab%2FWCF.git Fix for poorly nested HTML --- 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 9fb441d79c..2875f3a259 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 @@ -33,6 +33,9 @@ class HtmlInputNodeProcessor extends AbstractHtmlNodeProcessor { public function process() { EventHandler::getInstance()->fireAction($this, 'beforeProcess'); + // fix invalid html such as metacode markers outside of block elements + $this->fixDom(); + // process metacode markers first $this->invokeHtmlNode(new HtmlInputNodeWoltlabMetacodeMarker()); @@ -56,6 +59,39 @@ class HtmlInputNodeProcessor extends AbstractHtmlNodeProcessor { EventHandler::getInstance()->fireAction($this, 'afterProcess'); } + /** + * Fixes malformed HTML with metacode markers and text being placed + * outside of paragraphs. + */ + protected function fixDom() { + $appendToPreviousParagraph = function ($node) { + /** @var \DOMElement $paragraph */ + $paragraph = $node->previousSibling; + + if (!$paragraph || $paragraph->nodeName !== 'p') { + $paragraph = $node->ownerDocument->createElement('p'); + $node->parentNode->insertBefore($paragraph, $node); + } + + $paragraph->appendChild($node); + + return $paragraph; + }; + + /** @var \DOMNode $node */ + $node = $this->getDocument()->getElementsByTagName('body')->item(0)->firstChild; + while ($node) { + if ($node->nodeType === XML_ELEMENT_NODE && $node->nodeName === 'woltlab-metacode-marker') { + $node = $appendToPreviousParagraph($node); + } + else if ($node->nodeType === XML_TEXT_NODE) { + $node = $appendToPreviousParagraph($node); + } + + $node = $node->nextSibling; + } + } + /** * Trims leading and trailing whitespace. It will only remove text nodes containing * just whitespaces and


(including any whitespace-only text nodes).