From 4cddce09c95ede0e645a0f457370553b023042a7 Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Wed, 17 Jan 2024 14:51:06 +0100 Subject: [PATCH] Move the mayContainOtherContent check in separate function Simplify to find the next/previous br --- .../node/HtmlInputNodeProcessor.class.php | 87 ++++++++----------- .../html/node/HtmlNodePlainLink.class.php | 33 +++---- 2 files changed, 54 insertions(+), 66 deletions(-) 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 c7375f8852..ad98ebafd7 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 @@ -779,39 +779,16 @@ class HtmlInputNodeProcessor extends AbstractHtmlNodeProcessor $parent = $parent->parentNode; } $mayContainOtherContent = false; + $linebreaks = 0; if ($parent->nodeName === 'p' && $parent->textContent === $link->textContent) { // The line may contain nothing but the link, exceptions include basic formatting // and up to a single `
` element. - $linebreaks = 0; /** @var \DOMElement $element */ foreach ($parent->getElementsByTagName('*') as $element) { - switch ($element->nodeName) { - case 'br': - $linebreaks++; - break; - - case 'span': - if ($element->getAttribute('class')) { - $mayContainOtherContent = true; - break 2; - } - - // `` is used to hold text formatting. - break; - - case 'a': - case 'b': - case 'em': - case 'i': - case 'strong': - case 'u': - // These elements are perfectly fine. - break; - - default: - $mayContainOtherContent = true; - break 2; + if ($this->mayContainOtherContent($element, $linebreaks)) { + $mayContainOtherContent = true; + break; } } @@ -823,29 +800,9 @@ class HtmlInputNodeProcessor extends AbstractHtmlNodeProcessor $parentLinkElement = $link; while ($parentLinkElement->parentElement !== $parent) { $parentLinkElement = $parentLinkElement->parentElement; - switch ($parentLinkElement->nodeName) { - case 'span': - if ($parentLinkElement->getAttribute('class')) { - $mayContainOtherContent = true; - break 2; - } - - // `` is used to hold text formatting. - break; - - case 'br': - case 'a': - case 'b': - case 'em': - case 'i': - case 'strong': - case 'u': - // These elements are perfectly fine. - break; - - default: - $mayContainOtherContent = true; - break 2; + if ($this->mayContainOtherContent($parentLinkElement, $linebreaks)) { + $mayContainOtherContent = true; + break; } } @@ -905,4 +862,34 @@ class HtmlInputNodeProcessor extends AbstractHtmlNodeProcessor return null; } + + private function mayContainOtherContent(\DOMElement $element, int &$linebreaks): bool + { + switch ($element->nodeName) { + case 'br': + $linebreaks++; + break; + + case 'span': + if ($element->getAttribute('class')) { + return true; + } + + // `` is used to hold text formatting. + break; + + case 'a': + case 'b': + case 'em': + case 'i': + case 'strong': + case 'u': + // These elements are perfectly fine. + break; + + default: + return true; + } + return false; + } } diff --git a/wcfsetup/install/files/lib/system/html/node/HtmlNodePlainLink.class.php b/wcfsetup/install/files/lib/system/html/node/HtmlNodePlainLink.class.php index f0a5c9428d..2de10c3eff 100644 --- a/wcfsetup/install/files/lib/system/html/node/HtmlNodePlainLink.class.php +++ b/wcfsetup/install/files/lib/system/html/node/HtmlNodePlainLink.class.php @@ -72,6 +72,7 @@ class HtmlNodePlainLink { $this->standalone = false; $this->topLevelParent = null; + $this->aloneInParagraph = false; return $this; } @@ -180,38 +181,38 @@ class HtmlNodePlainLink $this->topLevelParent->parentNode->insertBefore($this->link, $this->topLevelParent); DOMUtil::removeNode($this->topLevelParent); } else { - //Split at the link and replace the link with the metacode element. + $replaceNode = null; + $parent = $this->link; $next = $this->findBr($this->link, 'nextSibling'); $previous = $this->findBr($this->link, 'previousSibling'); - $replaceNode = null; + //link inside other elements(u, i, b, …) + while ($next === null && $previous === null && $parent !== $this->topLevelParent) { + $parent = $parent->parentElement; + $next = $this->findBr($parent, 'nextSibling'); + $previous = $this->findBr($parent, 'previousSibling'); + } if ($next !== null) { $replaceNode = DOMUtil::splitParentsUntil( - $this->link, - $this->link->parentElement->parentElement, + $parent, + $this->topLevelParent->parentElement, false ); } if ($previous !== null) { $replaceNode = DOMUtil::splitParentsUntil( - $this->link, - $this->link->parentElement->parentElement + $parent, + $this->topLevelParent->parentElement ); } \assert($replaceNode instanceof \DOMElement); //remove
from start and end of the new block elements - if ($replaceNode->nextSibling !== null) { - $br = $this->findBr($replaceNode->nextSibling->firstChild, 'nextSibling'); - if ($br !== null) { - DOMUtil::removeNode($br); - } + if ($next !== null) { + DOMUtil::removeNode($next); } - if ($replaceNode->previousSibling !== null) { - $br = $this->findBr($replaceNode->previousSibling->lastChild, 'previousSibling'); - if ($br !== null) { - DOMUtil::removeNode($br); - } + if ($previous !== null) { + DOMUtil::removeNode($previous); } DOMUtil::replaceElement($replaceNode, $metacodeElement, false); -- 2.20.1