-
Notifications
You must be signed in to change notification settings - Fork 383
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
Add runtime CSS minification, !important replacement, and tree shaking #1048
Conversation
* Compress CSS output with compact format, removing whitespace and comments. * Reduce length of generated class names for inline styles. * Unify logic for handling inline style attributes with handling stylesheets in style/link.
…oser install in Travis build
…tion instructions
…uring end of body
…c selectors (following replace-important package) Eliminate illegal_css_important_qualifier validation error now that transformed
73ad61a
to
fd04b20
Compare
* Handle CSS parse errors. * Let spec inform which @-rules are allowed. * Improve handling of vendor-prefixed properties in whitelist/blacklists. * Inform when !important qualifier is removed from rules with selectors (that aren't keyframes).
…lid class references
$selectors = array(); | ||
if ( $should_tree_shake ) { | ||
foreach ( $selectors_parsed as $selector => $class_names ) { | ||
if ( count( $class_names ) === count( array_intersect( $class_names, $this->used_class_names ) ) ) { |
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.
Better to use array_diff
here instead maybe.
…ent false matches
@amedina yes, I referred to
|
Just occurred to me that we could help guard against this from happening by adding logic to skip removing selectors that reference Nevertheless, there would be no way to statically account for class names assigned via @pbakaus Any other key dynamic content cases to have in mind? |
b278bc2
to
9a6cec0
Compare
9a6cec0
to
313f0b9
Compare
@westonruter sounds sensible! Thats the only ones I have in mind right now. |
@westonruter usually the ones associated with And on the replace-important improvements feel free to edit the code here |
@camelburrito I added initial support for this via the @kienstra This is ready for review to merge so we can get this testing. I want to follow up later with some more improvements including the added ability to knowingly bypass removal of elements, attributes, and styles that are invalid but which a given site cannot afford to be removed. I'm thinking we'd want CSS over the 50KB limit to default to not be removed, even if that means it is invalid AMP. In terms of implementation here, I think this can be implemented by allowing the |
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.
Approved
Hi @westonruter,
This PR looks good, and it's approved. There's a small point about test_body_style_attribute_sanitizer()
, but it's not a blocker.
Also:
allowing the
validation_error_callback
to return false as a way to prevent removal from happening
That sounds good, finalize_stylesheet_set()
could check the return value of the validation_error_callback
before removing the stylesheet.
* @type callable $validation_error_callback Function to call when a validation error is encountered. | ||
* } | ||
*/ | ||
protected $args; |
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.
Nice DocBlock for this.
array(), | ||
), | ||
'styles_with_dynamic_elements' => array( | ||
implode( '', array( |
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.
Great idea to use implode()
here for the long document.
*/ | ||
public function test_body_style_attribute_sanitizer( $source, $expected_content, $expected_stylesheets ) { | ||
public function test_body_style_attribute_sanitizer( $source, $expected_content, $expected_stylesheets, $expected_errors = array() ) { |
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.
Maybe the 3rd $expected_errors
parameter isn't necessary, given that the dataProvider
get_link_and_style_test_data always has a 3rd array item: array()
. But this isn't a blocker to approving.
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.
Yeah, you're right. But I suppose it aligns with the other tests and opens the door to testing for validation errors in the future.
AMP_Response_Headers
class. See Incorporate Server Timing API #990.-moz-binding
andbehavior
).style
attributes.amp-keyframes
appears anywhere but end ofbody
, just move it to the end of thebody
.!important
qualifiers.@keyframes
to opacity, transform and -vendorPrefix-transform.:not(.classname)
. Also strip strings.Fixes #990.
Fixes #930.
Fixes #688.
Closes #610.
Ideas for later: