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

Commit 17cf76f

Browse files
fix($scope): fixes multiple root element error when there is a whitespace after a comment
Added new conditional for NODE_TYPE_TEXT inside removeComments method of $compile Added corresponding unit tests. Closes #15108
1 parent 0784977 commit 17cf76f

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/ng/compile.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3679,8 +3679,9 @@ function removeComments(jqNodes) {
36793679

36803680
while (i--) {
36813681
var node = jqNodes[i];
3682-
if (node.nodeType === NODE_TYPE_COMMENT) {
3683-
splice.call(jqNodes, i, 1);
3682+
if (node.nodeType === NODE_TYPE_COMMENT ||
3683+
(node.nodeType === NODE_TYPE_TEXT && node.nodeValue.trim() === '')) {
3684+
splice.call(jqNodes, i, 1);
36843685
}
36853686
}
36863687
return jqNodes;

test/ng/compileSpec.js

+36
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,22 @@ describe('$compile', function() {
13641364
});
13651365
});
13661366

1367+
it('should ignore whitespace betwee comment and root node when replacing with a template', function() {
1368+
module(function() {
1369+
directive('replaceWithWhitespace', valueFn({
1370+
replace: true,
1371+
template: '<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->'
1372+
}));
1373+
});
1374+
inject(function($compile, $rootScope) {
1375+
expect(function() {
1376+
element = $compile('<div><div replace-with-whitespace></div></div>')($rootScope);
1377+
}).not.toThrow();
1378+
expect(element.find('p').length).toBe(1);
1379+
expect(element.find('p').text()).toBe('Hello, world!');
1380+
});
1381+
});
1382+
13671383
it('should keep prototype properties on directive', function() {
13681384
module(function() {
13691385
function DirectiveClass() {
@@ -2303,6 +2319,26 @@ describe('$compile', function() {
23032319
});
23042320
});
23052321

2322+
it('should ignore whitespace between comment and root node when replacing with a templateUrl', function() {
2323+
module(function() {
2324+
directive('replaceWithWhitespace', valueFn({
2325+
replace: true,
2326+
templateUrl: 'templateWithWhitespace.html'
2327+
}));
2328+
});
2329+
inject(function($compile, $rootScope, $httpBackend) {
2330+
$httpBackend.whenGET('templateWithWhitespace.html').
2331+
respond('<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->');
2332+
expect(function() {
2333+
element = $compile('<div><div replace-with-whitespace></div></div>')($rootScope);
2334+
}).not.toThrow();
2335+
$httpBackend.flush();
2336+
expect(element.find('p').length).toBe(1);
2337+
expect(element.find('p').text()).toBe('Hello, world!');
2338+
});
2339+
});
2340+
2341+
23062342
it('should keep prototype properties on sync version of async directive', function() {
23072343
module(function() {
23082344
function DirectiveClass() {

0 commit comments

Comments
 (0)