From bcf48261f4bd50b1c2b8e6f060a5f5ecb701c341 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 11 Jul 2017 19:09:02 +0200 Subject: [PATCH] fix(checkbox, radio): setting id to null causes invalid id for input (#5398) * In some situations, developers will change the `id` of a component dynamically. At some point if they set the `[id]` to `null` the input id will look like the following: `null-input`. * If developers are setting the `id` using a binding then the DOM Id for the host component won't be set at all (because the @Input is triggered instead of the DOM property) Fixes #5394 --- e2e/components/checkbox-e2e.spec.ts | 4 ++-- src/lib/checkbox/checkbox.spec.ts | 16 +++++++++++++--- src/lib/checkbox/checkbox.ts | 20 ++++++++++---------- src/lib/radio/radio.ts | 14 +++++++------- src/lib/slide-toggle/slide-toggle.spec.ts | 10 ++++++---- src/lib/slide-toggle/slide-toggle.ts | 11 ++++------- 6 files changed, 42 insertions(+), 33 deletions(-) diff --git a/e2e/components/checkbox-e2e.spec.ts b/e2e/components/checkbox-e2e.spec.ts index 2256bf712043..4d4988f04a76 100644 --- a/e2e/components/checkbox-e2e.spec.ts +++ b/e2e/components/checkbox-e2e.spec.ts @@ -9,7 +9,7 @@ describe('checkbox', () => { it('should be checked when clicked, and unchecked when clicked again', async () => { let checkboxEl = element(by.id('test-checkbox')); - let inputEl = element(by.css('input[id=input-test-checkbox]')); + let inputEl = element(by.css('input[id=test-checkbox-input]')); screenshot('start'); checkboxEl.click(); @@ -32,7 +32,7 @@ describe('checkbox', () => { }); it('should toggle the checkbox when pressing space', () => { - let inputEl = element(by.css('input[id=input-test-checkbox]')); + let inputEl = element(by.css('input[id=test-checkbox-input]')); expect(inputEl.getAttribute('checked')) .toBeFalsy('Expect checkbox "checked" property to be false'); diff --git a/src/lib/checkbox/checkbox.spec.ts b/src/lib/checkbox/checkbox.spec.ts index 5ccecf52af5f..0fb39a761746 100644 --- a/src/lib/checkbox/checkbox.spec.ts +++ b/src/lib/checkbox/checkbox.spec.ts @@ -266,6 +266,15 @@ describe('MdCheckbox', () => { it('should preserve the user-provided id', () => { expect(checkboxNativeElement.id).toBe('simple-check'); + expect(inputElement.id).toBe('simple-check-input'); + }); + + it('should generate a unique id for the checkbox input if no id is set', () => { + testComponent.checkboxId = null; + fixture.detectChanges(); + + expect(checkboxInstance.inputId).toMatch(/md-checkbox-\d+/); + expect(inputElement.id).toBe(checkboxInstance.inputId); }); it('should project the checkbox content into the label element', () => { @@ -675,8 +684,8 @@ describe('MdCheckbox', () => { fixture.debugElement.queryAll(By.directive(MdCheckbox)) .map(debugElement => debugElement.nativeElement.querySelector('input').id); - expect(firstId).toBeTruthy(); - expect(secondId).toBeTruthy(); + expect(firstId).toMatch(/md-checkbox-\d+-input/); + expect(secondId).toMatch(/md-checkbox-\d+-input/); expect(firstId).not.toEqual(secondId); }); }); @@ -833,7 +842,7 @@ describe('MdCheckbox', () => { template: `