-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Block support: Add anchor support for dynamic blocks #74183
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
Conversation
bd79ce2 to
af17ec0
Compare
af17ec0 to
ade13f4
Compare
|
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. |
Is this due to the nature of the block (Classic, HTML, blocks without a wrapper) or some other limitation with the feature? |
|
Flaky tests detected in 17a3b1d. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/20487968254
|
SirLouen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@t-hamano I've tested it, and it's looking good. ✅
But there are two proposed changes to the unit tests:
- The file should end in
-test.phpso in this caseanchor-test.php - Can you add this test to the data provider?
'anchor with zero string is applied' => array(
'support' => true,
'value' => '0',
'expected' => array( 'id' => '0' ),
),
And test it?
aaronrobertshaw
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is testing well for me 👍
Other than the suggested rename of the test file and the additional test cases, I think this is pretty close. While updating the test cases I think we could cover some more edge cases there.
Some possible test cases
public function data_anchor_block_support() {
return array(
'anchor id attribute is applied' => array(
'support' => true,
'value' => 'my-anchor',
'expected' => array( 'id' => 'my-anchor' ),
),
'anchor id attribute is not applied if block does not support it' => array(
'support' => false,
'value' => 'my-anchor',
'expected' => array(),
),
'empty anchor value returns empty array' => array(
'support' => true,
'value' => '',
'expected' => array(),
),
'null anchor value returns empty array' => array(
'support' => true,
'value' => null,
'expected' => array(),
),
'whitespace-only anchor value is applied' => array(
'support' => true,
'value' => ' ',
'expected' => array( 'id' => ' ' ),
),
'anchor with hyphen and numbers' => array(
'support' => true,
'value' => 'section-123',
'expected' => array( 'id' => 'section-123' ),
),
'anchor with underscore' => array(
'support' => true,
'value' => 'my_anchor_id',
'expected' => array( 'id' => 'my_anchor_id' ),
),
'anchor with colon (valid in HTML5)' => array(
'support' => true,
'value' => 'my:anchor',
'expected' => array( 'id' => 'my:anchor' ),
),
'anchor with period (valid in HTML5)' => array(
'support' => true,
'value' => 'my.anchor',
'expected' => array( 'id' => 'my.anchor' ),
),
'numeric anchor value' => array(
'support' => true,
'value' => '123',
'expected' => array( 'id' => '123' ),
),
'zero value is treated as empty' => array(
'support' => true,
'value' => 0,
'expected' => array(),
),
'false value is treated as empty' => array(
'support' => true,
'value' => false,
'expected' => array(),
),
);
}I primarily tested with the Calendar, Latest Comments, Search, and Navigation blocks but did randomly try several more. Each worked as per the test instructions.
Screen.Recording.2025-12-23.at.5.33.23.pm.mp4
|
Thanks everyone for the reviews!
These blocks don't seem to work because they don't have a block wrapper in the first place. On the other hand, I thought we could add anchor support to the Template Part block, but it didn't work properly. Perhaps there's a problem with the server-side rendering code. I'll investigate this in a follow-up.
Updated the test cases 👍 |
| 'zero string anchor value is applied' => array( | ||
| 'support' => true, | ||
| 'value' => '0', | ||
| 'expected' => array( 'id' => '0' ), | ||
| ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this test result is correct, it doesn't actually produce the expected results. That is, when you set an ID value of zero, it doesn't actually render.
This is likely an issue with get_block_wrapper_attributes() function, which causes falsy values to be skipped.
This issue should also occur with className support and ariaLabel support.
I'll submit a follow-up core ticket regarding this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, empty() will return true for '0' and 0.
Mamaduka
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is test well for me ✅
Request changes have already been addressed.
Closes #51402
What?
This PR adds anchor support to almost all dynamic blocks.
Why?
Efforts to add anchor support to dynamic blocks were made in the past in #44771. However, that work also affected static blocks, potentially breaking them, so it was eventually reverted.
To add consistent anchor support regardless of whether the block is dynamic or static, the anchor is now always kept in comment delimiters (See #70993). This means we can now add anchor support to dynamic blocks.
How?
As a result, these are the blocks that do not support anchors. The reason I didn't enable it is either:
core/navigation-overlay-closecore/tabscore/accordion-panelcore/accordion-itemcore/patterncore/template-partcore/missingcore/freeformcore/blockcore/htmlcore/shortcodecore/text-columnscore/post-commentcore/form-submission-notificationcore/form-submit-buttoncore/comment-author-avatarTesting Instructions