Skip to content

Commit a8bc357

Browse files
MaggieCabreramikachanMamadukat-hamanoMaggieCabrera
authored
Table of Contents: Add aria label to the nav element (#71586)
* Added aria label to the nav element * fixed fixtures to include the new attr * Update edit.js * Update save.js * Try server-side rendering the block # Conflicts: # packages/block-library/src/table-of-contents/save.js * Fix coding standards errors * Simplify server-side approach * Regenerate test fixtures * Simplify $aria_label * Restore test fixtures * Revert changes to other test fixtures * Update packages/block-library/src/table-of-contents/index.php Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com> --------- Co-authored-by: Sarah Norris <sarah@sekai.co.uk> Co-authored-by: Sarah Norris <1645628+mikachan@users.noreply.github.com> Co-authored-by: George Mamadashvili <georgemamadashvili@gmail.com> Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com> Co-authored-by: MaggieCabrera <onemaggie@git.wordpress.org> Co-authored-by: mikachan <mikachan@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org>
1 parent 6a4cbfa commit a8bc357

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

docs/reference-guides/core-blocks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ Summarize your post with a list of headings. Add HTML anchors to Heading blocks
957957
- **Name:** core/table-of-contents
958958
- **Experimental:** true
959959
- **Category:** design
960-
- **Supports:** color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
960+
- **Supports:** ariaLabel, color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
961961
- **Attributes:** headings, maxLevel, onlyIncludeCurrentPage, ordered
962962

963963
## Tag Cloud

lib/blocks.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ function gutenberg_reregister_core_block_types() {
120120
'site-logo.php' => 'core/site-logo',
121121
'site-tagline.php' => 'core/site-tagline',
122122
'site-title.php' => 'core/site-title',
123+
'table-of-contents.php' => 'core/table-of-contents',
123124
'tag-cloud.php' => 'core/tag-cloud',
124125
'template-part.php' => 'core/template-part',
125126
'term-description.php' => 'core/term-description',

packages/block-library/src/table-of-contents/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
}
3030
},
3131
"supports": {
32+
"ariaLabel": true,
3233
"html": false,
3334
"color": {
3435
"text": true,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Server-side rendering of the `core/table-of-contents` block.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
/**
9+
* Adds an aria-label to the table of contents block content.
10+
*
11+
* @param array $attributes Attributes of the block being rendered.
12+
* @param string $content Content of the block being rendered.
13+
*
14+
* @return string The content of the block being rendered.
15+
*/
16+
function block_core_table_of_contents_render( $attributes, $content ) {
17+
if ( ! $content ) {
18+
return $content;
19+
}
20+
21+
// Get the aria-label from block attributes, or fallback to localized default.
22+
$aria_label = empty( $attributes['ariaLabel'] ) ? __( 'Table of Contents' ) : wp_strip_all_tags( $attributes['ariaLabel'] );
23+
24+
$p = new WP_HTML_Tag_Processor( $content );
25+
26+
if ( $p->next_tag( 'nav' ) ) {
27+
$p->set_attribute( 'aria-label', $aria_label );
28+
}
29+
30+
return $p->get_updated_html();
31+
}
32+
33+
/**
34+
* Registers the `core/table-of-contents` block on the server.
35+
*/
36+
function register_block_core_table_of_contents() {
37+
register_block_type_from_metadata(
38+
__DIR__ . '/table-of-contents',
39+
array(
40+
'render_callback' => 'block_core_table_of_contents_render',
41+
)
42+
);
43+
}
44+
add_action( 'init', 'register_block_core_table_of_contents' );

0 commit comments

Comments
 (0)