Improved `<pre>` linebreak fix
authorAlexander Ebert <ebert@woltlab.com>
Thu, 19 Jan 2017 12:05:30 +0000 (13:05 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 19 Jan 2017 12:05:30 +0000 (13:05 +0100)
wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php

index 8c90684a158dcb4d7f13cee0dab839dfd9e890a5..fa7d012de34b524949da1fee753a6d06f889b9b7 100644 (file)
@@ -76,6 +76,19 @@ abstract class AbstractHtmlNodeProcessor implements IHtmlNodeProcessor {
                // would conflict with already existing entities when reverting them.
                @$this->document->loadHTML('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>' . $html . '</body></html>');
                
+               // fix the `<pre>` linebreaks again
+               $pres = $this->document->getElementsByTagName('pre');
+               for ($i = 0, $length = $pres->length; $i < $length; $i++) {
+                       /** @var \DOMElement $pre */
+                       $pre = $pres[$i];
+                       /** @var \DOMNode $node */
+                       foreach ($pre->childNodes as $node) {
+                               if ($node->nodeType === XML_TEXT_NODE && mb_strpos($node->textContent, '@@@WCF_PRE_LINEBREAK@@@') !== false) {
+                                       $node->nodeValue = str_replace('@@@WCF_PRE_LINEBREAK@@@', "\n", $node->textContent);
+                               }
+                       }
+               }
+               
                $this->nodeData = [];
        }
        
@@ -108,9 +121,6 @@ abstract class AbstractHtmlNodeProcessor implements IHtmlNodeProcessor {
                        
                }
                
-               // restore line breaks inside code
-               $html = str_replace('@@@WCF_PRE_LINEBREAK@@@', "\n", $html);
-               
                // work-around for a libxml bug that causes a single space between
                // some inline elements to be dropped
                $html = str_replace('&nbsp;', ' ', $html);