From 2f15bc0d8dab31be0fd3343f2bd261a722095ad1 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 6 Oct 2020 14:34:25 +0200 Subject: [PATCH] fixup! figcaption: Adds figcaption --- data/themes/default/hypha.css | 4 ++++ system/core/pages.php | 23 ++++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/data/themes/default/hypha.css b/data/themes/default/hypha.css index 835f90ca..52740165 100644 --- a/data/themes/default/hypha.css +++ b/data/themes/default/hypha.css @@ -525,6 +525,10 @@ img { height: auto; } +#main figure .title + .attribution:before { + content: " - "; +} + #main img.left { float: left; margin: 0 13px 10px 0; diff --git a/system/core/pages.php b/system/core/pages.php index 157267af..79829fb6 100644 --- a/system/core/pages.php +++ b/system/core/pages.php @@ -604,7 +604,7 @@ function wikify_link($node) { function add_captions_to_all_images($element) { /** @var \DOMWrap\NodeList $img */ // process images that reside within "main" - foreach ($element->findXPath('//*[@id="main"]//img[@title] | //*[@id="main"]//img[@data-attribution]') as $img) { + foreach ($element->findXPath('//*[@id="main"]//img[@title or @data-attribution]') as $img) { // do not process images that reside within the wymeditor if ($img->parents('.wymeditor')->count() === 0) { add_caption_to_image($img); @@ -613,19 +613,24 @@ function add_captions_to_all_images($element) { } function add_caption_to_image($img) { - $figure = new DOMWrap\Element('figure'); - $img->parentNode->insertBefore($figure, $img); - $figure->append($img); + $doc = $img->document(); + + $img->wrap('
'); + $caption = $doc->createElement('figcaption'); + $img->after($caption); $title = $img->getAttribute('title'); - $text = $title ? $title : ''; - $caption = new DOMWrap\Element('figcaption', $text); - $figure->append($caption); + if ($title) { + $span = $doc->createElement('span', $title); + $span->addClass('title'); + $caption->append($span); + } $attribution = $img->getAttribute('data-attribution'); if ($attribution) { - $small = new DOMWrap\Element('small', $attribution); - $figure->append($small); + $small = $doc->createElement('small', $attribution); + $small->addClass('attribution'); + $caption->append($small); } }