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

Global styles: filter block level styles before compiling global stylesheet #42207

Merged
merged 1 commit into from
Jul 7, 2022

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Jul 7, 2022

Fixes #42206

What? Why? How?

Since #42005, global block styles are enqueued twice:

  1. In global-styles-inline-css
  2. In wp-block-social-links-inline-css

This causes duplicate styles in styles.blocks[ $block_name ] rendered to the HTML.

The gutenberg_get_style_nodes filter has to be registered before we call gutenberg_get_global_stylesheet() otherwise global styles won't filter out the block styles that are later enqueued.

Testing Instructions

Add some block styles to your theme.json. Any block will do.

I'm using emptytheme and the Social links block as an example.

Example theme.json
{
	"version": 2,
	"settings": {
		"appearanceTools": true,
		"layout": {
			"contentSize": "840px",
			"wideSize": "1100px"
		}
	},
	"styles": {
		"spacing": {
		},
		"blocks": {
			"core/social-links": {
				"spacing": {
					"blockGap": "55px",
					"margin": "100px",
					"padding": "150px"
				},
				"color": {
					"text": "red",
					"background": "yellow"
				}
			}
		}
	},
	"patterns": [
		"short-text-surrounded-by-round-images",
		"partner-logos"
	]
}

Create a post with that block and publish it.

Example block code
<!-- wp:social-links -->
<ul class="wp-block-social-links"><!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /--></ul>
<!-- /wp:social-links -->

Inspect the block HTML and see that the styles are NOT applied twice.

… call gutenberg_get_global_stylesheet() otherwise global styles won't filter out the block styles that are later enqueued.
@ramonjd ramonjd requested a review from spacedmonkey as a code owner July 7, 2022 01:45
@ramonjd ramonjd self-assigned this Jul 7, 2022
@ramonjd ramonjd added [Type] Bug An existing feature does not function as intended Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Jul 7, 2022
@ramonjd ramonjd requested review from scruffian and draganescu July 7, 2022 01:46
Copy link
Contributor

@aaronrobertshaw aaronrobertshaw 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 fixing this @ramonjd 👍

This PR tests as advertised

✅ Could replicate the issue on trunk
✅ After checking out this PR branch the duplicates were gone
✅ Tested using supplied snippets, different dynamic and static blocks, with different themes, and in both; site and block editors.

LGTM 🚢

Before After
Screen Shot 2022-07-07 at 5 39 38 pm Screen Shot 2022-07-07 at 5 40 27 pm

@ramonjd ramonjd merged commit dd8d577 into trunk Jul 7, 2022
@ramonjd ramonjd deleted the fix/global-block-styles-enqueued-twice branch July 7, 2022 22:36
@github-actions github-actions bot added this to the Gutenberg 13.7 milestone Jul 7, 2022
@scruffian
Copy link
Contributor

Thanks for fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Global styles: block level styles are enqueued twice
3 participants