-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add aria-label
attribute to navigation block only when it is open
#54816
Conversation
Size Change: +26.2 kB (+2%) Total Size: 1.65 MB
ℹ️ View Unchanged
|
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.
The new logic looks good to me, but I think we should escape the context before merging 🙂
@@ -698,7 +698,7 @@ function render_block_core_navigation( $attributes, $content, $block ) { | |||
if ( $should_load_view_script ) { | |||
$nav_element_directives = ' | |||
data-wp-interactive | |||
data-wp-context=\'{ "core": { "navigation": { "overlayOpenedBy": {}, "type": "overlay", "roleAttribute": "" } } }\' | |||
data-wp-context=\'{ "core": { "navigation": { "overlayOpenedBy": {}, "type": "overlay", "roleAttribute": "", "ariaLabel": "' . __( 'Menu' ) . '" } } }\' |
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.
We should escape this because translations might include not supported characters (like quotes or brackets…). Or just move the whole context into a $context
variable and use wp_json_encode( $context )
instead.
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.
That makes sense 🙂 I added the esc_attr__ function. I hope that is enough. If you feel it is clearer, I'm happy to mode it to a $context
variable.
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.
I think that one turns things like "
into "
, and it needs to be \"
. But I'm not an expert on this, so maybe I'm wrong. Can you check if JSON.parse()
turns the escaped items (like "
or >
) back to its original form?
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.
Oh, you're right. esc_attr__
doesn't seem to be working fine. If I include a quote "
inside the string, it is not able to parse it and it throws an error. I believe we have two options:
- Use wp_slash or directly addslashes.
- Move it to a variable and use
wp_json_encode
as you suggested. We are using it in the query block: link.
I've tested both approaches and they seem to work. Any preference?
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.
wp_json_encode
seems more explicit and secure 🙂
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.
Thanks for taking a look at this, @SantosGuillamot. I can see in the video you shared that the problem arises when the data-wp-context
directive runs in PHP.
What I don't fully understand is that it seems to be an issue serializing the context as JSON (instead of parsing)... 🤔
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.
I looked at the HTML when using it in the navigation block.
In the client
- Without the
JSON_HEX_APOS
, the HTML isdata-wp-context="{"text":"I" m="" a="" string"}'=""
. - With
JSON_HEX_APOS
, the HTML isdata-wp-context="{"text":"I\u0027m a string"}"
.
In the server
- Without the
JSON_HEX_APOS
, the HTML isdata-wp-context='{"text":"I'm a string"}'
. - With
JSON_HEX_APOS
, the HTML isdata-wp-context='{"text":"I\u0027m a string"}'
.
So something seems definitely broken, right?
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.
Okay, I've added the flags to both the navigation and the query blocks. Let me know if something else is needed.
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.
For context, the "Fatal error" that can be seen in #54816 (comment) is caused by a problem insidegutenberg_interactivity_process_wp_context
implementation, which is calling $context->rewind_context()
even when no context where added on a potential failure. That could empty the context stack and make subsequent $context->set_context( $new_context )
calls fail.
I'll open an issue to address that later on.
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.
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.
Thanks for working on this. From an a11y perspective looks good to me.
I'd leave the code review to others more familiar than me with this implementation.
Note: I could use this approach also for #55010
Flaky tests detected in b8f157d. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6430096160
|
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.
Thanks for looking into this, Mario & David!
We defintely need to create a helper function. People using the Interactivity API should not have to remember to use all those flags 😄
Discussion here:
Totally agree 🙂 It is not intuitive why those flags, and not others, are needed. By the way, it seems a test of the cover block is failing. I cannot see the relation with this pull request and it seems it's failing in other PRs like this one as well. I'll rerun the tests once that is solved. |
…54816) * Add `aria-label` only when is open * Remove unnecessary `label` property in edit * Escape translation * Move navigation context to `wp_json_encode` * Add `wp_json_encode` flags
I just cherry-picked this PR to the 6.4-beta3-2 branch to get it included in the next release: 80e0020 |
* Site Editor Styles Screen: Fix dancing styles previews (#55183) * Pulling across changes from WordPress/wordpress-develop#5441 (#55181) Removed var * Add `aria-label` attribute to navigation block only when it is open (#54816) * Add `aria-label` only when is open * Remove unnecessary `label` property in edit * Escape translation * Move navigation context to `wp_json_encode` * Add `wp_json_encode` flags * Paste: fix MS Word list paste (#55127) * Paste: fix MS Word list paste * Match mso-list:Ignore * Fix inline paste * Fix scrollbars on pattern transforms (#55069) * Fix scrollbars on pattern transforms * Fix single pattern previews * Improve classname semantics * Remove modal title * Reset styles on window resize (#55077) Co-authored-by: Ricardo Artemio Morales <ric.morales22@gmail.com> --------- Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Co-authored-by: Ramon <ramonjd@users.noreply.github.com> Co-authored-by: Mario Santos <34552881+SantosGuillamot@users.noreply.github.com> Co-authored-by: Ella <4710635+ellatrix@users.noreply.github.com> Co-authored-by: Daniel Richards <daniel.richards@automattic.com> Co-authored-by: Ricardo Artemio Morales <ric.morales22@gmail.com>
What?
Fix the issue reported here.
This pull request adds the logic to only add the
aria-label
attribute to the navigation block when this is open.Why?
As reported in the mentioned issue, it is causing accessibility issues.
How?
I basically removed that attribute from the server side rendering and added some logic in JavaScript to load it conditionally only when the menu is open.
I also removed an unnecessary prop of the React component used in the editor.
Testing Instructions
Before this fix
div
with classwp-block-navigation__responsive-dialog
. It has the attributearia-label="Menu"
, which is not correct.After this fix
div
with classwp-block-navigation__responsive-dialog
. It has NOT the attributearia-label
.aria-label="Menu"
is added.