Skip to content

Commit

Permalink
[html] improve srcset attribute parsing (#3769)
Browse files Browse the repository at this point in the history
Fix commas not being used for splitting, resulting in broken src URL in some cases:
srcset="url1.jpg, url2.jpg 2x" would give src="url1.jpg,"
  • Loading branch information
ORelio authored Oct 18, 2023
1 parent a41bb08 commit 7533ef1
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/html.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,26 @@ function convertLazyLoading($dom)
$dom = str_get_html($dom);
}

// Retrieve image URL from srcset attribute
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/srcset
// Example: convert "header640.png 640w, header960.png 960w, header1024.png 1024w" to "header1024.png"
$srcset_to_src = function ($srcset) {
$sources = explode(',', $srcset);
$last_entry = trim(end($sources));
$url = explode(' ', $last_entry)[0];
return $url;
};

// Process standalone images, embeds and picture sources
foreach ($dom->find('img, iframe, source') as $img) {
if (!empty($img->getAttribute('data-src'))) {
$img->src = $img->getAttribute('data-src');
} elseif (!empty($img->getAttribute('data-srcset'))) {
$img->src = explode(' ', $img->getAttribute('data-srcset'))[0];
$img->src = $srcset_to_src($img->getAttribute('data-srcset'));
} elseif (!empty($img->getAttribute('data-lazy-src'))) {
$img->src = $img->getAttribute('data-lazy-src');
} elseif (!empty($img->getAttribute('srcset'))) {
$img->src = explode(' ', $img->getAttribute('srcset'))[0];
$img->src = $srcset_to_src($img->getAttribute('srcset'));
} else {
continue; // Proceed to next element without removing attributes
}
Expand Down

0 comments on commit 7533ef1

Please sign in to comment.