mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-21 18:42:31 +08:00
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
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -167,4 +167,23 @@ describe('compiler', function(){
|
||||
expect(sortedHtml(scope.$element)).toEqual('<div>A<hr></hr>B<hr></hr>C<p></p>D</div>');
|
||||
});
|
||||
|
||||
|
||||
describe('retrieveScope', function() {
|
||||
it('should retrieve scope attached to the current element', function() {
|
||||
scope = compile('<i>foo</i>');
|
||||
expect(retrieveScope(scope.$element)).toBe(scope);
|
||||
});
|
||||
|
||||
it('should walk up the dom to find scope', function() {
|
||||
scope = compile('<ul><li><p><b>deep deep</b><p></li></ul>');
|
||||
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('<ul><li><p><b>deep deep</b><p></li></ul>'),
|
||||
deepChild = html[0].getElementsByTagName('b')[0];
|
||||
expect(retrieveScope(deepChild)).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user