Simplify the handling of the alignment CSS classes
authorAlexander Ebert <ebert@woltlab.com>
Fri, 14 Jul 2023 16:29:19 +0000 (18:29 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 14 Jul 2023 16:29:19 +0000 (18:29 +0200)
wcfsetup/install/files/lib/system/bbcode/AttachmentBBCode.class.php

index ea111666d8d8e0cb53c966c55effc35388f5b1a3..be5eb8250df60507817057b0c5e671204b0727a3 100644 (file)
@@ -35,7 +35,18 @@ final class AttachmentBBCode extends AbstractBBCode
         $outputType = $parser->getOutputType();
 
         if ($attachment->showAsImage() && $attachment->canViewPreview() && ($outputType == 'text/html' || $outputType == 'text/simplified-html')) {
-            return $this->showImage($attachment, $outputType, $openingTag['attributes']);
+            $hasParentLink = false;
+            if (!empty($closingTag['__parents'])) {
+                /** @var \DOMElement $parent */
+                foreach ($closingTag['__parents'] as $parent) {
+                    if ($parent->nodeName === 'a') {
+                        $hasParentLink = true;
+                        break;
+                    }
+                }
+            }
+
+            return $this->showImage($attachment, $outputType, $openingTag['attributes'], $hasParentLink);
         } elseif (\substr($attachment->fileType, 0, 6) === 'video/' && $outputType == 'text/html') {
             return $this->showVideoPlayer($attachment);
         } elseif (\substr($attachment->fileType, 0, 6) === 'audio/' && $outputType == 'text/html') {
@@ -45,28 +56,17 @@ final class AttachmentBBCode extends AbstractBBCode
         return StringUtil::getAnchorTag($attachment->getLink(), $attachment->filename);
     }
 
-    private function showImage(Attachment $attachment, string $outputType, array $attributes): string
+    private function showImage(Attachment $attachment, string $outputType, array $attributes, bool $hasParentLink): string
     {
         $alignment = $attributes[1] ?? '';
-
         $thumbnail = $this->renderImageAsThumbnail($attachment, $outputType, $attributes[2] ?? false);
 
-        $hasParentLink = false;
-        if (!empty($closingTag['__parents'])) {
-            /** @var \DOMElement $parent */
-            foreach ($closingTag['__parents'] as $parent) {
-                if ($parent->nodeName === 'a') {
-                    $hasParentLink = true;
-                    break;
-                }
-            }
-        }
-
         if ($thumbnail === false) {
-            $class = '';
-            if ($alignment === 'left' || $alignment === 'right') {
-                $class = 'messageFloatObject' . \ucfirst($alignment);
-            }
+            $class = match ($alignment) {
+                "left" => "messageFloatObjectLeft",
+                "right" => "messageFloatObjectRight",
+                default => ""
+            };
 
             $source = StringUtil::encodeHTML($attachment->getLink());
             $title = StringUtil::encodeHTML($attachment->filename);
@@ -115,10 +115,11 @@ final class AttachmentBBCode extends AbstractBBCode
             $linkParameters['thumbnail'] = 1;
         }
 
-        $class = '';
-        if ($alignment == 'left' || $alignment == 'right') {
-            $class = 'messageFloatObject' . \ucfirst($alignment);
-        }
+        $class = match ($alignment) {
+            "left" => "messageFloatObjectLeft",
+            "right" => "messageFloatObjectRight",
+            default => ""
+        };
 
         $imageClasses = '';
         if (!$attachment->hasThumbnail()) {