diff --git a/src/ng/compile.js b/src/ng/compile.js index f75cb7393f11..1034aa9b8974 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1481,7 +1481,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { if (jqLiteIsTextNode(directiveValue)) { $template = []; } else { - $template = jqLite(wrapTemplate(directive.templateNamespace, trim(directiveValue))); + $template = removeComments(jqLite(wrapTemplate(directive.templateNamespace, trim(directiveValue)))); } compileNode = $template[0]; @@ -1951,7 +1951,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { if (jqLiteIsTextNode(content)) { $template = []; } else { - $template = jqLite(wrapTemplate(templateNamespace, trim(content))); + $template = removeComments(jqLite(wrapTemplate(templateNamespace, trim(content)))); } compileNode = $template[0]; @@ -2362,3 +2362,16 @@ function tokenDifference(str1, str2) { } return values; } + +function removeComments(jqNodes) { + var i = jqNodes.length; + var splice = Array.prototype.splice; + + if (i <= 1) return jqNodes; + + while (i--) { + var node = jqNodes[i]; + if (node.nodeType === 8) splice.call(jqNodes, i, 1); + } + return jqNodes; +} diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 2a0d84d04150..0447e2162cae 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -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: '
Hello, world!
' + })); + }); + inject(function($compile, $rootScope) { + expect(function() { + element = $compile('Hello, world!
'); + expect(function() { + element = $compile('