-
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
Fix validation error due to native img tag when lightbox and carousel is enabled #7158
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0afb499
Add lightbox to px-verified-attrs in native img tag when lightbox is …
thelovekesh 7d64732
Remove layout and object-fit attrs for native img tag and add style attr
thelovekesh 31cb9a6
Add test cases for native img tag px verified lightbox attrs
thelovekesh 90c0fc8
Update native_img_used variable with provided default args
thelovekesh 22853be
Add test cases for replacing layout and object-fill attrs with style …
thelovekesh a1d80b0
Add check to find key in args to avoid error in tests
thelovekesh ec01348
Fix PHPStan errors
thelovekesh 30f7bcd
Update test case function name
thelovekesh 762c309
Update mentioned method in @covers
thelovekesh 60e2f46
Fix typo
thelovekesh 0755735
Update condition to check if we need to px verify lightbox attr
thelovekesh e4e5396
Update logic of creating node attribute
thelovekesh f0184f1
Fix test cases for TikTok embed handler
thelovekesh d4705ee
Fix test cases for YouTube embed handler
thelovekesh 7c3f6d6
Remove unused namespace aliases
thelovekesh 20e82cb
Remove native img attributes sanitization
thelovekesh 5374854
Add AMP_Native_Img_Attributes_Sanitizer for native image attributes s…
thelovekesh 0dc9a1f
Add AMP_Native_Img_Attributes_Sanitizer in sanitizers
thelovekesh 079f5d8
Remove test cases for native img attributes sanitization
thelovekesh ecf255f
Add test cases for AMP_Native_Img_Attributes_Sanitizer
thelovekesh 144b65f
Merge branch 'develop' of github.com:ampproject/amp-wp into fix/light…
westonruter ee80e10
Preserve original styles; support object-position; support non-cover …
westonruter 3e558de
Add composer-normalize to allowed-plugins before require
westonruter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
includes/sanitizers/class-amp-native-img-attributes-sanitizer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
/** | ||
* Native img attributes sanitizer. | ||
* | ||
* @since 2.3.1 | ||
* @package AMP | ||
*/ | ||
|
||
use AmpProject\Html\Attribute; | ||
use AmpProject\Dom\Element; | ||
use AmpProject\Layout; | ||
|
||
/** | ||
* Class AMP_Native_Img_Attributes_Sanitizer | ||
* | ||
* @since 2.3.1 | ||
* @internal | ||
*/ | ||
class AMP_Native_Img_Attributes_Sanitizer extends AMP_Base_Sanitizer { | ||
|
||
/** | ||
* Array of flags used to control sanitization. | ||
* | ||
* @var array { | ||
* @type bool $native_img_used Whether native img is being used. | ||
* } | ||
*/ | ||
protected $args; | ||
|
||
/** | ||
* Default args. | ||
* | ||
* @var array | ||
*/ | ||
protected $DEFAULT_ARGS = [ | ||
'native_img' => false, | ||
]; | ||
|
||
/** | ||
* Sanitize the Native img attributes. | ||
* | ||
* @since 2.3 | ||
*/ | ||
public function sanitize() { | ||
// Bail if native img is not being used. | ||
if ( ! isset( $this->args['native_img_used'] ) || ! $this->args['native_img_used'] ) { | ||
return; | ||
} | ||
|
||
// Images with layout=fill. | ||
$img_elements = $this->dom->xpath->query( | ||
'.//img[ @layout = "fill" ]', | ||
$this->dom->body | ||
); | ||
if ( $img_elements instanceof DOMNodeList ) { | ||
foreach ( $img_elements as $img_element ) { | ||
/** @var Element $img_element */ | ||
$img_element->removeAttribute( Attribute::LAYOUT ); | ||
$img_element->addInlineStyle( 'position:absolute;left:0;right:0;top:0;bottom:0;width:100%;height:100%' ); | ||
} | ||
} | ||
|
||
// Images with object-fit attributes. | ||
$img_elements = $this->dom->xpath->query( | ||
'.//img[ @object-fit ]', | ||
$this->dom->body | ||
); | ||
if ( $img_elements instanceof DOMNodeList ) { | ||
foreach ( $img_elements as $img_element ) { | ||
/** @var Element $img_element */ | ||
$value = $img_element->getAttribute( Attribute::OBJECT_FIT ); | ||
$img_element->removeAttribute( Attribute::OBJECT_FIT ); | ||
$img_element->addInlineStyle( sprintf( 'object-fit:%s', $value ) ); | ||
} | ||
} | ||
|
||
// Images with object-position attributes. | ||
$img_elements = $this->dom->xpath->query( | ||
'.//img[ @object-position ]', | ||
$this->dom->body | ||
); | ||
if ( $img_elements instanceof DOMNodeList ) { | ||
foreach ( $img_elements as $img_element ) { | ||
/** @var Element $img_element */ | ||
$value = $img_element->getAttribute( Attribute::OBJECT_POSITION ); | ||
$img_element->removeAttribute( Attribute::OBJECT_POSITION ); | ||
$img_element->addInlineStyle( sprintf( 'object-position:%s', $value ) ); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
tests/php/test-class-amp-native-img-attributes-sanitizer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
/** | ||
* Test cases for AMP_Native_Img_Attributes_Sanitizer | ||
* | ||
* @package AmpProject\AmpWP | ||
*/ | ||
|
||
use AmpProject\AmpWP\Tests\TestCase; | ||
use AmpProject\AmpWP\Tests\Helpers\MarkupComparison; | ||
|
||
/** | ||
* Class AMP_Native_Img_Attributes_Sanitizer_Test | ||
* | ||
* @since 2.3.1 | ||
* @coversDefaultClass AMP_Native_Img_Attributes_Sanitizer | ||
*/ | ||
class AMP_Native_Img_Attributes_Sanitizer_Test extends TestCase { | ||
|
||
use MarkupComparison; | ||
|
||
public function get_data_to_test_sanitize() { | ||
$amp_carousel_source = '<amp-carousel width="600" height="400" type="slides" layout="responsive" lightbox=""><figure class="slide"><img src="http://example.com/img.png" width="600" height="400" layout="fill" object-fit="cover"></figure></amp-carousel>'; | ||
$amp_carousel_sanitized = '<amp-carousel width="600" height="400" type="slides" layout="responsive" lightbox=""><figure class="slide"><img src="http://example.com/img.png" width="600" height="400" style="position:absolute;left:0;right:0;top:0;bottom:0;width:100%;height:100%;object-fit:cover"></figure></amp-carousel>'; | ||
|
||
return [ | ||
'disabled' => [ | ||
false, | ||
$amp_carousel_source, | ||
null, // Same as above. | ||
], | ||
'carousel_with_img' => [ | ||
true, | ||
$amp_carousel_source, | ||
$amp_carousel_sanitized, | ||
], | ||
'amp_img' => [ | ||
true, | ||
'<amp-img src="https://example.com/img.png" style="border: solid 1px red;" layout="fill" object-fit="cover"></amp-img>', | ||
null, // Same as above. | ||
], | ||
'img_with_layout_fill' => [ | ||
true, | ||
'<img src="https://example.com/img.png" style="border: solid 1px red" layout="fill">', | ||
'<img src="https://example.com/img.png" style="border: solid 1px red;position:absolute;left:0;right:0;top:0;bottom:0;width:100%;height:100%">', | ||
], | ||
'img_with_layout_nodisplay' => [ | ||
true, | ||
'<img src="https://example.com/img.png" style="border: solid 1px red;" layout="nodisplay">', | ||
null, // Same as above. | ||
], | ||
'img_with_object_fit_cover' => [ | ||
true, | ||
'<img src="https://example.com/img.png" style="border: solid 1px red;" object-fit="cover">', | ||
'<img src="https://example.com/img.png" style="border: solid 1px red;object-fit:cover">', | ||
], | ||
'img_with_object_fit_contain' => [ | ||
true, | ||
'<img src="https://example.com/img.png" style="border: solid 1px red;" object-fit="contain">', | ||
'<img src="https://example.com/img.png" style="border: solid 1px red;object-fit:contain">', | ||
], | ||
'img_with_object_position' => [ | ||
true, | ||
'<img src="https://example.com/img.png" style="border: solid 1px red;" object-position="top">', | ||
'<img src="https://example.com/img.png" style="border: solid 1px red;object-position:top">', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Test an native img tag has not layout or object-fit attributes. | ||
* | ||
* `layout` and `object-fit` will be replaced with a style attribute. | ||
* | ||
* @dataProvider get_data_to_test_sanitize | ||
* @covers ::sanitize() | ||
*/ | ||
public function test_sanitize( $native_img_used, $source, $expected ) { | ||
if ( null === $expected ) { | ||
$expected = $source; | ||
} | ||
|
||
$dom = AMP_DOM_Utils::get_dom_from_content( $source ); | ||
$sanitizer = new AMP_Native_Img_Attributes_Sanitizer( | ||
$dom, | ||
compact( 'native_img_used' ) | ||
); | ||
$sanitizer->sanitize(); | ||
|
||
$content = AMP_DOM_Utils::get_content_from_dom( $dom ); | ||
$this->assertEqualMarkup( $expected, $content ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
It turns out that
amp-video
also supportsobject-fit
andobject-position
attributes, so it could be that there could end up being somevideo
elements that have these attributes. In this case, the sanitizer could be made more generic instead of being specifically for images. But since we don't have any known cases of this happening for video, this could be done later. Or it could be done now if you want.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.
Can't think of any such use case. IMO we are amending changes to
img
tags only so can't we keep it as it is?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 mean, it could be that a theme/plugin author is adding:
<video object-fit=cover …>
and is then relying on the fact that this is converged to<amp-video object-fit=cover>
. But in moderate sandboxing, no conversion is being done, so this would then be a validation error and the attribute should be moved tostyle
as we're doing forimg
.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.
Oh. got it now. But I think a new PR should introduce it instead of adding it in the same. Also as mentioned
But since we don't have any known cases of this happening for video, this could be done later.
IMO adding it with some use case make more sense. Thoughts?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.
If a user reports this issue we can follow up with the change.