From c30eed086b3dc07027d9786bb584b3d681a394eb Mon Sep 17 00:00:00 2001 From: Martin Staffa Date: Tue, 18 Oct 2016 09:53:22 +0200 Subject: [PATCH] fix(input[radio]): use strict comparison when evaluating checked-ness Closes #15283 BREAKING CHANGE: When using input[radio], the checked status is now determined by doing a strict comparison between the value of the input and the ngModel.$viewValue. Previously, this was a non-strict comparison (==). This means in the following examples the radio is no longer checked: ``` ``` The migration strategy is to convert values that matched with non-strict conversion so that they will match with strict conversion. --- src/ng/directive/input.js | 4 +--- test/ng/directive/inputSpec.js | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 47a0fa8db508..ccd3b22ca042 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -1761,9 +1761,7 @@ function radioInputType(scope, element, attr, ctrl) { if (doTrim) { value = trim(value); } - // Strict comparison would cause a BC - // eslint-disable-next-line eqeqeq - element[0].checked = (value == ctrl.$viewValue); + element[0].checked = (value === ctrl.$viewValue); }; attr.$observe('value', ctrl.$render); diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 15471354c56f..b943be57846b 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -3867,9 +3867,7 @@ describe('input', function() { expect($rootScope.color).toBe('blue'); }); - - // We generally use strict comparison. This tests behavior we cannot change without a BC - it('should use non-strict comparison the evaluate checked-ness', function() { + it('should treat the value as a string when evaluating checked-ness', function() { var inputElm = helper.compileInput( ''); @@ -3877,7 +3875,7 @@ describe('input', function() { expect(inputElm[0].checked).toBe(true); $rootScope.$apply('model = 0'); - expect(inputElm[0].checked).toBe(true); + expect(inputElm[0].checked).toBe(false); }); @@ -4131,6 +4129,18 @@ describe('input', function() { }); + it('should use strict comparison between model and value', function() { + $rootScope.selected = false; + var inputElm = helper.compileInput('' + + '' + + ''); + + expect(inputElm[0].checked).toBe(true); + expect(inputElm[1].checked).toBe(false); + expect(inputElm[2].checked).toBe(false); + }); + + it('should watch the expression', function() { var inputElm = helper.compileInput('');