Block Style Variations: Simplify block style variation selector regex#79924
Conversation
|
Size Change: -92 B (0%) Total Size: 7.67 MB 📦 View Changed
|
ramonjd
left a comment
There was a problem hiding this comment.
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');
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. 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.
332846c to
1cfbfb1
Compare
|
Thanks for the speedy review @ramonjd 🚀 I've added a backport changelog now so will hit automerge. |
|
Flaky tests detected in d399152. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/28842643639
|
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?
/((?::\([^)]+\))?\s*)([^\s:]+)/to/[^\s:]+/and adds a comment with examples.$/\being interpreted).@wordpress/global-styles-engine) and backfills a missing:isselector-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.npm run test:unit:php:base -- --filter test_get_block_style_variation_selectornpm run test:unit packages/global-styles-engine/src/test/utils.test.ts