[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

View file

@ -249,7 +249,7 @@ function convertLazyLoading($dom)
// 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));
$last_entry = trim($sources[array_key_last($sources)]);
$url = explode(' ', $last_entry)[0];
return $url;
};
@ -262,12 +262,15 @@ function convertLazyLoading($dom)
$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('data-orig-file'))) {
$img->src = $img->getAttribute('data-orig-file');
} elseif (!empty($img->getAttribute('srcset'))) {
$img->src = $srcset_to_src($img->getAttribute('srcset'));
} else {
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)) {
$img->removeAttribute($attr);
}
@ -284,7 +287,7 @@ function convertLazyLoading($dom)
$img->tag = 'img';
}
// 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;
}
}