From 5340d1e0b1c29103e187a1cdc33bbee48d98a74d Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Sun, 23 Jan 2011 15:55:11 -0800 Subject: fix for infinite loop in retrieveScope with jQuery + specs - retrieveScope run into infinite loop if called on DOM tree that doesn't contain scope reference (happens only with jQuery) - added missing specs for retrieveScope function --- src/Compiler.js | 2 +- test/CompilerSpec.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Compiler.js b/src/Compiler.js index 3f5ba9e1..58a7a47b 100644 --- a/src/Compiler.js +++ b/src/Compiler.js @@ -75,7 +75,7 @@ Template.prototype = { function retrieveScope(element) { var scope; element = jqLite(element); - while (element && !(scope = element.data($$scope))) { + while (element && element.length && !(scope = element.data($$scope))) { element = element.parent(); } return scope; diff --git a/test/CompilerSpec.js b/test/CompilerSpec.js index d8c7c1b8..76d3d1ca 100644 --- a/test/CompilerSpec.js +++ b/test/CompilerSpec.js @@ -167,4 +167,23 @@ describe('compiler', function(){ expect(sortedHtml(scope.$element)).toEqual('
A
B
C

D
'); }); + + describe('retrieveScope', function() { + it('should retrieve scope attached to the current element', function() { + scope = compile('foo'); + expect(retrieveScope(scope.$element)).toBe(scope); + }); + + it('should walk up the dom to find scope', function() { + scope = compile(''); + var deepChild = scope.$element[0].getElementsByTagName('b')[0]; + expect(retrieveScope(deepChild)).toBe(scope); + }); + + it('should return undefined when no scope was found', function() { + var html = jqLite(''), + deepChild = html[0].getElementsByTagName('b')[0]; + expect(retrieveScope(deepChild)).toBeNull(); + }); + }); }); -- cgit v1.2.3