Skip to content
This repository was archived by the owner on Oct 19, 2022. It is now read-only.

Commit dc114ac

Browse files
committed
- Lazy juggling of srcset/sizes attributes introduced
- Pass matches to lazyload_images_placeholder_image filter allowing it to generate individually sized images - Allow data:image placeholder URLs
1 parent 3f08999 commit dc114ac

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

js/lazy-load.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717

1818
function lazy_load_image( img ) {
1919
var $img = jQuery( img ),
20-
src = $img.attr( 'data-lazy-src' );
20+
src = $img.attr( 'data-lazy-src' ),
21+
srcset = $img.attr( 'data-lazy-srcset' ),
22+
sizes = $img.attr( 'data-lazy-sizes' );
2123

2224
if ( ! src || 'undefined' === typeof( src ) )
2325
return;
2426

27+
if ( 'undefined' !== typeof( srcset ) && srcset ) {
28+
$img.removeAttr( 'data-lazy-srcset' );
29+
$img.removeAttr( 'data-lazy-sizes' );
30+
img.sizes = sizes;
31+
img.srcset = srcset;
32+
}
33+
2534
$img.unbind( 'scrollin' ) // remove event binding
2635
.hide()
2736
.removeAttr( 'data-lazy-src' )

lazy-load.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static function add_image_placeholders( $content ) {
6262

6363
static function process_image( $matches ) {
6464
// In case you want to change the placeholder image
65-
$placeholder_image = apply_filters( 'lazyload_images_placeholder_image', self::get_url( 'images/1x1.trans.gif' ) );
65+
$placeholder_image = apply_filters( 'lazyload_images_placeholder_image', self::get_url( 'images/1x1.trans.gif' ), $matches );
6666

6767
$old_attributes_str = $matches[2];
6868
$old_attributes = wp_kses_hair( $old_attributes_str, wp_allowed_protocols() );
@@ -73,13 +73,16 @@ static function process_image( $matches ) {
7373

7474
$image_src = $old_attributes['src']['value'];
7575

76-
// Remove src and lazy-src since we manually add them
77-
$new_attributes = $old_attributes;
78-
unset( $new_attributes['src'], $new_attributes['data-lazy-src'] );
76+
// Also retain the srcset, and sizes if present. The latter is to keep w3c validator happy (sizes requires srcset)
77+
$lazy_srcset_attribute = ( ! empty( $old_attributes['srcset'] ) ) ? 'data-lazy-srcset="' . $old_attributes['srcset']['value'] . '"' : '';
78+
$lazy_sizes_attribute = ( ! empty( $old_attributes['sizes'] ) ) ? 'data-lazy-sizes="' . $old_attributes['sizes']['value'] . '"' : '';
79+
// Remove src, lazy-src, srcset, lazy-srcset, sizes and data-lazy-sizes since we manually add them
80+
$new_attributes = $old_attributes;
81+
unset( $new_attributes['src'], $new_attributes['data-lazy-src'], $new_attributes['srcset'], $new_attributes['data-lazy-srcset'], $new_attributes['sizes'], $new_attributes['data-lazy-sizes'] );
7982

8083
$new_attributes_str = self::build_attributes_string( $new_attributes );
81-
82-
return sprintf( '<img src="%1$s" data-lazy-src="%2$s" %3$s><noscript>%4$s</noscript>', esc_url( $placeholder_image ), esc_url( $image_src ), $new_attributes_str, $matches[0] );
84+
$placeholder_url = strpos($placeholder_image, 'data:') == 0 ? $placeholder_image : esc_url( $placeholder_image );
85+
return sprintf( '<img src="%1$s" data-lazy-src="%2$s" %3$s %4$s %5$s><noscript>%6$s</noscript>', $placeholder_url , esc_url( $image_src ), $lazy_srcset_attribute, $lazy_sizes_attribute, $new_attributes_str, $matches[0] );
8386
}
8487

8588
private static function build_attributes_string( $attributes ) {

0 commit comments

Comments
 (0)