Skip to content

fix(a11y): don't set aria description if it's the same as the node's aria-label #15250

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
Feb 22, 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
7 changes: 7 additions & 0 deletions src/cdk/a11y/aria-describer/aria-describer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ describe('AriaDescriber', () => {
// Use `querySelectorAll` with an attribute since `getElementById` will stop at the first match.
expect(document.querySelectorAll(`[id='${MESSAGES_CONTAINER_ID}']`).length).toBe(1);
});

it('should not describe messages that match up with the aria-label of the element', () => {
component.element1.setAttribute('aria-label', 'Hello');
ariaDescriber.describe(component.element1, 'Hello');
ariaDescriber.describe(component.element1, 'Hi');
expectMessages(['Hi']);
});
});

function getMessagesContainer() {
Expand Down
18 changes: 15 additions & 3 deletions src/cdk/a11y/aria-describer/aria-describer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class AriaDescriber implements OnDestroy {

/** Removes the host element's aria-describedby reference to the message element. */
removeDescription(hostElement: Element, message: string) {
if (!this._canBeDescribed(hostElement, message)) {
if (!this._isElementNode(hostElement)) {
return;
}

Expand Down Expand Up @@ -218,10 +218,22 @@ export class AriaDescriber implements OnDestroy {

/** Determines whether a message can be described on a particular element. */
private _canBeDescribed(element: Element, message: string): boolean {
return element.nodeType === this._document.ELEMENT_NODE && message != null &&
!!`${message}`.trim();
if (!this._isElementNode(element)) {
return false;
}

const trimmedMessage = message == null ? '' : `${message}`.trim();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary template string literal? Also, omit the == null?

message ? '' : message.trim();

Alternatively,

const trimmedMessage = `${message || ''}`.trim();

The function signature also indicates that message can't be null or undefined. Should it include those?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

``${message || ''}.trim(); will swallow values like `0` or `false`. Even though the signature doesn't allow these values, we have some cases where the values from the view get passed straight into this method.

const ariaLabel = element.getAttribute('aria-label');

// We shouldn't set descriptions if they're exactly the same as the `aria-label` of the element,
// because screen readers will end up reading out the same text twice in a row.
return trimmedMessage ? (!ariaLabel || ariaLabel.trim() !== trimmedMessage) : false;
}

/** Checks whether a node is an Element node. */
private _isElementNode(element: Node): element is Element {
return element.nodeType === this._document.ELEMENT_NODE;
}
}


Expand Down