Skip to content

Block Style Variations: Simplify block style variation selector regex#79924

Merged
aaronrobertshaw merged 2 commits into
trunkfrom
fix/block-style-variation-selector-regex
Jul 7, 2026
Merged

Block Style Variations: Simplify block style variation selector regex#79924
aaronrobertshaw merged 2 commits into
trunkfrom
fix/block-style-variation-selector-regex

Conversation

@aaronrobertshaw

Copy link
Copy Markdown
Contributor

What?

Related: #58051
Follow up to: #58051 (comment)

Tidies up the regex used to add a block style variation's class (e.g. .is-style-custom) to a block's selector.

Why?

The regex is hard to follow and its first capture group is effectively dead. It only matches a literal :(...), which isn't valid selector syntax (functional pseudo-classes like :is() and :where() always have a name between the colon and the parenthesis), so $matches[1] is always empty. The second group does all the work, so dropping the dead group gives identical output and is much easier to follow.

How?

  • Simplifies the pattern from /((?::\([^)]+\))?\s*)([^\s:]+)/ to /[^\s:]+/ and adds a comment with examples.
  • Keeps the replacement callback rather than switching to backreferences, so the variation name is always treated as a literal (no chance of $/\ being interpreted).
  • Mirrors the change in the JS equivalent (@wordpress/global-styles-engine) and backfills a missing :is selector-list test case in the PHP suite for parity.

Testing Instructions

This is a behaviour-preserving change, so the output should be identical to trunk.

  1. Confirm the PHP tests pass: npm run test:unit:php:base -- --filter test_get_block_style_variation_selector
  2. Confirm the JS tests pass: npm run test:unit packages/global-styles-engine/src/test/utils.test.ts
  3. Apply a block style variation to a block (e.g. a Quote or a section Group) and confirm the styles still render correctly in the editor and on the front end.

@aaronrobertshaw aaronrobertshaw self-assigned this Jul 7, 2026
@aaronrobertshaw aaronrobertshaw added [Type] Code Quality Issues or PRs that relate to code quality [Feature] Design Tools Tools that impact the appearance of blocks both to expand the number of tools and improve the experi [Feature] Block Style Variations Issues or PRs that are related to the style variations for blocks labels Jul 7, 2026
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

Size Change: -92 B (0%)

Total Size: 7.67 MB

📦 View Changed
Filename Size Change
build/modules/lazy-editor/index.min.js 14.4 kB -23 B (-0.16%)
build/scripts/block-editor/index.min.js 417 kB -26 B (-0.01%)
build/scripts/edit-site/index.min.js 299 kB -26 B (-0.01%)
build/scripts/editor/index.min.js 491 kB -17 B (0%)

compressed-size-action

@ramonjd ramonjd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Confirmed the regex simplification preserves the status quo.

Also that the old regex matched things like :(foo).wp-block

I tested matching behaviour in the console:

const oldRegex = /((?::\([^)]+\))?\s*)([^\s:]+)/;
const newRegex = /[^\s:]+/;
const variationClass = '.is-style-custom';

const cases = [
  '.wp-block',
  '#wp-block',
  'p',
  '[style*="color"]',
  '.wp-block .inner',
  'div:first-child',
  '.wp-block:is(.outer .inner:first-child)',
  '.wp-block:not(.outer .inner:first-child)',
  '.wp-block:has(.outer .inner:first-child)',
  '.wp-block:where(.outer .inner:first-child)',
  '::before',
  ':hover',
  '  .wp-block',
  '.a:b:c',
];

const fails = cases.filter((input) => {
  const oldResult = input.replace(oldRegex, (_m, g1, g2) => g1 + g2 + variationClass);
  const newResult = input.replace(newRegex, (m) => m + variationClass);
  return oldResult !== newResult;
});

console.log(fails.length ? fails : 'All pass');

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Removes a redundant, never-matching capture group from the ancestor
selector regex used when injecting a block style variation class. The
optional `(?::\([^)]+\))?` branch only matched literal `:(...)`, which
is not valid CSS (pseudo-classes always carry a name between the colon
and the parenthesis), so its capture was always empty. The second group
alone produces identical output.

Also adds an explanatory comment with worked examples, mirrors the change
in the JS/TS twin in @wordpress/global-styles-engine, and backfills a
missing `:is` selector-list test case in the PHP suite for parity with
the JS tests.

Follow-up to review feedback on #58051.
@aaronrobertshaw

Copy link
Copy Markdown
Contributor Author

Thanks for the speedy review @ramonjd 🚀

I've added a backport changelog now so will hit automerge.

@aaronrobertshaw aaronrobertshaw enabled auto-merge (squash) July 7, 2026 04:58
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

Flaky tests detected in d399152.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/28842643639
📝 Reported issues:

@aaronrobertshaw aaronrobertshaw merged commit 0a2be48 into trunk Jul 7, 2026
43 checks passed
@aaronrobertshaw aaronrobertshaw deleted the fix/block-style-variation-selector-regex branch July 7, 2026 05:31
@github-actions github-actions Bot added this to the Gutenberg 23.6 milestone Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Block Style Variations Issues or PRs that are related to the style variations for blocks [Feature] Design Tools Tools that impact the appearance of blocks both to expand the number of tools and improve the experi [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants