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

[ModalUnstyled] Fix errors from the W3C validator about incorrect aria-hidden attribute on some elements #30920

Merged
merged 4 commits into from
Jun 8, 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
37 changes: 37 additions & 0 deletions packages/mui-base/src/ModalUnstyled/ModalManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,45 @@ describe('ModalManager', () => {
});

it('should add aria-hidden to container siblings', () => {
const secondSibling = document.createElement('input');
container2.appendChild(secondSibling);
modalManager.add({}, container2);
expect(container2.children[0]).toBeAriaHidden();
expect(container2.children[1]).toBeAriaHidden();
});

it('should not add aria-hidden to forbidden container siblings', () => {
[
'template',
'script',
'style',
'link',
'map',
'meta',
'noscript',
'picture',
'col',
'colgroup',
'param',
'slot',
'source',
'track',
].forEach(function createBlacklistSiblings(name) {
const sibling = document.createElement(name);
container2.appendChild(sibling);
});
const inputHiddenSibling = document.createElement('input');
inputHiddenSibling.setAttribute('type', 'hidden');
container2.appendChild(inputHiddenSibling);

const numberOfChildren = 16;
expect(container2.children.length).equal(numberOfChildren);

modalManager.add({}, container2);
expect(container2.children[0]).toBeAriaHidden();
for (let i = 1; i < numberOfChildren; i += 1) {
expect(container2.children[i]).not.toBeAriaHidden();
}
});

it('should add aria-hidden to previous modals', () => {
Expand Down
30 changes: 28 additions & 2 deletions packages/mui-base/src/ModalUnstyled/ModalManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ function getPaddingRight(element: Element): number {
return parseInt(ownerWindow(element).getComputedStyle(element).paddingRight, 10) || 0;
}

function isAriaHiddenForbiddenOnElement(element: Element): boolean {
// The forbidden HTML tags are the ones from ARIA specification that
// can be children of body and can't have aria-hidden attribute.
// cf. https://www.w3.org/TR/html-aria/#docconformance
const forbiddenTagNames = [
'TEMPLATE',
'SCRIPT',
'STYLE',
'LINK',
'MAP',
'META',
'NOSCRIPT',
'PICTURE',
'COL',
'COLGROUP',
'PARAM',
'SLOT',
'SOURCE',
'TRACK',
];
const isForbiddenTagName = forbiddenTagNames.indexOf(element.tagName) !== -1;
const isInputHidden = element.tagName === 'INPUT' && element.getAttribute('type') === 'hidden';
return isForbiddenTagName || isInputHidden;
}

function ariaHiddenSiblings(
container: Element,
mountElement: Element,
Expand All @@ -39,10 +64,11 @@ function ariaHiddenSiblings(
show: boolean,
): void {
const blacklist = [mountElement, currentElement, ...elementsToExclude];
const blacklistTagNames = ['TEMPLATE', 'SCRIPT', 'STYLE'];

[].forEach.call(container.children, (element: Element) => {
if (blacklist.indexOf(element) === -1 && blacklistTagNames.indexOf(element.tagName) === -1) {
const isNotExcludedElement = blacklist.indexOf(element) === -1;
const isNotForbiddenElement = !isAriaHiddenForbiddenOnElement(element);
if (isNotExcludedElement && isNotForbiddenElement) {
ariaHidden(element, show);
}
});
Expand Down