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

Commit 76d3daf

Browse files
renovatorrulerNarretz
authored andcommitted
fix($compile): don't throw tplrt error when there is a whitespace around a top-level comment
Added new conditional for NODE_TYPE_TEXT inside removeComments method of $compile Added corresponding unit tests. Closes #15108 PR (#15132)
1 parent 9e24e77 commit 76d3daf

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-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

+48
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() {
@@ -2092,6 +2108,18 @@ describe('$compile', function() {
20922108
$compile('<p template></p>');
20932109
$rootScope.$apply();
20942110
expect($exceptionHandler.errors).toEqual([]);
2111+
2112+
// comments are ok
2113+
$templateCache.put('template.html', '<!-- oh hi --><div></div> \n');
2114+
$compile('<p template></p>');
2115+
$rootScope.$apply();
2116+
expect($exceptionHandler.errors).toEqual([]);
2117+
2118+
// white space around comments is ok
2119+
$templateCache.put('template.html', ' <!-- oh hi --> <div></div> <!-- oh hi -->\n');
2120+
$compile('<p template></p>');
2121+
$rootScope.$apply();
2122+
expect($exceptionHandler.errors).toEqual([]);
20952123
});
20962124
});
20972125

@@ -2303,6 +2331,26 @@ describe('$compile', function() {
23032331
});
23042332
});
23052333

2334+
it('should ignore whitespace between comment and root node when replacing with a templateUrl', function() {
2335+
module(function() {
2336+
directive('replaceWithWhitespace', valueFn({
2337+
replace: true,
2338+
templateUrl: 'templateWithWhitespace.html'
2339+
}));
2340+
});
2341+
inject(function($compile, $rootScope, $httpBackend) {
2342+
$httpBackend.whenGET('templateWithWhitespace.html').
2343+
respond('<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->');
2344+
expect(function() {
2345+
element = $compile('<div><div replace-with-whitespace></div></div>')($rootScope);
2346+
}).not.toThrow();
2347+
$httpBackend.flush();
2348+
expect(element.find('p').length).toBe(1);
2349+
expect(element.find('p').text()).toBe('Hello, world!');
2350+
});
2351+
});
2352+
2353+
23062354
it('should keep prototype properties on sync version of async directive', function() {
23072355
module(function() {
23082356
function DirectiveClass() {

0 commit comments

Comments
 (0)