Skip to content

Commit 41988de

Browse files
siddiiNarretz
authored andcommitted
fix($compile): don't trim white-space in attributes
BREAKING CHANGE: White-space in attributes is no longer trimmed automatically. This includes leading and trailing whitespace, and attributes that are purely white-space. 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. 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: ``` $attrs.$observe('myAttr', function(newVal) { if (newVal === 'false') ... }); ``` Note that `$parse` trims expressions automatically, so attributes with expressions (e.g. directive bindings) are unlikely to be affected by stray white-space. Fixes angular#5513 Fixes angular#14539 Closes angular#5597 ngList test
1 parent ca1e39c commit 41988de

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/ng/compile.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4702,6 +4702,14 @@ describe('$compile', function() {
47024702
expect(componentScope.attrAlias).toEqual(componentScope.attr);
47034703
}));
47044704

4705+
it('should copy an attribute with spaces', inject(function() {
4706+
compile('<div><span my-component attr=" some text ">');
4707+
4708+
expect(componentScope.attr).toEqual(' some text ');
4709+
expect(componentScope.attrAlias).toEqual(' some text ');
4710+
expect(componentScope.attrAlias).toEqual(componentScope.attr);
4711+
}));
4712+
47054713
it('should set up the interpolation before it reaches the link function', inject(function() {
47064714
$rootScope.name = 'misko';
47074715
compile('<div><span my-component attr="hello {{name}}">');

test/ng/directive/ngListSpec.js

Lines changed: 6 additions & 0 deletions
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)