From 97bbf86a1979d099802f0d631c17c54b87563b40 Mon Sep 17 00:00:00 2001 From: Siddique Hameed Date: Thu, 2 Jan 2014 10:18:02 -0600 Subject: [PATCH] fix($compile): don't trim white-space in attributes This allows developers to use white-space in their attributes, for example as value for input[type=radio], as a separator in ngList, or as a value in any custom directive binding. It's also consistent with the HTML spec, which allows white-space in attributes, and consistent with Angular2, which also allows white-space in attributes. Fixes #5513 Fixes #14539 Closes #5597 BREAKING CHANGE: White-space in attributes is no longer trimmed automatically. This includes leading and trailing white-space, and attributes that are purely white-space. To migrate, attributes that require trimming must now be trimmed manually. A common cases where stray white-space can cause problems is when attribute values are compared, for example in an $observer: Before: ``` $attrs.$observe('myAttr', function(newVal) { if (newVal === 'false') ... }); ``` To migrate, the attribute value should be trimmed manually: ``` $attrs.$observe('myAttr', function(newVal) { if (newVal.trim() === 'false') ... }); ``` Note that `$parse` trims expressions automatically, so attributes with expressions (e.g. directive bindings) are unlikely to be affected by stray white-space. --- src/ng/compile.js | 2 +- test/ng/compileSpec.js | 8 ++++++++ test/ng/directive/ngListSpec.js | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index 741eaeb90..bc68eb04e 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1893,7 +1893,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/test/ng/compileSpec.js b/test/ng/compileSpec.js index 668f6c687..7ed97f062 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -4725,6 +4725,14 @@ describe('$compile', function() { expect(componentScope.attrAlias).toEqual(componentScope.attr); })); + it('should copy an attribute with spaces', inject(function() { + compile('
'); + + expect(componentScope.attr).toEqual(' some text '); + expect(componentScope.attrAlias).toEqual(' some text '); + expect(componentScope.attrAlias).toEqual(componentScope.attr); + })); + it('should set up the interpolation before it reaches the link function', inject(function() { $rootScope.name = 'misko'; compile('
'); diff --git a/test/ng/directive/ngListSpec.js b/test/ng/directive/ngListSpec.js index 06b909872..4a62358ef 100644 --- a/test/ng/directive/ngListSpec.js +++ b/test/ng/directive/ngListSpec.js @@ -130,6 +130,12 @@ describe('ngList', function() { helper.changeInputValueTo('a\nb'); expect($rootScope.list).toEqual(['a','b']); }); + + it('should support splitting on whitespace', function() { + helper.compileInput(''); + helper.changeInputValueTo('a b'); + expect($rootScope.list).toEqual(['a','b']); + }) }); });