From 1f23cfe9c751f84d9fc996df6d54961b2e46e868 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Mon, 18 Feb 2013 12:05:16 +0000 Subject: fix(compile): should not leak memory when there are top level empty text nodes The change to prevent elements being wrapped around empty text nodes caused these empty text nodes to have scopes and controllers attached, through jqLite.data() calls, which led to memory leaks and errors in IE8. Now we exclude all but document nodes and elements from having jqLite.data() set both in the compiler and in ng-view. Fixes: #1968 and #1876 --- test/ng/compileSpec.js | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'test/ng/compileSpec.js') diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index a7707cf9..3937f0e7 100644 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -116,12 +116,17 @@ describe('$compile', function() { describe('compile phase', function() { + it('should attach scope to the document node when it is compiled explicitly', inject(function($document){ + $compile($document)($rootScope); + expect($document.scope()).toBe($rootScope); + })); + it('should wrap root text nodes in spans', inject(function($compile, $rootScope) { element = jqLite('
A<a>B</a>C
'); var text = element.contents(); expect(text[0].nodeName).toEqual('#text'); text = $compile(text)($rootScope); - expect(lowercase(text[0].nodeName)).toEqual('span'); + expect(text[0].nodeName).toEqual('SPAN'); expect(element.find('span').text()).toEqual('ABC'); })); @@ -137,6 +142,41 @@ describe('$compile', function() { expect(spans.text().indexOf('C')).toEqual(0); }); + it('should not leak memory when there are top level empty text nodes', function() { + var calcCacheSize = function() { + var size = 0; + forEach(jqLite.cache, function(item, key) { size++; }); + return size; + }; + + // We compile the contents of element (i.e. not element itself) + // Then delete these contents and check the cache has been reset to zero + + // First with only elements at the top level + element = jqLite('
'); + $compile(element.contents())($rootScope); + element.html(''); + expect(calcCacheSize()).toEqual(0); + + // Next with non-empty text nodes at the top level + // (in this case the compiler will wrap them in a ) + element = jqLite('
xxx
'); + $compile(element.contents())($rootScope); + element.html(''); + expect(calcCacheSize()).toEqual(0); + + // Next with comment nodes at the top level + element = jqLite('
'); + $compile(element.contents())($rootScope); + element.html(''); + expect(calcCacheSize()).toEqual(0); + + // Finally with empty text nodes at the top level + element = jqLite('
\n
'); + $compile(element.contents())($rootScope); + element.html(''); + expect(calcCacheSize()).toEqual(0); + }); describe('multiple directives per element', function() { it('should allow multiple directives per element', inject(function($compile, $rootScope, log){ -- cgit v1.2.3