Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 97bbf86

Browse files
siddiiNarretz
authored andcommitted
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.
1 parent 3ba29c4 commit 97bbf86

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18931893

18941894
attr = nAttrs[j];
18951895
name = attr.name;
1896-
value = trim(attr.value);
1896+
value = attr.value;
18971897

18981898
// support ngAttr attribute binding
18991899
ngAttrName = directiveNormalize(name);

test/ng/compileSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -4725,6 +4725,14 @@ describe('$compile', function() {
47254725
expect(componentScope.attrAlias).toEqual(componentScope.attr);
47264726
}));
47274727

4728+
it('should copy an attribute with spaces', inject(function() {
4729+
compile('<div><span my-component attr=" some text ">');
4730+
4731+
expect(componentScope.attr).toEqual(' some text ');
4732+
expect(componentScope.attrAlias).toEqual(' some text ');
4733+
expect(componentScope.attrAlias).toEqual(componentScope.attr);
4734+
}));
4735+
47284736
it('should set up the interpolation before it reaches the link function', inject(function() {
47294737
$rootScope.name = 'misko';
47304738
compile('<div><span my-component attr="hello {{name}}">');

test/ng/directive/ngListSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ describe('ngList', function() {
130130
helper.changeInputValueTo('a\nb');
131131
expect($rootScope.list).toEqual(['a','b']);
132132
});
133+
134+
it('should support splitting on whitespace', function() {
135+
helper.compileInput('<textarea type="text" ng-model="list" ng-trim="false" ng-list=" "></textarea>');
136+
helper.changeInputValueTo('a b');
137+
expect($rootScope.list).toEqual(['a','b']);
138+
})
133139
});
134140
});
135141

0 commit comments

Comments
 (0)