Skip to content
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 to Nav block on front of site #39161

Merged
merged 5 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,14 @@ function block_core_navigation_get_fallback_blocks() {
*/
function render_block_core_navigation( $attributes, $content, $block ) {

static $seen_menu_names = array();

// Flag used to indicate whether the rendered output is considered to be
// a fallback (i.e. the block has no menu associated with it).
$is_fallback = false;

$nav_menu_name = '';

/**
* Deprecated:
* The rgbTextColor and rgbBackgroundColor attributes
Expand Down Expand Up @@ -428,6 +432,14 @@ function render_block_core_navigation( $attributes, $content, $block ) {
return '';
}

$nav_menu_name = $navigation_post->post_title;

if ( isset( $seen_menu_names[ $nav_menu_name ] ) ) {
++$seen_menu_names[ $nav_menu_name ];
} else {
$seen_menu_names[ $nav_menu_name ] = 1;
}

$parsed_blocks = parse_blocks( $navigation_post->post_content );

// 'parse_blocks' includes a null block with '\n\n' as the content when
Expand Down Expand Up @@ -508,10 +520,18 @@ function render_block_core_navigation( $attributes, $content, $block ) {

$block_styles = isset( $attributes['styles'] ) ? $attributes['styles'] : '';

// If the menu name has been used previously then append an ID
// to the name to ensure uniqueness across a given post.
if ( isset( $seen_menu_names[ $nav_menu_name ] ) && $seen_menu_names[ $nav_menu_name ] > 1 ) {
$count = $seen_menu_names[ $nav_menu_name ];
$nav_menu_name = $nav_menu_name . ' ' . ( $count );
}

$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => implode( ' ', $classes ),
'style' => $block_styles . $colors['inline_styles'] . $font_sizes['inline_styles'],
'class' => implode( ' ', $classes ),
'style' => $block_styles . $colors['inline_styles'] . $font_sizes['inline_styles'],
'aria-label' => $nav_menu_name,
)
);

Expand Down
29 changes: 29 additions & 0 deletions packages/e2e-tests/specs/editor/blocks/navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,35 @@ describe( 'Navigation', () => {
} );
} );

it( 'applies accessible label to block element', async () => {
await createNewPost();
await insertBlock( 'Navigation' );
const startEmptyButton = await page.waitForXPath( START_EMPTY_XPATH );
await startEmptyButton.click();

const appender = await page.waitForSelector(
'.wp-block-navigation .block-list-appender'
);
await appender.click();

// Add a link to the Link block.
await updateActiveNavigationLink( {
url: 'https://wordpress.org',
label: 'WP',
type: 'url',
} );

const previewPage = await openPreviewPage();
await previewPage.bringToFront();
await previewPage.waitForNetworkIdle();

const isAccessibleLabelPresent = await previewPage.$(
'nav[aria-label="Navigation"]'
);

expect( isAccessibleLabelPresent ).toBeTruthy();
} );

it( 'does not load the frontend script if no navigation blocks are present', async () => {
await createNewPost();
await insertBlock( 'Paragraph' );
Expand Down