$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 `<br>` 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;
- }
-
- // `<span>` 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;
}
}
$parentLinkElement = $link;
while ($parentLinkElement->parentElement !== $parent) {
$parentLinkElement = $parentLinkElement->parentElement;
- switch ($parentLinkElement->nodeName) {
- case 'span':
- if ($parentLinkElement->getAttribute('class')) {
- $mayContainOtherContent = true;
- break 2;
- }
-
- // `<span>` 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;
}
}
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;
+ }
+
+ // `<span>` 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;
+ }
}
{
$this->standalone = false;
$this->topLevelParent = null;
+ $this->aloneInParagraph = false;
return $this;
}
$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 <br> 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);