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

fix($scope): fixes multiple root element error when there is a whites… #15132

Merged
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
5 changes: 3 additions & 2 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3679,8 +3679,9 @@ function removeComments(jqNodes) {

while (i--) {
var node = jqNodes[i];
if (node.nodeType === NODE_TYPE_COMMENT) {
splice.call(jqNodes, i, 1);
if (node.nodeType === NODE_TYPE_COMMENT ||
(node.nodeType === NODE_TYPE_TEXT && node.nodeValue.trim() === '')) {
splice.call(jqNodes, i, 1);
}
}
return jqNodes;
Expand Down
48 changes: 48 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,22 @@ describe('$compile', function() {
});
});

it('should ignore whitespace betwee comment and root node when replacing with a template', function() {
module(function() {
directive('replaceWithWhitespace', valueFn({
replace: true,
template: '<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->'
}));
});
inject(function($compile, $rootScope) {
expect(function() {
element = $compile('<div><div replace-with-whitespace></div></div>')($rootScope);
}).not.toThrow();
expect(element.find('p').length).toBe(1);
expect(element.find('p').text()).toBe('Hello, world!');
});
});

it('should keep prototype properties on directive', function() {
module(function() {
function DirectiveClass() {
Expand Down Expand Up @@ -2092,6 +2108,18 @@ describe('$compile', function() {
$compile('<p template></p>');
$rootScope.$apply();
expect($exceptionHandler.errors).toEqual([]);

// comments are ok
$templateCache.put('template.html', '<!-- oh hi --><div></div> \n');
$compile('<p template></p>');
$rootScope.$apply();
expect($exceptionHandler.errors).toEqual([]);

// white space around comments is ok
$templateCache.put('template.html', ' <!-- oh hi --> <div></div> <!-- oh hi -->\n');
$compile('<p template></p>');
$rootScope.$apply();
expect($exceptionHandler.errors).toEqual([]);
});
});

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

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


it('should keep prototype properties on sync version of async directive', function() {
module(function() {
function DirectiveClass() {
Expand Down