-
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
Persist img
of picture
tag instead of removing it during sanitization.
#6896
Conversation
Plugin builds for 0d63423 are ready 🛎️!
|
I've asked if this will address the concerns by a support forum user: https://wordpress.org/support/topic/plugin-deleted-all-tags-picture/#post-15369498 That user wasn't able to use @milindmore22's mini plugin successfully, but I believe that is due in large part that they missed the required So we should require that |
you mean to only enable sanitization when there |
What I mean is that if there is a |
I have added that check in recent commit 39a57e6 |
…allow-child-img-of-picture * 'develop' of github.com:ampproject/amp-wp: (135 commits) Bump eslint-plugin-react from 7.29.2 to 7.29.3 Add tests for new specs Update tests for new component versions Add support for Bento extensions being defined in bundles.config.bento.json Update amphtml to 2202230359001 and add to latest-extension-versions Fix check_for_page_caching to account for wp_remote_retrieve_header() returning arrays Bump `@wordpress/scripts` to 22.1.0 Revert "Prevent copying PHP files from `src/` into `assets/`" Use links as exported from PHP, split out functions, and make list item matching more robust Bump postcss-preset-env from 7.4.1 to 7.4.2 Update namespace for Attribute and Tag interfaces Remove obsolete debug code Defer creation of post fixture until test Add assertion for post data being created before test Add yet more assertions to debug oEmbed problem on GHA Add oembed_request_post_id filter sooner Add more assertions Add assertions Ensure video shortcode is registered to test_process_text_widgets Use oembed_request_post_id for test ...
return; | ||
} | ||
|
||
$picture_nodes = $this->dom->getElementsByTagName( Tag::PICTURE ); |
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.
This shouldn't be needed because there is an XPath query right above.
*/ | ||
protected function process_picture_elements() { | ||
|
||
$nodes = $this->dom->xpath->query( '//picture//img' ); |
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.
This could be made to select the direct descendant:
$nodes = $this->dom->xpath->query( '//picture//img' ); | |
$nodes = $this->dom->xpath->query( '//picture/img' ); |
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.
Or better to select picture
elements that have img
children:
$nodes = $this->dom->xpath->query( '//picture//img' ); | |
$nodes = $this->dom->xpath->query( '//picture[ img ]' ); |
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.
Or or no, the first is better, as then you can loop over the img
elements and then to get the picture
you just get the parentNode
.
tests/php/test-amp-img-sanitizer.php
Outdated
'expected' => '<h1>Page heading</h1><ul><li>Item 1</li><li>Item 2</li></ul>', | ||
], | ||
'picture_without_img_allow_picture_false' => [ | ||
'input' => '<picture><div class="screen-reader-text"></div><source srcset="https://interactive-examples.mdn.mozilla.net/media/cc0-images/surfer-240-200.jpg" media="(min-width: 800px)"></picture>', |
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.
Humm. Why is there a <div class="screen-reader-text"></div>
as a child of the picture
? I thought only source
and img
elements were allowed as children.
tests/php/test-amp-img-sanitizer.php
Outdated
'args' => [ | ||
'allow_picture' => false, | ||
], | ||
'expected' => '<picture><div class="screen-reader-text"></div><source srcset="https://interactive-examples.mdn.mozilla.net/media/cc0-images/surfer-240-200.jpg" media="(min-width: 800px)"></picture>', |
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 allow_picture
is false
, why isn't the picture
converted? Oh, it's because there is no img
?
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.
This looks good to me. I left some minor feedback.
if ( 0 === $picture_img_query->length ) { | ||
return; | ||
} |
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 guess this is redundant. The $this->dom->xpath->query()
returns a DOMNodeList
. If it's an empty list, the foreach
loop below will do 0 iterations.
$picture_element->removeChild( $img_element ); | ||
$picture_element->parentNode->replaceChild( $img_element, $picture_element ); |
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 don't have much experience with the DOM API in PHP but is it necessary to remove the IMG tag first from the PICTURE and then replace the entire PICTURE with an IMG? Wouldn't just the replaceChild()
suffice?
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 recall this has been necessary since replacing a parent with a child while the child is still in the DOM could cause an error, if I remember correctly.
…tion (#6896) Co-authored-by: Dhaval Parekh <[email protected]>
Summary
Fixes #6676
These PR changes prevent the
img
tag inside thepicture
tag from being removed ifnative_img_used
isfalse
. and will mark thepicture
tag and its child element aspx-verified-tag
ifnative_img_used
is true.Checklist