From: Brandon Aaron Date: Wed, 12 Dec 2007 16:55:55 +0000 (+0000) Subject: Added more unit tests for offset. Fixed an offset issue relating to table header... X-Git-Url: http://git.asbjorn.biz/?p=jquery.git;a=commitdiff_plain;h=58c640fb1ce09a03399a3a441716cca23f3f2d04 Added more unit tests for offset. Fixed an offset issue relating to table header cells in Opera --- diff --git a/src/offset.js b/src/offset.js index e74556c..2570358 100644 --- a/src/offset.js +++ b/src/offset.js @@ -5,7 +5,7 @@ jQuery.fn.offset = function() { var left = 0, top = 0, elem = this[0], results; if ( elem ) with ( jQuery.browser ) { - var parent = elem.parentNode, + var parent = elem.parentNode, offsetChild = elem, offsetParent = elem.offsetParent, doc = elem.ownerDocument, @@ -17,10 +17,8 @@ jQuery.fn.offset = function() { var box = elem.getBoundingClientRect(); // Add the document scroll offsets - add( - box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft), - box.top + Math.max(doc.documentElement.scrollTop, doc.body.scrollTop) - ); + add(box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft), + box.top + Math.max(doc.documentElement.scrollTop, doc.body.scrollTop)); // IE adds the HTML element's border, by default it is medium which is 2px // IE 6 and 7 quirks mode the border width is overwritable by the following css html { border: 0; } @@ -35,7 +33,7 @@ jQuery.fn.offset = function() { // Initial element offsets add( elem.offsetLeft, elem.offsetTop ); - + // Get parent offsets while ( offsetParent ) { // Add offsetParent offsets @@ -58,8 +56,8 @@ jQuery.fn.offset = function() { // Get parent scroll offsets while ( parent.tagName && !/^body|html$/i.test(parent.tagName) ) { - // Remove parent scroll UNLESS that parent is inline or a table-row to work around Opera inline/table scrollLeft/Top bug - if ( !/^inline|table-row.*$/i.test(jQuery.css(parent, "display")) ) + // Remove parent scroll UNLESS that parent is inline or a table to work around Opera inline/table scrollLeft/Top bug + if ( !/^inline|table.*$/i.test(jQuery.css(parent, "display")) ) // Subtract parent scroll offsets add( -parent.scrollLeft, -parent.scrollTop ); @@ -79,10 +77,8 @@ jQuery.fn.offset = function() { // Add the document scroll offsets if position is fixed if ( fixed ) - add( - Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft), - Math.max(doc.documentElement.scrollTop, doc.body.scrollTop) - ); + add(Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft), + Math.max(doc.documentElement.scrollTop, doc.body.scrollTop)); } // Return an object with top and left properties diff --git a/test/data/offset/fixed.html b/test/data/offset/fixed.html new file mode 100644 index 0000000..1585576 --- /dev/null +++ b/test/data/offset/fixed.html @@ -0,0 +1,32 @@ + + + + + fixed + + + + + +
+
+
+
+ + \ No newline at end of file diff --git a/test/data/offset/scroll.html b/test/data/offset/scroll.html new file mode 100644 index 0000000..fa21e6b --- /dev/null +++ b/test/data/offset/scroll.html @@ -0,0 +1,38 @@ + + + + + scroll + + + + + +
+
+
+
+
+
+
+ + \ No newline at end of file diff --git a/test/data/offset/static.html b/test/data/offset/static.html new file mode 100644 index 0000000..a18469e --- /dev/null +++ b/test/data/offset/static.html @@ -0,0 +1,28 @@ + + + + + static + + + + + +
+
+
+ + \ No newline at end of file diff --git a/test/data/offset/table.html b/test/data/offset/table.html new file mode 100644 index 0000000..f10b76d --- /dev/null +++ b/test/data/offset/table.html @@ -0,0 +1,42 @@ + + + + + table + + + + + + + + + + + + + + + + + + + + +
th-1th-2th-3
td-1td-2td-3
+
+ + \ No newline at end of file diff --git a/test/unit/offset.js b/test/unit/offset.js index 857e089..328442a 100644 --- a/test/unit/offset.js +++ b/test/unit/offset.js @@ -41,14 +41,72 @@ testwin("absolute", function() { testwin("relative", function() { var $w = testwin["relative"].$; - equals( $w('#relative-1').offset().top, jQuery.browser.msie ? 6 : 7, "$('#relative-1').offset().top" ); + equals( $w('#relative-1').offset().top, $.browser.msie ? 6 : 7, "$('#relative-1').offset().top" ); equals( $w('#relative-1').offset().left, 7, "$('#relative-1').offset().left" ); - equals( $w('#relative-1-1').offset().top, jQuery.browser.msie ? 13 : 15, "$('#relative-1-1').offset().top" ); + equals( $w('#relative-1-1').offset().top, $.browser.msie ? 13 : 15, "$('#relative-1-1').offset().top" ); equals( $w('#relative-1-1').offset().left, 15, "$('#relative-1-1').offset().left" ); - equals( $w('#relative-2').offset().top, jQuery.browser.msie ? 141 : 142, "$('#relative-2').offset().top" ); + equals( $w('#relative-2').offset().top, $.browser.msie ? 141 : 142, "$('#relative-2').offset().top" ); equals( $w('#relative-2').offset().left, 27, "$('#relative-2').offset().left" ); testwin["relative"].close(); +}); + +testwin("static", function() { + var $w = testwin["static"].$; + + equals( $w('#static-1').offset().top, $.browser.msie ? 6 : 7, "$('#static-1').offset().top" ); + equals( $w('#static-1').offset().left, 7, "$('#static-1').offset().left" ); + + equals( $w('#static-1-1').offset().top, $.browser.msie ? 13 : 15, "$('#static-1-1').offset().top" ); + equals( $w('#static-1-1').offset().left, 15, "$('#static-1-1').offset().left" ); + + equals( $w('#static-1-1-1').offset().top, $.browser.msie ? 20 : 23, "$('#static-1-1-1').offset().top" ); + equals( $w('#static-1-1-1').offset().left, 23, "$('#static-1-1-1').offset().left" ); + + equals( $w('#static-2').offset().top, $.browser.msie ? 121 : 122, "$('#static-2').offset().top" ); + equals( $w('#static-2').offset().left, 7, "$('#static-2').offset().left" ); + + testwin["static"].close(); +}); + +if ( !$.browser.msie || ($.browser.msie && parseInt($.browser.version) > 6) ) + testwin("fixed", function() { + var $w = testwin["fixed"].$; + + equals( $w('#fixed-1').offset().top, 1001, "$('#fixed-1').offset().top" ); + equals( $w('#fixed-1').offset().left, $.browser.msie ? 994 : 1001, "$('#fixed-1').offset().left" ); + + equals( $w('#fixed-2').offset().top, 1021, "$('#fixed-2').offset().top" ); + equals( $w('#fixed-2').offset().left, $.browser.msie ? 1014 : 1021, "$('#fixed-2').offset().left" ); + + testwin["fixed"].close(); + }); + +testwin("table", function() { + var $w = testwin["table"].$; + + equals( $w('#table-1').offset().top, 6, "$('#table-1').offset().top" ); + equals( $w('#table-1').offset().left, 6, "$('#table-1').offset().left" ); + + equals( $w('#th-1').offset().top, 10, "$('#table-1').offset().top" ); + equals( $w('#th-1').offset().left, 10, "$('#table-1').offset().left" ); + + equals( $w('#th-2').offset().top, 10, "$('#table-1').offset().top" ); + equals( $w('#th-2').offset().left, 116, "$('#table-1').offset().left" ); + + testwin["table"].close(); +}); + +testwin("scroll", function() { + var $w = testwin["scroll"].$; + + equals( $w('#scroll-1').offset().top, $.browser.msie ? 6 : 7, "$('#scroll-1').offset().top" ); + equals( $w('#scroll-1').offset().left, 7, "$('#scroll-1').offset().left" ); + + equals( $w('#scroll-1-1').offset().top, $.browser.msie ? 9 : 11, "$('#scroll-1-1').offset().top" ); + equals( $w('#scroll-1-1').offset().left, 11, "$('#scroll-1-1').offset().left" ); + + testwin["scroll"].close(); }); \ No newline at end of file