Split Reader mode style template part stylesheet from action-supplied styles #4205
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Splitting up stylesheets in Reader mode
The Reader mode templates have for a long time output all styles in a single
<style amp-custom>
tag. This was needed because theAMP_Style_Sanitizer
was originally not able to handle the parsing of stylesheets but onlystyle
attributes. Now that the Reader mode templates are passed through the post-processor (via #2202) allstyle
elements will be combined into a singlestyle[amp-custom]
element automatically. It is advantageous to split styles into multiple stylesheets because when a stylesheet goes above the limit of 50KB, it will get excluded. If a plugin adds a lot of styles via theamp_post_template_head
action, the result can be that all of the styles in thestyle
template part would also get excluded.By splitting the two sets of styles into separate stylesheets, only the styles in
amp_post_template_head
will be excluded if they are excessive. This is better because those styles are less important than those being included in thestyle
template part, which control the overall layout and design of the template. Otherwise, there is no difference in how the styles are included in the page.The inclusion of the class names on the
style
elements is purely for the purpose of debugging, when/if Reader mode templates get shown as Validated URLs in the admin, with the new stylesheet analysis (see #2169).Here's some example code that causes too much CSS to be added in
amp_post_template_css
:See also #2044.
Preventing invalid
style[amp-custom]
from passing throughI also noticed that in the
develop
branch this code actually does not get sanitized at all. In fact, in Reader mode more than 50KB of CSS are getting added to the page:The reason is that when there is only one single single
style
on the page and this stylesheet is greater than 50KB, then this condition is not entered since there are no included stylesheets:amp-wp/includes/sanitizers/class-amp-style-sanitizer.php
Lines 2625 to 2626 in 0d3ce78
When that condition is not entered, then the previously-captured
amp_custom_style_element
element is not subsequently emptied out to populate with the processed CSS (since there is none):amp-wp/includes/sanitizers/class-amp-style-sanitizer.php
Lines 2644 to 2653 in 0d3ce78
So the fix is simply to discontinue capturing any original
style[amp-custom]
in favor of just creating a fresh empty new one to add to the document only if there are valid stylesheets to include.Checklist