Harden hero image optimization when noscript > img
fallbacks are present
#516
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This exposes an issue that arose with the AMP plugin v2.2.2 release. In this release, there was an update to the validator spec which included, for the first time,
data-hero
as being a recognized attribute onimg
elements. Because of this, when theAMP_Noscript_Fallback::initialize_noscript_allowed_attributes()
method ran inAMP_Img_Sanitzer
it includeddata-hero
among the attributes that are copied to the to thenoscript > img
fallback. Resulting in this change from v2.2.1 (where thedata-hero
attribute is now present):Now that the
noscript > img
fallback hasdata-hero
, the\AmpProject\Optimizer\Transformer\OptimizeHeroImages::findHeroImages()
method in this repo incorrectly starts including both theamp-img
and thenoscript > img
in the list of hero images to optimize. This later causes a fatal error when theremoveLazyLoading
method attempts to remove anyloading
attribute from thenoscript > img
because the element was removed from the DOM by the\AmpProject\Optimizer\Transformer\OptimizeHeroImages::generateImg()
method. The result is a fatal error:So the fix is just to make sure that we skip over any element from being considered a hero if it is the child of a
noscript
element.See support topic replies.
Update: It also turns out that the AMP plugin shouldn't have been including the
data-hero
attribute on theamp-img > noscript > img
fallbacks in the first place, as that is a validation error. So I've opened ampproject/amp-wp#7036 to fix this, but still the presence of this attribute should not cause the optimizer transformer to throw a fatal error.