Bug Tracker

Changeset 4217

Show
Ignore:
Timestamp:
12/18/07 03:53:09 (1 year ago)
Author:
brandon.aaron
Message:

width and height methods are now working properly

Location:
trunk/jquery
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/jquery/src/core.js

    r4212 r4217  
    765765    // A method for quickly swapping in/out CSS properties to get correct calculations 
    766766    swap: function( elem, options, callback ) { 
     767        var old = {}; 
    767768        // Remember the old values, and insert the new ones 
    768769        for ( var name in options ) { 
    769             elem.style[ "old" + name ] = elem.style[ name ]; 
     770            old[ name ] = elem.style[ name ]; 
    770771            elem.style[ name ] = options[ name ]; 
    771772        } 
     
    775776        // Revert the old values 
    776777        for ( var name in options ) 
    777             elem.style[ name ] = elem.style[ "old" + name ]; 
     778            elem.style[ name ] = old[ name ]; 
    778779    }, 
    779780 
    780781    css: function( elem, name, force ) { 
    781782        if ( name == "width" || name == "height" ) { 
    782             var width, height, props = { position: "absolute", visibility: "hidden", display:"block" }; 
     783            var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ]; 
    783784         
    784785            function getWH() { 
    785                 width = elem.clientWidth; 
    786                 height = elem.clientHeight; 
     786                val = name == "width" ? elem.offsetWidth : elem.offsetHeight; 
     787                var padding = 0, border = 0; 
     788                jQuery.each( which, function() { 
     789                    padding += parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0; 
     790                    border += parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0; 
     791                }); 
     792                val -= Math.round(padding + border); 
    787793            } 
    788794         
     
    791797            else 
    792798                jQuery.swap( elem, props, getWH ); 
    793  
    794             return name == "width" ? width : height; 
     799             
     800            return val; 
    795801        } 
    796802         
  • trunk/jquery/test/index.html

    r4196 r4217  
    2121     
    2222    <!-- Test HTML --> 
    23     <div id="nothiddendiv" style="height:1px;background:white;"></div> 
     23    <div id="nothiddendiv" style="height:1px;background:white;"> 
     24        <div id="nothiddendivchild"></div> 
     25    </div> 
    2426    <!-- this iframe is outside the #main so it won't reload constantly wasting time, but it means the tests must be "safe" and clean up after themselves --> 
    2527    <iframe id="loadediframe" name="loadediframe" style="display:none;" src="data/iframe.html"></iframe> 
  • trunk/jquery/test/unit/core.js

    r4212 r4217  
    474474 
    475475test("width()", function() { 
    476     expect(2); 
    477  
    478     $("#nothiddendiv").width(30); 
    479     equals($("#nothiddendiv").width(), 30, "Test set to 30 correctly"); 
    480     $("#nothiddendiv").width(-1); // handle negative numbers by ignoring #1599 
    481     equals($("#nothiddendiv").width(), 30, "Test negative width ignored"); 
     476    expect(9); 
     477 
     478    var $div = $("#nothiddendiv"); 
     479    $div.width(30); 
     480    equals($div.width(), 30, "Test set to 30 correctly"); 
     481    $div.width(-1); // handle negative numbers by ignoring #1599 
     482    equals($div.width(), 30, "Test negative width ignored"); 
     483    $div.css("padding", "20px"); 
     484    equals($div.width(), 30, "Test padding specified with pixels"); 
     485    $div.css("border", "2px solid #fff"); 
     486    equals($div.width(), 30, "Test border specified with pixels"); 
     487    $div.css("padding", "2em"); 
     488    equals($div.width(), 30, "Test padding specified with ems"); 
     489    $div.css("border", "1em solid #fff"); 
     490    equals($div.width(), 30, "Test border specified with ems"); 
     491    $div.css("padding", "2%"); 
     492    equals($div.width(), 30, "Test padding specified with percent"); 
     493    $div.hide(); 
     494    equals($div.width(), 30, "Test hidden div"); 
     495     
     496    $div.css({ display: "", border: "", padding: "" }); 
     497     
     498    $("#nothiddendivchild").css({ padding: "3px", border: "2px solid #fff" }); 
     499    equals($("#nothiddendivchild").width(), 20, "Test child width with border and padding"); 
     500    $("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", width: "" }); 
     501}); 
     502 
     503test("height()", function() { 
     504    expect(8); 
     505 
     506    var $div = $("#nothiddendiv"); 
     507    $div.height(30); 
     508    equals($div.height(), 30, "Test set to 30 correctly"); 
     509    $div.height(-1); // handle negative numbers by ignoring #1599 
     510    equals($div.height(), 30, "Test negative height ignored"); 
     511    $div.css("padding", "20px"); 
     512    equals($div.height(), 30, "Test padding specified with pixels"); 
     513    $div.css("border", "2px solid #fff"); 
     514    equals($div.height(), 30, "Test border specified with pixels"); 
     515    $div.css("padding", "2em"); 
     516    equals($div.height(), 30, "Test padding specified with ems"); 
     517    $div.css("border", "1em solid #fff"); 
     518    equals($div.height(), 30, "Test border specified with ems"); 
     519    $div.css("padding", "2%"); 
     520    equals($div.height(), 30, "Test padding specified with percent"); 
     521    $div.hide(); 
     522    equals($div.height(), 30, "Test hidden div"); 
     523     
     524    $div.css({ display: "", border: "", padding: "", height: "1px" }); 
    482525}); 
    483526