WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.58k stars 4.23k forks source link

Warn if there is more than one main HTML element, or if the main is missing #35354

Open carolinan opened 3 years ago

carolinan commented 3 years ago

What problem does this address?

A few blocks have an option to change the HTML element to <main>, but there must only be one main element on a page. Having more than one <main> element (that is not hidden) is invalid HTML and an accessibility issue.

What is your proposed solution?

Add a warning if there is more than one main element -perhaps in the document outline "table of content" details popover? It already warns for incorrect heading levels.

aristath commented 3 years ago

Note from the MDN Docs:

A document mustn't have more than one <main> element that doesn't have the hidden attribute specified.

It is possible to have more than one main, but only one that's not hidden. Right now I don't believe we add hidden anywhere, but any check we add should take the hidden scenario into account. 👍

carolinan commented 3 years ago

And perhaps also warn if there is no <main> 🤔

bph commented 4 months ago

The accessibility features "Skip to content" + <main/> section could use the attention similar to the color low contrast warning, when selecting text and background colors.

A user can so easily remove a <main/> section tag by just removing a root group block. This came up in two discussions in the #outreach channel as well,

Apart from the warning, and update on documentation would also be beneficial to add the HTML Element selector to other blocks, like post content, media+text when used in a template context.

@WordPress/outreach

carolinan commented 4 months ago

Some thoughts:

luminuu commented 4 months ago

I've recently worked on a solution where I've implemented this via a block theme itself and it's working fine. Here's how it looks: CleanShot 2024-08-05 at 16 01 40@2x

It's only available in the site editor and on templates (not template parts, patterns and pages). It recursively checks the content with wp.data.select( 'core/editor' ).getEditorBlocks(). The message changes if there's no main tag found or if there's more than one main tag found and it also displays how many tags were found, however not where specifically.

getdave commented 1 day ago

Related to https://github.com/WordPress/gutenberg/issues/65400. Soon Zoom Out will not be available if <main> does not exist. Therefore it would be great if there could be a warning about:

getdave commented 1 day ago

@luminuu Looks great, would you be open to raising a PR with your code? 🙏

carolinan commented 1 day ago

I like that the message shows as a notice this way and not as a warning in the Document Outline panel where it would be less visible.

juanfra commented 1 day ago

I like that the message shows as a notice this way and not as a warning in the Document Outline panel where it would be less visible.

+1

This will be especially helpful for theme developers working on implementing zoom-out functionality. While building TT5 and working with templates that include nested groups, we encountered some challenges.