Skip to content

Commit 330b496

Browse files
authored
fix(cdk/a11y): avoid prototype conflicts in id generator (#33356)
Updates the ID generator so we don't run into issues if we try to generate an ID for something like `prototype`.
1 parent 5a8f7df commit 330b496

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

src/cdk/a11y/id-generator.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import {APP_ID, inject, Service} from '@angular/core';
1212
* Keeps track of the ID count per prefix. This helps us make the IDs a bit more deterministic
1313
* like they were before the service was introduced. Note that ideally we wouldn't have to do
1414
* this, but there are some internal tests that rely on the IDs.
15+
*
16+
* Note: use a map to avoid conflicts with built-in properties.
1517
*/
16-
const counters: Record<string, number> = {};
18+
const counters = new Map<string, number>();
1719

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

36-
if (!counters.hasOwnProperty(prefix)) {
37-
counters[prefix] = 0;
38+
let count = counters.get(prefix);
39+
40+
if (count === undefined) {
41+
count = 0;
42+
} else {
43+
count++;
3844
}
3945

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

0 commit comments

Comments
 (0)