Skip to content

Commit e43c059

Browse files
authored
perf(cdk/a11y): Micro-optimizations to aria-reference.ts. (#28337)
1 parent 6db225e commit e43c059

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/cdk/a11y/aria-describer/aria-reference.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ const ID_DELIMITER = ' ';
1515
*/
1616
export function addAriaReferencedId(el: Element, attr: `aria-${string}`, id: string) {
1717
const ids = getAriaReferenceIds(el, attr);
18-
if (ids.some(existingId => existingId.trim() == id.trim())) {
18+
id = id.trim();
19+
if (ids.some(existingId => existingId.trim() === id)) {
1920
return;
2021
}
21-
ids.push(id.trim());
22+
ids.push(id);
2223

2324
el.setAttribute(attr, ids.join(ID_DELIMITER));
2425
}
@@ -29,7 +30,8 @@ export function addAriaReferencedId(el: Element, attr: `aria-${string}`, id: str
2930
*/
3031
export function removeAriaReferencedId(el: Element, attr: `aria-${string}`, id: string) {
3132
const ids = getAriaReferenceIds(el, attr);
32-
const filteredIds = ids.filter(val => val != id.trim());
33+
id = id.trim();
34+
const filteredIds = ids.filter(val => val !== id);
3335

3436
if (filteredIds.length) {
3537
el.setAttribute(attr, filteredIds.join(ID_DELIMITER));
@@ -44,5 +46,6 @@ export function removeAriaReferencedId(el: Element, attr: `aria-${string}`, id:
4446
*/
4547
export function getAriaReferenceIds(el: Element, attr: string): string[] {
4648
// Get string array of all individual ids (whitespace delimited) in the attribute value
47-
return (el.getAttribute(attr) || '').match(/\S+/g) || [];
49+
const attrValue = el.getAttribute(attr);
50+
return attrValue?.match(/\S+/g) ?? [];
4851
}

0 commit comments

Comments
 (0)