Skip to content
Open
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
15 changes: 11 additions & 4 deletions src/cdk/a11y/id-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {APP_ID, inject, Service} from '@angular/core';
* Keeps track of the ID count per prefix. This helps us make the IDs a bit more deterministic
* like they were before the service was introduced. Note that ideally we wouldn't have to do
* this, but there are some internal tests that rely on the IDs.
*
* Note: use a map to avoid conflicts with built-in properties.
*/
const counters: Record<string, number> = {};
const counters = new Map<string, number>();

/** Service that generates unique IDs for DOM nodes. */
@Service()
Expand All @@ -33,10 +35,15 @@ export class _IdGenerator {
prefix += this._appId;
}

if (!counters.hasOwnProperty(prefix)) {
counters[prefix] = 0;
let count = counters.get(prefix);

if (count === undefined) {
count = 0;
} else {
count++;
}

return `${prefix}${randomize ? _IdGenerator._infix + '-' : ''}${counters[prefix]++}`;
counters.set(prefix, count);
return `${prefix}${randomize ? _IdGenerator._infix + '-' : ''}${count}`;
}
}
Loading