Elements: Align class name parsing with custom CSS implementation#79023
Conversation
Mirror in `gutenberg_render_elements_class_name()` the hardening already applied to `gutenberg_render_custom_css_class_name()`: replace the regular expression with a `str_contains()` short-circuit followed by an HTML-spec-compliant `strtok()` walk over the class tokens. This also corrects a latent matching bug, since the previous `\bwp-elements-\S+\b` pattern treated the hyphen as a word boundary, so a class such as `my-wp-elements-foo` was erroneously matched. Tokenizing the attribute first ensures only a standalone `wp-elements-*` class is applied. Refactor `gutenberg_render_custom_css_class_name()` to use a shared `$class_name_prefix` variable and `$matched_class_name` so the two functions stay structurally identical, and add a regression test covering the substring-prefix case. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
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. |
|
Flaky tests detected in b4ec983. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/27177955117
|
aaronrobertshaw
left a comment
There was a problem hiding this comment.
Thanks for the PR and the core tweaks @westonruter 🙇
✅ Changes here faithfully match related core commits
✅ Backport changelog looks good
✅ New and existing tests pass
LGTM 🚢
What?
Follow-up to #78841 and its core companion WordPress/wordpress-develop#12028 (committed in r62475), bringing the additional hardening developed during the core review back to Gutenberg so the two stay in sync.
During the core review of #78841,
wp_render_elements_class_name()was aligned withwp_render_custom_css_class_name()(#78217 / r62359). This PR applies the same alignment here.Why?
gutenberg_render_elements_class_name()matched thewp-elements-*class with\bwp-elements-\S+\b. Because a hyphen is a\bword boundary in regex, a class such asmy-wp-elements-foowas erroneously matched. Parsing theclassNameattribute into tokens per the HTML spec and checking each withstr_starts_with()fixes this, and avoids the regex engine entirely for the common case via an upfrontstr_contains()short-circuit.How?
gutenberg_render_elements_class_name(): replaced the regex with astr_contains()guard followed by astrtok()walk over the class tokens; added an@phpstan-paramarray shape for$block.gutenberg_render_custom_css_class_name(): refactored to use a shared$class_name_prefixvariable and$matched_class_nameso both functions remain structurally identical.my-wp-elements-foosubstring-prefix case (the non-stringclassNametest already existed).These match the corresponding core changes in r62475 byte-for-byte (modulo the
gutenberg_/wp_prefixes).Testing Instructions
npm run test:unit:php -- --filter WP_Block_Supports_Elements_Testtest_elements_block_support_class_with_invalid_elements_prefixandtest_elements_block_support_class_with_non_string_class_namepass, and existing tests continue to pass.🤖 Generated with Claude Code