[html] add data-orig-file tag (#3777)

Add support for data-orig-file tag in convertLazyLoading()
Remplace end() with array_key_last() as discussed in #3769
Fix typo in comment
This commit is contained in:
ORelio 2023-10-20 13:31:52 +02:00 committed by GitHub
parent 658391263e
commit 8ff39f64f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 3 deletions

View File

@ -249,7 +249,7 @@ function convertLazyLoading($dom)
// Example: convert "header640.png 640w, header960.png 960w, header1024.png 1024w" to "header1024.png" // Example: convert "header640.png 640w, header960.png 960w, header1024.png 1024w" to "header1024.png"
$srcset_to_src = function ($srcset) { $srcset_to_src = function ($srcset) {
$sources = explode(',', $srcset); $sources = explode(',', $srcset);
$last_entry = trim(end($sources)); $last_entry = trim($sources[array_key_last($sources)]);
$url = explode(' ', $last_entry)[0]; $url = explode(' ', $last_entry)[0];
return $url; return $url;
}; };
@ -262,12 +262,15 @@ function convertLazyLoading($dom)
$img->src = $srcset_to_src($img->getAttribute('data-srcset')); $img->src = $srcset_to_src($img->getAttribute('data-srcset'));
} elseif (!empty($img->getAttribute('data-lazy-src'))) { } elseif (!empty($img->getAttribute('data-lazy-src'))) {
$img->src = $img->getAttribute('data-lazy-src'); $img->src = $img->getAttribute('data-lazy-src');
} elseif (!empty($img->getAttribute('data-orig-file'))) {
$img->src = $img->getAttribute('data-orig-file');
} elseif (!empty($img->getAttribute('srcset'))) { } elseif (!empty($img->getAttribute('srcset'))) {
$img->src = $srcset_to_src($img->getAttribute('srcset')); $img->src = $srcset_to_src($img->getAttribute('srcset'));
} else { } else {
continue; // Proceed to next element without removing attributes continue; // Proceed to next element without removing attributes
} }
foreach (['loading', 'decoding', 'srcset', 'data-src', 'data-srcset'] as $attr) { // Remove attributes that may be processed by the client (data-* are not)
foreach (['loading', 'decoding', 'srcset'] as $attr) {
if ($img->hasAttribute($attr)) { if ($img->hasAttribute($attr)) {
$img->removeAttribute($attr); $img->removeAttribute($attr);
} }
@ -284,7 +287,7 @@ function convertLazyLoading($dom)
$img->tag = 'img'; $img->tag = 'img';
} }
// Adding/removing node would change its position inside the parent element, // Adding/removing node would change its position inside the parent element,
// So instead we rewrite the node in-place though the outertext attribute // So instead we rewrite the node in-place through the outertext attribute
$picture->outertext = $img->outertext; $picture->outertext = $img->outertext;
} }
} }