]> source.dussan.org Git - gwtquery.git/commitdiff
- domManip was modifying the set of noded passed.
authorManolo Carrasco <manolo@apache.org>
Wed, 23 Jun 2010 09:35:22 +0000 (09:35 +0000)
committerManolo Carrasco <manolo@apache.org>
Wed, 23 Jun 2010 09:35:22 +0000 (09:35 +0000)
- 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.

gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ClipAnimation.java
gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java

index e7a3c67b498404c1f06d28fa597b7842a97dce1d..b19c1eae6570b59d3e58658bee6a42d8eabf554f 100644 (file)
@@ -22,6 +22,7 @@ import com.google.gwt.core.client.GWT;
 import com.google.gwt.core.client.JavaScriptObject;\r
 import com.google.gwt.core.client.JsArray;\r
 import com.google.gwt.core.client.JsArrayString;\r
+import com.google.gwt.dom.client.BodyElement;\r
 import com.google.gwt.dom.client.ButtonElement;\r
 import com.google.gwt.dom.client.Document;\r
 import com.google.gwt.dom.client.Element;\r
@@ -47,6 +48,8 @@ import com.google.gwt.user.client.Window;
  */\r
 public class GQuery implements Lazy<GQuery, LazyGQuery> {\r
 \r
+  private static final String OLD_DATA_PREFIX = "old-";\r
+\r
   /**\r
    * A POJO used to store the top/left CSS positioning values of an element.\r
    */\r
@@ -141,6 +144,8 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
   \r
   public static final Document document = Document.get();\r
+  public static final BodyElement body = Document.get().getBody();\r
+  \r
   public static boolean fxOff = false;\r
 \r
   public static Class<GQuery> GQUERY = GQuery.class;\r
@@ -170,9 +175,14 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * Wrap a GQuery around an existing element.\r
    */\r
   public static GQuery $(Element element) {\r
-    JSArray a = JSArray.create();\r
-    a.addNode(element);\r
-    return new GQuery(a);\r
+    return new GQuery(JSArray.create(element));\r
+  }\r
+\r
+  /**\r
+   * Wrap a GQuery around an existing node.\r
+   */\r
+  public static GQuery $(Node n) {\r
+    return new GQuery(JSArray.create(n));\r
   }\r
 \r
   /**\r
@@ -283,7 +293,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     plugins.put(plugin, pluginFactory);\r
   }\r
 \r
-  protected static JSArray clean(String elem) {\r
+  protected static GQuery clean(String elem) {\r
     String tags = elem.trim().toLowerCase();\r
     String preWrap = "", postWrap = "";\r
     int wrapPos = 0;\r
@@ -321,7 +331,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
       n = n.getLastChild();\r
     }\r
     // TODO: add fixes for IE TBODY issue\r
-    return n.getChildNodes().cast();\r
+    return $((NodeList<Element>)n.getChildNodes().cast());\r
   }\r
 \r
   protected static <S> Object data(Element item, String name, S value) {\r
@@ -457,7 +467,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * another if it's not in the page).\r
    */\r
   public GQuery after(GQuery query) {\r
-    return domManip(query.elements, FUNC_AFTER);\r
+    return domManip(query, FUNC_AFTER);\r
   }\r
 \r
   /**\r
@@ -466,7 +476,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * another if it's not in the page).\r
    */\r
   public GQuery after(Node n) {\r
-    return domManip(JSArray.create(n), FUNC_AFTER);\r
+    return domManip($(n), FUNC_AFTER);\r
   }\r
 \r
   /**\r
@@ -493,7 +503,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * into the document.\r
    */\r
   public GQuery append(GQuery query) {\r
-    return domManip(query.elements, FUNC_APPEND);\r
+    return domManip(query, FUNC_APPEND);\r
   }\r
 \r
   /**\r
@@ -502,7 +512,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * into the document.\r
    */\r
   public GQuery append(Node n) {\r
-    return domManip(JSArray.create(n), FUNC_APPEND);\r
+    return domManip($(n), FUNC_APPEND);\r
   }\r
 \r
   /**\r
@@ -515,13 +525,39 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   /**\r
-   * Append all of the matched elements to another, specified, set of elements.\r
-   * This operation is, essentially, the reverse of doing a regular\r
-   * $(A).append(B), in that instead of appending B to A, you're appending A to\r
-   * B.\r
+   * All of the matched set of elements will be inserted at the end \r
+   * of the element(s) specified by the parameter other.\r
+   * \r
+   * The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular\r
+   * $(A).append(B), instead of appending B to A, you're appending A to B.\r
    */\r
   public GQuery appendTo(GQuery other) {\r
-    return other.append(this);\r
+    other.append(this);\r
+    return this;\r
+  }\r
+\r
+  /**\r
+   * All of the matched set of elements will be inserted at the end \r
+   * of the element(s) specified by the parameter other.\r
+   * \r
+   * The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular\r
+   * $(A).append(B), instead of appending B to A, you're appending A to B.\r
+   */\r
+  public GQuery appendTo(Node n) {\r
+    $(n).append(this);\r
+    return this;\r
+  }\r
+\r
+  /**\r
+   * All of the matched set of elements will be inserted at the end \r
+   * of the element(s) specified by the parameter other.\r
+   * \r
+   * The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular\r
+   * $(A).append(B), instead of appending B to A, you're appending A to B.\r
+   */\r
+  public GQuery appendTo(String html) {\r
+    $(html).append(this);\r
+    return this;\r
   }\r
 \r
   /**\r
@@ -591,7 +627,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * another if it's not in the page).\r
    */\r
   public GQuery before(GQuery query) {\r
-    return domManip(query.elements, FUNC_BEFORE);\r
+    return domManip(query, FUNC_BEFORE);\r
   }\r
 \r
   /**\r
@@ -600,7 +636,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * another if it's not in the page).\r
    */\r
   public GQuery before(Node n) {\r
-    return domManip(JSArray.create(n), FUNC_BEFORE);\r
+    return domManip($(n), FUNC_BEFORE);\r
   }\r
 \r
   /**\r
@@ -1426,7 +1462,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    */\r
   public GQuery offsetParent() {\r
     Element offParent = SelectorEngine.\r
-        or(elements.getItem(0).getOffsetParent(), document.getBody());\r
+        or(elements.getItem(0).getOffsetParent(), body);\r
     while (offParent != null && !"body".equalsIgnoreCase(offParent.getTagName())\r
         && !"html".equalsIgnoreCase(offParent.getTagName()) && "static".\r
         equals(styleImpl.curCSS(offParent, "position", true))) {\r
@@ -1515,7 +1551,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * elements.\r
    */\r
   public GQuery prepend(GQuery query) {\r
-    return domManip(query.elements, FUNC_PREPEND);\r
+    return domManip(query, FUNC_PREPEND);\r
   }\r
 \r
   /**\r
@@ -1524,7 +1560,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * elements.\r
    */\r
   public GQuery prepend(Node n) {\r
-    return domManip(JSArray.create(n), FUNC_PREPEND);\r
+    return domManip($(n), FUNC_PREPEND);\r
   }\r
 \r
   /**\r
@@ -1535,15 +1571,41 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   public GQuery prepend(String html) {\r
     return domManip(html, FUNC_PREPEND);\r
   }\r
+  \r
+  /**\r
+   * All of the matched set of elements will be inserted at the beginning \r
+   * of the element(s) specified by the parameter other.\r
+   * \r
+   * The operation $(A).prependTo(B) is, essentially, the reverse of doing a regular\r
+   * $(A).prepend(B), instead of prepending B to A, you're prepending A to B.\r
+   */\r
+  public GQuery prependTo(GQuery other) {\r
+    other.prepend(this);\r
+    return this;\r
+  }\r
 \r
   /**\r
-   * Prepend all of the matched elements to another, specified, set of elements.\r
-   * This operation is, essentially, the reverse of doing a regular\r
-   * $(A).prepend(B), in that instead of prepending B to A, you're prepending A\r
-   * to B.\r
+   * All of the matched set of elements will be inserted at the beginning \r
+   * of the element(s) specified by the parameter other.\r
+   * \r
+   * The operation $(A).prependTo(B) is, essentially, the reverse of doing a regular\r
+   * $(A).prepend(B), instead of prepending B to A, you're prepending A to B.\r
+   */\r
+  public GQuery prependTo(Node n) {\r
+    $(n).prepend(this);\r
+    return this;\r
+  }\r
+\r
+  /**\r
+   * All of the matched set of elements will be inserted at the beginning \r
+   * of the element(s) specified by the parameter other.\r
+   * \r
+   * The operation $(A).prependTo(B) is, essentially, the reverse of doing a regular\r
+   * $(A).prepend(B), instead of prepending B to A, you're prepending A to B.\r
    */\r
-  public GQuery prependTo(GQuery elms) {\r
-    return elms.prepend(this);\r
+  public GQuery prependTo(String html) {\r
+    $(html).prepend(this);\r
+    return this;\r
   }\r
 \r
   /**\r
@@ -1634,7 +1696,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     }\r
     return this;\r
   }\r
-\r
+  \r
   /**\r
    * Replaces the elements matched by the specified selector with the matched\r
    * elements. This function is the complement to replaceWith() which does the\r
@@ -1699,7 +1761,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   public void restoreCssAttrs(String... cssProps) {\r
     for (Element e : elements()) {\r
       for (String a : cssProps) {\r
-        styleImpl.setStyleProperty(e, a, (String) data(e, "old-" + a, null));\r
+        styleImpl.setStyleProperty(e, a, (String) data(e, OLD_DATA_PREFIX + a, null));\r
       }\r
     }\r
   }\r
@@ -1710,11 +1772,20 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   public void saveCssAttrs(String... cssProps) {\r
     for (Element e : elements()) {\r
       for (String a : cssProps) {\r
-        data("old-" + a, styleImpl.curCSS(e, a, false));\r
+        data(OLD_DATA_PREFIX + a, styleImpl.curCSS(e, a, false));\r
       }\r
     }\r
   }\r
 \r
+  /**\r
+   * Force the current matched set of elements to become\r
+   * the specified array of elements.\r
+   */\r
+  public GQuery setArray(NodeList<Element> nodes){\r
+    this.elements = nodes;\r
+    return this;\r
+  }\r
+\r
   /**\r
    * Bind a set of functions to the scroll event of each matched element.\r
    * Or trigger the event if no functions are provided.\r
@@ -2338,29 +2409,37 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     }\r
   }\r
 \r
-  private GQuery domManip(NodeList<?> nodes, int func) {\r
-    for (Element e : elements()) {\r
-      for (int i = 0; i < nodes.getLength(); i++) {\r
-        Node n = nodes.getItem(i);\r
-        if (nodes.getLength() > 1) {\r
+  private GQuery domManip(GQuery g, int func) {\r
+    JSArray newNodes = JSArray.create();\r
+    for (int i = 0; i < elements().length; i++) {\r
+      Element e = elements()[i];\r
+      if (document.equals(e)){\r
+        e = body;\r
+      }\r
+      for (int j = 0; j < g.size(); j++) {\r
+        Node n = g.get(j);\r
+        if (g.size() > 1) {\r
           n = n.cloneNode(true);\r
         }\r
         switch (func) {\r
           case FUNC_PREPEND:\r
-            e.insertBefore(n, e.getFirstChild());\r
+            newNodes.addNode(e.insertBefore(n, e.getFirstChild()));\r
             break;\r
           case FUNC_APPEND:\r
-            e.appendChild(n);\r
+            newNodes.addNode(e.appendChild(n));\r
             break;\r
           case FUNC_AFTER:\r
-            e.getParentNode().insertBefore(n, e.getNextSibling());\r
+            newNodes.addNode(e.getParentNode().insertBefore(n, e.getNextSibling()));\r
             break;\r
           case FUNC_BEFORE:\r
-            e.getParentNode().insertBefore(n, e);\r
+            newNodes.addNode(e.getParentNode().insertBefore(n, e));\r
             break;\r
         }\r
       }\r
     }\r
+    if (newNodes.size() > 0) {\r
+      g.setArray(newNodes);\r
+    }\r
     return this;\r
   }\r
 \r
index fc701a314a4ce95a7f09c0042518a8c970b872aa..6aa219e6df368335f2cd16c973bb742e545a6427 100755 (executable)
@@ -91,7 +91,7 @@ public class ClipAnimation extends Animation {
   public void onStart() {\r
     g.show();\r
     g.saveCssAttrs(attrsToSave);\r
-    if (!"absolute".equalsIgnoreCase(g.css("position"))) {\r
+    if (!"absolute".equalsIgnoreCase(g.css("position", true))) {\r
       g.css("position", "absolute");\r
       g.css("top", g.offset().top + "px");\r
       g.css("left", g.offset().left + "px");\r
index df34fb8e4f788289194426e563f2281156a784d0..9bbba14d32cfb2022f2733ad32c39e97de632ed2 100644 (file)
@@ -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 = "<p>I would like to say: <b>Hello</b></p>";
     $(e).html(bTxt + pTxt);
-    $("b", e).appendTo($("p", e));
+    GQuery g = $("b", e).appendTo($("p", e));
     assertHtmlEquals(expected, $(e).html());
+    assertHtmlEquals("<b>Hello</b>", g.toString());
+    // document is a valid node, actually it is substituted by body
+    g.appendTo(document);
+    expected = "<p>I would like to say: </p>";
+    assertHtmlEquals(expected, $(e).html());
+    g.remove();
+    // Check that the new elements are returned and can be modified
+    $("<div id='mid'>Hello</div>").appendTo(e).css("color", "white");
+    assertEquals("white", $("#mid").css("color"));
 
     // prepend()
     expected = "<p><b>Hello</b>I would like to say: </p>";
@@ -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
+    $("<div id='mid'>Hello</div>").prependTo(e).css("color", "yellow");
+    assertEquals("yellow", $("#mid").css("color"));
 
     // prependTo()
     expected = "<b>Hello</b><p><b>Hello</b>I would like to say: </p>";
@@ -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 = "<span>s</span>"; 
+    expected = "<p>p</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 = "<p class=\"selected\">Hello</p>";
     assertHtmlEquals(expected, $("p", e).filter(".selected"));
 
-    // filter()
-    // Commented because GQuery doesn't support this syntax yet
-    // expected = "<p class=\"selected\">Hello</p>";
-    // assertHtmlEquals(expected, $("p", e).filter(".selected, :first").toString());
-
     // not()
     expected = "<p>First</p><p>How are you?</p>";
     assertEquals(2, $("p", e).not(".selected").size());