Bug Tracker

Ticket #12313 (closed bug: fixed)

Opened 2 years ago

Last modified 2 years ago

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

Reported by: mbaker.pdx@… Owned by: mikesherov
Priority: blocker Milestone: 1.8.1
Component: css Version: 1.8.0
Keywords: Cc: mikesherov
Blocking: Blocked by:

Description

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

comment:1 Changed 2 years ago by dmethvin

  • Cc mikesherov added
  • Priority changed from undecided to blocker
  • Status changed from new to open
  • Component changed from unfiled to css
  • Milestone changed from None to 1.8.1

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

comment:2 Changed 2 years ago by mikesherov

  • Owner set to mikesherov
  • Status changed from open to assigned

TIL that:

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

PR to come shortly.

comment:4 Changed 2 years ago by Mike Sherov

  • Status changed from assigned to closed
  • Resolution set to fixed

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

Changeset: c078b83b3fea891e0c228a9f2d101481678b4c9d

Note: See TracTickets for help on using tickets.