Skip to content

Commit f570701

Browse files
committed
Media: Avoid images with sizes=auto to be displayed downsized in supporting browsers.
Based on the user agent stylesheet rules outlined in https://html.spec.whatwg.org/multipage/rendering.html#img-contain-size, images that have `sizes=auto` while applying `width: auto` or `width: fit-content` would be constrained to only 300px width. This changeset overrides said user agent stylesheet rule with a much larger constraint, to avoid the problem. Additionally, it introduces a filter `wp_img_tag_add_auto_sizes` which can be used to opt out of the functionality, as an additional measure. Reviewed by desrosj, joemcgill. Merges [59415] to the 6.7 branch. Props joemcgill, flixos90, dooperweb, SirLouen, azaozz, mukesh27, apermo. Fixes #62413. See #61847, #62345. git-svn-id: https://develop.svn.wordpress.org/branches/6.7@59435 602fd350-edb4-49c9-b593-d223f7449a82
1 parent bad04a8 commit f570701

File tree

3 files changed

+145
-22
lines changed

3 files changed

+145
-22
lines changed

src/wp-includes/default-filters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@
611611
add_action( 'wp_default_styles', 'wp_default_styles' );
612612
add_filter( 'style_loader_src', 'wp_style_loader_src', 10, 2 );
613613

614+
add_action( 'wp_head', 'wp_print_auto_sizes_contain_css_fix', 1 );
614615
add_action( 'wp_head', 'wp_maybe_inline_styles', 1 ); // Run for styles enqueued in <head>.
615616
add_action( 'wp_footer', 'wp_maybe_inline_styles', 1 ); // Run for late-loaded styles in the footer.
616617

src/wp-includes/media.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,8 +1137,12 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
11371137
}
11381138
}
11391139

1140+
/** This filter is documented in wp-includes/media.php */
1141+
$add_auto_sizes = apply_filters( 'wp_img_tag_add_auto_sizes', true );
1142+
11401143
// Adds 'auto' to the sizes attribute if applicable.
11411144
if (
1145+
$add_auto_sizes &&
11421146
isset( $attr['loading'] ) &&
11431147
'lazy' === $attr['loading'] &&
11441148
isset( $attr['sizes'] ) &&
@@ -1985,6 +1989,17 @@ function wp_filter_content_tags( $content, $context = null ) {
19851989
* @return string The filtered image tag markup.
19861990
*/
19871991
function wp_img_tag_add_auto_sizes( string $image ): string {
1992+
/**
1993+
* Filters whether auto-sizes for lazy loaded images is enabled.
1994+
*
1995+
* @since 6.7.1
1996+
*
1997+
* @param boolean $enabled Whether auto-sizes for lazy loaded images is enabled.
1998+
*/
1999+
if ( ! apply_filters( 'wp_img_tag_add_auto_sizes', true ) ) {
2000+
return $image;
2001+
}
2002+
19882003
$processor = new WP_HTML_Tag_Processor( $image );
19892004

19902005
// Bail if there is no IMG tag.
@@ -1993,8 +2008,19 @@ function wp_img_tag_add_auto_sizes( string $image ): string {
19932008
}
19942009

19952010
// Bail early if the image is not lazy-loaded.
1996-
$value = $processor->get_attribute( 'loading' );
1997-
if ( ! is_string( $value ) || 'lazy' !== strtolower( trim( $value, " \t\f\r\n" ) ) ) {
2011+
$loading = $processor->get_attribute( 'loading' );
2012+
if ( ! is_string( $loading ) || 'lazy' !== strtolower( trim( $loading, " \t\f\r\n" ) ) ) {
2013+
return $image;
2014+
}
2015+
2016+
/*
2017+
* Bail early if the image doesn't have a width attribute.
2018+
* Per WordPress Core itself, lazy-loaded images should always have a width attribute.
2019+
* However, it is possible that lazy-loading could be added by a plugin, where we don't have that guarantee.
2020+
* As such, it still makes sense to ensure presence of a width attribute here in order to use `sizes=auto`.
2021+
*/
2022+
$width = $processor->get_attribute( 'width' );
2023+
if ( ! is_string( $width ) || '' === $width ) {
19982024
return $image;
19992025
}
20002026

@@ -2029,6 +2055,28 @@ function wp_sizes_attribute_includes_valid_auto( string $sizes_attr ): bool {
20292055
return 'auto' === strtolower( trim( $first_size, " \t\f\r\n" ) );
20302056
}
20312057

2058+
/**
2059+
* Prints a CSS rule to fix potential visual issues with images using `sizes=auto`.
2060+
*
2061+
* This rule overrides the similar rule in the default user agent stylesheet, to avoid images that use e.g.
2062+
* `width: auto` or `width: fit-content` to appear smaller.
2063+
*
2064+
* @since 6.7.1
2065+
* @see https://html.spec.whatwg.org/multipage/rendering.html#img-contain-size
2066+
* @see https://core.trac.wordpress.org/ticket/62413
2067+
*/
2068+
function wp_print_auto_sizes_contain_css_fix() {
2069+
/** This filter is documented in wp-includes/media.php */
2070+
$add_auto_sizes = apply_filters( 'wp_img_tag_add_auto_sizes', true );
2071+
if ( ! $add_auto_sizes ) {
2072+
return;
2073+
}
2074+
2075+
?>
2076+
<style>img:is([sizes="auto" i], [sizes^="auto," i]) { contain-intrinsic-size: 3000px 1500px }</style>
2077+
<?php
2078+
}
2079+
20322080
/**
20332081
* Adds optimization attributes to an `img` HTML tag.
20342082
*

0 commit comments

Comments
 (0)