From 799d64264b5c3168b65a6c7909a4621aad87e4d9 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Sat, 16 Mar 2019 18:11:53 +0100 Subject: [PATCH] fix(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`. --- src/material/slide-toggle/slide-toggle.spec.ts | 6 ++++++ src/material/slide-toggle/slide-toggle.ts | 1 + 2 files changed, 7 insertions(+) diff --git a/src/material/slide-toggle/slide-toggle.spec.ts b/src/material/slide-toggle/slide-toggle.spec.ts index 4d6e881aed64..29e0f61fd014 100644 --- a/src/material/slide-toggle/slide-toggle.spec.ts +++ b/src/material/slide-toggle/slide-toggle.spec.ts @@ -1041,6 +1041,12 @@ describe('MatSlideToggle with forms', () => { expect(testComponent.isSubmitted).toBe(true); }); + + it('should clear static name attribute from the slide toggle host node', () => { + const hostNode = fixture.nativeElement.querySelector('.mat-slide-toggle'); + expect(inputElement.getAttribute('name')).toBeTruthy(); + expect(hostNode.hasAttribute('name')).toBe(false); + }); }); describe('with model and change event', () => { diff --git a/src/material/slide-toggle/slide-toggle.ts b/src/material/slide-toggle/slide-toggle.ts index 9d621fc5713b..d2737d7d4d5d 100644 --- a/src/material/slide-toggle/slide-toggle.ts +++ b/src/material/slide-toggle/slide-toggle.ts @@ -87,6 +87,7 @@ const _MatSlideToggleMixinBase: '[id]': 'id', // Needs to be `-1` so it can still receive programmatic focus. '[attr.tabindex]': 'disabled ? null : -1', + '[attr.name]': 'null', '[class.mat-checked]': 'checked', '[class.mat-disabled]': 'disabled', '[class.mat-slide-toggle-label-before]': 'labelPosition == "before"',