From 6e30312c96b46b6c67d71665e1f77279fafc3112 Mon Sep 17 00:00:00 2001 From: frederikprijck Date: Thu, 12 Jan 2017 23:28:55 +0100 Subject: [PATCH 1/8] fix(ngValue): setting binding to undefined should update the DOM value to an empty string Previously, when setting the binding to undefined would call jqLite/jQuery's `prop()` function, passing undefined as the second argument: `element.prop('value', undefined)`. This is identical to `element.prop('value') and will retrieve the dom value instead of updating it. This commit will prevent from undefined being passed as a second argument by passing `null` instead. Fixes: 15603 --- src/ng/directive/input.js | 2 +- test/ng/directive/inputSpec.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 49f38ebbeca5..1bc8e6d20a08 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -2142,7 +2142,7 @@ var ngValueDirective = function() { * makes it possible to use ngValue as a sort of one-way bind. */ function updateElementValue(element, attr, value) { - element.prop('value', value); + element.prop('value', angular.isUndefined(value) ? null : value); attr.$set('value', value); } diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 47515471122e..f1b57eb27a86 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -4211,6 +4211,22 @@ describe('input', function() { expect(inputElm[0].getAttribute('value')).toBe('something'); }); + it('should update the dom "value" "" and attribute to null when the binding is set to undefind', function() { + var inputElm = helper.compileInput(''); + + $rootScope.$apply('value = \'something\''); + + expect(inputElm[0].value).toBe('something'); + expect(inputElm[0].getAttribute('value')).toBe('something'); + + $rootScope.$apply(function() { + delete $rootScope.value; + }); + + expect(inputElm[0].value).toBe(''); + expect(inputElm[0].getAttribute('value')).toBe(null); + }); + they('should update the $prop "value" property and attribute after the bound expression changes', { input: '', textarea: '' From 3ae9f0ff29743394d9d90ddf3b200d6e6e14a384 Mon Sep 17 00:00:00 2001 From: frederikprijck Date: Thu, 12 Jan 2017 23:40:20 +0100 Subject: [PATCH 2/8] fixup! fix(ngValue): setting binding to undefined should update the DOM value to an empty string --- test/ng/directive/inputSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index f1b57eb27a86..6c510e7af309 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -4211,7 +4211,7 @@ describe('input', function() { expect(inputElm[0].getAttribute('value')).toBe('something'); }); - it('should update the dom "value" "" and attribute to null when the binding is set to undefind', function() { + it('should update the dom "value" to "" and attribute to null when the binding is set to undefind', function() { var inputElm = helper.compileInput(''); $rootScope.$apply('value = \'something\''); From db99f4e6b2fe7ab0e170924e0b7f5fa9011676d4 Mon Sep 17 00:00:00 2001 From: frederikprijck Date: Thu, 12 Jan 2017 23:43:16 +0100 Subject: [PATCH 3/8] fixup! fix(ngValue): setting binding to undefined should update the DOM value to an empty string --- test/ng/directive/inputSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 6c510e7af309..d39e77909f93 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -4211,7 +4211,7 @@ describe('input', function() { expect(inputElm[0].getAttribute('value')).toBe('something'); }); - it('should update the dom "value" to "" and attribute to null when the binding is set to undefind', function() { + it('should update the dom "value" to "" and attribute to null when the binding is set to undefined', function() { var inputElm = helper.compileInput(''); $rootScope.$apply('value = \'something\''); From a8e2cec6e563f6c52d233ae57777415748f59223 Mon Sep 17 00:00:00 2001 From: frederikprijck Date: Fri, 13 Jan 2017 08:10:57 +0100 Subject: [PATCH 4/8] fixup! fix(ngValue): setting binding to undefined should update the DOM value to an empty string --- test/ng/directive/inputSpec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index d39e77909f93..586c92c44c26 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -4218,11 +4218,11 @@ describe('input', function() { expect(inputElm[0].value).toBe('something'); expect(inputElm[0].getAttribute('value')).toBe('something'); - + $rootScope.$apply(function() { delete $rootScope.value; }); - + expect(inputElm[0].value).toBe(''); expect(inputElm[0].getAttribute('value')).toBe(null); }); From a29ece6c368e4fae22f177ab3891e1e33a3dca40 Mon Sep 17 00:00:00 2001 From: frederikprijck Date: Fri, 13 Jan 2017 12:52:52 +0100 Subject: [PATCH 5/8] fixup! fix(ngValue): setting binding to undefined should update the DOM value to an empty string --- src/ng/directive/input.js | 3 ++- test/ng/directive/inputSpec.js | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 1bc8e6d20a08..09ebf5b693cb 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -2142,7 +2142,8 @@ var ngValueDirective = function() { * makes it possible to use ngValue as a sort of one-way bind. */ function updateElementValue(element, attr, value) { - element.prop('value', angular.isUndefined(value) ? null : value); + var propValue = angular.isDefined(value) ? value : (msie === 9) ? '' : null; + element.prop('value', propValue); attr.$set('value', value); } diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 586c92c44c26..157b46805384 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -4214,7 +4214,7 @@ describe('input', function() { it('should update the dom "value" to "" and attribute to null when the binding is set to undefined', function() { var inputElm = helper.compileInput(''); - $rootScope.$apply('value = \'something\''); + $rootScope.$apply('value = "something"'); expect(inputElm[0].value).toBe('something'); expect(inputElm[0].getAttribute('value')).toBe('something'); @@ -4224,7 +4224,11 @@ describe('input', function() { }); expect(inputElm[0].value).toBe(''); - expect(inputElm[0].getAttribute('value')).toBe(null); + // Support: IE 9-11 + // In IE it is not possible to remove the `value` attribute from an input element. + if (!msie) { + expect(inputElm[0].getAttribute('value')).toBeNull(); + } }); they('should update the $prop "value" property and attribute after the bound expression changes', { From 496a2af1c44d283ee51db606873cd002f4ab4137 Mon Sep 17 00:00:00 2001 From: frederikprijck Date: Fri, 13 Jan 2017 13:03:26 +0100 Subject: [PATCH 6/8] fixup! fix(ngValue): setting binding to undefined should update the DOM value to an empty string --- test/ng/directive/inputSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 157b46805384..c254e62e4042 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -4211,7 +4211,7 @@ describe('input', function() { expect(inputElm[0].getAttribute('value')).toBe('something'); }); - it('should update the dom "value" to "" and attribute to null when the binding is set to undefined', function() { + it('should clear the "dom" value property and attribute when the value is undefined', function() { var inputElm = helper.compileInput(''); $rootScope.$apply('value = "something"'); From ffb122bebbd1adc98a41aabf976600d7ee60940a Mon Sep 17 00:00:00 2001 From: frederikprijck Date: Fri, 13 Jan 2017 13:08:11 +0100 Subject: [PATCH 7/8] fixup! fix(ngValue): setting binding to undefined should update the DOM value to an empty string --- src/ng/directive/input.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 09ebf5b693cb..cda3e1cc17bb 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -2142,7 +2142,7 @@ var ngValueDirective = function() { * makes it possible to use ngValue as a sort of one-way bind. */ function updateElementValue(element, attr, value) { - var propValue = angular.isDefined(value) ? value : (msie === 9) ? '' : null; + var propValue = isDefined(value) ? value : (msie === 9) ? '' : null; element.prop('value', propValue); attr.$set('value', value); } From 011c3a8663674f745c3c8da6a98281d5d1db82de Mon Sep 17 00:00:00 2001 From: frederikprijck Date: Fri, 13 Jan 2017 13:50:37 +0100 Subject: [PATCH 8/8] fixup! fix(ngValue): setting binding to undefined should update the DOM value to an empty string --- src/ng/directive/input.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index cda3e1cc17bb..40a18d66dba2 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -2142,6 +2142,8 @@ var ngValueDirective = function() { * makes it possible to use ngValue as a sort of one-way bind. */ function updateElementValue(element, attr, value) { + // Support: IE9 only + // In IE9 values are converted to string (e.g. `input.value = null` results in `input.value === 'null'`). var propValue = isDefined(value) ? value : (msie === 9) ? '' : null; element.prop('value', propValue); attr.$set('value', value);