Print IMG auto-sizes contain CSS fix by enqueueing inline style#8954
Print IMG auto-sizes contain CSS fix by enqueueing inline style#8954westonruter wants to merge 10 commits intoWordPress:trunkfrom
IMG auto-sizes contain CSS fix by enqueueing inline style#8954Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
| wp_add_inline_style( $handle, 'img:is([sizes="auto" i], [sizes^="auto," i]) { contain-intrinsic-size: 3000px 1500px }' ); | ||
|
|
||
| // Make sure inline style is printed first. | ||
| array_unshift( wp_styles()->queue, $handle ); |
There was a problem hiding this comment.
This seems a bit hacky to me, but I understand the purpose. I assume there's not a better way to control the order of registered styles besides maybe making sure that the wp_register_style() call is made on an earlier hook, like init, correct?
src/wp-includes/media.php
Outdated
| * @link https://html.spec.whatwg.org/multipage/rendering.html#img-contain-size | ||
| * @link https://core.trac.wordpress.org/ticket/62413 | ||
| */ | ||
| function wp_print_auto_sizes_contain_css_fix() { |
There was a problem hiding this comment.
I don't love this name now, since it's implying this function does something that it doesn't.
We could potentially deprecate (effectively rename) it, though let's discuss how reasonable that would be.
- Obviously there's a BC risk if people expect this function to be hooked into
wp_headand want to remove it to disable the snippet. - However, there's also a BC risk if we just change what this function does, in case someone calls it manually somewhere else.
- If someone unhooks this function to disable the snippet, and we wanted to replace it with a new function, we could still add the hook first, see if it is removed, and only before the function would be called ensure it is not invoked. This way we know whether it was removed, and could "forward" this removal to the new function.
- I believe we did something like that in Core somewhere else before, but I can't recall where exactly.
- A nice side-effect of deprecating / replacing would be that we could also use the proper hook
wp_enqueue_scriptsfor the new function.
There was a problem hiding this comment.
@felixarntz see e7144a3 for that which was the original approach I took.
I believe we did something like that in Core somewhere else before, but I can't recall where exactly.
Yes. We did it with printing the emoji styles in Core-58775.
There was a problem hiding this comment.
There was a problem hiding this comment.
If we go with the function rename and move from wp_head to wp_enqueue_scripts, since this function was introduced with the There are only 4 examples of this being done on GitHub.wp_img_tag_add_auto_sizes filter to disable the style, it seems overkill to also retain back-compat to see if someone removed the action on wp_head. If they had done this, they were essentially doing it wrong.
Update: The wp_img_tag_add_auto_sizes filter doesn't just prevent the style from being printed. It also omits sizes=auto from the IMG tags as well. See #8954 (comment)
There was a problem hiding this comment.
I've opened pull requests to use the filter instead:
- Use correct mechanism to disable auto-sizes CSS fix szepeviktor/zero-bytes-theme#3 (Revert "Use correct mechanism to disable auto-sizes CSS fix" szepeviktor/zero-bytes-theme#4)
- Use correct mechanism to disable auto-sizes CSS fix yuupiyo/MyPortfolio#1
- Use correct mechanism to disable auto-sizes CSS fix pavelleonenko007/Utopia-wordpress-theme#1
- Use correct mechanism to disable auto-sizes CSS fix conedevelopment/base-starter-theme#4
In any case, if someone doesn't update the hook, the risk of breakage is very low, as it will just mean an additional STYLE is output unexpectedly.
There was a problem hiding this comment.
@felixarntz I believe your concerns are addressed by introducing a separate function here. If so, please approve the PR.
There was a problem hiding this comment.
After re-examining the purpose of the wp_img_tag_add_auto_sizes filter, I realize I was wrong to suggest that it was the right way to prevent the style from being printed. See #8954 (comment)
…into fix/trac-62731
|
I just merged in the latest from <style id='wp-img-auto-sizes-contain-inline-css'>
img:is([sizes="auto" i], [sizes^="auto," i]) { contain-intrinsic-size: 3000px 1500px }
/*# sourceURL=wp-img-auto-sizes-contain-inline-css */
</style> |
| * @see https://core.trac.wordpress.org/ticket/62413 | ||
| */ | ||
| function wp_print_auto_sizes_contain_css_fix() { | ||
| function wp_enqueue_img_auto_sizes_contain_css_fix(): void { |
There was a problem hiding this comment.
Shouldn't we, either within this function, or in some other logic that runs on a hook shortly before this, check for has_action( 'wp_head', 'wp_print_auto_sizes_contain_css_fix' ), and only remove the deprecated action at the point that we know it wasn't intentionally removed by any 3P code?
I think we should include this for BC, to make sure sites that removed the original hook don't see adverse effects now because we changed the hook. This is similar to how we did it elsewhere before I think.
There was a problem hiding this comment.
@felixarntz Thanks for raising this again. After re-examining the purpose of the wp_img_tag_add_auto_sizes filter, I realize I was wrong to suggest that it was the right way to prevent the style from being printed. See #8954 (comment)
Co-authored-by: Felix Arntz <flixos90@git.wordpress.org>
|
In 67d527c I've addressed the concerns with backwards-compatibility with themes/plugins that have intended to prevent this style from being printed by unhooking the action via: remove_action( 'wp_head', 'wp_print_auto_sizes_contain_css_fix', 1 );In looking at this further, I realize I was mistaken in thinking that the In order to maintain backwards-compatibility, the now-deprecated I've added tests for these various scenarios. |
felixarntz
left a comment
There was a problem hiding this comment.
@westonruter Thank you for the updates, looks good to go IMO!
…` tags having `sizes=auto`.
This deprecates the `wp_print_auto_sizes_contain_css_fix()` function running at `wp_head` priority 1, in favor of a `wp_enqueue_img_auto_sizes_contain_css_fix()` function which runs just before at `wp_head` priority 0. The latter function unhooks the former while also enqueueing an inline style to be printed with all other styles but up front to preserve the cascade. This eliminates directly printing the `STYLE` tag, which was a change done similarly before for the emoji styles. See #58775.
For backwards compatibility, the CSS can still be prevented from being enqueued/printed via:
remove_action( 'wp_head', 'wp_print_auto_sizes_contain_css_fix', 1 );
This change ensures that all styles are printed together using the correct API for emitting styles.
Developed in #8954.
Follow-up to [59435].
Props westonruter, sabernhardt, SirLouen, flixos90, joemcgill, SergeyBiryukov, superpoincare.
See #62413.
Fixes #62731.
git-svn-id: https://develop.svn.wordpress.org/trunk@60910 602fd350-edb4-49c9-b593-d223f7449a82
…` tags having `sizes=auto`.
This deprecates the `wp_print_auto_sizes_contain_css_fix()` function running at `wp_head` priority 1, in favor of a `wp_enqueue_img_auto_sizes_contain_css_fix()` function which runs just before at `wp_head` priority 0. The latter function unhooks the former while also enqueueing an inline style to be printed with all other styles but up front to preserve the cascade. This eliminates directly printing the `STYLE` tag, which was a change done similarly before for the emoji styles. See #58775.
For backwards compatibility, the CSS can still be prevented from being enqueued/printed via:
remove_action( 'wp_head', 'wp_print_auto_sizes_contain_css_fix', 1 );
This change ensures that all styles are printed together using the correct API for emitting styles.
Developed in WordPress/wordpress-develop#8954.
Follow-up to [59435].
Props westonruter, sabernhardt, SirLouen, flixos90, joemcgill, SergeyBiryukov, superpoincare.
See #62413.
Fixes #62731.
Built from https://develop.svn.wordpress.org/trunk@60910
git-svn-id: http://core.svn.wordpress.org/trunk@60246 1a063a9b-81f0-0310-95a4-ce76da25c4cd
…` tags having `sizes=auto`.
This deprecates the `wp_print_auto_sizes_contain_css_fix()` function running at `wp_head` priority 1, in favor of a `wp_enqueue_img_auto_sizes_contain_css_fix()` function which runs just before at `wp_head` priority 0. The latter function unhooks the former while also enqueueing an inline style to be printed with all other styles but up front to preserve the cascade. This eliminates directly printing the `STYLE` tag, which was a change done similarly before for the emoji styles. See #58775.
For backwards compatibility, the CSS can still be prevented from being enqueued/printed via:
remove_action( 'wp_head', 'wp_print_auto_sizes_contain_css_fix', 1 );
This change ensures that all styles are printed together using the correct API for emitting styles.
Developed in WordPress/wordpress-develop#8954.
Follow-up to [59435].
Props westonruter, sabernhardt, SirLouen, flixos90, joemcgill, SergeyBiryukov, superpoincare.
See #62413.
Fixes #62731.
Built from https://develop.svn.wordpress.org/trunk@60910
git-svn-id: https://core.svn.wordpress.org/trunk@60246 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Trac ticket: https://core.trac.wordpress.org/ticket/62731
Instead of manually printing the
STYLEtag to fixsizes=autoissue with images, this enqueues the inline style viaWP_Styles. This ensures the styles are printed with other styles as opposed to being printed very early, even before theTITLEtag. This also allows plugins to do additional processing of the stylesheet prior to it being printed. Note thatarray_unshift( wp_scripts()->queue, $handle )is used instead ofwp_enqueue_style( $handle )in order to ensure that the stylesheet is printed before any others, preserving the existing cascade.This is very similar to the change that was made to replace
print_emoji_styles()withwp_enqueue_emoji_styles()in Core-58775.Diff on rendered page (after Prettier formatting):
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.