$this->plainLinks[] = $plainLink->setIsStandalone($parent);
continue;
}
+ } elseif ($parent->nodeName === 'p') {
+ $nextSibling = $this->getNoneEmptyNode($link, 'nextSibling');
+ $previousSibling = $this->getNoneEmptyNode($link, 'previousSibling');
+
+ //Check whether the link is at the beginning or end of the paragraph
+ //and whether the next or previous sibling is a line break.
+ //<p><a href="https://example.com">https://example.com</a><br>…</p>
+ //<p>…<br><a href="https://example.com">https://example.com</a></p>
+ if (
+ ($nextSibling === null && $previousSibling !== null && $previousSibling->nodeName === 'br') ||
+ ($previousSibling === null && $nextSibling !== null && $nextSibling->nodeName === 'br')
+ ) {
+ $this->plainLinks[] = $plainLink->setIsStandalone();
+ continue;
+ }
+ //If not, the previous and next sibling may be a line break.
+ //<p>…<br><a href="https://example.com">https://example.com</a><br>…</p>
+ if (
+ $previousSibling !== null && $previousSibling->nodeName === 'br' &&
+ $nextSibling !== null && $nextSibling->nodeName === 'br'
+ ) {
+ $this->plainLinks[] = $plainLink->setIsStandalone();
+ continue;
+ }
}
$this->plainLinks[] = $plainLink->setIsInline();
}
}
}
+
+ private function getNoneEmptyNode(?\DOMNode $element, string $property): ?\DOMNode
+ {
+ while ($element = $element->{$property}) {
+ if (!DOMUtil::isEmpty($element)) {
+ return $element;
+ }
+ }
+
+ return null;
+ }
}
/**
* Marks the link as standalone, which means that it is the only content in a line.
*
- * @param \DOMElement $topLevelParent
+ * @param \DOMElement|null $topLevelParent
* @return $this
*/
- public function setIsStandalone(\DOMElement $topLevelParent)
+ public function setIsStandalone(?\DOMElement $topLevelParent = null)
{
$this->standalone = true;
$this->topLevelParent = $topLevelParent;
throw new \LogicException('Cannot inject a block bbcode in an inline context.');
}
- // Replace the top level parent with the link itself, which will be replaced with the bbcode afterwards.
- $this->topLevelParent->parentNode->insertBefore($this->link, $this->topLevelParent);
- DOMUtil::removeNode($this->topLevelParent);
+ if ($this->topLevelParent !== null) {
+ // Replace the top level parent with the link itself, which will be replaced with the bbcode afterwards.
+ $this->topLevelParent->parentNode->insertBefore($this->link, $this->topLevelParent);
+ DOMUtil::removeNode($this->topLevelParent);
+ }
}
DOMUtil::replaceElement($this->link, $metacodeElement, false);