Skip to content

Add stylesheet information to validated URL screen#4026

Merged
westonruter merged 42 commits intodevelopfrom
add/sanitizer-augmented-validate-response-data
Jan 24, 2020
Merged

Add stylesheet information to validated URL screen#4026
westonruter merged 42 commits intodevelopfrom
add/sanitizer-augmented-validate-response-data

Conversation

@westonruter
Copy link
Copy Markdown
Member

@westonruter westonruter commented Jan 5, 2020

Summary

  • Add “CSS Usage” information to AMP admin bar menu item. The total percentage of the 50KB limit is shown, with a ⚠️ if the percentage is ≥80% and 🚫 if greater than 100%. Fixes Show notice on frontend when there is excessive CSS #1801.
  • CSS Usage information is also shown in the Validated URL post list table.
  • Add “Stylesheets” metabox to the validated URL screen which includes an expandable list of all stylesheets with details including;
    • Original size
    • Minified size
    • Minification rate
    • Relative size compared to the rest of the CSS on the page
    • Priority (e.g. theme stylesheets are more important than plugin stylesheets)
    • Status: 🚫 when excluded, ⚠️ when included but over limit (in Standard mode)
    • Original markup which added the stylesheet.
    • Sources, whether plugin, theme, or core. Full source stack available after expanding.
    • CSS code with checkbox to reveal which code was removed after tree shaking.
  • The CSS manifest HTML comment is removed (since the validated URL screen has all the same information and more), thus the include_manifest_comment argument to the style sanitizer has been eliminated.
  • The format of the parsed CSS stylesheet is improved to strip braces from the stored strings and to store properties as an array rather than a serialized string.
  • The validation response data (also obtainable via wp amp validation check-url) includes the raw stylesheet information. To facilitate this, a new get_validate_response_data method has been added to the base sanitizer which is used during a validation response for sanitizers to augment the response with additional sanitizer-specific data (such as stylesheets).

Fixes #2169.

Note 1: The work here was done within the architecture of the current codebase, which makes the implementation not as ideal as possible, especially for the newly added UI. This will be addressed when the UI is revamped to be more modern.

Note 2: The storage of the stylesheet data alongside the validation errors and the queried object are highlighting deficiencies of the amp_validated_url post type data format and the API of AMP_Validated_URL_Post_Type::store_validation_errors(). These will need to be addressed with a later refactor.

See screenshots in comments below.

