From 0b9a0475bc14f3e583c8dc16f621761a63b655c9 Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Thu, 14 Dec 2023 12:05:09 +0100 Subject: [PATCH 1/2] Handle img has an a-tag --- .../input/node/HtmlInputNodeImg.class.php | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php b/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php index 1ec5f2ee691..bc7cba77693 100644 --- a/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php +++ b/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php @@ -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(?image-style-side-left|image-style-side)\b~', $parent->getAttribute('class'), $matches)) { + $figure = $this->getParentFigure($element); + if ($figure !== null) { + if (\preg_match('~\b(?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"); @@ -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 && $parent->tagName === "figure") { if (\preg_match('~\b(?image-style-side-left|image-style-side)\b~', $parent->getAttribute('class'), $matches)) { $float = ($matches['float'] === 'image-style-side-left') ? 'left' : 'right'; } else { @@ -289,9 +291,11 @@ protected function mirrorWidthAttribute(\DOMElement $element): void { // Aligned images are wrapped in a `
` 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; } @@ -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; } @@ -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; + } } From 69e4c451272b9fec74737d9799bbcfce424c2b67 Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Thu, 14 Dec 2023 12:11:14 +0100 Subject: [PATCH 2/2] Remove unnecessary check --- .../files/lib/system/html/input/node/HtmlInputNodeImg.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php b/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php index bc7cba77693..b2bf2b07418 100644 --- a/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php +++ b/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php @@ -186,7 +186,7 @@ protected function handleMedium(\DOMElement $element, $class) $replaceElement = $element; $parent = $this->getParentFigure($element); - if ($parent !== null && $parent->tagName === "figure") { + if ($parent !== null) { if (\preg_match('~\b(?image-style-side-left|image-style-side)\b~', $parent->getAttribute('class'), $matches)) { $float = ($matches['float'] === 'image-style-side-left') ? 'left' : 'right'; } else {