Skip to content
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

Prevent completely tree-shaken styles from triggering excessive CSS #6624

Merged
merged 3 commits into from
Dec 2, 2021

Conversation

westonruter
Copy link
Member

Summary

This fixes an issue encountered by @delawski where even though a stylesheet may have zero bytes after tree-shaking, it can still end up causing an “Excessive CSS” validation error. The stylesheet here in question is the emoji styles being printed:

/**
* Print the important emoji-related styles.
*
* @see print_emoji_styles()
* @staticvar bool $printed
*/
public static function print_emoji_styles() {
static $printed = false;
if ( $printed ) {
return;
}
$printed = true;
?>
<style type="text/css">
img.wp-smiley,
img.emoji {
display: inline-block !important; /* Patched from core, which had display:inline */
border: none !important;
box-shadow: none !important;
height: 1em !important;
width: 1em !important;
margin: 0 .07em !important;
vertical-align: -0.1em !important;
background: none !important;
padding: 0 !important;
}
</style>
<?php
}

After quite a bit of trial and error, I got the stylesheets for the theme to add up to the maximum bytes on the page (which is 75000 minus the byte count for the source map comment). I then added a plugin to add a stylesheet before the AMP emoji styles:

<?php
/**
 * Plugin Name: Add Style To Beginning of Head
 */

add_action('wp_head', function () {
	?>
	<style>
		body:after { content:'a' }
	</style>
	<?php
}, 0);

I then created a post which had no emoji on it so that all of the emoji styles would be tree-shaken.

I marked the validation error for the styles from “Add Style To Beginning of Head” as kept to so that those styles would be forcibly added to the page and trigger any other styles as going over the limit.

Before After
image image

Notice that the entirely tree-shaken stylesheet is no longer showing as excluded and it is no longer raising a validation error.

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@github-actions
Copy link
Contributor

github-actions bot commented Sep 28, 2021

Plugin builds for b256089 are ready 🛎️!

…sheet-concat

* 'develop' of github.com:ampproject/amp-wp: (643 commits)
  Update CODEOWNERS
  Update amphtml spec to 2111242025000
  Use CF7 instead of Autoptimize for AMP-incompatible plugin in E2E tests
  Replace autoptimize with akismet for E2E tests
  Pin NodeJS to v14 to match WP core and Gutenberg
  Regenerate package-lock.json at lockfileVersion 1
  Update Gutenberg package dependencies
  Stop mapping E2E test plugins dir to container volume
  Add demo plugin ZIP archive
  Fix script command
  Fix lint issues
  Ensure React state updates are not performed on unmounted components
  Use different Jest configs for E2E tests; use `.env` config file
  Refactor "Other Settings" E2E tests
  Ensure selector is available before scrolling element into view
  Use more robust internal activate/deactivate functions in E2E tests
  Introduce general `saveSettings` util function for E2E tests
  Remove unnecessary cleanup functions in E2E tests
  Install Hestia theme globally as it is used in multiple tests
  Use local demo plugin in E2E tests
  ...
…sheet-concat

* 'develop' of github.com:ampproject/amp-wp:
  Add test case for Bento lacking layout attributes; improve test coverage
  Fix phpcs alignment warning
  Cast term IDs to integers instead of using sanitize_key()
  Update Gutenberg package dependencies
  Use strict check for in_array() and improve comment
  Derive intrinsic layout when max-width defined
  Validate supported layouts for converted Bento components; add support for fill and nodisplay
  Update AMP_Base_Sanitier::set_layout() to account for 100% width/height styles
  Harmonize Bento component name parsing
  Fix variable name
  Fix typos in comments
  Add support for converting Bento component dimensions from inline styles to AMP layout
  Further improve test converage and remove dead code
  Improve test coverage for ID attribute changes
  Improve Bento sanitizer and add tests
  Prevent bento components from being treated as light shadow DOM wrappers
  Prevent JS error on validated URL screen when no validation errors present
  Fix handling of ID attributes during sanitizer conversion
  Introduce Bento sanitizer to deal with BentoJS components
Copy link
Collaborator

@delawski delawski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me.

When testing locally, I was able to reproduce the issue. After applying this patch, a fully tree shaken stylesheet was no longer marked as "Excluded":

Before After
Screenshot 2021-12-02 at 20 29 27 Screenshot 2021-12-02 at 20 30 04

@westonruter westonruter merged commit 0fce6d3 into develop Dec 2, 2021
@westonruter westonruter deleted the fix/stylesheet-concat branch December 2, 2021 20:07
@fellyph fellyph self-assigned this Dec 10, 2021
@fellyph
Copy link
Collaborator

fellyph commented Dec 13, 2021

QA Passed

Screen Shot 2021-12-13 at 10 38 16

@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelogged Whether the issue/PR has been added to release notes. CSS Validation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants