Skip to content

Commit

Permalink
Offset: fix wrong origin value (jquery#334)
Browse files Browse the repository at this point in the history
offset should return undefined instead of {top:0,left:0} for non DOM element.
  • Loading branch information
panlina authored Apr 10, 2020
1 parent 3373b21 commit da094d8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/offset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ var oldOffset = jQuery.fn.offset;
jQuery.fn.offset = function() {
var docElem,
elem = this[ 0 ],
origin = { top: 0, left: 0 };
bogus = { top: 0, left: 0 };

if ( !elem || !elem.nodeType ) {
migrateWarn( "jQuery.fn.offset() requires a valid DOM element" );
return origin;
return undefined;
}

docElem = ( elem.ownerDocument || window.document ).documentElement;
if ( !jQuery.contains( docElem, elem ) ) {
migrateWarn( "jQuery.fn.offset() requires an element connected to a document" );
return origin;
return bogus;
}

return oldOffset.apply( this, arguments );
Expand Down
10 changes: 5 additions & 5 deletions test/offset.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ QUnit.test( ".offset()", function( assert ) {
} );

expectWarning( assert, ".offset() on window", function() {
assert.deepEqual(
assert.strictEqual(
jQuery( window ).offset(),
bogus, "window bogus top/left 0"
undefined, "window undefined"
);
} );

Expand All @@ -38,10 +38,10 @@ QUnit.test( ".offset()", function( assert ) {
);
} );

expectWarning( assert, ".offset() on plain object", function() {
assert.deepEqual(
expectWarning(assert, ".offset() on plain object", function () {
assert.strictEqual(
jQuery( { space: "junk", zero: 0 } ).offset(),
bogus, "plain object bogus top/left 0"
undefined, "plain object undefined"
);
} );
} );

0 comments on commit da094d8

Please sign in to comment.