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

Commit 49f9e4c

Browse files
petebacondarwinIgorMinar
authored andcommitted
fix($compile): do not wrap empty root text nodes in spans
Closes #1059
1 parent 7e74601 commit 49f9e4c

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ function $CompileProvider($provide) {
318318
// We can not compile top level text elements since text nodes can be merged and we will
319319
// not be able to attach scope data to them, so we will wrap them in <span>
320320
forEach($compileNodes, function(node, index){
321-
if (node.nodeType == 3 /* text node */) {
321+
if (node.nodeType == 3 /* text node */ && node.nodeValue.match(/\S+/) /* non-empty */ ) {
322322
$compileNodes[index] = jqLite(node).wrap('<span></span>').parent()[0];
323323
}
324324
});

test/ng/compileSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ describe('$compile', function() {
125125
expect(element.find('span').text()).toEqual('A<a>B</a>C');
126126
}));
127127

128+
129+
it('should not wrap root whitespace text nodes in spans', function() {
130+
element = jqLite(
131+
'<div> <div>A</div>\n '+ // The spaces and newlines here should not get wrapped
132+
'<div>B</div>C\t\n '+ // The "C", tabs and spaces here will be wrapped
133+
'</div>');
134+
$compile(element.contents())($rootScope);
135+
var spans = element.find('span');
136+
expect(spans.length).toEqual(1);
137+
expect(spans.text().indexOf('C')).toEqual(0);
138+
});
139+
140+
128141
describe('multiple directives per element', function() {
129142
it('should allow multiple directives per element', inject(function($compile, $rootScope, log){
130143
element = $compile(

0 commit comments

Comments
 (0)