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.
This commit is contained in:
Siddique Hameed
2014-01-02 10:18:02 -06:00
committed by Martin Staffa
parent 3ba29c464d
commit 97bbf86a19
3 changed files with 15 additions and 1 deletions
+1 -1
View File
@@ -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);
+8
View File
@@ -4725,6 +4725,14 @@ describe('$compile', function() {
expect(componentScope.attrAlias).toEqual(componentScope.attr);
}));
it('should copy an attribute with spaces', inject(function() {
compile('<div><span my-component attr=" some text ">');
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('<div><span my-component attr="hello {{name}}">');
+6
View File
@@ -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('<textarea type="text" ng-model="list" ng-trim="false" ng-list=" "></textarea>');
helper.changeInputValueTo('a b');
expect($rootScope.list).toEqual(['a','b']);
})
});
});