From b89492a987f579583d2fbacac112bc79d30b7471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 2 Aug 2024 15:31:22 -0400 Subject: [PATCH 1/3] remove loading lazy from images excluded from lazyload --- src/Image.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Image.php b/src/Image.php index bd77d8f..0f54e06 100644 --- a/src/Image.php +++ b/src/Image.php @@ -30,9 +30,14 @@ public function lazyloadImages( $html, $buffer, $use_native = true ) { $images = array_unique( $images, SORT_REGULAR ); foreach ( $images as $image ) { - $image = $this->canLazyload( $image ); + $original_image = $image; + $image = $this->canLazyload( $image ); if ( ! $image ) { + $image_no_lazy = preg_replace( '/loading=["\']lazy["\']/i', '', $original_image ); + + $html = str_replace( $original_image, $image_no_lazy, $html ); + continue; } From 0ff7459837631cb58b51edf308897902e147d7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 2 Aug 2024 15:31:27 -0400 Subject: [PATCH 2/3] update fixtures --- Tests/Fixtures/image/images.html | 4 ++-- Tests/Fixtures/image/imageslazyloaded.html | 4 ++-- Tests/Fixtures/image/imageslazyloadednative.html | 4 ++-- Tests/Fixtures/image/imageslazyloadednofallback.html | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Tests/Fixtures/image/images.html b/Tests/Fixtures/image/images.html index 8cd299c..b388b34 100644 --- a/Tests/Fixtures/image/images.html +++ b/Tests/Fixtures/image/images.html @@ -27,5 +27,5 @@ - - + + diff --git a/Tests/Fixtures/image/imageslazyloaded.html b/Tests/Fixtures/image/imageslazyloaded.html index 6fb2cb3..c7b1294 100644 --- a/Tests/Fixtures/image/imageslazyloaded.html +++ b/Tests/Fixtures/image/imageslazyloaded.html @@ -27,5 +27,5 @@ - - + + diff --git a/Tests/Fixtures/image/imageslazyloadednative.html b/Tests/Fixtures/image/imageslazyloadednative.html index b6f299e..e90f4a7 100644 --- a/Tests/Fixtures/image/imageslazyloadednative.html +++ b/Tests/Fixtures/image/imageslazyloadednative.html @@ -27,5 +27,5 @@ - - + + diff --git a/Tests/Fixtures/image/imageslazyloadednofallback.html b/Tests/Fixtures/image/imageslazyloadednofallback.html index 7ca0e26..6e9e3b1 100644 --- a/Tests/Fixtures/image/imageslazyloadednofallback.html +++ b/Tests/Fixtures/image/imageslazyloadednofallback.html @@ -27,5 +27,5 @@ - - + + From c7cba59e3b075e53562a8e1fbc9527d327df70e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 2 Aug 2024 15:46:18 -0400 Subject: [PATCH 3/3] add guard if null --- src/Image.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Image.php b/src/Image.php index 0f54e06..e1053da 100644 --- a/src/Image.php +++ b/src/Image.php @@ -31,11 +31,15 @@ public function lazyloadImages( $html, $buffer, $use_native = true ) { foreach ( $images as $image ) { $original_image = $image; - $image = $this->canLazyload( $image ); + $image = $this->canLazyload( $image ); if ( ! $image ) { $image_no_lazy = preg_replace( '/loading=["\']lazy["\']/i', '', $original_image ); + if ( null === $image_no_lazy ) { + continue; + } + $html = str_replace( $original_image, $image_no_lazy, $html ); continue;