Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/class-tiny-picture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion src/class-tiny-source-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down
24 changes: 24 additions & 0 deletions test/unit/TinyPictureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<img alt="no source">';
$expected = '<img alt="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 = '<img srcset="/wp-content/uploads/2025/09/test_250x250.png 350w" alt="no source but has srcset">';
$expected = '<picture><source srcset="/wp-content/uploads/2025/09/test_250x250.webp 350w" type="image/webp" /><img srcset="/wp-content/uploads/2025/09/test_250x250.png 350w" alt="no source but has srcset"></picture>';
$output = $this->tiny_picture->replace_sources($input);

$this->assertSame($expected, $output);
}
}