-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Harden logic to add auto
keyword to sizes
attribute to prevent duplicate keyword
#1445
Conversation
Co-authored-by: Weston Ruter <[email protected]>
…d in combination with auto.
'within, without space' => array( | ||
'(max-width: 1024px) 100vw, auto,1024px', | ||
false, | ||
), | ||
'within, with space' => array( | ||
'(max-width: 1024px) 100vw, auto, 1024px', | ||
false, | ||
), | ||
'at the end, without space' => array( | ||
'(max-width: 1024px) 100vw,auto', | ||
false, | ||
), | ||
'at the end, with space' => array( | ||
'(max-width: 1024px) 100vw, auto', | ||
false, | ||
), | ||
'sole keyword' => array( | ||
'auto', | ||
false, | ||
), | ||
'with space at beginning' => array( | ||
' auto, (max-width: 1024px) 100vw, 1024px', | ||
false, | ||
), | ||
'with uppercase' => array( | ||
'AUTO, (max-width: 1024px) 100vw, 1024px', | ||
false, | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: All of these would have failed prior to the production code changes in this PR.
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
@felixarntz can you give an example of what problem this is actually solving? Per the HTML spec, |
Is that correct? Will the value not be parsed if it's placed elsewhere? Regardless, if that's the case, some valid usages were not covered by the implementation so far, such as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to see us consolidate some of this early logic for checking whether sizes
already exists with "auto"
applied into a helper function, but we should only be looking for "auto"
in the first position of the attribute. Otherwise, I don't believe the attribute will have the intended effect in the first place as it's expected to be the first thing in the attribute.
If there are valid cases like that which we can demonstrate get parsed correctly by browsers, then I agree we should handle them. The standalone |
@joemcgill It seems like the spec does allow
|
The |
I'm happy to update this PR to only look for an Most of this PR is test coverage anyway, and it'll still add support for the valid case of |
@joemcgill In f34ab23 I've updated the PR to only look for I kept all tests from before and just changed the expected value, so that it is captured that in the case of |
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: dmsnell <[email protected]>
Only the initial token is now examined for being auto
*/ | ||
function auto_sizes_attribute_includes_valid_auto( string $sizes_attr ): bool { | ||
$token = strtok( strtolower( $sizes_attr ), ',' ); | ||
return false !== $token && 'auto' === trim( $token, " \t\f\r\n" ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@westonruter just curious, did you confirm in the browsers that ,,,,,,,auto
is still registered as auto
for the size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not confirm, no.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since I know basically nothing about this feature, I had to look up a lot (and discovered that only Chrome supports this feature).
Also I relied on this post in addition to the HTML spec to figure out what's going on.
Ultimately I confirmed that Chrome respects the spec, and strangely enough, if the attribute value is ,,,,,auto
then no image is lazy-loaded or displayed at all. In other words, we're still trading one broken auto
for another. Now, I don't want to be a broken record, but the code I posted before doesn't suffer this same problem.
Setup | Chrome | Safari |
---|---|---|
sizes=auto |
✅ | 🟡 Loads the 1200w image |
sizes=",auto" |
🚫 Loads nothing | 🟡 Loads the 1200w image |
sizes="auto," |
✅ | 🟡 Loads the 1200w image |
Interestingly enough this broken behavior only seems to affect lazy-loaded images in Chrome. That is, if the page/tab is visible while loading the page, then no image loads. However, if you can get the page to load the first time when the tab is hidden, then the 1200w image will load, matching Safari's behavior. To reproduce this, open up a test page where Chrome fails to render any image with the invalid attribute value, open another tab, quit Chrome, and then re-open where it restores the session. The first srcset
image will now be displayed. I suspect this is a bug in Chrome, but its behavior is worse under the presence of this attribute value defect than Safari's, which lacks support for the auto
value.
<!-- borrowed from the linked article -->
<img
loading="lazy"
sizes=",,,,,auto"
srcset="
https://o.img.rodeo/w_1200,h_1200,b_tomato/t_WxH/_.png 1200w,
https://o.img.rodeo/w_900,h_900,b_goldenrod/t_WxH/_.png 900w,
https://o.img.rodeo/w_600,h_600,b_forestgreen/t_WxH/_.png 400w,
https://o.img.rodeo/w_300,h_300,b_dodgerblue/t_WxH/_.png 300w"
src="https://o.img.rodeo/w_300,h_300,b_forestgreen/t_WxH/_.png"
alt="An example image that reports its natural dimensions"
>
This also seemed to have shipped without checking if the attribute is sizes
or only ends in sizes
, so anything like data-wp-sizes="state.imageSizes"
will also be corrupted by it, and the sizes
attribute may not be updated properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #1476 which handles the case of leading ,,,
chars.
Currently, the Enhanced Responsive Images functions that conditionally add the
auto
keyword to the imagesizes
attribute only check for the attribute in one specific way. This works to prevent double processing of an image that the plugin itself has added, but may lead to problems if theauto
keyword was added through another means, where it can appear in a different position within thesizes
attribute.Relevant technical choices
auto_sizes_attribute_includes_auto( string $sizes_attr ): bool
that checks a sizes attribute for presence of theauto
keyword, so that this logic can be reused between both filter callbacks.auto
keywords.