]> source.dussan.org Git - jquery.git/commitdiff
Fix #12243, $("col").width() should return the column's width. Close gh-916.
authorMike Sherov <mike.sherov@gmail.com>
Wed, 29 Aug 2012 12:50:56 +0000 (08:50 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Wed, 29 Aug 2012 16:34:12 +0000 (12:34 -0400)
src/css.js
test/unit/dimensions.js

index 5144ee9cadd4019612d819f1e1caa3bae285b36a..9e72073eb1149ef10107e26818d30433e20289ab 100644 (file)
@@ -2,6 +2,9 @@ var curCSS, iframe, iframeDoc,
        ralpha = /alpha\([^)]*\)/i,
        ropacity = /opacity=([^)]*)/,
        rposition = /^(top|right|bottom|left)$/,
+       // swappable if display is none or starts with table except "table", "table-cell", or "table-caption"
+       // see here for display values: https://developer.mozilla.org/en-US/docs/CSS/display
+       rdisplayswap = /^(none|table(?!-c[ea]).+)/,
        rmargin = /^margin/,
        rnumsplit = new RegExp( "^(" + core_pnum + ")(.*)$", "i" ),
        rnumnonpx = new RegExp( "^(" + core_pnum + ")(?!px)[a-z%]+$", "i" ),
@@ -493,12 +496,14 @@ jQuery.each([ "height", "width" ], function( i, name ) {
        jQuery.cssHooks[ name ] = {
                get: function( elem, computed, extra ) {
                        if ( computed ) {
-                               if ( elem.offsetWidth !== 0 || curCSS( elem, "display" ) !== "none" ) {
-                                       return getWidthOrHeight( elem, name, extra );
-                               } else {
+                               // certain elements can have dimension info if we invisibly show them
+                               // however, it must have a current display style that would benefit from this
+                               if ( elem.offsetWidth === 0 && rdisplayswap.test( curCSS( elem, "display" ) ) ) {
                                        return jQuery.swap( elem, cssShow, function() {
                                                return getWidthOrHeight( elem, name, extra );
                                        });
+                               } else {
+                                       return getWidthOrHeight( elem, name, extra );
                                }
                        }
                },
index b3c15f2839fac74b57c1e76eb84aeabbde7e7960..2e5c6557d92de3a75105bd6b1222638f4333df04 100644 (file)
@@ -292,14 +292,15 @@ test("getting dimensions shouldnt modify runtimeStyle see #9233", function() {
        $div.remove();
 });
 
-test( "getting dimensions of zero width/height table elements shouldn't alter dimensions", function() {
-       expect( 1 );
-
-       var table = jQuery("<table><tbody><tr><td></td><td>a</td></tr><tr><td></td><td>a</td></tr></tbody></table>").appendTo("#qunit-fixture"),
-               elem = table.find("tr:eq(0) td:eq(0)");
+test( "table dimensions", 2, function() {
+       var table = jQuery("<table><colgroup><col/><col/></colgroup><tbody><tr><td></td><td>a</td></tr><tr><td></td><td>a</td></tr></tbody></table>").appendTo("#qunit-fixture"),
+               tdElem = table.find("tr:eq(0) td:eq(0)"),
+               colElem = table.find("col:eq(1)").width( 300 );
 
        table.find("td").css({ "margin": 0, "padding": 0 });
-       equal( elem.width(), elem.width(), "width() doesn't alter dimension values" );
+
+       equal( tdElem.width(), tdElem.width(), "width() doesn't alter dimension values of empty cells, see #11293" );
+       equal( colElem.width(), 300, "col elements have width(), see #12243" );
 });
 
 test("box-sizing:border-box child of a hidden elem (or unconnected node) has accurate inner/outer/Width()/Height()  see #10413", function() {