diff --git a/src/dom/dom.js b/src/dom/dom.js index 6ab0241..7f0c054 100644 --- a/src/dom/dom.js +++ b/src/dom/dom.js @@ -1190,11 +1190,13 @@ Element.Methods = { **/ cumulativeOffset: function(element) { var valueT = 0, valueL = 0; - do { - valueT += element.offsetTop || 0; - valueL += element.offsetLeft || 0; - element = element.offsetParent; - } while (element); + if (element.parentNode) { + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + element = element.offsetParent; + } while (element); + } return Element._returnOffset(valueL, valueT); }, @@ -1558,8 +1560,7 @@ else if (Prototype.Browser.IE) { function(proceed, element) { element = $(element); // IE throws an error if element is not in document - try { element.offsetParent } - catch(e) { return $(document.body) } + if (!element.parentNode) return $(document.body); var position = element.getStyle('position'); if (position !== 'static') return proceed(element); element.setStyle({ position: 'relative' }); @@ -1573,8 +1574,7 @@ else if (Prototype.Browser.IE) { Element.Methods[method] = Element.Methods[method].wrap( function(proceed, element) { element = $(element); - try { element.offsetParent } - catch(e) { return Element._returnOffset(0,0) } + if (!element.parentNode) return Element._returnOffset(0, 0); var position = element.getStyle('position'); if (position !== 'static') return proceed(element); // Trigger hasLayout on the offset parent so that IE6 reports @@ -1590,14 +1590,6 @@ else if (Prototype.Browser.IE) { ); }); - Element.Methods.cumulativeOffset = Element.Methods.cumulativeOffset.wrap( - function(proceed, element) { - try { element.offsetParent } - catch(e) { return Element._returnOffset(0,0) } - return proceed(element); - } - ); - Element.Methods.getStyle = function(element, style) { element = $(element); style = (style == 'float' || style == 'cssFloat') ? 'styleFloat' : style.camelize(); diff --git a/test/unit/dom_test.js b/test/unit/dom_test.js index 3299584..deb2026 100644 --- a/test/unit/dom_test.js +++ b/test/unit/dom_test.js @@ -1322,6 +1322,10 @@ new Test.Unit.Runner({ this.assertEnumEqual([0,0], offset); this.assertIdentical(0, offset.top); this.assertIdentical(0, offset.left); + + var innerEl = new Element('div'), outerEl = new Element('div'); + outerEl.appendChild(innerEl); + this.assertEnumEqual([0,0], innerEl.cumulativeOffset()); }, testViewportOffset: function() {