From 40e00f08ee470de2cf151f7431386faf97a394c3 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Sat, 6 Jun 2026 13:30:52 +0200 Subject: [PATCH] fix(cdk/a11y): avoid prototype conflicts in id generator Updates the ID generator so we don't run into issues if we try to generate an ID for something like `prototype`. --- src/cdk/a11y/id-generator.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/cdk/a11y/id-generator.ts b/src/cdk/a11y/id-generator.ts index 8d75a7d0f50c..58856ecc22f7 100644 --- a/src/cdk/a11y/id-generator.ts +++ b/src/cdk/a11y/id-generator.ts @@ -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 = {}; +const counters = new Map(); /** Service that generates unique IDs for DOM nodes. */ @Service() @@ -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}`; } }