Fixes Issue #575.
Removes DOMImplMozilla's reliance on getBoxObjectFor in getAbsoluteTop/Left.
getBoxObjectFor changed between 1.0.7 and current Mozilla making it hard
to provide consistent behavior in hosted and web modes. Also, the new
implementation returns an offset that is incompatible with our convention
for gwtAbsoluteTop/Left. This also adds a simple test to cover
getAbsoluteTop/Left.
Review by: jgw
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1012 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/impl/DOMImplMozilla.java b/user/src/com/google/gwt/user/client/impl/DOMImplMozilla.java
index b203e1b..31d61c8 100644
--- a/user/src/com/google/gwt/user/client/impl/DOMImplMozilla.java
+++ b/user/src/com/google/gwt/user/client/impl/DOMImplMozilla.java
@@ -52,37 +52,6 @@
return evt.detail;
}-*/;
- public native int getAbsoluteLeft(Element elem) /*-{
- var left = $doc.getBoxObjectFor(elem).x;
- var parent = elem;
-
- while (parent) {
- // Sometimes get NAN.
- if (parent.scrollLeft > 0) {
- left = left - parent.scrollLeft;
- }
- parent = parent.parentNode;
- }
-
- // Must cover both Standard and Quirks mode.
- return left + $doc.body.scrollLeft + $doc.documentElement.scrollLeft;
- }-*/;
-
- public native int getAbsoluteTop(Element elem) /*-{
- var top = $doc.getBoxObjectFor(elem).y;
- var parent = elem;
- while (parent) {
- // Sometimes get NAN.
- if (parent.scrollTop > 0) {
- top -= parent.scrollTop;
- }
- parent = parent.parentNode;
- }
-
- // Must cover both Standard and Quirks mode.
- return top + $doc.body.scrollTop + $doc.documentElement.scrollTop;
- }-*/;
-
public native int getChildIndex(Element parent, Element toFind) /*-{
var count = 0, child = parent.firstChild;
while (child) {
diff --git a/user/test/com/google/gwt/user/client/ui/DOMTest.java b/user/test/com/google/gwt/user/client/ui/DOMTest.java
index 865d0d1..9613a94 100644
--- a/user/test/com/google/gwt/user/client/ui/DOMTest.java
+++ b/user/test/com/google/gwt/user/client/ui/DOMTest.java
@@ -16,7 +16,9 @@
package com.google.gwt.user.client.ui;
import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
+import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Element;
/**
@@ -55,6 +57,39 @@
}
/**
+ * Tests {@link DOM#getAbsoluteLeft(Element)} and
+ * {@link DOM#getAbsoluteTop(Element)}.
+ */
+ public void testGetAbsolutePosition() {
+ final int border = 8;
+ final int margin = 9;
+ final int padding = 10;
+
+ final int top = 15;
+ final int left = 14;
+
+ final Element elem = DOM.createDiv();
+ DOM.appendChild(RootPanel.getBodyElement(), elem);
+
+ DOM.setStyleAttribute(elem, "position", "absolute");
+ DOM.setStyleAttribute(elem, "border", border + "px");
+ DOM.setStyleAttribute(elem, "padding", padding + "px");
+ DOM.setStyleAttribute(elem, "margin", margin + "px");
+
+ DOM.setStyleAttribute(elem, "top", top + "px");
+ DOM.setStyleAttribute(elem, "left", left + "px");
+
+ delayTestFinish(1000);
+ DeferredCommand.addCommand(new Command() {
+ public void execute() {
+ assertEquals(top + margin, DOM.getAbsoluteTop(elem));
+ assertEquals(left + margin, DOM.getAbsoluteLeft(elem));
+ finishTest();
+ }
+ });
+ }
+
+ /**
* Tests the ability to do a parent-ward walk in the DOM.
*/
public void testGetParent() {
@@ -71,7 +106,7 @@
// If we get here, we pass, because we encountered no errors going to the
// top of the parent hierarchy.
}
-
+
/**
* Tests {@link DOM#insertChild(Element, Element, int)}.
*