Unwrap link previews nested in paragraphs
authorAlexander Ebert <ebert@woltlab.com>
Sat, 5 Aug 2023 09:58:06 +0000 (11:58 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 5 Aug 2023 09:58:06 +0000 (11:58 +0200)
See https://www.woltlab.com/community/thread/301006-leading-rich-embeds-yield-empty-paragraphs/

com.woltlab.wcf/templates/unfurlUrl.tpl
wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrl.class.php
wcfsetup/install/files/lib/system/html/output/node/HtmlOutputUnfurlUrlNode.class.php
wcfsetup/install/files/style/ui/unfurlUrl.scss

index 0258cc8ad762634a768bcd3486cf09fae83c90ed..80cfae04d3207e6688fa578367ac8d8bf78b6a6c 100644 (file)
@@ -1,4 +1,4 @@
-{if $object->status == "SUCCESSFUL"}
+{if $object->hasFetchedContent()}
        {if $object->isPlainUrl()}
                <a {anchorAttributes url=$object->url isUgc=$enableUgc}>{$object->title}</a>
        {else}
index 25af59080bbb0b8b442801e94e4f505c471a25d4..f684d44854023fd5f5a87d3c2d84f3597d3a5b45 100644 (file)
@@ -151,6 +151,14 @@ class UnfurlUrl extends DatabaseObject
         return self::IMAGE_COVER;
     }
 
+    /**
+     * @since 6.0
+     */
+    public function hasFetchedContent(): bool
+    {
+        return $this->status === self::STATUS_SUCCESSFUL;
+    }
+
     /**
      * Returns the unfurl url object for a given url.
      *
index dfd82dbc9d63c2a639716958c451e94e503f97e1..b93eee4628d6422311f114eff61f8f1117aaac97 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace wcf\system\html\output\node;
 
+use wcf\data\unfurl\url\UnfurlUrl;
 use wcf\system\html\AbstractHtmlProcessor;
 use wcf\system\html\node\AbstractHtmlNodeProcessor;
 use wcf\system\html\node\HtmlNodeUnfurlLink;
@@ -62,7 +63,18 @@ class HtmlOutputUnfurlUrlNode extends AbstractHtmlOutputNode
                     'enableUgc' => $enableUgc,
                 ]);
 
+                $parentParagraph = null;
+                if ($this->getUnfurlUrl($attribute)->hasFetchedContent() && $element->parentNode->nodeName === 'p') {
+                    $parentParagraph = $element->parentNode;
+                    $parentParagraph->parentNode->insertBefore($element, $parentParagraph);
+                }
+
                 $htmlNodeProcessor->renameTag($element, $tagName);
+
+                if ($parentParagraph !== null && !$parentParagraph->hasChildNodes()) {
+                    /** @var \DOMElement $parentParagraph */
+                    $parentParagraph->remove();
+                }
             }
         }
     }
@@ -72,9 +84,14 @@ class HtmlOutputUnfurlUrlNode extends AbstractHtmlOutputNode
      */
     public function replaceTag(array $data)
     {
-        /** @var \wcf\data\unfurl\url\UnfurlUrl $object */
-        $object = MessageEmbeddedObjectManager::getInstance()->getObject('com.woltlab.wcf.unfurlUrl', $data['urlId']);
+        return $this->getUnfurlUrl($data['urlId'])->render($data['enableUgc']);
+    }
 
-        return $object->render($data['enableUgc']);
+    /**
+     * @since 6.0
+     */
+    protected function getUnfurlUrl(int $id): UnfurlUrl
+    {
+        return MessageEmbeddedObjectManager::getInstance()->getObject('com.woltlab.wcf.unfurlUrl', $id);
     }
 }
index 95584158c5b4cfbaa75fe5f80e903e734790abe2..2373daa7f31c34cd080020af07161d33fb9b93ae 100644 (file)
@@ -1,10 +1,12 @@
 .unfurlUrlCard {
        background-color: var(--wcfContentBackground);
        border-radius: 3px;
-       box-shadow: 0 0 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
+       box-shadow:
+               0 0 3px rgba(0, 0, 0, 0.12),
+               0 1px 2px rgba(0, 0, 0, 0.24);
        color: var(--wcfContentText);
        display: inline-block;
-       margin: 10px 0;
+       margin: 1em 0 0 0;
        max-width: 100%;
        overflow: hidden;
        position: relative;