From: Alexander Ebert Date: Tue, 12 Jul 2016 10:16:23 +0000 (+0200) Subject: Proper handling of code bbcode X-Git-Tag: 3.0.0_Beta_1~1192 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=1f9d08afaa1fa6c0e470de46163800ab19d67cee;p=GitHub%2FWoltLab%2FWCF.git Proper handling of code bbcode --- diff --git a/wcfsetup/install/files/lib/system/html/metacode/converter/CodeMetacodeConverter.class.php b/wcfsetup/install/files/lib/system/html/metacode/converter/CodeMetacodeConverter.class.php index 24fb747e3d..e0dd65f0f2 100644 --- a/wcfsetup/install/files/lib/system/html/metacode/converter/CodeMetacodeConverter.class.php +++ b/wcfsetup/install/files/lib/system/html/metacode/converter/CodeMetacodeConverter.class.php @@ -1,5 +1,7 @@ `. @@ -65,6 +67,41 @@ class CodeMetacodeConverter extends AbstractMetacodeConverter { $element->appendChild($fragment); + // convert code lines + $childNodes = DOMUtil::getChildNodes($element); + /** @var \DOMElement $node */ + foreach ($childNodes as $node) { + if ($node->nodeType === XML_ELEMENT_NODE && $node->nodeName === 'p') { + DOMUtil::insertAfter($node->ownerDocument->createTextNode("\n\n"), $node); + + $brs = $node->getElementsByTagName('br'); + while ($brs->length) { + $br = $brs->item(0); + DOMUtil::insertBefore($br->ownerDocument->createTextNode("\n"), $br); + DOMUtil::removeNode($br); + } + + DOMUtil::removeNode($node, true); + } + } + + // clear any other elements contained within + $elements = $element->getElementsByTagName('*'); + while ($elements->length) { + /** @var \DOMElement $child */ + $child = $elements->item(0); + if ($child->nodeName === 'a') { + DOMUtil::insertBefore($child->ownerDocument->createTextNode($child->getAttribute('href')), $child); + DOMUtil::removeNode($child); + continue; + } + + DOMUtil::removeNode($child, true); + } + + // trim code block + $element->textContent = StringUtil::trim($element->textContent); + return $element; } diff --git a/wcfsetup/install/files/lib/util/DOMUtil.class.php b/wcfsetup/install/files/lib/util/DOMUtil.class.php index 36a94ae0fa..68ab736e7d 100644 --- a/wcfsetup/install/files/lib/util/DOMUtil.class.php +++ b/wcfsetup/install/files/lib/util/DOMUtil.class.php @@ -54,6 +54,21 @@ final class DOMUtil { return false; } + /** + * Returns a static list of child nodes of provided element. + * + * @param \DOMElement $element target element + * @return \DOMNode[] list of child nodes + */ + public static function getChildNodes(\DOMElement $element) { + $nodes = []; + foreach ($element->childNodes as $node) { + $nodes[] = $node; + } + + return $nodes; + } + /** * Returns the common ancestor of both nodes. *