diff --git a/src/class-tiny-picture.php b/src/class-tiny-picture.php index bde4441a..ab318131 100644 --- a/src/class-tiny-picture.php +++ b/src/class-tiny-picture.php @@ -195,6 +195,10 @@ private function filter_images( $content ) { } $images = array(); foreach ( $matches[0] as $img ) { + // Skip images without src or srcset + if ( ! preg_match( '/\b(?:src|srcset)\s*=/i', $img ) ) { + continue; + } $images[] = new Tiny_Source_Image( $img, $this->base_dir, diff --git a/src/class-tiny-source-base.php b/src/class-tiny-source-base.php index 22e254b8..859b208d 100644 --- a/src/class-tiny-source-base.php +++ b/src/class-tiny-source-base.php @@ -189,7 +189,10 @@ protected function create_alternative_sources( $original_source_html ) { $srcsets = $this->get_image_srcsets( $original_source_html ); if ( empty( $srcsets ) ) { // no srcset, try src attribute - $srcsets[] = $this->get_image_src( $original_source_html ); + $src = $this->get_image_src( $original_source_html ); + if ( ! empty( $src ) ) { + $srcsets[] = $src; + } } if ( empty( $srcsets ) ) { diff --git a/test/unit/TinyPictureTest.php b/test/unit/TinyPictureTest.php index 79ee7fda..68ba2959 100644 --- a/test/unit/TinyPictureTest.php +++ b/test/unit/TinyPictureTest.php @@ -354,4 +354,28 @@ public function test_does_not_register_hooks_when_pagebuilder_request() $_GET = array(); } + + /** + * images that have no src or srcset will be unchanged + */ + public function test_image_without_src() { + $input = 'no source'; + $expected = 'no source'; + $output = $this->tiny_picture->replace_sources($input); + + $this->assertSame($expected, $output); + } + + /** + * Images with only a srcset are valid and will be wrapped + */ + public function test_image_with_only_srcset() { + $this->wp->createImage(37857, '2025/09', 'test_250x250.webp'); + + $input = 'no source but has srcset'; + $expected = 'no source but has srcset'; + $output = $this->tiny_picture->replace_sources($input); + + $this->assertSame($expected, $output); + } }