From 9251bdbfa822a7e17af7a27291b84b68a3d6da45 Mon Sep 17 00:00:00 2001 From: Julien Dramaix Date: Tue, 11 Jan 2011 17:37:29 +0000 Subject: [PATCH] Fix issue 57 --- .../com/google/gwt/query/client/GQuery.java | 53 ++++++++++++++++--- .../gwt/query/client/GQueryCoreTest.java | 14 +++++ 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index a14216d6..b9cea84d 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -19,11 +19,6 @@ import static com.google.gwt.query.client.plugins.Effects.Effects; import static com.google.gwt.query.client.plugins.Events.Events; import static com.google.gwt.query.client.plugins.Widgets.Widgets; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; - import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArray; @@ -38,8 +33,8 @@ import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.NodeList; import com.google.gwt.dom.client.OptionElement; import com.google.gwt.dom.client.SelectElement; -import com.google.gwt.dom.client.Style.Display; import com.google.gwt.dom.client.TextAreaElement; +import com.google.gwt.dom.client.Style.Display; import com.google.gwt.query.client.css.CssProperty; import com.google.gwt.query.client.css.Length; import com.google.gwt.query.client.css.Percentage; @@ -53,6 +48,11 @@ import com.google.gwt.user.client.EventListener; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Widget; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; + /** * Gwt Query is a GWT clone of the popular jQuery library. */ @@ -1720,8 +1720,45 @@ public class GQuery implements Lazy { * accurate calculations make sure to use pixel values for margins, borders * and padding. This method only works with visible elements. */ - public com.google.gwt.query.client.GQuery.Offset position() { - return new Offset(get(0).getOffsetLeft(), get(0).getOffsetTop()); + public Offset position() { + Element element = get(0); + if (element == null) { + return null; + } + // Get *real* offsetParent + Element offsetParent = element.getOffsetParent(); + // Get correct offsets + Offset offset = offset(); + Offset parentOffset = null; + if ("body".equalsIgnoreCase(offsetParent.getNodeName()) + || "html".equalsIgnoreCase(offsetParent.getNodeName())) { + parentOffset = new Offset(0, 0); + } else { + parentOffset = $(offsetParent).offset(); + } + + // Subtract element margins + int topMargin = (int) GQUtils.cur(element, "marginTop", true); + // When margin-left = auto, Safari and chrome return a value while IE and + // Firefox return 0 + // force the margin-left to 0 if margin-left = auto. + int leftMargin = 0; + if (!"auto".equals(element.getStyle().getMarginLeft())) { + leftMargin = (int) GQUtils.cur(element, "marginLeft", true); + } + + offset = offset.add(-leftMargin, -topMargin); + + // Add offsetParent borders + int parentOffsetBorderTop = (int) GQUtils.cur(offsetParent, + "borderTopWidth", true); + int parentOffsetBorderLeft = (int) GQUtils.cur(offsetParent, + "borderLeftWidth", true); + parentOffset = parentOffset.add(parentOffsetBorderLeft, + parentOffsetBorderTop); + + // Subtract the two offsets + return offset.add(-parentOffset.left, -parentOffset.top); } /** diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java index fd9d7dcc..e287f294 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java @@ -460,6 +460,20 @@ public class GQueryCoreTest extends GWTTestCase { assertEquals("0.4", g.css("opacity", true)); } + public void testPosition(){ + $(e).html("
test
"); + GQuery g = $("#child"); + assertEquals(20, g.position().left); + assertEquals(20, g.position().top); + + $(e).html("
test
"); + g = $("#child"); + assertEquals(35, g.position().left); + assertEquals(15, g.position().top); + + + } + public void testProperties() { Properties p = $$("border:'1px solid black'"); assertEquals(1, p.keys().length); -- 2.39.5