Merge branch 'master' of github.com:jquery/jquery
authorBrandon Aaron <brandon.aaron@gmail.com>
Wed, 24 Mar 2010 03:40:38 +0000 (22:40 -0500)
committerBrandon Aaron <brandon.aaron@gmail.com>
Wed, 24 Mar 2010 03:40:38 +0000 (22:40 -0500)
src/offset.js
test/data/offset/absolute.html
test/unit/offset.js

index 365b35a..0ce4c19 100644 (file)
@@ -150,23 +150,38 @@ jQuery.offset = {
        },
        
        setOffset: function( elem, options, i ) {
+               var position = jQuery.curCSS( elem, "position" );
+
                // set position first, in-case top/left are set even on static elem
-               if ( /static/.test( jQuery.curCSS( elem, "position" ) ) ) {
+               if ( position === "static" ) {
                        elem.style.position = "relative";
                }
-               var curElem   = jQuery( elem ),
-                       curOffset = curElem.offset(),
-                       curTop    = parseInt( jQuery.curCSS( elem, "top",  true ), 10 ) || 0,
-                       curLeft   = parseInt( jQuery.curCSS( elem, "left", true ), 10 ) || 0;
+
+               var curElem    = jQuery( elem ),
+                       curOffset  = curElem.offset(),
+                       curCSSTop  = jQuery.curCSS( elem, "top", true ),
+                       curCSSLeft = jQuery.curCSS( elem, "left", true ),
+                       calculatePosition = (position === "absolute" && jQuery.inArray('auto', [curCSSTop, curCSSLeft]) > -1),
+                       props = {}, curPosition = {}, curTop, curLeft;
+
+               // need to be able to calculate position if either top or left is auto and position is absolute
+               if ( calculatePosition ) {
+                       curPosition = curElem.position();
+               }
+
+               curTop  = calculatePosition ? curPosition.top  : parseInt( curCSSTop,  10 ) || 0;
+               curLeft = calculatePosition ? curPosition.left : parseInt( curCSSLeft, 10 ) || 0;
 
                if ( jQuery.isFunction( options ) ) {
                        options = options.call( elem, i, curOffset );
                }
 
-               var props = {
-                       top:  (options.top  - curOffset.top)  + curTop,
-                       left: (options.left - curOffset.left) + curLeft
-               };
+               if (options.top != null) {
+                       props.top = (options.top - curOffset.top) + curTop;
+               }
+               if (options.left != null) {
+                       props.left = (options.left - curOffset.left) + curLeft;
+               }
                
                if ( "using" in options ) {
                        options.using.call( elem, props );
index bbd4681..dc0ba22 100644 (file)
@@ -13,6 +13,7 @@
                        #absolute-2 { top: 19px; left: 19px; }
                        #marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; }
                        p.instructions { position: absolute; bottom: 0; }
+                       #positionTest { position: absolute; }
                </style>
                <script src="../../../src/core.js"></script>
                <script src="../../../src/support.js"></script>
@@ -42,6 +43,7 @@
                        </div>
                </div>
                <div id="absolute-2" class="absolute">absolute-2</div>
+               <div id="positionTest">Has absolute position but no values set for the location ('auto').</div>
                <div id="marker"></div>
                <p class="instructions">Click the white box to move the marker to it. Clicking the box also changes the position to absolute (if not already) and sets the position according to the position method.</p>
        </body>
index 0f5c964..3b6af50 100644 (file)
@@ -35,7 +35,7 @@ testoffset("absolute"/* in iframe */, function($, iframe) {
 });
 
 testoffset("absolute", function( jQuery ) {
-       expect(144);
+       expect(178);
        
        // get offset tests
        var tests = [
@@ -62,6 +62,11 @@ testoffset("absolute", function( jQuery ) {
                equals( jQuery( this.id ).position().left, this.left, "jQuery('" + this.id + "').position().left" );
        });
        
+       // test #5781
+       var offset = jQuery( '#positionTest' ).offset({ top: 10, left: 10 }).offset();
+       equals( offset.top,  10, "Setting offset on element with position absolute but 'auto' values." )
+       equals( offset.left, 10, "Setting offset on element with position absolute but 'auto' values." )
+       
        
        // set offset
        tests = [
@@ -94,8 +99,14 @@ testoffset("absolute", function( jQuery ) {
                        equals( val.left, left, "Verify incoming top position." );
                        return { top: top + 1, left: left + 1 };
                });
-               equals( jQuery( this.id ).offset().top,  this.top + 1,  "jQuery('" + this.id + "').offset({ top: "  + this.top  + " })" );
-               equals( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + this.left + " })" );
+               equals( jQuery( this.id ).offset().top,  this.top  + 1, "jQuery('" + this.id + "').offset({ top: "  + (this.top  + 1) + " })" );
+               equals( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + (this.left + 1) + " })" );
+               
+               jQuery( this.id )
+                       .offset({ left: this.left + 2 })
+                       .offset({ top:  this.top  + 2 });
+               equals( jQuery( this.id ).offset().top,  this.top  + 2, "Setting one property at a time." );
+               equals( jQuery( this.id ).offset().left, this.left + 2, "Setting one property at a time." );
                
                jQuery( this.id ).offset({ top: this.top, left: this.left, using: function( props ) {
                        jQuery( this ).css({
@@ -112,7 +123,7 @@ testoffset("relative", function( jQuery ) {
        expect(60);
        
        // IE is collapsing the top margin of 1px
-       var ie = jQuery.browser.msie && parseInt( jQuery.browser.version ) < 8;
+       var ie = jQuery.browser.msie && parseInt( jQuery.browser.version, 10 ) < 8;
        
        // get offset
        var tests = [
@@ -173,7 +184,7 @@ testoffset("static", function( jQuery ) {
        expect(80);
        
        // IE is collapsing the top margin of 1px
-       var ie = jQuery.browser.msie && parseInt( jQuery.browser.version ) < 8;
+       var ie = jQuery.browser.msie && parseInt( jQuery.browser.version, 10 ) < 8;
        
        // get offset
        var tests = [
@@ -302,7 +313,7 @@ testoffset("table", function( jQuery ) {
 testoffset("scroll", function( jQuery, win ) {
        expect(16);
        
-       var ie = jQuery.browser.msie && parseInt( jQuery.browser.version ) < 8;
+       var ie = jQuery.browser.msie && parseInt( jQuery.browser.version, 10 ) < 8;
        
        // IE is collapsing the top margin of 1px
        equals( jQuery('#scroll-1').offset().top, ie ? 6 : 7, "jQuery('#scroll-1').offset().top" );
@@ -404,7 +415,7 @@ function testoffset(name, fn) {
        });
        
        function loadFixture() {
-               var src = './data/offset/' + name + '.html?' + parseInt( Math.random()*1000 ),
+               var src = './data/offset/' + name + '.html?' + parseInt( Math.random()*1000, 10 ),
                        iframe = jQuery('<iframe />').css({
                                width: 500, height: 500, position: 'absolute', top: -600, left: -600, visiblity: 'hidden'
                        }).appendTo('body')[0];