Skip to content

Commit 670e3ff

Browse files
mikesherovdmethvin
authored andcommitted
Fix #12243, $("col").width() should return the column's width. Close jquerygh-916.
1 parent be2899b commit 670e3ff

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/css.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ var curCSS, iframe, iframeDoc,
22
ralpha = /alpha\([^)]*\)/i,
33
ropacity = /opacity=([^)]*)/,
44
rposition = /^(top|right|bottom|left)$/,
5+
// swappable if display is none or starts with table except "table", "table-cell", or "table-caption"
6+
// see here for display values: https://developer.mozilla.org/en-US/docs/CSS/display
7+
rdisplayswap = /^(none|table(?!-c[ea]).+)/,
58
rmargin = /^margin/,
69
rnumsplit = new RegExp( "^(" + core_pnum + ")(.*)$", "i" ),
710
rnumnonpx = new RegExp( "^(" + core_pnum + ")(?!px)[a-z%]+$", "i" ),
@@ -493,12 +496,14 @@ jQuery.each([ "height", "width" ], function( i, name ) {
493496
jQuery.cssHooks[ name ] = {
494497
get: function( elem, computed, extra ) {
495498
if ( computed ) {
496-
if ( elem.offsetWidth !== 0 || curCSS( elem, "display" ) !== "none" ) {
497-
return getWidthOrHeight( elem, name, extra );
498-
} else {
499+
// certain elements can have dimension info if we invisibly show them
500+
// however, it must have a current display style that would benefit from this
501+
if ( elem.offsetWidth === 0 && rdisplayswap.test( curCSS( elem, "display" ) ) ) {
499502
return jQuery.swap( elem, cssShow, function() {
500503
return getWidthOrHeight( elem, name, extra );
501504
});
505+
} else {
506+
return getWidthOrHeight( elem, name, extra );
502507
}
503508
}
504509
},

test/unit/dimensions.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,15 @@ test("getting dimensions shouldnt modify runtimeStyle see #9233", function() {
292292
$div.remove();
293293
});
294294

295-
test( "getting dimensions of zero width/height table elements shouldn't alter dimensions", function() {
296-
expect( 1 );
297-
298-
var table = jQuery("<table><tbody><tr><td></td><td>a</td></tr><tr><td></td><td>a</td></tr></tbody></table>").appendTo("#qunit-fixture"),
299-
elem = table.find("tr:eq(0) td:eq(0)");
295+
test( "table dimensions", 2, function() {
296+
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"),
297+
tdElem = table.find("tr:eq(0) td:eq(0)"),
298+
colElem = table.find("col:eq(1)").width( 300 );
300299

301300
table.find("td").css({ "margin": 0, "padding": 0 });
302-
equal( elem.width(), elem.width(), "width() doesn't alter dimension values" );
301+
302+
equal( tdElem.width(), tdElem.width(), "width() doesn't alter dimension values of empty cells, see #11293" );
303+
equal( colElem.width(), 300, "col elements have width(), see #12243" );
303304
});
304305

305306
test("box-sizing:border-box child of a hidden elem (or unconnected node) has accurate inner/outer/Width()/Height() see #10413", function() {

0 commit comments

Comments
 (0)