From 2c57453608c703a05d44176fd485e2fd2bdd4979 Mon Sep 17 00:00:00 2001 From: Gert Date: Mon, 26 Jan 2015 21:30:17 +0100 Subject: [PATCH] don't output srcset when only one image --- .../Common/Markdown/ParsedownGravTrait.php | 29 ++++++++++++------- system/src/Grav/Common/Page/Medium.php | 10 +++++-- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/system/src/Grav/Common/Markdown/ParsedownGravTrait.php b/system/src/Grav/Common/Markdown/ParsedownGravTrait.php index 8ac3e63d4..fdc0a09d6 100644 --- a/system/src/Grav/Common/Markdown/ParsedownGravTrait.php +++ b/system/src/Grav/Common/Markdown/ParsedownGravTrait.php @@ -151,28 +151,37 @@ trait ParsedownGravTrait // set the src element with the new generated url if (!isset($actions['lightbox'])) { $excerpt['element']['attributes']['src'] = $data['img_src']; - $excerpt['element']['attributes']['srcset'] = $data['img_srcset'];; - $excerpt['element']['attributes']['sizes'] = '100vw'; + + if ($data['img_srcset']) { + $excerpt['element']['attributes']['srcset'] = $data['img_srcset'];; + $excerpt['element']['attributes']['sizes'] = '100vw'; + } + } else { // Create the custom lightbox element $attributes = $data['a_attributes']; $attributes['href'] = $data['a_href']; + $img_attributes = [ + 'src' => $data['img_src'], + 'alt' => $alt, + 'title' => $title + ]; + + if ($data['img_srcset']) { + $img_attributes['srcset'] = $data['img_srcset']; + $img_attributes['sizes'] = '100vw'; + } + $element = array( 'name' => 'a', 'attributes' => $attributes, 'handler' => 'element', 'text' => array( 'name' => 'img', - 'attributes' => array( - 'src' => $data['img_src'], - 'srcset' => $data['img_srcset'], - 'sizes' => '100vw', - 'alt' => $alt, - 'title' => $title - ) - ), + 'attributes' => $img_attributes + ) ); // Set any custom classes on the lightbox element diff --git a/system/src/Grav/Common/Page/Medium.php b/system/src/Grav/Common/Page/Medium.php index 31f49e99b..8497c2589 100644 --- a/system/src/Grav/Common/Page/Medium.php +++ b/system/src/Grav/Common/Page/Medium.php @@ -173,6 +173,11 @@ class Medium extends Data */ public function srcset($reset = true) { + if (empty($this->alternatives)) { + if ($reset) $this->reset(); + return ''; + } + $srcset = [ $this->url($reset) . ' ' . $this->get('width') . 'w' ]; foreach ($this->alternatives as $ratio => $medium) { @@ -213,13 +218,14 @@ class Medium extends Data */ public function html($title = null, $class = null, $reset = true) { - $data = $this->htmlRaw($title, $class, $reset); + $data = $this->htmlRaw($reset); $title = $title ? $title : $this->get('title'); $class = $class ? $class : ''; if ($this->image) { - $output = '' . $title . ''; + $attributes = $data['img_srcset'] ? ' srcset="' . $data['img_srcset'] . '" sizes="100vw"' : ''; + $output = '' . $title . ''; } else { $output = $data['text']; }