Skip to content

Block Supports: Guard elements hover rendering against missing hover selector#79511

Merged
aaronrobertshaw merged 3 commits into
trunkfrom
fix/elements-hover-selector-guard
Jun 25, 2026
Merged

Block Supports: Guard elements hover rendering against missing hover selector#79511
aaronrobertshaw merged 3 commits into
trunkfrom
fix/elements-hover-selector-guard

Conversation

@aaronrobertshaw

@aaronrobertshaw aaronrobertshaw commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

What?

Related: #77894

Only render :hover element styles for element types that actually define a hover selector.

Why?

In gutenberg_render_elements_support_styles(), only the link element type defines a hover_selector. If a block's style.elements carries a :hover object for button or heading, the code reaches for $element_config['hover_selector'] on a type that doesn't have it. That emits an "Undefined array key 'hover_selector'" warning and passes an empty selector to the style engine.

It's hard to reach through the default UI, but the attribute data can be persisted on a block directly, so the warning stands on its own.

How?

  • Updates the guard here in Gutenberg to match the approach already in WordPress core.
  • Adds regression test to cover missing hover selectors while an element has hover styles.

Testing Instructions

  1. Updated WP_Block_Supports_Elements_Test should pass

@aaronrobertshaw aaronrobertshaw self-assigned this Jun 24, 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 labels Jun 24, 2026
@github-actions

github-actions Bot commented Jun 24, 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.

@github-actions

github-actions Bot commented Jun 25, 2026

Copy link
Copy Markdown

Flaky tests detected in f4c170d.
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/28149287568
📝 Reported issues:

@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.

Nice find. I didn't test, but it's clear from the code that the guard makes sense since we're trying to access 'selector' => $element_config['hover_selector'], from within the condition block.

Comment thread lib/block-supports/elements.php Outdated
Comment thread backport-changelog/7.1/12312.md
aaronrobertshaw added a commit to aaronrobertshaw/wordpress-develop that referenced this pull request Jun 25, 2026
Only render `:hover` element styles for element types that actually define a
hover selector. In `wp_render_elements_support_styles()`, only the `link`
element type defines a `hover_selector`. If a block's `style.elements` carries
a `:hover` object for `button` or `heading`, accessing
`$element_config['hover_selector']` reaches for a key that does not exist,
emitting an "Undefined array key 'hover_selector'" warning and passing an
empty selector to the style engine.

Add a `! empty( $element_config['hover_selector'] )` guard so hover processing
is skipped for element types without a hover selector, and extend the elements
styles test with a `button` `:hover` case that confirms only the base rule is
emitted.

Backport of Gutenberg PR WordPress/gutenberg#79511.

Props aaronrobertshaw.
See #65538.
Co-authored-by: Ramon <ramonjd@users.noreply.github.com>
@aaronrobertshaw aaronrobertshaw added the No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core label Jun 25, 2026
@aaronrobertshaw

Copy link
Copy Markdown
Contributor Author

I'll get this merged. I had overlooked an outdated regex in the test backported to core but that should all be good now.

@aaronrobertshaw aaronrobertshaw removed the No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core label Jun 25, 2026
@aaronrobertshaw aaronrobertshaw merged commit 737e347 into trunk Jun 25, 2026
50 of 52 checks passed
@aaronrobertshaw aaronrobertshaw deleted the fix/elements-hover-selector-guard branch June 25, 2026 20:46
@github-actions github-actions Bot added this to the Gutenberg 23.6 milestone Jun 25, 2026
SainathPoojary pushed a commit to SainathPoojary/gutenberg that referenced this pull request Jun 29, 2026
…selector (WordPress#79511)

Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[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