Bug Tracker

Opened 10 years ago

Closed 10 years ago

#12313 closed bug (fixed)

.height() and .width() no longer fall back to CSS if offsetWidth is undefined.

Reported by: [email protected] Owned by: mikesherov
Priority: blocker Milestone: 1.8.1
Component: css Version: 1.8.0
Keywords: Cc: mikesherov
Blocked by: Blocking:


offsetWidth and offsetHeight are undefined for SVG elements in Firefox and IE 9. jQuery used to fall back to computed, then uncomputed CSS in this case, but that no longer happens after the following commit: https://github.com/jquery/jquery/commit/5376a809c0d2bee4b7872847c2821e458dfdcc3b

In "getWidthOrHeight":

// Start with offset property
var val = name === "width" ? elem.offsetWidth : elem.offsetHeight,
  i = name === "width" ? 1 : 0,
  len = 4;

if ( val > 0 ) {

// Fall back to computed then uncomputed css if necessary
  val = curCSS( elem, name );

"val > 0" failed if offsetWidth or offsetHeight were undefined, so fallback would always occur.

In 5376a809 the condition was changed to the following:

if ( val <= 0 ) {
    // Fall back to computed then uncomputed css if necessary
    val = curCSS( elem, name );

"undefined" will never be <= 0. I think it's possible the former conditional logic caught the "undefined" case by accident. If not, the intent of the conditional logic was not clear if it was supposed to handle "undefined" (but may have saved bytes).

Test case: http://jsfiddle.net/Jxdej/5/

This will show NaNpx for height and width in Firefox and IE9, 200 in Chrome. The assertion in the console will also fail.

It looks like a bug for offsetWidth/Height being undefined on SVG elements in Firefox has been filed: https://bugzilla.mozilla.org/show_bug.cgi?id=649285

Change History (4)

comment:1 Changed 10 years ago by dmethvin

Cc: mikesherov added
Component: unfiledcss
Milestone: None1.8.1
Priority: undecidedblocker
Status: newopen

Mike, looks like it should be fixable if we tweak the conditional?

comment:2 Changed 10 years ago by mikesherov

Owner: set to mikesherov
Status: openassigned

TIL that:

undefined < 0 // false
undefined > 0 // false
undefined == 0 // false
!undefined // true

PR to come shortly.

comment:4 Changed 10 years ago by Mike Sherov

Resolution: fixed
Status: assignedclosed

Fix #12313, .height()/.width() just use CSS if no offsetWidth. Close gh-909.

Changeset: c078b83b3fea891e0c228a9f2d101481678b4c9d

Note: See TracTickets for help on using tickets.