Skip to content

Commit e280988

Browse files
committed
fix(material/slide-toggle): clear name from host node
Along the same lines as #15422 and #15368. Clears the static `name` attribute from the slide toggle's host node in order to prevent both the underlying input and the host from showing up in `document.getElementsByName` or `By.name`.
1 parent ddc2e23 commit e280988

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

src/material-experimental/mdc-slide-toggle/slide-toggle.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,12 @@ describe('MDC-based MatSlideToggle with forms', () => {
763763
expect(slideToggleEl.classList).toContain('ng-invalid');
764764
expect(slideToggleEl.classList).not.toContain('ng-valid');
765765
});
766+
767+
it('should clear static name attribute from the slide toggle host node', () => {
768+
const hostNode = fixture.nativeElement.querySelector('.mat-mdc-slide-toggle');
769+
expect(inputElement.getAttribute('name')).toBeTruthy();
770+
expect(hostNode.hasAttribute('name')).toBe(false);
771+
});
766772
});
767773

768774
describe('with model and change event', () => {

src/material-experimental/mdc-slide-toggle/slide-toggle.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export class MatSlideToggleChange {
7575
// Needs to be `-1` so it can still receive programmatic focus.
7676
'[attr.tabindex]': 'disabled ? null : -1',
7777
'[attr.aria-label]': 'null',
78+
'[attr.name]': 'null',
7879
'[attr.aria-labelledby]': 'null',
7980
'[class.mat-primary]': 'color === "primary"',
8081
'[class.mat-accent]': 'color !== "primary" && color !== "warn"',

src/material/slide-toggle/slide-toggle.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,12 @@ describe('MatSlideToggle with forms', () => {
839839
expect(slideToggleEl.classList).toContain('ng-invalid');
840840
expect(slideToggleEl.classList).not.toContain('ng-valid');
841841
});
842+
843+
it('should clear static name attribute from the slide toggle host node', () => {
844+
const hostNode = fixture.nativeElement.querySelector('.mat-slide-toggle');
845+
expect(inputElement.getAttribute('name')).toBeTruthy();
846+
expect(hostNode.hasAttribute('name')).toBe(false);
847+
});
842848
});
843849

844850
describe('with model and change event', () => {

src/material/slide-toggle/slide-toggle.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const _MatSlideToggleMixinBase:
8585
'[attr.tabindex]': 'disabled ? null : -1',
8686
'[attr.aria-label]': 'null',
8787
'[attr.aria-labelledby]': 'null',
88+
'[attr.name]': 'null',
8889
'[class.mat-checked]': 'checked',
8990
'[class.mat-disabled]': 'disabled',
9091
'[class.mat-slide-toggle-label-before]': 'labelPosition == "before"',

0 commit comments

Comments
 (0)