fix($compile): remove comment nodes from templates before asserting single root node

The compiler will no longer throw if a directive template contains comment nodes in addition to a
single root node. If a template contains less than 2 nodes, the nodes are unaltered.

BREAKING CHANGE:

If a template contains directives within comment nodes, and there is more than a single node in the
template, those comment nodes are removed. The impact of this breaking change is expected to be
quite low.

Closes #9212
Closes #9215
This commit is contained in:
Caitlin Potter
2014-09-22 13:14:31 -04:00
parent 7b6c1d08ac
commit feba0174db
4 changed files with 58 additions and 2 deletions

View File

@@ -1078,6 +1078,22 @@ describe('$compile', function() {
});
});
}
it('should ignore comment nodes when replacing with a template', function() {
module(function() {
directive('replaceWithComments', valueFn({
replace: true,
template: '<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->'
}));
});
inject(function($compile, $rootScope) {
expect(function() {
element = $compile('<div><div replace-with-comments></div></div>')($rootScope);
}).not.toThrow();
expect(element.find('p').length).toBe(1);
expect(element.find('p').text()).toBe('Hello, world!');
});
});
});
@@ -1977,6 +1993,26 @@ describe('$compile', function() {
});
});
}
it('should ignore comment nodes when replacing with a templateUrl', function() {
module(function() {
directive('replaceWithComments', valueFn({
replace: true,
templateUrl: 'templateWithComments.html'
}));
});
inject(function($compile, $rootScope, $httpBackend) {
$httpBackend.whenGET('templateWithComments.html').
respond('<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->');
expect(function() {
element = $compile('<div><div replace-with-comments></div></div>')($rootScope);
}).not.toThrow();
$httpBackend.flush();
expect(element.find('p').length).toBe(1);
expect(element.find('p').text()).toBe('Hello, world!');
});
});
});