From 2c28f229e5eb49f1469fd36ee979529a492f0f42 Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Thu, 21 Jan 2010 23:18:54 +0800 Subject: [PATCH] fixed offset(coordinates) returns null when no matching elements in jQuery object (fixes ticket #5888) --- src/offset.js | 16 ++++++++-------- test/unit/effects.js | 3 +-- test/unit/offset.js | 7 +++++++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/offset.js b/src/offset.js index c318374..f80574e 100644 --- a/src/offset.js +++ b/src/offset.js @@ -2,16 +2,16 @@ if ( "getBoundingClientRect" in document.documentElement ) { jQuery.fn.offset = function( options ) { var elem = this[0]; - if ( !elem || !elem.ownerDocument ) { - return null; - } - if ( options ) { return this.each(function( i ) { jQuery.offset.setOffset( this, options, i ); }); } + if ( !elem || !elem.ownerDocument ) { + return null; + } + if ( elem === elem.ownerDocument.body ) { return jQuery.offset.bodyOffset( elem ); } @@ -28,16 +28,16 @@ if ( "getBoundingClientRect" in document.documentElement ) { jQuery.fn.offset = function( options ) { var elem = this[0]; - if ( !elem || !elem.ownerDocument ) { - return null; - } - if ( options ) { return this.each(function( i ) { jQuery.offset.setOffset( this, options, i ); }); } + if ( !elem || !elem.ownerDocument ) { + return null; + } + if ( elem === elem.ownerDocument.body ) { return jQuery.offset.bodyOffset( elem ); } diff --git a/test/unit/effects.js b/test/unit/effects.js index e44739b..467631c 100644 --- a/test/unit/effects.js +++ b/test/unit/effects.js @@ -29,9 +29,8 @@ test("show()", function() { div.hide().show(speed, function() { pass = false; }); - ok( pass, "Show with " + name + " does not call animate callback"); + ok( pass, "Show with " + name + " does not call animate callback" ); }); - jQuery("#main").append('

'); diff --git a/test/unit/offset.js b/test/unit/offset.js index dc03bc2..dbaddc8 100644 --- a/test/unit/offset.js +++ b/test/unit/offset.js @@ -305,6 +305,13 @@ testoffset("body", function( jQuery ) { equals( jQuery('body').offset().left, 1, "jQuery('#body').offset().left" ); }); +test("Chaining offset(coords) returns jQuery object", function() { + expect(2); + var coords = { top: 1, left: 1 }; + equals( jQuery("#absolute-1").offset(coords).selector, "#absolute-1", "offset(coords) returns jQuery object" ); + equals( jQuery("#non-existent").offset(coords).selector, "#non-existent", "offset(coords) with empty jQuery set returns jQuery object" ); +}); + test("offsetParent", function(){ expect(11); -- 1.7.10.4