From ec1c6c400ae33d4d6468efa5a82dd0f43025cb37 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Wed, 28 Feb 2024 09:18:45 +0100 Subject: [PATCH] fix(material/checkbox): clear name from host node Currently we forward the name attribute from the host node to the underlying input, however we leave the name on the host node intact. This can throw off functions like `document.getElementsByName` or the `By.name` Protractor selector. --- src/material/checkbox/checkbox.spec.ts | 6 ++++++ src/material/checkbox/checkbox.ts | 1 + 2 files changed, 7 insertions(+) diff --git a/src/material/checkbox/checkbox.spec.ts b/src/material/checkbox/checkbox.spec.ts index 5ed24e69547f..ee8917c8501f 100644 --- a/src/material/checkbox/checkbox.spec.ts +++ b/src/material/checkbox/checkbox.spec.ts @@ -959,6 +959,12 @@ describe('MDC-based MatCheckbox', () => { expect(inputElement.getAttribute('name')).toBe('test-name'); })); + + it('should clear the name attribute from the host node', () => { + const checkboxElement = fixture.debugElement.query(By.directive(MatCheckbox)); + + expect(checkboxElement.nativeElement.getAttribute('name')).toBeFalsy(); + }); }); describe('with form control', () => { diff --git a/src/material/checkbox/checkbox.ts b/src/material/checkbox/checkbox.ts index 7591b46dac0c..2397ae975260 100644 --- a/src/material/checkbox/checkbox.ts +++ b/src/material/checkbox/checkbox.ts @@ -92,6 +92,7 @@ const defaults = MAT_CHECKBOX_DEFAULT_OPTIONS_FACTORY(); '[attr.tabindex]': 'null', '[attr.aria-label]': 'null', '[attr.aria-labelledby]': 'null', + '[attr.name]': 'null', '[class._mat-animation-noopable]': `_animationMode === 'NoopAnimations'`, '[class.mdc-checkbox--disabled]': 'disabled', '[id]': 'id',