Skip to content
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

Prevent distortion of images with a 'sizes' attribute #4622

Merged
merged 2 commits into from
Apr 28, 2020

Conversation

pierlon
Copy link
Contributor

@pierlon pierlon commented Apr 23, 2020

Summary

This PR re-adds the disabled sizes attribute to the amp-img component, and disables the inline width being added by adding the disable-inline-width attribute. This should prevent any distortion of the image when viewed with an AMP page.

I'm using the post content @westonruter posted in #2036 to test some of the possible ways an image can be used on a page. This verifies that there are no apparent regressions:

Desktop

Non-AMP AMP AMP without fix
Screenshot_2020-04-23 Bison Image Test – WordPress Develop Screenshot_2020-04-23 Bison Image Test – WordPress Develop Screenshot_2020-04-23 Bison Image Test – WordPress Develop

Mobile

Non-AMP AMP AMP without fix
Screenshot_2020-04-23 Bison Image Test – WordPress Develop(7) Screenshot_2020-04-23 Bison Image Test – WordPress Develop(8) Screenshot_2020-04-23 Bison Image Test – WordPress Develop(9)

Fixes #4606.

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@googlebot googlebot added the cla: yes Signed the Google CLA label Apr 23, 2020
Copy link
Collaborator

@schlessera schlessera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All-in with the constants... :)

Comment on lines 334 to 335
if ( isset( $new_attributes['sizes'] ) ) {
$new_attributes['disable-inline-width'] = '';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use Attribute here.

Suggested change
if ( isset( $new_attributes['sizes'] ) ) {
$new_attributes['disable-inline-width'] = '';
if ( isset( $new_attributes[ Attribute::SIZES ] ) ) {
$new_attributes[ Attribute::DISABLE_INLINE_WIDTH'] = '';

Note: You'll have to add the DISABLE_INLINE_WIDTH constant to the Layout class as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constants everywhere 😄!

includes/sanitizers/class-amp-img-sanitizer.php Outdated Show resolved Hide resolved
includes/sanitizers/class-amp-img-sanitizer.php Outdated Show resolved Hide resolved
includes/sanitizers/class-amp-img-sanitizer.php Outdated Show resolved Hide resolved
includes/sanitizers/class-amp-img-sanitizer.php Outdated Show resolved Hide resolved
includes/sanitizers/class-amp-img-sanitizer.php Outdated Show resolved Hide resolved
@pierlon pierlon requested a review from schlessera April 23, 2020 18:47
Comment on lines +336 to +338
if ( isset( $new_attributes[ Attribute::SIZES ] ) ) {
$new_attributes[ Attribute::DISABLE_INLINE_WIDTH ] = '';
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relevant change ⬆️.

@westonruter
Copy link
Member

westonruter commented Apr 23, 2020

As expected, on a page with images, the changes in this PR result in the i-amphtml-no-boilerplate attribute being removed, with this comment added to the end of the head:

AMP optimization could not be completed due to the following:
 - CannotRemoveBoilerplate: Cannot remove boilerplate as either heights, media or sizes attribute is set: <amp-img noloading="" width="1600" height="1200" src="https://wordpressdev.lndo.site/content/uploads/2011/07/dsc09114.j…>
 - CannotRemoveBoilerplate: Cannot remove boilerplate as either heights, media or sizes attribute is set: <amp-img noloading="" width="1600" height="1200" src="https://wordpressdev.lndo.site/content/uploads/2011/07/dsc09114.j…>

This is expected as this regression will then be addressed as part of #4482.

@westonruter
Copy link
Member

westonruter commented Apr 23, 2020

I'm testing this with the issue in #1939 regarding inline images. In Twenty Nineteen, adding content like so:

image

The non-AMP version and the AMP version on develop look like this:

image

But with this PR there is a regression:

image

This is due to the amp-img having the disable-inline-width attribute which causes the element to get this AMP shared CSS rule applied:

.i-amphtml-layout-responsive,
[layout=responsive][width][height]:not(.i-amphtml-layout-responsive),
[width][height][sizes]:not(.i-amphtml-layout-responsive)
{
  display: block;
  position: relative;
}

If I'm not mistaken, I think this CSS may actually be not updated for to account for the disable-inline-width attribute. I think that the last selector should perhaps be changed to not apply with the disable-inline-width attribute is present:

[width][height][sizes]:not([disable-inline-width]):not(.i-amphtml-layout-responsive)

I'm checking with the AMP team: ampproject/amphtml#27083 (comment).

@westonruter
Copy link
Member

Note this issue did not show up in testing Twenty Twenty because it (erroneously) has a display:block style added to all images.

@westonruter
Copy link
Member

The display:block style also has the effect of centering the image on the attachment template (at least on Twenty Seventeen). See #1237.

@westonruter
Copy link
Member

Opened a PR to fix the the issue in ampshared.css: ampproject/amphtml#28020

@westonruter westonruter dismissed schlessera’s stale review April 27, 2020 21:32

Constants have been applied

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upstream PR merged: ampproject/amphtml#28020

@westonruter
Copy link
Member

@schlessera anything else on this PR to receive your ✅?

@westonruter
Copy link
Member

I guess not 😄 I'll merge and you can amend as needed in #4482.

@westonruter westonruter merged commit b3e4907 into develop Apr 28, 2020
@westonruter westonruter deleted the enhancement/4606-remove-sizes-attr branch April 28, 2020 22:32
@pierlon pierlon added this to the v1.6 milestone Jun 2, 2020
@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Jul 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelogged Whether the issue/PR has been added to release notes. cla: yes Signed the Google CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discontinue removing sizes attribute when converting img to amp-img
4 participants