Skip to content

fix(a11y): remove aria-describedby attribute when no descriptions are left #17074

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

Merged
merged 1 commit into from
Sep 18, 2019
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
12 changes: 12 additions & 0 deletions src/cdk/a11y/aria-describer/aria-describer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ describe('AriaDescriber', () => {
'Expected description node to still be in the DOM after it is no longer being used.');
});

it('should remove the aria-describedby attribute if there are no more messages', () => {
const element = component.element1;

expect(element.hasAttribute('aria-describedby')).toBe(false);

ariaDescriber.describe(component.element1, 'Message');
expect(element.hasAttribute('aria-describedby')).toBe(true);

ariaDescriber.removeDescription(component.element1, 'Message');
expect(element.hasAttribute('aria-describedby')).toBe(false);
});

});

function getMessagesContainer() {
Expand Down
3 changes: 2 additions & 1 deletion src/cdk/a11y/aria-describer/aria-reference.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ describe('AriaReference', () => {
* value
*/
function expectIds(attr: string, ids: string[]) {
const value = testElement!.getAttribute(attr);
expect(getAriaReferenceIds(testElement!, attr)).toEqual(ids);
expect(testElement!.getAttribute(attr)).toBe(ids.length ? ids.join(' ') : '');
ids.length ? expect(value).toBe(ids.join(' ')) : expect(value).toBeFalsy();
}
});
6 changes: 5 additions & 1 deletion src/cdk/a11y/aria-describer/aria-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export function removeAriaReferencedId(el: Element, attr: string, id: string) {
const ids = getAriaReferenceIds(el, attr);
const filteredIds = ids.filter(val => val != id.trim());

el.setAttribute(attr, filteredIds.join(ID_DELIMINATOR));
if (filteredIds.length) {
el.setAttribute(attr, filteredIds.join(ID_DELIMINATOR));
} else {
el.removeAttribute(attr);
}
}

/**
Expand Down