-
Notifications
You must be signed in to change notification settings - Fork 382
Description
In order to be able to reliably excise !important from styles (#927), we need a proper CSS parser to be able to parse the rules so that higher-specificity selectors can be used instead of !important.
Additionally, a CSS parser used in conjunction with sanitization where the document has been output buffered and parsed into a DOM tree will allow us to strip out all CSS rules from the document that are not relevant to the current page, such as by checking to see if a rule references a class name that does not exist on the current page.
This will also address minification in #688. Class names could be minified in the CSS rules as well as in the document's elements that have the class attributes.
Current work can be seen in the add/css-parser branch. The idea is to use the same phpegjs library that is being used by Gutenberg to parse blocks. The PEG grammar needs to be written for CSS. The grammar currently in the branch needs to be simplified to reduce the granularity. We don't need each token separately parsed for a given selector, but rather just the entire selector string as a whole.
As part of this we should probably augment \AMP_Theme_Support::ensure_required_markup() to ensure the amp-boilerplate is present. Likewise, the way that AMP_WP_Styles waits to output styles should be removed in favor of just outputting them right away to then allow the sanitizer to collect them. The CSS sanitizer can then be responsible actually for adding the style[amp-custom] to the head.
This will supersede and incorporate #610 to switch from blacklist to whitelist. It will also need to improve the approach to scrubbing !important in #927. See #927 (comment).