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

fix($compile): remove comment nodes from templates before asserting single root node #9215

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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))));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how many more function calls can we make in this line of code? :)

}
compileNode = $template[0];

Expand Down Expand Up @@ -2362,3 +2362,16 @@ function tokenDifference(str1, str2) {
}
return values;
}

function removeComments(jqNodes) {
var i = jqNodes.length;
var splice = Array.prototype.splice;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a global splice var already coming from Angular.js

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like the global splice was removed at some point. I can re-add it if that's better


if (i <= 1) return jqNodes;

while (i--) {
var node = jqNodes[i];
if (node.nodeType === 8) splice.call(jqNodes, i, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't use single line ifs. using proper blocks makes it easier to refactor the code in the future and makes code diffs easier to read.

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't suppose that splice will update the jqNodes.length property for you, so you need to updated it manually. otherwise the jq collection will be messed up. please check the behavior of splice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://jsfiddle.net/dku33bfy/ at least in modern browsers we're good there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting. thanks for checking

return jqNodes;
}
36 changes: 36 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->'
}));
});
inject(function($compile, $rootScope) {
expect(function() {
element = $compile('<div><div replace-with-comments></div></div>')($rootScope);
}).not.toThrow();
expect(element.find('p').length).toBe(1);
expect(element.find('p').text()).toBe('Hello, world!');
});
});
});


Expand Down Expand Up @@ -1977,6 +1993,26 @@ describe('$compile', function() {
});
});
}

it('should ignore comment nodes when replacing with a templateUrl', function() {
module(function() {
directive('replaceWithComments', valueFn({
replace: true,
templateUrl: 'templateWithComments.html'
}));
});
inject(function($compile, $rootScope, $httpBackend) {
$httpBackend.whenGET('templateWithComments.html').
respond('<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->');
expect(function() {
element = $compile('<div><div replace-with-comments></div></div>')($rootScope);
}).not.toThrow();
$httpBackend.flush();
expect(element.find('p').length).toBe(1);
expect(element.find('p').text()).toBe('Hello, world!');
});
});

});


Expand Down