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

Commit 8381790

Browse files
committed
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. The comment nodes will be removed before processing. Closes #9212
1 parent 6e7fbe7 commit 8381790

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/ng/compile.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
14811481
if (jqLiteIsTextNode(directiveValue)) {
14821482
$template = [];
14831483
} else {
1484-
$template = jqLite(wrapTemplate(directive.templateNamespace, trim(directiveValue)));
1484+
$template = removeComments(jqLite(wrapTemplate(directive.templateNamespace, trim(directiveValue))));
14851485
}
14861486
compileNode = $template[0];
14871487

@@ -1951,7 +1951,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19511951
if (jqLiteIsTextNode(content)) {
19521952
$template = [];
19531953
} else {
1954-
$template = jqLite(wrapTemplate(templateNamespace, trim(content)));
1954+
$template = removeComments(jqLite(wrapTemplate(templateNamespace, trim(content))));
19551955
}
19561956
compileNode = $template[0];
19571957

@@ -2362,3 +2362,16 @@ function tokenDifference(str1, str2) {
23622362
}
23632363
return values;
23642364
}
2365+
2366+
function removeComments(jqNodes) {
2367+
var i = jqNodes.length;
2368+
var splice = Array.prototype.splice;
2369+
2370+
if (i <= 1) return jqNodes;
2371+
2372+
while (i--) {
2373+
var node = jqNodes[i];
2374+
if (node.nodeType === 8) splice.call(jqNodes, i, 1);
2375+
}
2376+
return jqNodes;
2377+
}

test/ng/compileSpec.js

+36
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,22 @@ describe('$compile', function() {
10781078
});
10791079
});
10801080
}
1081+
1082+
it('should ignore comment nodes when replacing with a template', function() {
1083+
module(function() {
1084+
directive('replaceWithComments', valueFn({
1085+
replace: true,
1086+
template: '<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->'
1087+
}));
1088+
});
1089+
inject(function($compile, $rootScope) {
1090+
expect(function() {
1091+
element = $compile('<div><div replace-with-comments></div></div>')($rootScope);
1092+
}).not.toThrow();
1093+
expect(element.find('p').length).toBe(1);
1094+
expect(element.find('p').text()).toBe('Hello, world!');
1095+
});
1096+
});
10811097
});
10821098

10831099

@@ -1977,6 +1993,26 @@ describe('$compile', function() {
19771993
});
19781994
});
19791995
}
1996+
1997+
it('should ignore comment nodes when replacing with a templateUrl', function() {
1998+
module(function() {
1999+
directive('replaceWithComments', valueFn({
2000+
replace: true,
2001+
templateUrl: 'templateWithComments.html'
2002+
}));
2003+
});
2004+
inject(function($compile, $rootScope, $httpBackend) {
2005+
$httpBackend.whenGET('templateWithComments.html').
2006+
respond('<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->');
2007+
expect(function() {
2008+
element = $compile('<div><div replace-with-comments></div></div>')($rootScope);
2009+
}).not.toThrow();
2010+
$httpBackend.flush();
2011+
expect(element.find('p').length).toBe(1);
2012+
expect(element.find('p').text()).toBe('Hello, world!');
2013+
});
2014+
});
2015+
19802016
});
19812017

19822018

0 commit comments

Comments
 (0)