-
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
1094: Transform CSS selectors according to sanitizer HTML element to AMP component conversions #1175
1094: Transform CSS selectors according to sanitizer HTML element to AMP component conversions #1175
Changes from 5 commits
e530045
f2395ee
958fd48
488ebd2
077a094
ed1cc03
6091533
7a218aa
fcc0b44
d815891
12c9ff8
91e3eef
04f6b85
19db2c1
1b8405a
592aca7
a564410
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -942,6 +942,12 @@ private function real_path_urls( $urls, $stylesheet_url ) { | |
private function process_css_declaration_block( RuleSet $ruleset, CSSList $css_list, $options ) { | ||
$validation_errors = array(); | ||
|
||
if ( method_exists( $ruleset, 'getSelectors' ) ) { | ||
foreach ( $ruleset->getSelectors() as $selector ) { | ||
$this->ampify_selector( $selector ); | ||
} | ||
} | ||
|
||
// Remove disallowed properties. | ||
if ( ! empty( $options['property_whitelist'] ) ) { | ||
$properties = $ruleset->getRules(); | ||
|
@@ -1435,6 +1441,35 @@ private function finalize_styles() { | |
} | ||
} | ||
|
||
/** | ||
* Ampify CSS selectors. | ||
* | ||
* @param Selector $selector CSS Selector. | ||
*/ | ||
private function ampify_selector( $selector ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @westonruter Reworked the logic to replace the components before tree shaking instead (compared to the previous commit). Wondering if it's OK to just replace the selectors or is it possible that this way we'll be replacing a selector that's actually in use. Also, in case of img it can be replaced with either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be ideal if the selector mappings could be added to each sanitizer. For example, on array(
'img' => array( 'amp-img', 'amp-anim' )
) Whereas on the array(
'video' => array( 'amp-video ),
)
In other words, there could be multiple AMP components that could be converted so the selector should do replacements for each one, potentially duplicating a selector however many times is needed. As for how to get the I have a concern with that approach of passing class instances as sanitizer args, however. It could cause problems with the response caching introduced in #1156, since the cache key is computed by passing the sanitizer args: /cc @juanchaur1 |
||
// @todo What about amp-anim, amp-playbuzz? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've got an idea I'm working on for this… |
||
$mappings = array( | ||
'audio' => 'amp-audio', | ||
'iframe' => 'amp-iframe', | ||
'img' => 'amp-img', | ||
'video' => 'amp-video', | ||
); | ||
|
||
$new_selector = $selector->getSelector(); | ||
foreach ( $mappings as $tag => $amp_tag ) { | ||
if ( 0 === strpos( $selector->getSelector(), $tag ) ) { | ||
$new_selector = substr_replace( $new_selector, $amp_tag, 0, strlen( $tag ) ); | ||
} | ||
if ( false !== strpos( $selector->getSelector(), ' ' . $tag ) ) { | ||
$new_selector = str_replace( ' ' . $tag, ' ' . $amp_tag, $new_selector ); | ||
} | ||
} | ||
|
||
if ( $selector->getSelector() !== $new_selector ) { | ||
$selector->setSelector( $new_selector ); | ||
} | ||
} | ||
|
||
/** | ||
* Finalize a stylesheet set (amp-custom or amp-keyframes). | ||
* | ||
|
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 is illegal in AMP to use
i-amphtml-replaced-content
class names. See:https://www.ampproject.org/docs/fundamentals/spec#classes
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.
Yes, think I even saw a todo note recently somewhere as well to remove these from CSS within style sanitizer and realized that it's illegal. Will look into which selector to use.
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 tree shaking isn't done for attribute values, I suggest the way to fix this is to use an attribute selector like
[src]
.