Offset: Increase search depth when finding the 'real' offset parent

Changes:
* Increase search depth when finding for the real offset parent
* Ignore offset for statically positioned offset parent
* Add tests for the position of an element in a table

Closes gh-4861
This commit is contained in:
Liam James 2024-04-19 23:47:52 +10:00 committed by GitHub
parent df1df9503a
commit 556eaf4a19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 4 deletions

View File

@ -121,12 +121,13 @@ jQuery.fn.extend( {
doc = elem.ownerDocument; doc = elem.ownerDocument;
offsetParent = elem.offsetParent || doc.documentElement; offsetParent = elem.offsetParent || doc.documentElement;
while ( offsetParent && while ( offsetParent &&
( offsetParent === doc.body || offsetParent === doc.documentElement ) && offsetParent !== doc.documentElement &&
jQuery.css( offsetParent, "position" ) === "static" ) { jQuery.css( offsetParent, "position" ) === "static" ) {
offsetParent = offsetParent.parentNode; offsetParent = offsetParent.offsetParent || doc.documentElement;
} }
if ( offsetParent && offsetParent !== elem && offsetParent.nodeType === 1 ) { if ( offsetParent && offsetParent !== elem && offsetParent.nodeType === 1 &&
jQuery.css( offsetParent, "position" ) !== "static" ) {
// Incorporate borders into its offset, since they are outside its content origin // Incorporate borders into its offset, since they are outside its content origin
parentOffset = jQuery( offsetParent ).offset(); parentOffset = jQuery( offsetParent ).offset();

View File

@ -436,13 +436,16 @@ testIframe( "fixed", "offset/fixed.html", function( assert, $, window ) {
} ); } );
testIframe( "table", "offset/table.html", function( assert, $ ) { testIframe( "table", "offset/table.html", function( assert, $ ) {
assert.expect( 4 ); assert.expect( 6 );
assert.equal( $( "#table-1" ).offset().top, 6, "jQuery('#table-1').offset().top" ); assert.equal( $( "#table-1" ).offset().top, 6, "jQuery('#table-1').offset().top" );
assert.equal( $( "#table-1" ).offset().left, 6, "jQuery('#table-1').offset().left" ); assert.equal( $( "#table-1" ).offset().left, 6, "jQuery('#table-1').offset().left" );
assert.equal( $( "#th-1" ).offset().top, 10, "jQuery('#th-1').offset().top" ); assert.equal( $( "#th-1" ).offset().top, 10, "jQuery('#th-1').offset().top" );
assert.equal( $( "#th-1" ).offset().left, 10, "jQuery('#th-1').offset().left" ); assert.equal( $( "#th-1" ).offset().left, 10, "jQuery('#th-1').offset().left" );
assert.equal( $( "#th-1" ).position().top, 10, "jQuery('#th-1').position().top" );
assert.equal( $( "#th-1" ).position().left, 10, "jQuery('#th-1').position().left" );
} ); } );
testIframe( "scroll", "offset/scroll.html", function( assert, $, win ) { testIframe( "scroll", "offset/scroll.html", function( assert, $, win ) {