Skip to content

Commit

Permalink
Merge pull request #5728 from WoltLab/bugfix-media-converted-to-img
Browse files Browse the repository at this point in the history
Fix media/attachment inside a-tag
  • Loading branch information
dtdesign authored Dec 14, 2023
2 parents 17c5958 + 69e4c45 commit c20aada
Showing 1 changed file with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,19 @@ protected function handleAttachment(\DOMElement $element, $class)
}

$replaceElement = $element;
$parent = $element->parentNode;
\assert($parent instanceof \DOMElement);
if ($parent->tagName === "figure") {
if (\preg_match('~\b(?<float>image-style-side-left|image-style-side)\b~', $parent->getAttribute('class'), $matches)) {
$figure = $this->getParentFigure($element);
if ($figure !== null) {
if (\preg_match('~\b(?<float>image-style-side-left|image-style-side)\b~', $figure->getAttribute('class'), $matches)) {
$float = ($matches['float'] === 'image-style-side-left') ? 'left' : 'right';
} else {
$float = 'center';
}
$replaceElement = $figure;

$replaceElement = $parent;
if (($element->parentNode instanceof \DOMElement) && $element->parentNode->nodeName === "a") {
DOMUtil::replaceElement($figure, $element->parentNode, false);
$replaceElement = $element;
}
}

$width = $replaceElement->getAttribute("data-width");
Expand Down Expand Up @@ -182,9 +185,8 @@ protected function handleMedium(\DOMElement $element, $class)
}

$replaceElement = $element;
$parent = $element->parentNode;
\assert($parent instanceof \DOMElement);
if ($parent->tagName === "figure") {
$parent = $this->getParentFigure($element);
if ($parent !== null) {
if (\preg_match('~\b(?<float>image-style-side-left|image-style-side)\b~', $parent->getAttribute('class'), $matches)) {
$float = ($matches['float'] === 'image-style-side-left') ? 'left' : 'right';
} else {
Expand Down Expand Up @@ -289,9 +291,11 @@ protected function mirrorWidthAttribute(\DOMElement $element): void
{
// Aligned images are wrapped in a `<figure>` element that is the target
// of the resize operation.
if ($element->parentNode->nodeName === 'figure') {
$this->mirrorWidthAttribute($element->parentNode);

if ($element->nodeName !== 'img') {
$figure = $this->getParentFigure($element);
if ($figure !== null) {
$this->mirrorWidthAttribute($figure);
}
return;
}

Expand All @@ -317,8 +321,8 @@ protected function mirrorWidthAttribute(\DOMElement $element): void
*/
protected function moveClassNameFromFigureToImage(\DOMElement $img): void
{
$figure = $img->parentNode;
if (!($figure instanceof \DOMElement) || $figure->nodeName !== 'figure') {
$figure = $this->getParentFigure($img);
if ($figure === null) {
return;
}

Expand All @@ -343,4 +347,22 @@ static function (string $className) use ($img) {

$figure->setAttribute("class", \implode(' ', $classNames));
}

private function getParentFigure(\DOMElement $img): ?\DOMElement
{
$parent = $img->parentNode;
if ($parent instanceof \DOMElement) {
if ($parent->nodeName === 'figure') {
return $parent;
}
if ($parent->nodeName === 'a') {
$parent = $parent->parentNode;
if ($parent instanceof \DOMElement && $parent->nodeName === 'figure') {
return $parent;
}
}
}

return null;
}
}

0 comments on commit c20aada

Please sign in to comment.