From 726d9ce3c8e66dd0177966ad0a837ff17ce236d2 Mon Sep 17 00:00:00 2001 From: jdramaix Date: Mon, 5 Nov 2012 15:49:27 +0100 Subject: [PATCH] fix issue with size computing when element is not attached --- .../query/client/impl/DocumentStyleImpl.java | 30 ++++++++++++++----- .../google/gwt/query/client/js/JsUtils.java | 11 +++++-- .../gwt/query/client/GQueryCoreTestGwt.java | 12 ++++++++ 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java index 6fcfca31..d57c5906 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java @@ -15,6 +15,7 @@ */ package com.google.gwt.query.client.impl; +import static com.google.gwt.query.client.GQuery.$; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.Node; @@ -95,11 +96,11 @@ public class DocumentStyleImpl { String ret = elem.getStyle().getProperty(name); if (force) { - // If the element is dettached to the DOM we attach temporary to it - Element parent = null; - if (JsUtils.isDettached(elem)) { - parent = elem.getParentElement(); - Document.get().getBody().appendChild(elem); + + Element toDetach = null; + if (JsUtils.isDetached(elem)) { + // If the element is detached to the DOM we attach temporary to it + toDetach = attachTemporary(elem); } if (sizeRegex.test(name)) { @@ -110,15 +111,28 @@ public class DocumentStyleImpl { ret = getComputedStyle(elem, JsUtils.hyphenize(name), name, null); } - // If the element had a parent previously to be attached, append to it. - if (parent != null) { - parent.appendChild(elem); + // If the element was previously attached, detached it. + if (toDetach != null) { + toDetach.removeFromParent(); } } return ret == null ? "" : ret; } + private Element attachTemporary(Element elem) { + Element lastParent = $(elem).parents().last().get(0); + + if (lastParent == null){ + //the element itself is detached + lastParent = elem; + } + + Document.get().getBody().appendChild(lastParent); + + return lastParent; +} + /** * Fix style property names. */ diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java index 8a6c589b..787eb07c 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java @@ -276,9 +276,16 @@ public class JsUtils { }-*/; /** - * Return whether a node is dettached to the dom + * Return whether a node is detached to the dom + * Be careful : This method works only on node that should be inserted within the body node. */ - public static boolean isDettached(Node n) { + public static boolean isDetached(Node n) { + assert n != null; + + if ("html".equalsIgnoreCase(n.getNodeName())){ + return true; + } + return !getOwnerDocument(n).getBody().isOrHasChild(n); } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java index b14533c9..ef8a2643 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java @@ -1921,5 +1921,17 @@ public class GQueryCoreTestGwt extends GWTTestCase { Element span = $("span", e).get(0); assertEquals("l3", $("li", e).has(span).id()); } + + public void testDetachedElement(){ + GQuery view = $("
"); + + int viewWidth = view.width(); + + assertEquals(300, viewWidth); + + int innerViewWidth = view.children().width(); + + assertEquals(150, innerViewWidth); + } } -- 2.39.5