Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.0] Disable lazyloading on images that have no dimensions or added on the first page view #30218

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@

if (Joomla.selectedFile.url) {
if (!isElement(editor) && (typeof editor !== 'object')) {
Joomla.editors.instances[editor].replaceSelection(`<img loading="lazy" src="${Joomla.selectedFile.url}" alt=""/>`);
Joomla.editors.instances[editor].replaceSelection(`<img loading="lazy" src="${Joomla.selectedFile.url}" width="${Joomla.selectedFile.width}px" height="${Joomla.selectedFile.height}px" alt=""/>`);
} else if (!isElement(editor) && (typeof editor === 'object' && editor.id)) {
window.parent.Joomla.editors.instances[editor.id].replaceSelection(`<img loading="lazy" src="${Joomla.selectedFile.url}" alt=""/>`);
window.parent.Joomla.editors.instances[editor.id].replaceSelection(`<img loading="lazy" src="${Joomla.selectedFile.url}" width="${Joomla.selectedFile.width}px" height="${Joomla.selectedFile.height}px" alt=""/>`);
} else {
editor.value = Joomla.selectedFile.url;
fieldClass.updatePreview();
Expand Down
4 changes: 2 additions & 2 deletions libraries/src/HTML/HTMLHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,8 @@ public static function image($file, $alt, $attribs = null, $relative = false, $r
return $file;
}

// Default to lazy you can disable lazyloading by passing $attribs['loading'] = 'eager';
if (!isset($attribs['loading']))
// Default to lazy when the width and height are set you can disable lazyloading by passing $attribs['loading'] = 'eager';
if (!isset($attribs['loading']) && isset($attribs['width']) && isset($attribs['height']))
{
$attribs['loading'] = 'lazy';
}
Expand Down
7 changes: 5 additions & 2 deletions plugins/content/imagelazyload/imagelazyload.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ public function onContentPrepare($context, &$row, &$params, $page = 0)

foreach ($matches[0] as $image)
{
// Make sure we have a src but no loading attribute
if (strpos($image, ' src=') !== false && strpos($image, ' loading=') === false)
// Make sure we have a src, width and height but no loading attribute
if (strpos($image, ' src=') !== false
&& strpos($image, ' width=') !== false
&& strpos($image, ' height=') !== false
&& strpos($image, ' loading=') === false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wilsonge what's the impact of this plugin after these 2 conditionals (width, height) given that the media selector doesn't even provide entry points for these attributes?
Sorry to be an @$$ all the time but this is targeting something less than 1% of the current 3.x sites, so it shouldn't be part of the core. My offer for both https://github.com/ttc-freebies/joomla-3.x-images-lazy-loading and https://github.com/ttc-freebies/com_addlazyloading is still there, fork it or do whatever you want with those...

Screenshot 2020-08-01 at 11 27 41

{
$lazyloadImage = str_replace('<img ', '<img loading="lazy" ', $image);
$row->text = str_replace($image, $lazyloadImage, $row->text);
Expand Down