Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9416 (closed defect: fixed)

Opened 3 years ago

Last modified 2 years ago

[PATCH] [TEST] "Unspecified Error" in IE with offsetParent

Reported by: psiborg Assigned to: sam
Priority: normal Milestone: 2.x
Component: Prototype Version: edge
Severity: normal Keywords: IE
Cc: sur

Description

I'm using script.aculo.us 1.7.1 beta 3 with prototype 1.5.1.1, and I've found a bug when I use draggables in Internet Explorer. If I modify the DOM tree and then try to drag an object, I get an "Unspecified Error" at line 3088 (in prototype.js version 1.5.1.1).

This works fine on other browsers like Firefox and Opera. I think the problem is that IE doesn't return null.

After searching for a solution, I came across this post (http://forums.asp.net/p/1038609/1444564.aspx) which suggests wrapping the offsetParent code in a try...catch block. So I tried it and my code now works in IE.

  cumulativeOffset: function(element) {
    var valueT = 0, valueL = 0;
    try {
        do {
          valueT += element.offsetTop  || 0;
          valueL += element.offsetLeft || 0;
          element = element.offsetParent;
        } while (element);
    }
    catch( ex ) {
    }
    return [valueL, valueT];
  },

I'm not sure if this is the best solution, but it seems like the simplest. :)

I suspect there are other places in the library that this fix could apply to as well.

Attachments

offsetParent_patch_test.diff (2.6 kB) - added by kangax on 02/01/08 02:24:43.
offsetParent_wrap.diff (2.7 kB) - added by kangax on 02/01/08 12:54:04.
offsetParent_wrap_more_tests.diff (3.1 kB) - added by kangax on 02/01/08 13:06:01.

Change History

08/29/07 19:11:12 changed by sur

  • cc set to sur.
  • keywords set to IE.

02/01/08 02:24:15 changed by kangax

  • summary changed from "Unspecified Error" in IE with offsetParent to [PATCH] [TEST] "Unspecified Error" in IE with offsetParent.

Phew....

getOffsetParent, cumulativeOffset, positionedOffset and viewportOffset - all of these appeared to be vulnerable. This was also affecting clonePosition (which relies on getOffsetParent).

The issue is amazingly simple to reproduce:

new Element('div').cumulativeOffset(); // or any of the above mentioned methods

throws Error in IE6, returns "undefined" in IE7

Patch and tests are attached.

I believe this also closes ~5 related tickets.

02/01/08 02:24:43 changed by kangax

  • attachment offsetParent_patch_test.diff added.

02/01/08 12:54:04 changed by kangax

  • attachment offsetParent_wrap.diff added.

02/01/08 13:05:31 changed by kangax

I just realized how unnecessary it is to slow down all these methods because of an edge case. Alternative patch is using wrap to augment IE specific behavior. Also testing for proper top/left properties.

02/01/08 13:06:01 changed by kangax

  • attachment offsetParent_wrap_more_tests.diff added.

02/03/08 22:29:34 changed by tobie

  • status changed from new to closed.
  • resolution set to fixed.

(In [8799]) prototype: Prevent Element#cumulativeOffset, Element#getOffsetParent, Element#positionedOffset, Element#viewportOffset and Element#clonePosition from throwing an error in IE when called on a parent-less element. Closes #9416, #10192, #10248.