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

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

Open
carolinan opened this issue Oct 5, 2021 · 10 comments
Open
Labels
[Feature] Document Outline An option that outlines content based on a title and headings used in the post/page [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). Needs Accessibility Feedback Need input from accessibility Needs Design Needs design efforts. [Type] Enhancement A suggestion for improvement.

Comments

@carolinan
Copy link
Contributor

carolinan commented Oct 5, 2021

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.

@carolinan carolinan added [Type] Enhancement A suggestion for improvement. [Feature] Document Outline An option that outlines content based on a title and headings used in the post/page labels Oct 5, 2021
@aristath
Copy link
Member

aristath commented Oct 6, 2021

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
Copy link
Contributor Author

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

@carolinan carolinan added the [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). label May 19, 2022
@carolinan carolinan changed the title Warn if there is more than one main HTML element Warn if there is more than one main HTML element, or if the main is missing Jul 19, 2024
@bph
Copy link
Contributor

bph commented Aug 2, 2024

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

@bph bph added Needs Design Needs design efforts. Needs Accessibility Feedback Need input from accessibility labels Aug 2, 2024
@carolinan
Copy link
Contributor Author

carolinan commented Aug 3, 2024

Some thoughts:

  • I think there are performance issues with looping through all blocks on the current page, find blocks that use the tagName attribute, and then check if that tagName is main. The check for the heading level is much more limited because it only checks the heading block.
  • The check should be limited to block themes, because the editor does not know what other tags there are in classic theme templates. (*To do, check if the heading outline is available on classic themes and if it is accurate).
  • The check may need to be limited to the Site Editor, unless it can check both template and block editor contents.
  • A new help text needs to be written, explaining why the main is needed.
  • A link to more information should be added, similar to the link added to the alt text help.

@luminuu
Copy link
Member

luminuu commented Aug 5, 2024

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
Copy link
Contributor

getdave commented Dec 2, 2024

Related to #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:

  • more than one <main>
  • lack of <main>

@getdave
Copy link
Contributor

getdave commented Dec 2, 2024

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

@carolinan
Copy link
Contributor Author

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
Copy link
Member

juanfra commented Dec 2, 2024

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.

@luminuu
Copy link
Member

luminuu commented Dec 5, 2024

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

@getdave The code is specific to a theme, I assume it has to be rewritten in order to land in core? The code can be found here: https://github.com/Greyd-Suite/greyd-wp/blob/main/assets/js/editor-main-tag-warning.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Document Outline An option that outlines content based on a title and headings used in the post/page [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). Needs Accessibility Feedback Need input from accessibility Needs Design Needs design efforts. [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

6 participants