From 26c945224d64370406cbff835d0a6f099d6cb2bb Mon Sep 17 00:00:00 2001 From: Lucas Galfaso Date: Fri, 12 Jun 2015 00:19:02 +0200 Subject: [PATCH] feat(ngTrim): allow ng-trim to work for elements input[type=radio] Allow elements of type input[type=radio] to specify ng-trim. Do not trim attributes values by default. BREAKING CHANGE: Attribute values are not trimmed by default Migration: when depending on attribute values to be trimmed, then manually trim them. Close: #11859 --- src/ng/compile.js | 2 +- src/ng/directive/input.js | 11 +++++++- test/ng/directive/inputSpec.js | 47 ++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index 9248181f4ad2..cc9b4d6684d3 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1498,7 +1498,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { attr = nAttrs[j]; name = attr.name; - value = trim(attr.value); + value = attr.value; // support ngAttr attribute binding ngAttrName = directiveNormalize(name); diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index c3294ed6547c..c6e276761127 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -1379,14 +1379,20 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) { } function radioInputType(scope, element, attr, ctrl) { + var doTrim = !attr.ngTrim || trim(attr.ngTrim) !== 'false'; // make the name unique, if not defined if (isUndefined(attr.name)) { element.attr('name', nextUid()); } var listener = function(ev) { + var value; if (element[0].checked) { - ctrl.$setViewValue(attr.value, ev && ev.type); + value = attr.value; + if (doTrim) { + value = trim(value); + } + ctrl.$setViewValue(value, ev && ev.type); } }; @@ -1394,6 +1400,9 @@ function radioInputType(scope, element, attr, ctrl) { ctrl.$render = function() { var value = attr.value; + if (doTrim) { + value = trim(value); + } element[0].checked = (value == ctrl.$viewValue); }; diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index becc6b72aa49..86c309e431e3 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -2440,6 +2440,53 @@ describe('input', function() { expect(inputElm[0].checked).toBe(false); expect(inputElm[1].checked).toBe(false); }); + + + it('should allow the use of ngTrim', function() { + $rootScope.some = 11; + var inputElm = helper.compileInput( + '' + + '' + + '' + + '' + + ''); + + $rootScope.$apply(function() { + $rootScope.value = 'blue'; + $rootScope.some = 'blue'; + }); + + expect(inputElm[0].checked).toBe(false); + expect(inputElm[1].checked).toBe(false); + expect(inputElm[2].checked).toBe(false); + expect(inputElm[3].checked).toBe(true); + expect(inputElm[4].checked).toBe(false); + + browserTrigger(inputElm[1], 'click'); + expect($rootScope.value).toBe('opt2'); + browserTrigger(inputElm[2], 'click'); + expect($rootScope.value).toBe(' opt3 '); + browserTrigger(inputElm[3], 'click'); + expect($rootScope.value).toBe('blue'); + browserTrigger(inputElm[4], 'click'); + expect($rootScope.value).toBe(' blue '); + + $rootScope.$apply("value = ' opt2 '"); + expect(inputElm[1].checked).toBe(false); + $rootScope.$apply("value = 'opt2'"); + expect(inputElm[1].checked).toBe(true); + $rootScope.$apply("value = ' opt3 '"); + expect(inputElm[2].checked).toBe(true); + $rootScope.$apply("value = 'opt3'"); + expect(inputElm[2].checked).toBe(false); + + $rootScope.$apply("value = 'blue'"); + expect(inputElm[3].checked).toBe(true); + expect(inputElm[4].checked).toBe(false); + $rootScope.$apply("value = ' blue '"); + expect(inputElm[3].checked).toBe(false); + expect(inputElm[4].checked).toBe(true); + }); });