<?php
namespace wcf\system\html\metacode\converter;
+use wcf\util\DOMUtil;
+use wcf\util\StringUtil;
/**
* Converts code bbcode into `<pre>`.
$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;
}
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.
*