-
Notifications
You must be signed in to change notification settings - Fork 384
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
Inherit object-fit and object-position styles so more conversions work out of the box #5955
Conversation
|
2f28231
to
e579216
Compare
…ndant with object-fit=contain attr
…nts and noscript fallbacks
be2f77d
to
c280227
Compare
* Additionally, in side of \AMP_Img_Sanitizer::determine_dimensions() it could $amp_img->setAttribute( 'object-fit', 'contain' ) | ||
* so that the following rules wouldn't be needed. |
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.
Codecov Report
@@ Coverage Diff @@
## develop #5955 +/- ##
=============================================
+ Coverage 75.27% 75.39% +0.12%
+ Complexity 5709 5700 -9
=============================================
Files 218 218
Lines 17283 17255 -28
=============================================
Hits 13009 13009
+ Misses 4274 4246 -28
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Plugin builds for ed1c930 are ready 🛎️!
|
*/ | ||
amp-img.amp-wp-enforced-sizes[layout="intrinsic"] > img, | ||
amp-anim.amp-wp-enforced-sizes[layout="intrinsic"] > img { | ||
.amp-wp-enforced-sizes { |
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.
There is a chance that this may have too much specificity and override user styles, although this is very unlikely.
There is a new hack to implement low specificity selector as seen here:
Example: https://codepen.io/westonruter/pen/JjENYGK
This being the case, this selector could actually be changed to:
.amp-wp-enforced-sizes { | |
:where(.amp-wp-enforced-sizes) { |
And this would ensure that if anyone has a bare img { object-fit:cover }
it won't get overridden by the default style here.
Nevertheless, :where
is not supported yet by all browsers: https://caniuse.com/mdn-css_selectors_where
It is very unlikely that someone is going to want to style all images with object-fit
, and the examples of using object-fit
in core all make use of selectors that have much more specificity than 0.1.0:
Selector | Specificity |
---|---|
.has-header-image .custom-header-media img |
0.2.1 |
.has-header-video .custom-header-media video |
0.2.1 |
.has-header-image:not(.twentyseventeen-front-page):not(.home) .custom-header-media img |
0.4.1 |
.site-featured-image .post-thumbnail img |
0.2.1 |
.site-header.featured-image .site-featured-image .post-thumbnail img |
0.4.1 |
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.
Although unlikely, I could see someone using something like header > img { object-fit:cover }
, which would have a specificity of 0.0.2.
Unfortunately @supports selector
is not widely supported by all browsers either so we cannot rely on that.
// Remove attributes which are likely to cause styling conflicts, as the noscript fallback should get treated like it has fill layout. | ||
unset( | ||
$this->noscript_fallback_allowed_attributes[ Attribute::ID ], | ||
$this->noscript_fallback_allowed_attributes[ Attribute::CLASS_ ], |
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.
Removing the class attribute has inadvertently caused noscript>img
elements of custom logos to be detected as hero images, and because of that the AMP page fails to render due to a PHP fatal error:
[06-Apr-2021 08:46:38 UTC] PHP Warning: Couldn't fetch AmpProject\Dom\Element. Node no longer exists in /app/public/content/plugins/amp/vendor/ampproject/amp-toolbox/src/Dom/ElementDump.php on line 55
[06-Apr-2021 08:46:38 UTC] PHP Notice: Undefined property: AmpProject\Dom\Element::attributes in /app/public/content/plugins/amp/vendor/ampproject/amp-toolbox/src/Dom/Element.php on line 100
[06-Apr-2021 08:46:38 UTC] PHP Fatal error: Method AmpProject\Dom\ElementDump::__toString() must not throw an exception, caught TypeError: Argument 1 passed to iterator_to_array() must implement interface Traversable, null given in /app/public/content/plugins/amp/vendor/ampproject/amp-toolbox/src/Optimizer/Error/CannotPreloadImage.php on line 0
[06-Apr-2021 08:46:38 UTC] PHP Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0
[06-Apr-2021 08:49:25 UTC] PHP Warning: Couldn't fetch AmpProject\Dom\Element. Node no longer exists in /app/public/content/plugins/amp/vendor/ampproject/amp-toolbox/src/Dom/ElementDump.php on line 55
[06-Apr-2021 08:49:25 UTC] PHP Notice: Undefined property: AmpProject\Dom\Element::attributes in /app/public/content/plugins/amp/vendor/ampproject/amp-toolbox/src/Dom/Element.php on line 100
[06-Apr-2021 08:49:25 UTC] PHP Fatal error: Method AmpProject\Dom\ElementDump::__toString() must not throw an exception, caught TypeError: Argument 1 passed to iterator_to_array() must implement interface Traversable, null given in /app/public/content/plugins/amp/vendor/ampproject/amp-toolbox/src/Optimizer/Error/CannotPreloadImage.php on line 0
[06-Apr-2021 08:49:25 UTC] PHP Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0
Further investigation led to the query AmpProject\AmpWP\Transformer\DetermineHeroImages::CUSTOM_HEADER_XPATH_QUERY
incorrectly detecting the noscript>img
due to the subquery not( contains( concat( ' ', normalize-space( @class ), ' ' ), ' custom-logo ' ) )
now being true as the custom-logo
class is no longer being copied.
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.
Fatal error prevented with fb0928f but I'm still not entirely clear why the error occurred in the first place.
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.
In any case, the change in fb0928f is key because we never want to select a noscript > img
for SSR.
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.
Actually, it doesn't even make sense to select any img
now that I think of it, because all of the sanitizers will have already run and so there should be no img
left in the page that we need to SSR.
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.
Done in c72d487.
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.
Fatal error prevented with fb0928f but I'm still not entirely clear why the error occurred in the first place.
The error occurred because the noscript>img
was receiving the data-hero-candidate
attribute, and AmpProject\Optimizer\Transformer::detectImageWithAttribute()
detects it as a valid candidate because when it retrieves the CSS background image URL for the image (which resolves to an empty string ''
) and parses it as a URL (which becomes the valid URL path /
), it is returned as a hero image for preloading, which should not be the case.
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 understand why the error was happening: PreloadHeroImage::findHeroImages()
was including the noscript>img
for an amp-img
that was going to be prerendered. So it included both the amp-img
and the amp-img > noscript > img
as the hero images. When prerendering occurred, then the latter got removed and so it was no longer in the document. And then the exception ensued.
This should all be sorted now.
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.
Really great work done here, LGTM!
Summary
This is a
n experimentalfollow-up on #5927. We can fix issues withobject-fit
/object-position
for the Cover Block and for any otheramp-img
/amp-video
that gets converted fromimg
/video
if we merely inherit the properties.object-fit:contain
on images converted by the plugin. This is necessary for images which get a 100% width in content to prevent them from getting stretched or cropped. For example:(Note the image does appear to be centered in AMP, but this is an existing issue: #4735.)
object-fit
,object-position
, andimage-resolution
are inherited from an AMP component wrapper to the contained shadow element andnoscript
fallback element. This allows us to entirely remove theampify_cover_block
logic.noscript
fallback elements which are likely to cause styling problems. Fixes Ensure noscript fallbacks get expected styling #6028.QA
object-fit
,object-position
, andimage-rendering
styles apply on theamp-img > img
andamp-img > noscript > img
, both with JavaScript enabled and disabled. For example, given a Custom HTML block with this markup:noscript
fallback elements are styled as expected. For example, a Custom HTML block with:Checklist