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

Commit 12752f6

Browse files
renovatorrulerpetebacondarwin
authored andcommitted
fix($compile): don't throw tplrt error when there is 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 fbdfd19 commit 12752f6

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
@@ -3660,8 +3660,9 @@ function removeComments(jqNodes) {
36603660

36613661
while (i--) {
36623662
var node = jqNodes[i];
3663-
if (node.nodeType === NODE_TYPE_COMMENT) {
3664-
splice.call(jqNodes, i, 1);
3663+
if (node.nodeType === NODE_TYPE_COMMENT ||
3664+
(node.nodeType === NODE_TYPE_TEXT && node.nodeValue.trim() === '')) {
3665+
splice.call(jqNodes, i, 1);
36653666
}
36663667
}
36673668
return jqNodes;

test/ng/compileSpec.js

+48
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,22 @@ describe('$compile', function() {
13521352
});
13531353
});
13541354

1355+
it('should ignore whitespace betwee comment and root node when replacing with a template', function() {
1356+
module(function() {
1357+
directive('replaceWithWhitespace', valueFn({
1358+
replace: true,
1359+
template: '<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->'
1360+
}));
1361+
});
1362+
inject(function($compile, $rootScope) {
1363+
expect(function() {
1364+
element = $compile('<div><div replace-with-whitespace></div></div>')($rootScope);
1365+
}).not.toThrow();
1366+
expect(element.find('p').length).toBe(1);
1367+
expect(element.find('p').text()).toBe('Hello, world!');
1368+
});
1369+
});
1370+
13551371
it('should keep prototype properties on directive', function() {
13561372
module(function() {
13571373
function DirectiveClass() {
@@ -2080,6 +2096,18 @@ describe('$compile', function() {
20802096
$compile('<p template></p>');
20812097
$rootScope.$apply();
20822098
expect($exceptionHandler.errors).toEqual([]);
2099+
2100+
// comments are ok
2101+
$templateCache.put('template.html', '<!-- oh hi --><div></div> \n');
2102+
$compile('<p template></p>');
2103+
$rootScope.$apply();
2104+
expect($exceptionHandler.errors).toEqual([]);
2105+
2106+
// white space around comments is ok
2107+
$templateCache.put('template.html', ' <!-- oh hi --> <div></div> <!-- oh hi -->\n');
2108+
$compile('<p template></p>');
2109+
$rootScope.$apply();
2110+
expect($exceptionHandler.errors).toEqual([]);
20832111
});
20842112
});
20852113

@@ -2291,6 +2319,26 @@ describe('$compile', function() {
22912319
});
22922320
});
22932321

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+
22942342
it('should keep prototype properties on sync version of async directive', function() {
22952343
module(function() {
22962344
function DirectiveClass() {

0 commit comments

Comments
 (0)