From: Manolo Carrasco Date: Wed, 23 Jun 2010 09:35:22 +0000 (+0000) Subject: - domManip was modifying the set of noded passed. X-Git-Tag: release-1.3.2~677 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=bb60379df2d66648740c7d38d628fde2ff1f98bd;p=gwtquery.git - domManip was modifying the set of noded passed. - appendTo and prependTo was returning an incorrect gquery object. - overloaded the methods appendTo and prependTo in order to accept the same options other methods do. - allow document node to be used in append and prepend methods. - Fixed an issue in ClipAnimation when the element has an absolute position set by css. --- 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 e7a3c67b..b19c1eae 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 @@ -22,6 +22,7 @@ import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArray; import com.google.gwt.core.client.JsArrayString; +import com.google.gwt.dom.client.BodyElement; import com.google.gwt.dom.client.ButtonElement; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; @@ -47,6 +48,8 @@ import com.google.gwt.user.client.Window; */ public class GQuery implements Lazy { + private static final String OLD_DATA_PREFIX = "old-"; + /** * A POJO used to store the top/left CSS positioning values of an element. */ @@ -141,6 +144,8 @@ public class GQuery implements Lazy { } public static final Document document = Document.get(); + public static final BodyElement body = Document.get().getBody(); + public static boolean fxOff = false; public static Class GQUERY = GQuery.class; @@ -170,9 +175,14 @@ public class GQuery implements Lazy { * Wrap a GQuery around an existing element. */ public static GQuery $(Element element) { - JSArray a = JSArray.create(); - a.addNode(element); - return new GQuery(a); + return new GQuery(JSArray.create(element)); + } + + /** + * Wrap a GQuery around an existing node. + */ + public static GQuery $(Node n) { + return new GQuery(JSArray.create(n)); } /** @@ -283,7 +293,7 @@ public class GQuery implements Lazy { plugins.put(plugin, pluginFactory); } - protected static JSArray clean(String elem) { + protected static GQuery clean(String elem) { String tags = elem.trim().toLowerCase(); String preWrap = "", postWrap = ""; int wrapPos = 0; @@ -321,7 +331,7 @@ public class GQuery implements Lazy { n = n.getLastChild(); } // TODO: add fixes for IE TBODY issue - return n.getChildNodes().cast(); + return $((NodeList)n.getChildNodes().cast()); } protected static Object data(Element item, String name, S value) { @@ -457,7 +467,7 @@ public class GQuery implements Lazy { * another if it's not in the page). */ public GQuery after(GQuery query) { - return domManip(query.elements, FUNC_AFTER); + return domManip(query, FUNC_AFTER); } /** @@ -466,7 +476,7 @@ public class GQuery implements Lazy { * another if it's not in the page). */ public GQuery after(Node n) { - return domManip(JSArray.create(n), FUNC_AFTER); + return domManip($(n), FUNC_AFTER); } /** @@ -493,7 +503,7 @@ public class GQuery implements Lazy { * into the document. */ public GQuery append(GQuery query) { - return domManip(query.elements, FUNC_APPEND); + return domManip(query, FUNC_APPEND); } /** @@ -502,7 +512,7 @@ public class GQuery implements Lazy { * into the document. */ public GQuery append(Node n) { - return domManip(JSArray.create(n), FUNC_APPEND); + return domManip($(n), FUNC_APPEND); } /** @@ -515,13 +525,39 @@ public class GQuery implements Lazy { } /** - * Append all of the matched elements to another, specified, set of elements. - * This operation is, essentially, the reverse of doing a regular - * $(A).append(B), in that instead of appending B to A, you're appending A to - * B. + * All of the matched set of elements will be inserted at the end + * of the element(s) specified by the parameter other. + * + * The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular + * $(A).append(B), instead of appending B to A, you're appending A to B. */ public GQuery appendTo(GQuery other) { - return other.append(this); + other.append(this); + return this; + } + + /** + * All of the matched set of elements will be inserted at the end + * of the element(s) specified by the parameter other. + * + * The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular + * $(A).append(B), instead of appending B to A, you're appending A to B. + */ + public GQuery appendTo(Node n) { + $(n).append(this); + return this; + } + + /** + * All of the matched set of elements will be inserted at the end + * of the element(s) specified by the parameter other. + * + * The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular + * $(A).append(B), instead of appending B to A, you're appending A to B. + */ + public GQuery appendTo(String html) { + $(html).append(this); + return this; } /** @@ -591,7 +627,7 @@ public class GQuery implements Lazy { * another if it's not in the page). */ public GQuery before(GQuery query) { - return domManip(query.elements, FUNC_BEFORE); + return domManip(query, FUNC_BEFORE); } /** @@ -600,7 +636,7 @@ public class GQuery implements Lazy { * another if it's not in the page). */ public GQuery before(Node n) { - return domManip(JSArray.create(n), FUNC_BEFORE); + return domManip($(n), FUNC_BEFORE); } /** @@ -1426,7 +1462,7 @@ public class GQuery implements Lazy { */ public GQuery offsetParent() { Element offParent = SelectorEngine. - or(elements.getItem(0).getOffsetParent(), document.getBody()); + or(elements.getItem(0).getOffsetParent(), body); while (offParent != null && !"body".equalsIgnoreCase(offParent.getTagName()) && !"html".equalsIgnoreCase(offParent.getTagName()) && "static". equals(styleImpl.curCSS(offParent, "position", true))) { @@ -1515,7 +1551,7 @@ public class GQuery implements Lazy { * elements. */ public GQuery prepend(GQuery query) { - return domManip(query.elements, FUNC_PREPEND); + return domManip(query, FUNC_PREPEND); } /** @@ -1524,7 +1560,7 @@ public class GQuery implements Lazy { * elements. */ public GQuery prepend(Node n) { - return domManip(JSArray.create(n), FUNC_PREPEND); + return domManip($(n), FUNC_PREPEND); } /** @@ -1535,15 +1571,41 @@ public class GQuery implements Lazy { public GQuery prepend(String html) { return domManip(html, FUNC_PREPEND); } + + /** + * All of the matched set of elements will be inserted at the beginning + * of the element(s) specified by the parameter other. + * + * The operation $(A).prependTo(B) is, essentially, the reverse of doing a regular + * $(A).prepend(B), instead of prepending B to A, you're prepending A to B. + */ + public GQuery prependTo(GQuery other) { + other.prepend(this); + return this; + } /** - * Prepend all of the matched elements to another, specified, set of elements. - * This operation is, essentially, the reverse of doing a regular - * $(A).prepend(B), in that instead of prepending B to A, you're prepending A - * to B. + * All of the matched set of elements will be inserted at the beginning + * of the element(s) specified by the parameter other. + * + * The operation $(A).prependTo(B) is, essentially, the reverse of doing a regular + * $(A).prepend(B), instead of prepending B to A, you're prepending A to B. + */ + public GQuery prependTo(Node n) { + $(n).prepend(this); + return this; + } + + /** + * All of the matched set of elements will be inserted at the beginning + * of the element(s) specified by the parameter other. + * + * The operation $(A).prependTo(B) is, essentially, the reverse of doing a regular + * $(A).prepend(B), instead of prepending B to A, you're prepending A to B. */ - public GQuery prependTo(GQuery elms) { - return elms.prepend(this); + public GQuery prependTo(String html) { + $(html).prepend(this); + return this; } /** @@ -1634,7 +1696,7 @@ public class GQuery implements Lazy { } return this; } - + /** * Replaces the elements matched by the specified selector with the matched * elements. This function is the complement to replaceWith() which does the @@ -1699,7 +1761,7 @@ public class GQuery implements Lazy { public void restoreCssAttrs(String... cssProps) { for (Element e : elements()) { for (String a : cssProps) { - styleImpl.setStyleProperty(e, a, (String) data(e, "old-" + a, null)); + styleImpl.setStyleProperty(e, a, (String) data(e, OLD_DATA_PREFIX + a, null)); } } } @@ -1710,11 +1772,20 @@ public class GQuery implements Lazy { public void saveCssAttrs(String... cssProps) { for (Element e : elements()) { for (String a : cssProps) { - data("old-" + a, styleImpl.curCSS(e, a, false)); + data(OLD_DATA_PREFIX + a, styleImpl.curCSS(e, a, false)); } } } + /** + * Force the current matched set of elements to become + * the specified array of elements. + */ + public GQuery setArray(NodeList nodes){ + this.elements = nodes; + return this; + } + /** * Bind a set of functions to the scroll event of each matched element. * Or trigger the event if no functions are provided. @@ -2338,29 +2409,37 @@ public class GQuery implements Lazy { } } - private GQuery domManip(NodeList nodes, int func) { - for (Element e : elements()) { - for (int i = 0; i < nodes.getLength(); i++) { - Node n = nodes.getItem(i); - if (nodes.getLength() > 1) { + private GQuery domManip(GQuery g, int func) { + JSArray newNodes = JSArray.create(); + for (int i = 0; i < elements().length; i++) { + Element e = elements()[i]; + if (document.equals(e)){ + e = body; + } + for (int j = 0; j < g.size(); j++) { + Node n = g.get(j); + if (g.size() > 1) { n = n.cloneNode(true); } switch (func) { case FUNC_PREPEND: - e.insertBefore(n, e.getFirstChild()); + newNodes.addNode(e.insertBefore(n, e.getFirstChild())); break; case FUNC_APPEND: - e.appendChild(n); + newNodes.addNode(e.appendChild(n)); break; case FUNC_AFTER: - e.getParentNode().insertBefore(n, e.getNextSibling()); + newNodes.addNode(e.getParentNode().insertBefore(n, e.getNextSibling())); break; case FUNC_BEFORE: - e.getParentNode().insertBefore(n, e); + newNodes.addNode(e.getParentNode().insertBefore(n, e)); break; } } } + if (newNodes.size() > 0) { + g.setArray(newNodes); + } return this; } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ClipAnimation.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ClipAnimation.java index fc701a31..6aa219e6 100755 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ClipAnimation.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ClipAnimation.java @@ -91,7 +91,7 @@ public class ClipAnimation extends Animation { public void onStart() { g.show(); g.saveCssAttrs(attrsToSave); - if (!"absolute".equalsIgnoreCase(g.css("position"))) { + if (!"absolute".equalsIgnoreCase(g.css("position", true))) { g.css("position", "absolute"); g.css("top", g.offset().top + "px"); g.css("left", g.offset().left + "px"); 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 df34fb8e..9bbba14d 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 @@ -17,6 +17,7 @@ package com.google.gwt.query.client; import static com.google.gwt.query.client.GQuery.$; import static com.google.gwt.query.client.GQuery.$$; +import static com.google.gwt.query.client.GQuery.document; import com.google.gwt.dom.client.Element; import com.google.gwt.junit.client.GWTTestCase; @@ -310,8 +311,17 @@ public class GQueryCoreTest extends GWTTestCase { // appendTo() expected = "

I would like to say: Hello

"; $(e).html(bTxt + pTxt); - $("b", e).appendTo($("p", e)); + GQuery g = $("b", e).appendTo($("p", e)); assertHtmlEquals(expected, $(e).html()); + assertHtmlEquals("Hello", g.toString()); + // document is a valid node, actually it is substituted by body + g.appendTo(document); + expected = "

I would like to say:

"; + assertHtmlEquals(expected, $(e).html()); + g.remove(); + // Check that the new elements are returned and can be modified + $("
Hello
").appendTo(e).css("color", "white"); + assertEquals("white", $("#mid").css("color")); // prepend() expected = "

HelloI would like to say:

"; @@ -324,6 +334,9 @@ public class GQueryCoreTest extends GWTTestCase { $(e).html(bTxt + pTxt); $("b", e).prependTo($("p", e)); assertHtmlEquals(expected, $(e).html()); + // Check that the new elements are returned and can be modified + $("
Hello
").prependTo(e).css("color", "yellow"); + assertEquals("yellow", $("#mid").css("color")); // prependTo() expected = "Hello

HelloI would like to say:

"; @@ -378,6 +391,35 @@ public class GQueryCoreTest extends GWTTestCase { $(e).html(bTxt + pTxt); $("p", e).after($("b", e).clone().get(0)); assertHtmlEquals(expected, $(e).html()); + + // The set of elements should be the same after the manipulation + String content = "s"; + expected = "

p

"; + GQuery g1 = $(content); + GQuery g2 = $(expected); + $(e).html("").append(g1); + assertEquals(1, g1.size()); + assertEquals(content, g1.toString()); + + $(g1).append(g2); + assertEquals(1, g1.size()); + assertEquals(1, g2.size()); + assertEquals(expected, g2.toString()); + + $(g1).prepend(g2); + assertEquals(1, g1.size()); + assertEquals(1, g2.size()); + assertEquals(expected, g2.toString()); + + $(g1).after(g2); + assertEquals(1, g1.size()); + assertEquals(1, g2.size()); + assertEquals(expected, g2.toString()); + + $(g1).before(g2); + assertEquals(1, g1.size()); + assertEquals(1, g2.size()); + assertEquals(expected, g2.toString()); } public void testOpacity() { @@ -419,11 +461,6 @@ public class GQueryCoreTest extends GWTTestCase { expected = "

Hello

"; assertHtmlEquals(expected, $("p", e).filter(".selected")); - // filter() - // Commented because GQuery doesn't support this syntax yet - // expected = "

Hello

"; - // assertHtmlEquals(expected, $("p", e).filter(".selected, :first").toString()); - // not() expected = "

First

How are you?

"; assertEquals(2, $("p", e).not(".selected").size());