-
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
Conversation
$selectors[] = $amp_selectors_parsed['selector']; | ||
} else { | ||
$selectors = array_keys( $selectors_parsed ); | ||
} |
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.
@westonruter Started adding the logic within this method since it seemed to be related with tree shaking in terms of adding/not adding the selector if it's not found after replacing. That's still WIP (e.g. currently works only in case ! $should_tree_shake
and is missing most of the mappings) but let me know if there are any concerns with this approach.
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.
Hmm, maybe one potential issue could be that the size of the style could exceed the limit if doing the replacement here.
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.
Moving the logic to process_css_declaration_block
to avoid issues with style size changing after the size check.
* | ||
* @param Selector $selector CSS Selector. | ||
*/ | ||
private function ampify_selector( $selector ) { |
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.
@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 amp-img
or amp-anim
, thinking if we should add an additional selector for potential amp-anim
, too.
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 would be ideal if the selector mappings could be added to each sanitizer. For example, on AMP_Img_Sanitizer
there could be a get_selector_mapping()
method which returns:
array(
'img' => array( 'amp-img', 'amp-anim' )
)
Whereas on the AMP_Audio_Sanitizer
class it would return:
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 AMP_Style_Sanitizer
to be able to invoke such a get_selector_mapping
method on the other sanitizers, it could be done similar to as proposed for passing embed handlers to an embed sanitizer: https://github.com/Automattic/amp-wp/pull/1128/files#diff-2585472f207548f626a43a7ff3cab922R150
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
assets/css/amp-default.css
Outdated
@@ -1,4 +1,4 @@ | |||
.amp-wp-unknown-size img { | |||
.amp-wp-unknown-size .i-amphtml-replaced-content { |
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:
Internal AMP class names prefixed with
-amp-
andi-amp-
are disallowed in AMP HTML.
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]
.
@miina it's also important to note that A suggestion would be to maybe have a rule that any class starting with |
@DavidCramer Do you mean "exempted" as excluded from sanitizing and never removed from CSS? |
@miina yes, but only if there is an element with the class. .amp-wp-unknown-size img {
....
} if it finds an element with |
…dd/1094-convert_css_selectors
* @param Selector $selector CSS Selector. | ||
*/ | ||
private function ampify_selector( $selector ) { | ||
// @todo What about amp-anim, amp-playbuzz? |
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've got an idea I'm working on for this…
…ize for selector conversion
// Let the sanitizers. | ||
foreach ( $sanitizers as $sanitizer ) { | ||
$sanitizer->after_sanitize( $sanitizers ); | ||
} |
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 we're not using this, I think we should just kill after_sanitize
.
|
||
// Let the sanitizers know about each other prior to sanitizing. | ||
foreach ( $sanitizers as $sanitizer ) { | ||
$sanitizer->before_sanitize( $sanitizers ); |
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 do think we should rename this to init
$replacements = 0; | ||
foreach ( $ruleset->getSelectors() as $old_selector ) { | ||
$edited_selectors = array( $old_selector->getSelector() ); | ||
foreach ( $this->selector_mappings as $html_selector => $amp_selectors ) { // Note: The $selector_mappings array contains ~6 items. |
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.
These notes are here because 4 level of loop nesting is normally something that is a big red flag for performance.
assets/css/amp-default.css
Outdated
@@ -1,4 +1,4 @@ | |||
.amp-wp-unknown-size img { | |||
.amp-wp-unknown-size .i-amphtml-replaced-content { |
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]
.
…c/amp-wp into add/1094-convert_css_selectors
@@ -514,6 +524,8 @@ public function filter_attachment_layout_attributes( $node, $new_attributes, $la | |||
|
|||
/** | |||
* Add <amp-image-lightbox> element to body tag if it doesn't exist yet. | |||
* | |||
* @todo Move this to AMP_Img_Sanitizer? | |||
*/ | |||
public function maybe_add_amp_image_lightbox_node() { |
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.
Note that this is also used by AMP_Gallery_Block_Sanitizer
.
|
||
if ( isset( $parent_attributes['data-amp-lightbox'] ) && true === filter_var( $parent_attributes['data-amp-lightbox'], FILTER_VALIDATE_BOOLEAN ) ) { | ||
$attributes['data-amp-lightbox'] = ''; | ||
$attributes['on'] = 'tap:' . self::AMP_IMAGE_LIGHTBOX_ID; |
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.
Note that self:AMP_IMAGE_LIGHTBOX_ID
is also used by AMP_Gallery_Block_Sanitizer
, that's why it to AMP_Base_Sanitizer
.
…lectors like amp-img>img
…c ancestor is absent
…c/amp-wp into add/1094-convert_css_selectors
Modify
AMP_Style_Sanitizer
so that it would convert non-amp CSS selectors to amp component selectors in case it seems to be necessary. Fixes issue with potential broken design, for example with Twenty Ten header.Fixes #1094.