Skip to content

Commit 41706fd

Browse files
authored
Merge pull request #1476 from WordPress/update/auto-sizes-code-reuse
Move Auto Sizes logic from Enhanced Responsive Images to Image Prioritizer
2 parents 38811ea + b0a9c3f commit 41706fd

10 files changed

+237
-200
lines changed

plugins/auto-sizes/auto-sizes.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,3 @@
2828
define( 'IMAGE_AUTO_SIZES_VERSION', '1.2.0' );
2929

3030
require_once __DIR__ . '/hooks.php';
31-
32-
require_once __DIR__ . '/optimization-detective.php';

plugins/auto-sizes/hooks.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ function auto_sizes_update_content_img_tag( $html ): string {
9898
* @return bool True if the 'auto' keyword is present, false otherwise.
9999
*/
100100
function auto_sizes_attribute_includes_valid_auto( string $sizes_attr ): bool {
101-
$token = strtok( strtolower( $sizes_attr ), ',' );
102-
return false !== $token && 'auto' === trim( $token, " \t\f\r\n" );
101+
list( $first_size ) = explode( ',', $sizes_attr, 2 );
102+
return 'auto' === strtolower( trim( $first_size, " \t\f\r\n" ) );
103103
}
104104

105105
/**

plugins/auto-sizes/optimization-detective.php

Lines changed: 0 additions & 66 deletions
This file was deleted.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ public function data_provider_to_test_auto_sizes_update_content_img_tag(): array
262262
'input' => '<img src="https://example.com/foo-300x225.jpg" srcset="https://example.com/foo-300x225.jpg 300w, https://example.com/foo-1024x768.jpg 1024w, https://example.com/foo-768x576.jpg 768w, https://example.com/foo-1536x1152.jpg 1536w, https://example.com/foo-2048x1536.jpg 2048w" sizes=auto loading="lazy">',
263263
'expected' => '<img src="https://example.com/foo-300x225.jpg" srcset="https://example.com/foo-300x225.jpg 300w, https://example.com/foo-1024x768.jpg 1024w, https://example.com/foo-768x576.jpg 768w, https://example.com/foo-1536x1152.jpg 1536w, https://example.com/foo-2048x1536.jpg 2048w" sizes=auto loading="lazy">',
264264
),
265+
'expected_when_auto_size_preceded_by_extra_commas' => array(
266+
'input' => '<img src="https://example.com/foo-300x225.jpg" srcset="https://example.com/foo-300x225.jpg 300w, https://example.com/foo-1024x768.jpg 1024w, https://example.com/foo-768x576.jpg 768w, https://example.com/foo-1536x1152.jpg 1536w, https://example.com/foo-2048x1536.jpg 2048w" sizes=",,,,,,,,,auto, (max-width: 650px) 100vw, 650px" loading="lazy">',
267+
'expected' => '<img src="https://example.com/foo-300x225.jpg" srcset="https://example.com/foo-300x225.jpg 300w, https://example.com/foo-1024x768.jpg 1024w, https://example.com/foo-768x576.jpg 768w, https://example.com/foo-1536x1152.jpg 1536w, https://example.com/foo-2048x1536.jpg 2048w" sizes="auto, ,,,,,,,,,auto, (max-width: 650px) 100vw, 650px" loading="lazy">',
268+
),
265269
);
266270
}
267271

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

Lines changed: 0 additions & 123 deletions
This file was deleted.

plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,28 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool {
4040

4141
$xpath = $processor->get_xpath();
4242

43+
/**
44+
* Gets attribute value.
45+
*
46+
* @param string $attribute_name Attribute name.
47+
* @return string|true|null Normalized attribute value.
48+
*/
49+
$get_attribute_value = static function ( string $attribute_name ) use ( $processor ) {
50+
$value = $processor->get_attribute( $attribute_name );
51+
if ( is_string( $value ) ) {
52+
$value = strtolower( trim( $value, " \t\f\r\n" ) );
53+
}
54+
return $value;
55+
};
56+
4357
/*
4458
* When the same LCP element is common/shared among all viewport groups, make sure that the element has
4559
* fetchpriority=high, even though it won't really be needed because a preload link with fetchpriority=high
4660
* will also be added. Additionally, ensure that this common LCP element is never lazy-loaded.
4761
*/
4862
$common_lcp_element = $context->url_metrics_group_collection->get_common_lcp_element();
4963
if ( ! is_null( $common_lcp_element ) && $xpath === $common_lcp_element['xpath'] ) {
50-
if ( 'high' === $processor->get_attribute( 'fetchpriority' ) ) {
64+
if ( 'high' === $get_attribute_value( 'fetchpriority' ) ) {
5165
$processor->set_meta_attribute( 'fetchpriority-already-added', true );
5266
} else {
5367
$processor->set_attribute( 'fetchpriority', 'high' );
@@ -81,7 +95,7 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool {
8195
} else {
8296
// Otherwise, make sure visible elements omit the loading attribute, and hidden elements include loading=lazy.
8397
$is_visible = $element_max_intersection_ratio > 0.0;
84-
$loading = (string) $processor->get_attribute( 'loading' );
98+
$loading = $get_attribute_value( 'loading' );
8599
if ( $is_visible && 'lazy' === $loading ) {
86100
$processor->remove_attribute( 'loading' );
87101
} elseif ( ! $is_visible && 'lazy' !== $loading ) {
@@ -90,6 +104,23 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool {
90104
}
91105
// TODO: If an image is visible in one breakpoint but not another, add loading=lazy AND add a regular-priority preload link with media queries (unless LCP in which case it should already have a fetchpriority=high link) so that the image won't be eagerly-loaded for viewports on which it is not shown.
92106

107+
// Ensure that sizes=auto is set properly.
108+
$sizes = $processor->get_attribute( 'sizes' );
109+
if ( is_string( $sizes ) ) {
110+
$is_lazy = 'lazy' === $get_attribute_value( 'loading' );
111+
$has_auto = $this->sizes_attribute_includes_valid_auto( $sizes );
112+
113+
if ( $is_lazy && ! $has_auto ) {
114+
$processor->set_attribute( 'sizes', "auto, $sizes" );
115+
} elseif ( ! $is_lazy && $has_auto ) {
116+
// Remove auto from the beginning of the list.
117+
$processor->set_attribute(
118+
'sizes',
119+
(string) preg_replace( '/^[ \t\f\r\n]*auto[ \t\f\r\n]*(,[ \t\f\r\n]*)?/i', '', $sizes )
120+
);
121+
}
122+
}
123+
93124
// If this element is the LCP (for a breakpoint group), add a preload link for it.
94125
foreach ( $context->url_metrics_group_collection->get_groups_by_lcp_element( $xpath ) as $group ) {
95126
$link_attributes = array_merge(
@@ -110,8 +141,8 @@ static function ( string $value ): bool {
110141
)
111142
);
112143

113-
$crossorigin = $processor->get_attribute( 'crossorigin' );
114-
if ( is_string( $crossorigin ) ) {
144+
$crossorigin = $get_attribute_value( 'crossorigin' );
145+
if ( null !== $crossorigin ) {
115146
$link_attributes['crossorigin'] = 'use-credentials' === $crossorigin ? 'use-credentials' : 'anonymous';
116147
}
117148

@@ -126,4 +157,24 @@ static function ( string $value ): bool {
126157

127158
return true;
128159
}
160+
161+
/**
162+
* Checks whether the given 'sizes' attribute includes the 'auto' keyword as the first item in the list.
163+
*
164+
* Per the HTML spec, if present it must be the first entry.
165+
*
166+
* @since n.e.x.t
167+
*
168+
* @param string $sizes_attr The 'sizes' attribute value.
169+
* @return bool True if the 'auto' keyword is present, false otherwise.
170+
*/
171+
private function sizes_attribute_includes_valid_auto( string $sizes_attr ): bool {
172+
if ( function_exists( 'wp_sizes_attribute_includes_valid_auto' ) ) {
173+
return wp_sizes_attribute_includes_valid_auto( $sizes_attr );
174+
} elseif ( function_exists( 'auto_sizes_attribute_includes_valid_auto' ) ) {
175+
return auto_sizes_attribute_includes_valid_auto( $sizes_attr );
176+
} else {
177+
return 'auto' === $sizes_attr || str_starts_with( $sizes_attr, 'auto,' );
178+
}
179+
}
129180
}

plugins/image-prioritizer/tests/test-cases/common-lcp-image-and-lazy-loaded-image-outside-viewport-with-fully-populated-sample-data.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<p>Now the following image is definitely outside the initial viewport.</p>
5656
<img src="https://example.com/baz.jpg" alt="Baz" width="10" height="10" fetchpriority="high">
5757
<img src="https://example.com/qux.jpg" alt="Qux" width="10" height="10" fetchpriority="high" loading="eager">
58-
<img src="https://example.com/quux.jpg" alt="Quux" width="10" height="10" loading="lazy"><!-- This one is all good. -->
58+
<img src="https://example.com/quux.jpg" alt="Quux" width="10" height="10" loading="LAZY"><!-- This one is all good. -->
5959
</body>
6060
</html>
6161
',
@@ -73,7 +73,7 @@
7373
<p>Now the following image is definitely outside the initial viewport.</p>
7474
<img data-od-added-loading data-od-removed-fetchpriority="high" loading="lazy" src="https://example.com/baz.jpg" alt="Baz" width="10" height="10" >
7575
<img data-od-removed-fetchpriority="high" data-od-replaced-loading="eager" src="https://example.com/qux.jpg" alt="Qux" width="10" height="10" loading="lazy">
76-
<img src="https://example.com/quux.jpg" alt="Quux" width="10" height="10" loading="lazy"><!-- This one is all good. -->
76+
<img src="https://example.com/quux.jpg" alt="Quux" width="10" height="10" loading="LAZY"><!-- This one is all good. -->
7777
</body>
7878
</html>
7979
',

0 commit comments

Comments
 (0)