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 tree-shaking tag/ID selectors for amp-lightbox-gallery #3226

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,33 @@ private function has_used_class_name( $class_names ) {
return true;
}

/**
* Determine if all the supplied IDs are used.
*
* @since 1.3
*
* @param string[] $ids IDs.
* @return bool All used.
*/
private function has_used_ids( $ids ) {
foreach ( $ids as $id ) {

// Prevent tree-shaking the `<amp-lightbox-gallery id="amp-lightbox-gallery">` element which is added to the
// page dynamically by the amp-lightbox-gallery component when there are lightbox attribute(s) on the page.
if ( 'amp-lightbox-gallery' === $id ) {
if ( ! $this->has_used_attributes( [ 'lightbox' ] ) ) {
return false;
}
continue;
}

if ( ! $this->dom->getElementById( $id ) ) {
return false;
}
}
return true;
}

/**
* Get list of all the tag names used in the document.
*
Expand Down Expand Up @@ -649,6 +676,15 @@ private function has_used_tag_names( $tag_names ) {
$this->get_used_tag_names();
}
foreach ( $tag_names as $tag_name ) {

// Prevent tree-shaking the <amp-lightbox-gallery> element which is added to the page dynamically by the component.
if ( 'amp-lightbox-gallery' === $tag_name ) {
if ( ! $this->has_used_attributes( [ 'lightbox' ] ) ) {
return false;
}
continue;
}

if ( ! isset( $this->used_tag_names[ $tag_name ] ) ) {
return false;
}
Expand Down Expand Up @@ -2934,14 +2970,7 @@ private function finalize_stylesheet_group( $group, $group_config ) {
(
empty( $parsed_selector[ self::SELECTOR_EXTRACTED_IDS ] )
||
0 === count(
array_filter(
$parsed_selector[ self::SELECTOR_EXTRACTED_IDS ],
function( $id ) {
return ! $this->dom->getElementById( $id );
}
)
)
$this->has_used_ids( $parsed_selector[ self::SELECTOR_EXTRACTED_IDS ] )
)
&&
// If tag names are present in the doc.
Expand Down
7 changes: 6 additions & 1 deletion tests/php/test-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public function get_link_and_style_test_data() {
],
[],
],
'dynamic_classes_and_attributes_preserved_conditionally' => [
'dynamic_nodes_preserved_conditionally' => [
'
<html amp><head>
<style> .amp-viewer { color: blue; } </style>
Expand All @@ -482,6 +482,8 @@ public function get_link_and_style_test_data() {
<style> .amp-iso-country-us { color: oldlace; } </style>
<style> .amp-video-eq { display: none; } </style>
<style> #accord section[expanded] { outline: solid 1px blue; } </style>
<style> #amp-lightbox-gallery { outline: solid 1px yellow; } </style>
<style> amp-lightbox-gallery { outline: solid 1px purple; } </style>
<style> .non-existent { color: black; } </style>
</head>
<body>
Expand All @@ -497,6 +499,7 @@ public function get_link_and_style_test_data() {
<amp-video dock width="720" height="305" layout="responsive" src="https://yourhost.com/videos/myvideo.mp4" poster="https://yourhost.com/posters/poster.png" artwork="https://yourhost.com/artworks/artwork.png" title="Awesome video" artist="Awesome artist" album="Amazing album"></amp-video>
<amp-geo layout="nodisplay"><script type="application/json">{"ISOCountryGroups": {"foo":["us"]}}</script></amp-geo>
<amp-accordion id="accord" disable-session-states><section><h2>Section 1</h2><p>Content in section 1.</p></section><section><h2>Section 2</h2><div>Content in section 2.</div></section></amp-accordion>
<amp-img lightbox src="https://example.com/static/samples/img/road-1.jpg" width="300" height="200" layout="responsive"></amp-img>
</body>
</html>
',
Expand All @@ -519,6 +522,8 @@ public function get_link_and_style_test_data() {
'.amp-iso-country-us{color:oldlace}',
'.amp-video-eq{display:none}',
'#accord section[expanded]{outline:solid 1px blue}',
'#amp-lightbox-gallery{outline:solid 1px yellow}',
'amp-lightbox-gallery{outline:solid 1px purple}',
'', // Because no non-existent.
],
[],
Expand Down