From 5423fb2a8dc5cbc5344c0612597e0c52b167dd29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Mon, 5 Aug 2024 09:31:59 -0400 Subject: [PATCH] Remove loading=lazy attribute on images excluded from lazyload (#121) --- Tests/Fixtures/image/images.html | 4 ++-- Tests/Fixtures/image/imageslazyloaded.html | 4 ++-- Tests/Fixtures/image/imageslazyloadednative.html | 4 ++-- Tests/Fixtures/image/imageslazyloadednofallback.html | 4 ++-- src/Image.php | 11 ++++++++++- 5 files changed, 18 insertions(+), 9 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 @@ - - + + diff --git a/src/Image.php b/src/Image.php index bd77d8f..e1053da 100644 --- a/src/Image.php +++ b/src/Image.php @@ -30,9 +30,18 @@ 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 ); + + if ( null === $image_no_lazy ) { + continue; + } + + $html = str_replace( $original_image, $image_no_lazy, $html ); + continue; }