Skip to content

fix(aria-describer): remove unsafe !.s in aria-describer #7011

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 12, 2017
Merged
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
23 changes: 14 additions & 9 deletions src/cdk/a11y/aria-describer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class AriaDescriber {
* message element.
*/
describe(hostElement: Element, message: string) {
if (!this._platform.isBrowser || !`${message}`.trim()) { return; }
if (!this._platform.isBrowser || !message.trim()) { return; }

if (!messageRegistry.has(message)) {
createMessageElement(message);
Expand All @@ -66,19 +66,20 @@ export class AriaDescriber {

/** Removes the host element's aria-describedby reference to the message element. */
removeDescription(hostElement: Element, message: string) {
if (!this._platform.isBrowser || !`${message}`.trim()) {
if (!this._platform.isBrowser || !message.trim()) {
return;
}

if (isElementDescribedByMessage(hostElement, message)) {
removeMessageReference(hostElement, message);
}

if (messageRegistry.get(message)!.referenceCount === 0) {
const registeredMessage = messageRegistry.get(message);
if (registeredMessage && registeredMessage.referenceCount === 0) {
deleteMessageElement(message);
}

if (messagesContainer!.childNodes.length === 0) {
if (messagesContainer && messagesContainer.childNodes.length === 0) {
deleteMessagesContainer();
}
}
Expand Down Expand Up @@ -118,8 +119,11 @@ function createMessageElement(message: string) {

/** Deletes the message element from the global messages container. */
function deleteMessageElement(message: string) {
const messageElement = messageRegistry.get(message)!.messageElement;
messagesContainer!.removeChild(messageElement);
const registeredMessage = messageRegistry.get(message);
const messageElement = registeredMessage && registeredMessage.messageElement;
if (messagesContainer && messageElement) {
messagesContainer.removeChild(messageElement);
}
messageRegistry.delete(message);
}

Expand Down Expand Up @@ -174,11 +178,12 @@ function removeMessageReference(element: Element, message: string) {
}

/** Returns true if the element has been described by the provided message ID. */
function isElementDescribedByMessage(element: Element, message: string) {
function isElementDescribedByMessage(element: Element, message: string): boolean {
const referenceIds = getAriaReferenceIds(element, 'aria-describedby');
const messageId = messageRegistry.get(message)!.messageElement.id;
const registeredMessage = messageRegistry.get(message);
const messageId = registeredMessage && registeredMessage.messageElement.id;

return referenceIds.indexOf(messageId) != -1;
return !!messageId && referenceIds.indexOf(messageId) != -1;
}

/** @docs-private */
Expand Down