Opened 12 years ago

Closed 11 years ago

#6657 closed defect (fixed)

dojo.getComputedStyle functions poorly in IE6 and FF when given the document object

Reported by: Joseph Scheuhammer Owned by: sjmiles
Priority: high Milestone: 1.2
Component: HTML Version: 1.1.0
Keywords: Cc: Douglas Hays, davidb, Becky Gibson
Blocked By: Blocking:

Description

The document object is sometimes passed to dojo.getComputedStyle(). That causes a null value to be returned, or an exception to be thrown, depending on the browser.

getComputedStyle() is actually defined as gcs() in .../dojo/_base/html.js, and is defined differently for different browsers.

For IE6, the return value is node.currentStyle, but that is null when node is the document. I suggest returning an empty object instead.

For FF2 and FF3, the style is retrieved via document.defaultView.getComputedStyle(node). When node is the document, an exception is thrown: "[Exception:..."Could not convert JavaScript argument" ...]

This is because defaultView.getComputedStyle() takes an Element as its argument, and document is not an element.

A possible solution is to catch the exception and return an empty object.

The suggested changes are given in the attached patch file.

Another possibility is to restrict the argument type to Element when invoking gcs(), but I'm unsure how much other code is affected by that change.

Attachments (1)

6657.patch (534 bytes) - added by Joseph Scheuhammer 12 years ago.
Patch to IE and FF definitions of gcs().

Download all attachments as: .zip

Change History (3)

Changed 12 years ago by Joseph Scheuhammer

Attachment: 6657.patch added

Patch to IE and FF definitions of gcs().

comment:1 Changed 12 years ago by sjmiles

Status: newassigned

The contract is that input to getComputedStyle must be Element. That the function cannot handle bogus input is not, strictly speaking, a defect.

This came up once before and our solution was to fix the calling code not to try to compute a style on document.

If it's really clear that this is going to keep happening (I assume because document is the ultimate parentNode for the DOM) then I suppose we should protect against this input.

The suggested IE fix is obviously really simple, although it breaks the previous contract on IE that always returned a CSSStyleDeclaration object (and never {}). The Safari branch already does return {} sometimes, so probably this is a non-issue.

I generally think that try..catch is a bad way to handle edge conditions. Perhaps on FF we should actually try to detect document as input.

comment:2 Changed 11 years ago by sjmiles

Resolution: fixed
Status: assignedclosed

(In [14385]) Return {} if input to getComputedStyle is not Element. Fixes #6657. !strict

Note: See TracTickets for help on using tickets.