Skip to content

Commit f34ab23

Browse files
committed
Only look for auto as the first entry in the sizes attribute.
1 parent 471a1d0 commit f34ab23

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

plugins/auto-sizes/hooks.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function auto_sizes_update_image_attributes( $attr ): array {
3434
}
3535

3636
// Don't add 'auto' to the sizes attribute if it already exists.
37-
if ( auto_sizes_attribute_includes_auto( $attr['sizes'] ) ) {
37+
if ( auto_sizes_attribute_includes_valid_auto( $attr['sizes'] ) ) {
3838
return $attr;
3939
}
4040

@@ -68,7 +68,7 @@ function auto_sizes_update_content_img_tag( $html ): string {
6868
}
6969

7070
// Don't add 'auto' to the sizes attribute if it already exists.
71-
if ( auto_sizes_attribute_includes_auto( $match[1] ) ) {
71+
if ( auto_sizes_attribute_includes_valid_auto( $match[1] ) ) {
7272
return $html;
7373
}
7474

@@ -79,20 +79,22 @@ function auto_sizes_update_content_img_tag( $html ): string {
7979
add_filter( 'wp_content_img_tag', 'auto_sizes_update_content_img_tag' );
8080

8181
/**
82-
* Checks whether the given 'sizes' attribute includes the 'auto' keyword.
82+
* Checks whether the given 'sizes' attribute includes the 'auto' keyword as the first item in the list.
83+
*
84+
* Per the HTML spec, if present it must be the first entry.
8385
*
8486
* @since n.e.x.t
8587
*
8688
* @param string $sizes_attr The 'sizes' attribute value.
8789
* @return bool True if the 'auto' keyword is present, false otherwise.
8890
*/
89-
function auto_sizes_attribute_includes_auto( string $sizes_attr ): bool {
91+
function auto_sizes_attribute_includes_valid_auto( string $sizes_attr ): bool {
9092
$parts = preg_split( '/\s*,\s*/', strtolower( trim( $sizes_attr ) ) );
91-
if ( ! is_array( $parts ) ) {
93+
if ( ! is_array( $parts ) || ! isset( $parts[0] ) ) {
9294
return false;
9395
}
9496

95-
return in_array( 'auto', $parts, true );
97+
return 'auto' === $parts[0];
9698
}
9799

98100
/**

plugins/auto-sizes/tests/test-auto-sizes.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function test_content_image_without_lazy_loading_does_not_have_auto_sizes
9898
* Test generated markup for an image with 'auto' keyword already present in sizes does not receive it again.
9999
*
100100
* @covers ::auto_sizes_update_image_attributes
101-
* @covers ::auto_sizes_attribute_includes_auto
101+
* @covers ::auto_sizes_attribute_includes_valid_auto
102102
* @dataProvider data_image_with_existing_auto_sizes
103103
*/
104104
public function test_image_with_existing_auto_sizes_is_not_processed_again( string $initial_sizes, bool $expected_processed ): void {
@@ -123,7 +123,7 @@ public function test_image_with_existing_auto_sizes_is_not_processed_again( stri
123123
* Test content filtered markup with 'auto' keyword already present in sizes does not receive it again.
124124
*
125125
* @covers ::auto_sizes_update_content_img_tag
126-
* @covers ::auto_sizes_attribute_includes_auto
126+
* @covers ::auto_sizes_attribute_includes_valid_auto
127127
* @dataProvider data_image_with_existing_auto_sizes
128128
*/
129129
public function test_content_image_with_existing_auto_sizes_is_not_processed_again( string $initial_sizes, bool $expected_processed ): void {
@@ -168,33 +168,41 @@ public function data_image_with_existing_auto_sizes(): array {
168168
'auto, (max-width: 1024px) 100vw, 1024px',
169169
false,
170170
),
171+
'sole keyword' => array(
172+
'auto',
173+
false,
174+
),
175+
'with space before' => array(
176+
' auto, (max-width: 1024px) 100vw, 1024px',
177+
false,
178+
),
179+
'with uppercase' => array(
180+
'AUTO, (max-width: 1024px) 100vw, 1024px',
181+
false,
182+
),
183+
184+
/*
185+
* The following scenarios technically include the 'auto' keyword,
186+
* but it is in the wrong place, as per the HTML spec it must be
187+
* the first entry in the list.
188+
* Therefore in these invalid cases the 'auto' keyword should still
189+
* be added to the beginning of the list.
190+
*/
171191
'within, without space' => array(
172192
'(max-width: 1024px) 100vw, auto,1024px',
173-
false,
193+
true,
174194
),
175195
'within, with space' => array(
176196
'(max-width: 1024px) 100vw, auto, 1024px',
177-
false,
197+
true,
178198
),
179199
'at the end, without space' => array(
180200
'(max-width: 1024px) 100vw,auto',
181-
false,
201+
true,
182202
),
183203
'at the end, with space' => array(
184204
'(max-width: 1024px) 100vw, auto',
185-
false,
186-
),
187-
'sole keyword' => array(
188-
'auto',
189-
false,
190-
),
191-
'with space at beginning' => array(
192-
' auto, (max-width: 1024px) 100vw, 1024px',
193-
false,
194-
),
195-
'with uppercase' => array(
196-
'AUTO, (max-width: 1024px) 100vw, 1024px',
197-
false,
205+
true,
198206
),
199207
);
200208
}

0 commit comments

Comments
 (0)