Skip to content

fix(aria-describer): better handling of non-string values #9959

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 18, 2018
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
9 changes: 9 additions & 0 deletions src/cdk/a11y/aria-describer/aria-describer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ describe('AriaDescriber', () => {
expect(getMessageElements()).toBe(null);
});

it('should not register non-string values', () => {
expect(() => ariaDescriber.describe(component.element1, null!)).not.toThrow();
expect(getMessageElements()).toBe(null);
});

it('should not throw when trying to remove non-string value', () => {
expect(() => ariaDescriber.removeDescription(component.element1, null!)).not.toThrow();
});

it('should de-dupe a message registered multiple times', () => {
ariaDescriber.describe(component.element1, 'My Message');
ariaDescriber.describe(component.element2, 'My Message');
Expand Down
10 changes: 8 additions & 2 deletions src/cdk/a11y/aria-describer/aria-describer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class AriaDescriber {
* message element.
*/
describe(hostElement: Element, message: string) {
if (hostElement.nodeType !== this._document.ELEMENT_NODE || !message.trim()) {
if (!this._canBeDescribed(hostElement, message)) {
return;
}

Expand All @@ -75,7 +75,7 @@ export class AriaDescriber {

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

Expand Down Expand Up @@ -196,6 +196,12 @@ export class AriaDescriber {
return !!messageId && referenceIds.indexOf(messageId) != -1;
}

/** 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();
}

}

/** @docs-private */
Expand Down