namespace wcf\system\html\upcast\node;
use wcf\data\bbcode\BBCodeCache;
+use wcf\system\html\metacode\upcast\EmptyMetacodeUpcast;
use wcf\system\html\metacode\upcast\IMetacodeUpcast;
use wcf\system\html\node\AbstractHtmlNodeProcessor;
use wcf\util\DOMUtil;
/** @var IMetacodeUpcast[] $upcasters */
$upcasters = [];
$nodes = [];
+ $emptyMetacodeUpcast = new EmptyMetacodeUpcast();
/** @var \DOMElement $element */
foreach ($elements as $element) {
if ($attributes === []) {
$element->removeAttribute('data-attributes');
}
+ $bbcode = BBCodeCache::getInstance()->getBBCodeByTag($name);
+ if (!$bbcode->originIsSystem) {
+ $nodes[] = [$element, $name, $emptyMetacodeUpcast, $attributes];
+ continue;
+ }
// check for upcast
$upcast = $upcasters[$name] ?? null;
} else {
// Replace this with a text node
/** @see HtmlBBCodeParser::buildBBCodeTag() */
- $attributes = \array_filter($attributes, fn($value) => $value !== null);
+ $attributes = \array_filter($attributes, static fn($value) => $value !== null);
if (!empty($attributes)) {
foreach ($attributes as &$attribute) {
} else {
$attributes = '';
}
- $text = \sprintf('[%s%s][/%s]', $name, $attributes, $name);
$bbcode = BBCodeCache::getInstance()->getBBCodeByTag($name);
if ($bbcode === null || $bbcode->isBlockElement) {
$newElement = $element->ownerDocument->createElement('p');
- $newElement->textContent = $text;
- DOMUtil::replaceElement($element, $newElement);
+ $newElement->appendChild($element->ownerDocument->createTextNode("[{$name}{$attributes}]"));
+ if ($bbcode->isSourceCode) {
+ $newElement->appendChild(
+ $element->ownerDocument->createTextNode($element->textContent)
+ );
+ DomUtil::replaceElement($element, $newElement, false);
+ } else {
+ DomUtil::replaceElement($element, $newElement);
+ }
+ $newElement->appendChild($element->ownerDocument->createTextNode("[/{$name}]"));
} else {
- $element->parentNode->insertBefore($element->ownerDocument->createTextNode($text), $element);
- $element->parentNode->removeChild($element);
+ $insertNode = $element->parentNode->insertBefore(
+ $element->ownerDocument->createTextNode("[{$name}{$attributes}]"),
+ $element
+ );
+ if ($bbcode->isSourceCode) {
+ $insertNode->parentNode->appendChild(
+ $element->ownerDocument->createTextNode($element->textContent)
+ );
+ DOMUtil::removeNode($element);
+ } else {
+ DOMUtil::removeNode($element, true);
+ }
+ $insertNode->parentNode->appendChild(
+ $element->ownerDocument->createTextNode("[/{$name}]")
+ );
}
}
}