don't output srcset when only one image

This commit is contained in:
Gert
2015-01-26 21:30:17 +01:00
parent 03f729602b
commit 2c57453608
2 changed files with 27 additions and 12 deletions

View File

@@ -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

View File

@@ -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 = '<img src="' . $data['img_src'] . '" srcset="' . $data['img_srcset'] . '" sizes="100vw" class="'. $class . '" alt="' . $title . '" />';
$attributes = $data['img_srcset'] ? ' srcset="' . $data['img_srcset'] . '" sizes="100vw"' : '';
$output = '<img src="' . $data['img_src'] . '"' . $attributes . ' class="'. $class . '" alt="' . $title . '" />';
} else {
$output = $data['text'];
}