From 99e20d1b009680904fe828e4159f7ef00d79778b Mon Sep 17 00:00:00 2001 From: Marek Bachmann Date: Wed, 7 Dec 2022 13:22:28 +0100 Subject: [PATCH] Temporary hack to remove images after last text line by DOM inspection --- src/Model/Item.php | 58 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 634dbbd0ea..767c41acfd 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -3086,11 +3086,6 @@ class Item 'filter_reasons' => $filter_reasons ]; Hook::callAll('prepare_body', $hook_data); - // Remove old images -// $hook_data['html'] = preg_replace('|(.*)|', '', $hook_data['html']); -// $grid = self::make_image_grid($hook_data); -// $s = $hook_data['html'] . $grid; - $s = $hook_data['html']; unset($hook_data); if (!$attach) { @@ -3128,9 +3123,15 @@ class Item $s = HTML::applyContentFilter($s, $filter_reasons); + if (count($attachments['visual']) > 1) { + // make imgae grid only for multiple images + $s = self::cutAttachedImages($s); + $grid = self::make_image_grid($item, $attachments); + $s .= $grid; + } + $hook_data = ['item' => $item, 'html' => $s]; Hook::callAll('prepare_body_final', $hook_data); - return $hook_data['html']; } @@ -3140,14 +3141,47 @@ class Item * @param array $rendered_html * @return array */ - private function cutAttachedImages(array &$rendered_html) { + private function cutAttachedImages($rendered_html) + { $doc = new DOMDocument(); + libxml_use_internal_errors(true); $doc->loadHTML($rendered_html); + libxml_clear_errors(); + + $root = $doc->getElementsByTagName("p")[0]; + + $lastTextNode = null; + if ($root && $root->childNodes) { + foreach ($root->childNodes as $node) { + if ($node->nodeName == "#text" && strlen(trim($node->nodeValue)) > 0) { + $lastTextNode = $node; + } + } + } + + if ($lastTextNode == null) { + // no text at all, return nothing: + return ''; + } - $xpathsearch = new DOMXPath($doc); - $nodes = $xpathsearch->query("*"); + $toremove = array(); + if ($lastTextNode) { + $sibling = $lastTextNode->nextSibling; + while ($sibling) { + $toremove[] = array($sibling); + $sibling = $sibling->nextSibling; + } + foreach ($toremove as $remove) { + $root->removeChild($remove[0]); + } + $html = ''; + foreach ($root->childNodes as $node) { + $html .= $node->ownerDocument->saveHTML($node); + } + return $html; + } - return $nodes; + return $rendered_html; } /** @@ -3155,11 +3189,9 @@ class Item * @return string|void * @throws \Friendica\Network\HTTPException\ServiceUnavailableException */ - private function make_image_grid(array &$data) + private function make_image_grid(array $item, array $attachments) { - $item = $data['item']; if ($item['has-media']) { - $attachments = Post\Media::splitAttachments($item['uri-id'], [], $item['has-media'] ?? false); if (count($attachments['visual']) > 1) { $img_tags_fc = array(); $img_tags_sc = array(); -- 2.39.5