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

Style engine: trim multiple selector strings #45873

Merged
merged 3 commits into from
Nov 22, 2022

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Nov 18, 2022

What?

This PR trims multiple selector string, that is a string of selectors separated by a comma, passed to the style engine.

Basically removing spaces between selectors in a given string. A "selector" is defined by a comma boundary: ','.

For example, let's say we pass a selector of .poirot, .poirot:active, #miss-marple > .st-mary-mead for a given set of CSS declarations.

Before

.poirot, .poirot:active, #miss-marple > .st-mary-mead{margin-left:0;font-family:Detective Sans;}

After

.poirot,.poirot:active,#miss-marple > .st-mary-mead{margin-left:0;font-family:Detective Sans;}'

And, prettified, this:

.poirot,
 .poirot:active,
 #miss-marple > .st-mary-mead {
	margin-left: 0;
	font-family: Detective Sans;
}

Becomes this:

.poirot,
.poirot:active,
#miss-marple > .st-mary-mead {
	margin-left: 0;
	font-family: Detective Sans;
}

Why?

This is so we can correctly align prettified content, and remove spaces between multiple selectors with the same styles.

How?

Exploding, mapping, trimming, imploding.

Testing Instructions

Run the tests!

npm run test:unit:php -- --filter WP_Style_Engine_CSS_Rule_Test

If you're feeling really keen, try adding multiple selectors to a block supports style. Layouts is a good place to test.

Here's a diff:

diff --git a/lib/block-supports/layout.php b/lib/block-supports/layout.php
index a50e1fb883..2f9a882e2e 100644
--- a/lib/block-supports/layout.php
+++ b/lib/block-supports/layout.php
@@ -54,11 +54,11 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
 					$slug            = _wp_to_kebab_case( substr( $gap_value, $index_to_splice ) );
 					$gap_value       = "var(--wp--preset--spacing--$slug)";
 				}
-
+				$test_id = wp_unique_id( 'pachycephalosaurus-' );
 				array_push(
 					$layout_styles,
 					array(
-						'selector'     => "$selector > *",
+						'selector'     => "$selector > *, .$test_id",
 						'declarations' => array(
 							'margin-block-start' => '0',
 							'margin-block-end'   => '0',

For any page with a default layout (e.g., add a simple group block to a page), the styles should be printed out neatly:

<style id='core-block-supports-inline-css'>
.wp-container-16.wp-container-16 > *,
.pachycephalosaurus-17,
.wp-container-19.wp-container-19 > *,
.pachycephalosaurus-20 {
	margin-block-start: 0;
	margin-block-end: 0;
}
</style>

@ramonjd ramonjd added [Type] Enhancement A suggestion for improvement. [Package] Style Engine /packages/style-engine labels Nov 18, 2022
@ramonjd ramonjd self-assigned this Nov 18, 2022
@codesandbox
Copy link

codesandbox bot commented Nov 18, 2022

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Thanks for picking this one up, and for the eagle eyes on the formatting of the output!

I've left a comment — I'm mostly wondering if it's worth only enabling this for prettified output, to attempt to (albeit very slightly) avoid extra processing when not prettifying. What do you think?

Comment on lines 116 to 119
// Trims any multiple selectors strings.
$selector_trimmed = implode( ',', array_map( 'trim', explode( ',', $this->get_selector() ) ) );
$selector = $should_prettify ? str_replace( array( ',' ), ",\n", $selector_trimmed ) : $selector_trimmed;
$css_declarations = $this->declarations->get_declarations_string( $should_prettify, $declarations_indent );
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this adds an extra step of processing, what do you think about only enabling this when $should_prettify is true?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for looking at this.

That's a fair point, and I totally agree.

Just to note that if we skip it, it'll mean that non-prettified selectors will have the space, e.g., the difference between:

.test, .me{...}

and

.test,.me{...}

This is, as they say, "no big deal". 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

DONE!

…d by a comma, passed to the style engine.

This is so we can correctly align prettified content,
and remove spaces between multiple selectors with the same styles.
@ramonjd ramonjd force-pushed the add/style-engine-trim-combined-selectors branch from 77c4092 to 7e6f26c Compare November 22, 2022 05:40
Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Thanks for the follow-up!

Prettifying works as expected:

image

And the default non-prettified behaviour appears to be unaffected:

image

LGTM! ✨

@ramonjd ramonjd merged commit 4c21122 into trunk Nov 22, 2022
@ramonjd ramonjd deleted the add/style-engine-trim-combined-selectors branch November 22, 2022 09:53
@github-actions github-actions bot added this to the Gutenberg 14.7 milestone Nov 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Style Engine /packages/style-engine [Type] Enhancement A suggestion for improvement.
Projects
Status: 🏆 Done
Development

Successfully merging this pull request may close these issues.

2 participants