-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Table of Contents: Add aria label to the nav element #71586
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
827c0a3
Added aria label to the nav element
MaggieCabrera ef9f831
fixed fixtures to include the new attr
MaggieCabrera 9bd08b7
Update edit.js
mikachan 15848d4
Update save.js
mikachan 1536e44
Merge branch 'trunk' into toc-aria-label
mikachan 94942db
Try server-side rendering the block
mikachan efb3b7a
Fix coding standards errors
mikachan e03920d
Simplify server-side approach
mikachan 80f8cb2
Regenerate test fixtures
mikachan 14496a2
Simplify $aria_label
mikachan 276f4fe
Restore test fixtures
mikachan 9eb29a4
Revert changes to other test fixtures
mikachan 672461d
Update packages/block-library/src/table-of-contents/index.php
Mamaduka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Try server-side rendering the block
# Conflicts: # packages/block-library/src/table-of-contents/save.js
- Loading branch information
commit 94942db456455b71fe949d258946934dfd3b38f7
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| <?php | ||
| /** | ||
| * Server-side rendering of the `core/table-of-contents` block. | ||
| * | ||
| * @package WordPress | ||
| */ | ||
|
|
||
| /** | ||
| * Renders the `core/table-of-contents` block on the server. | ||
| * | ||
| * @param array $attributes Block attributes. | ||
| * @return string Returns the table of contents markup. | ||
| */ | ||
| function render_block_core_table_of_contents( $attributes ) { | ||
| $headings = isset( $attributes['headings'] ) ? $attributes['headings'] : array(); | ||
|
|
||
| if ( empty( $headings ) ) { | ||
| return ''; | ||
| } | ||
|
|
||
| // Get the aria-label from block attributes, or fallback to localized default. | ||
| $aria_label = ''; | ||
| if ( isset( $attributes['ariaLabel'] ) && ! empty( $attributes['ariaLabel'] ) ) { | ||
| $aria_label = $attributes['ariaLabel']; | ||
| } else { | ||
| $aria_label = __( 'Table of Contents', 'gutenberg' ); | ||
| } | ||
|
|
||
| $wrapper_attributes = get_block_wrapper_attributes( | ||
| array( | ||
| 'aria-label' => $aria_label, | ||
| ) | ||
| ); | ||
|
|
||
| $nested_headings = convert_linear_to_nested_headings( $headings ); | ||
| $list_items = build_nested_list_items( $nested_headings ); | ||
|
|
||
| $html = sprintf( | ||
| '<nav %s><ol>%s</ol></nav>', | ||
| $wrapper_attributes, | ||
| $list_items | ||
| ); | ||
|
|
||
| $processor = new WP_HTML_Tag_Processor( $html ); | ||
| if ( $processor->next_tag( 'nav' ) ) { | ||
| $processor->set_attribute( 'aria-label', $aria_label ); | ||
| } | ||
|
|
||
| return $processor->get_updated_html(); | ||
| } | ||
|
|
||
| /** | ||
| * Converts linear heading list to nested structure. | ||
| * | ||
| * @param array $headings Linear array of headings. | ||
| * @return array Nested array of headings. | ||
| */ | ||
| function convert_linear_to_nested_headings( $headings ) { | ||
|
Check failure on line 58 in packages/block-library/src/table-of-contents/index.php
|
||
| $nested_heading_list = array(); | ||
|
|
||
| foreach ( $headings as $key => $heading ) { | ||
| if ( empty( $heading['content'] ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| if ( $heading['level'] === $headings[0]['level'] ) { | ||
| if ( isset( $headings[ $key + 1 ] ) && $headings[ $key + 1 ]['level'] > $heading['level'] ) { | ||
| $end_of_slice = count( $headings ); | ||
| for ( $i = $key + 1; $i < count( $headings ); $i++ ) { | ||
| if ( $headings[ $i ]['level'] === $heading['level'] ) { | ||
| $end_of_slice = $i; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| $nested_heading_list[] = array( | ||
| 'heading' => $heading, | ||
| 'children' => convert_linear_to_nested_headings( | ||
| array_slice( $headings, $key + 1, $end_of_slice - $key - 1 ) | ||
| ), | ||
| ); | ||
| } else { | ||
| $nested_heading_list[] = array( | ||
| 'heading' => $heading, | ||
| 'children' => array(), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $nested_heading_list; | ||
| } | ||
|
|
||
| /** | ||
| * Builds nested list items HTML recursively. | ||
| * | ||
| * @param array $nested_headings Nested array of headings. | ||
| * @return string HTML for nested list items. | ||
| */ | ||
| function build_nested_list_items( $nested_headings ) { | ||
|
Check failure on line 100 in packages/block-library/src/table-of-contents/index.php
|
||
| $html = ''; | ||
|
|
||
| foreach ( $nested_headings as $item ) { | ||
| $heading = $item['heading']; | ||
| $link = ! empty( $heading['link'] ) ? $heading['link'] : ''; | ||
|
|
||
| $html .= '<li>'; | ||
| $html .= sprintf( | ||
| '<a class="wp-block-table-of-contents__entry" href="%s">%s</a>', | ||
| esc_url( $link ), | ||
| esc_html( $heading['content'] ) | ||
| ); | ||
|
|
||
| if ( ! empty( $item['children'] ) ) { | ||
| $html .= '<ol>' . build_nested_list_items( $item['children'] ) . '</ol>'; | ||
| } | ||
|
|
||
| $html .= '</li>'; | ||
| } | ||
|
|
||
| return $html; | ||
| } | ||
|
|
||
| /** | ||
| * Registers the `core/table-of-contents` block on the server. | ||
| */ | ||
| function register_block_core_table_of_contents() { | ||
| register_block_type_from_metadata( | ||
| __DIR__ . '/table-of-contents', | ||
| array( | ||
| 'render_callback' => 'render_block_core_table_of_contents', | ||
| ) | ||
| ); | ||
| } | ||
| add_action( 'init', 'register_block_core_table_of_contents' ); | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1 @@ | ||
| <!-- wp:table-of-contents {"headings":[{"content":"Heading text","level":2,"link":"#heading-id-1"},{"content":"A sub-heading","level":3,"link":"#heading-id-2"}]} --> | ||
| <nav class="wp-block-table-of-contents" aria-label="Table of Contents"><ol><li><a class="wp-block-table-of-contents__entry" href="#heading-id-1">Heading text</a><ol><li><a class="wp-block-table-of-contents__entry" href="#heading-id-2">A sub-heading</a></li></ol></li></ol></nav> | ||
| <!-- /wp:table-of-contents --> | ||
| <!-- wp:table-of-contents {"headings":[{"content":"Heading text","level":2,"link":"#heading-id-1"},{"content":"A sub-heading","level":3,"link":"#heading-id-2"}]} /--> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
test/integration/fixtures/blocks/core__table-of-contents.serialized.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1 @@ | ||
| <!-- wp:table-of-contents {"headings":[{"content":"Heading text","level":2,"link":"#heading-id-1"},{"content":"A sub-heading","level":3,"link":"#heading-id-2"}]} --> | ||
| <nav class="wp-block-table-of-contents" aria-label="Table of Contents"><ol><li><a class="wp-block-table-of-contents__entry" href="#heading-id-1">Heading text</a><ol><li><a class="wp-block-table-of-contents__entry" href="#heading-id-2">A sub-heading</a></li></ol></li></ol></nav> | ||
| <!-- /wp:table-of-contents --> | ||
| <!-- wp:table-of-contents {"headings":[{"content":"Heading text","level":2,"link":"#heading-id-1"},{"content":"A sub-heading","level":3,"link":"#heading-id-2"}]} /--> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.