From da5cd29efda0703c4ca0e04a2551ae78b17bedd4 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Wed, 9 Jan 2013 22:23:50 +0000 Subject: [PATCH] fix(compile): Do not wrap empty root text nodes in spans --- src/ng/compile.js | 2 +- test/ng/compileSpec.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index 9ed7f1079a31..555796778279 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -318,7 +318,7 @@ function $CompileProvider($provide) { // We can not compile top level text elements since text nodes can be merged and we will // not be able to attach scope data to them, so we will wrap them in forEach($compileNodes, function(node, index){ - if (node.nodeType == 3 /* text node */) { + if (node.nodeType == 3 /* text node */ && node.nodeValue.match(/\S+/) /* non-empty */ ) { $compileNodes[index] = jqLite(node).wrap('').parent()[0]; } }); diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 75af01810e12..3095d1682210 100644 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -125,6 +125,17 @@ describe('$compile', function() { expect(element.find('span').text()).toEqual('ABC'); })); + it('should not wrap root whitespace text nodes in spans', function() { + element = jqLite( + '
A
\n '+ // The spaces and newlines here should not get wrapped + '
B
C\t\n '+ // The "C", tabs and spaces here will be wrapped + '
'); + $compile(element.contents())($rootScope); + var spans = element.find('span'); + expect(spans.length).toEqual(1); + expect(spans.text().indexOf('C')).toEqual(0); + }); + describe('multiple directives per element', function() { it('should allow multiple directives per element', inject(function($compile, $rootScope, log){ element = $compile(