Skip to content

Commit ae0b6db

Browse files
fix: sizes attribute on sourceset (#101)
1 parent 4f2cfd2 commit ae0b6db

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/class-tiny-source-base.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ protected function create_alternative_sources( $original_source_html ) {
240240
$source_attr_parts[ $attr ] = $attr_value;
241241
}
242242
}
243+
} else {
244+
$sizes_value = $this->get_attribute_value( $original_source_html, 'sizes' );
245+
if ( $sizes_value ) {
246+
$source_attr_parts['sizes'] = $sizes_value;
247+
}
243248
}
244249

245250
$source_attr_parts['type'] = $mimetype;

test/unit/TinyPictureTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,21 @@ public function test_image_without_src() {
366366
$this->assertSame($expected, $output);
367367
}
368368

369+
/**
370+
* sizes attribute on <img> also have to be set to <source> elements
371+
*/
372+
public function test_img_sizes_attribute_is_propagated_to_sources() {
373+
$this->wp->createImage(1000, '2026/04', 'test-320w.webp');
374+
$this->wp->createImage(1000, '2026/04', 'test-640w.webp');
375+
$this->wp->createImage(1000, '2026/04', 'test.webp');
376+
377+
$input = '<img srcset="/wp-content/uploads/2026/04/test-320w.jpg 320w, /wp-content/uploads/2026/04/test-640w.jpg 640w" sizes="(max-width: 600px) 320px, 640px" src="/wp-content/uploads/2026/04/test.jpg">';
378+
$expected = '<picture><source srcset="/wp-content/uploads/2026/04/test-320w.webp 320w, /wp-content/uploads/2026/04/test-640w.webp 640w" sizes="(max-width: 600px) 320px, 640px" type="image/webp" /><img srcset="/wp-content/uploads/2026/04/test-320w.jpg 320w, /wp-content/uploads/2026/04/test-640w.jpg 640w" sizes="(max-width: 600px) 320px, 640px" src="/wp-content/uploads/2026/04/test.jpg"></picture>';
379+
$output = $this->tiny_picture->replace_sources($input);
380+
381+
$this->assertSame($expected, $output);
382+
}
383+
369384
/**
370385
* Images with only a srcset are valid and will be wrapped
371386
*/

0 commit comments

Comments
 (0)