Todo

  • Verify memory usage.
  • Ensure sources each appear on a separate line (missing display:block)
  • Trim needless braces from stored declaration blocks.
  • For style attributes and other style rules, add explanation for why :not(#_) shows up so much.
  • Add meter or percentage usage column in Validated URL post list table.
  • Show stylesheets with warnings even when in standard mode, when they go over 50KB.
  • Add AMP admin bar item for CSS budget used along with notice (see Show notice on frontend when there is excessive CSS #1801); link to stylesheets metabox on validated URL screen.
  • Remove CSS manifest comment entirely since obsolete.
  • Ensure that all information for the stylesheet is presented in the table, including the handle (and ideally the dependencies).

For Later

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • 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).

@westonruter westonruter added this to the v1.5 milestone Jan 5, 2020
@googlebot googlebot added the cla: yes Signed the Google CLA label Jan 5, 2020
@westonruter
Copy link
Copy Markdown
Member Author

Latest changes:

image

Showing what CSS was removed during tree-shaking ant what CSS remains:

image

Showing where a stylesheet came from (even when it didn't cause an excessive CSS error):

image

…e_document method

Add tests for AMP_Content_Sanitizer, removing obsolete AMP_Test_World_Sanitizer
…alidation_Manager::get_validate_response_data()
…udget but still included

Also improve message
<?php
$origin_abbr_text = '?';
if ( 'link_element' === $stylesheet['origin'] ) {
$origin_abbr_text = '<link&nbsp;&hellip;>'; // @todo Consider adding the basename of the CSS file.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Including the handle would be useful as well.

.shaken-stylesheet {
display: block;
white-space: pre-wrap;
overflow-x: scroll;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this needed? In my (brief) testing I was not able to find a stylesheet that overflowed the edges, and instead rendered a disabled horizontal scroll bar and a vertical scroll bar:

image

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not seeing that horizontal scrollbar in either Chrome or Firefox.

You're right, though. This should not be necessary because of white-space: pre-wrap; and also because each property is now on a separate line after e0055b5.

Removed in da85b8f.

@westonruter
Copy link
Copy Markdown
Member Author

FYI: I used the branch for this PR to do the analysis of all themes on WordPress.org.

@westonruter westonruter requested a review from amedina January 24, 2020 07:10
Copy link
Copy Markdown
Contributor

@kienstra kienstra left a comment

Choose a reason for hiding this comment

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

Looks good!

Hi @westonruter,
This is a huge improvement, having a UI for this. The percentage in the admin bar is great, also.

There's only one comment here related to logic. The rest are UI-related.

}

$assets = AMP_Content_Sanitizer::sanitize_document( $dom, self::$sanitizer_classes, $args );
$sanitization_results = AMP_Content_Sanitizer::sanitize_document( $dom, self::$sanitizer_classes, $args );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice renaming

if ( empty( $stylesheets ) ) {
printf(
'<p><em>%s</em></p>',
esc_html__( 'No stylesheet data available. Please try re-checking this URL.', 'amp' )
Copy link
Copy Markdown
Contributor

@kienstra kienstra Jan 24, 2020

Choose a reason for hiding this comment

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

Nice handling of this case:

handling

<?php AMP_Validation_Error_Taxonomy::render_sources( $stylesheet['sources'] ); ?>
</dd>

<dt><?php esc_html_e( 'CSS Code', 'amp' ); ?></dt>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You've probably considered this, but this section can have a lot of code, like from twentynineteen-style-css:

css-code

Maybe a <details><summary>, similar to 'Sources'?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not seeing that. Can you please try clearing/disabling your cache?

Here's what I see:

image

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sure

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, it looks fine after a hard refresh:

refresh-hard

if ( $del_count > 0 ) {
printf(
'<p><label><input type="checkbox" class="show-removed-styles"> %s</label></p>',
esc_html__( 'Show styles removed during tree-shaking', 'amp' )
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

removed-tree-shaking

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You should be seeing this:

image

Only when the checkbox is checked should the styles be displayed:

image

Please try disabling/clearing cache.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, you're right that it looks fine after a hard refresh:

refresh-hard

const showRemovedStylesCheckbox = stylesheetDetailsElements.querySelector( '.show-removed-styles' );
if ( showRemovedStylesCheckbox ) {
showRemovedStylesCheckbox.addEventListener( 'click', () => {
shakenStylesheetContainer.classList.toggle( 'removed-styles-shown', showRemovedStylesCheckbox.checked );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This doesn't look to be working in my local:

div-stylesheet

I didn't see any console error.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is working fine now with a hard refresh.

<dt><?php esc_html_e( 'Original Markup', 'amp' ); ?></dt>
<dd><code class="stylesheet-origin-markup"><?php echo esc_html( $origin_html ); ?></code></dd>

<dt><?php esc_html_e( 'Sources', 'amp' ); ?></dt>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks really good:
sources-good

font-weight: bold;
text-align: right;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe adding something like this would help:

.amp-stylesheet-list th {
    width: 15%;
}

Without it, the columns are pretty narrow on a 16" display:

columns-narrow-here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The stylesheet actually already has:

image

Please try clearing/disabling cache and check again.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, sorry. The styling looks fine with a hard refresh:

refresh-hard

}

$css_usage_percentage = ceil( ( $total_size / $this->style_custom_cdata_spec['max_bytes'] ) * 100 );
$menu_item_text = __( 'CSS Usage', 'amp' ) . ': ';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

u-css

</button>
</td>
<td class="column-stylesheet_order">
<?php echo (int) $row; ?>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

$row is already an int here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes. This is done purely as a way to explicitly mark the variable as safe for printing. So it's an alternative to doing esc_html().

Co-Authored-By: Pierre Gordon <16200219+pierlon@users.noreply.github.com>
Copy link
Copy Markdown
Member

@amedina amedina left a comment

Choose a reason for hiding this comment

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

In the "Stylesheets" section part of the Validate URL screen, the status column can be seen as redundant. Since there is so much info on that table, I propose combining the Percentage column and the Status column into one. The semantics of the Status column can be conveyed by either just the color of the Percentage bar, or adding the Status symbol next to the Percentage bar.

Copy link
Copy Markdown
Member

@amedina amedina left a comment

Choose a reason for hiding this comment

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

Similarly, The Minified and Final Size columns can be combined, effectively removing another column and making the table less crowded. This can be done by Displaying Final Size (Minified %). For example: 220B (-40.5%).

<

@amedina
Copy link
Copy Markdown
Member

amedina commented Jan 24, 2020

In the "Stylesheets" section part of the Validate URL screen, the status column can be seen as redundant. Since there is so much info on that table, I propose combining the Percentage column and the Status column into one. The semantics of the Status column can be conveyed by either just the color of the Percentage bar, or adding the Status symbol next to the Percentage bar.

Discussed offline. These two columns play an important role for developers. We will keep them and will further the discussion to devise improvements afterwards.

Copy link
Copy Markdown
Member

@amedina amedina left a comment

Choose a reason for hiding this comment

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

Discussed comments offline. Ship it!

@westonruter westonruter merged commit 50d76db into develop Jan 24, 2020
@westonruter westonruter deleted the add/sanitizer-augmented-validate-response-data branch January 25, 2020 07:22
@westonruter
Copy link
Copy Markdown
Member Author

I should have added more tests. See #4175.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Signed the Google CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggestion: display CSS sizes in Compatibility Tool (move manifest) Show notice on frontend when there is excessive CSS

5 participants