]> source.dussan.org Git - gwtquery.git/commitdiff
correct behavior of replaceWith and replaceAll methods !
authorJulien Dramaix <julien.dramaix@gmail.com>
Fri, 4 Mar 2011 16:00:03 +0000 (16:00 +0000)
committerJulien Dramaix <julien.dramaix@gmail.com>
Fri, 4 Mar 2011 16:00:03 +0000 (16:00 +0000)
improve javadocs

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

index 9f95680a09b90675725a5a8ba9edaf110142c05a..be78d55131677a74ec78fcfffbadb5e211a1c77d 100644 (file)
@@ -18,11 +18,6 @@ package com.google.gwt.query.client;
 import static com.google.gwt.query.client.plugins.Effects.Effects;\r
 import static com.google.gwt.query.client.plugins.Events.Events;\r
 \r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-\r
 import com.google.gwt.core.client.GWT;\r
 import com.google.gwt.core.client.JavaScriptObject;\r
 import com.google.gwt.core.client.JsArray;\r
@@ -37,9 +32,9 @@ import com.google.gwt.dom.client.Node;
 import com.google.gwt.dom.client.NodeList;\r
 import com.google.gwt.dom.client.OptionElement;\r
 import com.google.gwt.dom.client.SelectElement;\r
+import com.google.gwt.dom.client.TextAreaElement;\r
 import com.google.gwt.dom.client.Style.Display;\r
 import com.google.gwt.dom.client.Style.HasCssName;\r
-import com.google.gwt.dom.client.TextAreaElement;\r
 import com.google.gwt.query.client.css.CSS;\r
 import com.google.gwt.query.client.css.HasCssValue;\r
 import com.google.gwt.query.client.css.TakesCssValue;\r
@@ -58,6 +53,11 @@ import com.google.gwt.user.client.EventListener;
 import com.google.gwt.user.client.Window;\r
 import com.google.gwt.user.client.ui.Widget;\r
 \r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+\r
 /**\r
  * GwtQuery is a GWT clone of the popular jQuery library.\r
  */\r
@@ -114,7 +114,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   private static final String OLD_DATA_PREFIX = "old-";\r
 \r
   private static JsMap<Class<? extends GQuery>, Plugin<? extends GQuery>> plugins;\r
-  \r
+\r
   private static DocumentStyleImpl styleImpl = GWT.create(DocumentStyleImpl.class);\r
 \r
   private static Element windowData = null;\r
@@ -126,7 +126,6 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     return new GQuery(JsNodeArray.create());\r
   }\r
 \r
\r
   /**\r
    * Wrap a GQuery around an existing element.\r
    */\r
@@ -155,25 +154,24 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   public static GQuery $(NodeList<Element> elements) {\r
     return new GQuery(elements);\r
   }\r
-  \r
+\r
   /**\r
-   * Create a new GQuery given a list of nodes, elements or widgets \r
+   * Create a new GQuery given a list of nodes, elements or widgets\r
    */\r
   public static GQuery $(List<?> nodesOrWidgets) {\r
     JsNodeArray elements = JsNodeArray.create();\r
     if (nodesOrWidgets != null) {\r
-      for (Object o : nodesOrWidgets ) {\r
+      for (Object o : nodesOrWidgets) {\r
         if (o instanceof Node) {\r
-          elements.addNode((Node)o);\r
+          elements.addNode((Node) o);\r
         } else if (o instanceof Widget) {\r
-          elements.addNode(((Widget)o).getElement());\r
+          elements.addNode(((Widget) o).getElement());\r
         }\r
       }\r
     }\r
     return new GQuery(elements);\r
   }\r
 \r
-\r
   /**\r
    * This function accepts a string containing a CSS selector which is then used\r
    * to match a set of elements, or it accepts raw HTML creating a GQuery\r
@@ -268,7 +266,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   /**\r
    * Wrap a GQuery around one widget or an array of existing ones.\r
    */\r
-  public static GQuery $(Widget... widgets){\r
+  public static GQuery $(Widget... widgets) {\r
     return $(Arrays.asList(widgets));\r
   }\r
 \r
@@ -853,6 +851,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * \r
    * \r
    * ex :\r
+   * \r
    * <pre class="code">\r
    * $("#myDiv").css(CSS.TOP.with(Length.cm(15)));\r
    * $("#myDiv").css(CSS.BACKGROUND.with(RGBColor.SILVER, ImageValue.url(""),\r
@@ -878,6 +877,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * matched elements.\r
    * \r
    * Example:\r
+   * \r
    * <pre class="code">\r
    *  $(".item").css(Properties.create("color: 'red', background:'blue'"))\r
    * </pre>\r
@@ -1986,60 +1986,82 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   /**\r
-   * Replaces the element <code>elem</code> by the specified selector with the matched\r
-   * elements. This function is the complement to replaceWith() which does the\r
-   * same task with the parameters reversed.\r
+   * Replaces the element <code>elem</code> by the specified selector with the\r
+   * matched elements. This function is the complement to replaceWith() which\r
+   * does the same task with the parameters reversed.\r
+   * \r
+   * @return a {@link GQuery} object containing the new elements.\r
    */\r
   public GQuery replaceAll(Element elem) {\r
     return replaceAll($(elem));\r
   }\r
 \r
   /**\r
-   * Replaces the elements matched by the target with the matched\r
-   * elements. This function is the complement to replaceWith() which does the\r
-   * same task with the parameters reversed.\r
+   * Replaces the elements matched by the target with the selected elements.\r
+   * This function is the complement to replaceWith() which does the same task\r
+   * with the parameters reversed.\r
+   * \r
+   * @return a {@link GQuery} object containing the new elements.\r
    */\r
   public GQuery replaceAll(GQuery target) {\r
-    for (Element e : target.elements()) {\r
-      $(e).replaceWith(this);\r
+    // if there is only one element and it is not attached to the dom, we have\r
+    // to clone it to be reused on each element of target (if target contains\r
+    // more than one element)\r
+    boolean mustBeCloned = length() == 1\r
+        && parents().filter("body").length() == 0;\r
+\r
+    List<Element> newElements = new ArrayList<Element>();\r
+    for (int i = 0; i < target.elements().length; i++) {\r
+      GQuery _this = (i > 0 && mustBeCloned) ? this.clone() : this;\r
+      $(target.get(i)).replaceWith(_this);\r
+\r
+      newElements.addAll(Arrays.asList(_this.elements()));\r
+\r
     }\r
-    return this;\r
+    return $(newElements);\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
    * same task with the parameters reversed.\r
+   * \r
+   * @return a {@link GQuery} object containing the new elements.\r
    */\r
   public GQuery replaceAll(String selector) {\r
     return replaceAll($(selector));\r
   }\r
 \r
   /**\r
-   * Replaces all matched elements with the specified HTML or DOM elements. This\r
-   * returns the GQuery element that was just replaced, which has been removed\r
-   * from the DOM.\r
+   * Replaces all matched elements with the specified element.\r
+   * \r
+   * @return the GQuery element that was just replaced, which has been removed\r
+   *         from the DOM and not the new element that has replaced it.\r
    */\r
   public GQuery replaceWith(Element elem) {\r
     return replaceWith($(elem));\r
   }\r
 \r
   /**\r
-   * Replaces all matched elements with the specified HTML or DOM elements. This\r
-   * returns the GQuery element that was just replaced, which has been removed\r
-   * from the DOM.\r
+   * Replaces all matched elements with elements selected by <code>target</code>\r
+   * .\r
+   * \r
+   * @return the GQuery element that was just replaced, which has been removed\r
+   *         from the DOM and not the new element that has replaced it.\r
    */\r
-  public GQuery replaceWith(GQuery query) {\r
-    return after(query).remove();\r
+  public GQuery replaceWith(GQuery target) {\r
+    return after(target).remove();\r
+\r
   }\r
 \r
   /**\r
-   * Replaces all matched elements with the specified HTML or DOM elements. This\r
-   * returns the GQuery element that was just replaced, which has been removed\r
-   * from the DOM.\r
+   * Replaces all matched elements with the specified HTML.\r
+   * \r
+   * @return the GQuery element that was just replaced, which has been removed\r
+   *         from the DOM and not the new element that has replaced it.\r
    */\r
   public GQuery replaceWith(String html) {\r
-    return replaceWith($(html));\r
+    return after(html).remove();\r
   }\r
 \r
   /**\r
@@ -2531,7 +2553,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    */\r
   @SuppressWarnings("unchecked")\r
   public <W extends Widget> W widget() {\r
-    return (W)widget(0);\r
+    return (W) widget(0);\r
   }\r
 \r
   /**\r
@@ -2796,7 +2818,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
       elms = elements();\r
     }\r
     for (Element e : elms) {\r
-      e.getOwnerDocument();\r
+      // e.getOwnerDocument();\r
       if (e.getNodeType() == Node.DOCUMENT_NODE) {\r
         e = e.<Document> cast().getBody();\r
       }\r
@@ -2840,6 +2862,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
       }\r
       domManip(g.clone(), func, e);\r
     }\r
+\r
     return this;\r
   }\r
 \r
index 09a14f4ae55cdf10a1b7ba87c636a21720a64306..4036ab449d159d48eba85710ab32f589cdeef062 100644 (file)
 package com.google.gwt.query.client;
 import static com.google.gwt.query.client.plugins.Effects.Effects;
 import static com.google.gwt.query.client.plugins.Events.Events;
-import java.util.ArrayList;
-import java.util.Arrays;
-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;
@@ -34,9 +30,9 @@ 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.TextAreaElement;
 import com.google.gwt.dom.client.Style.Display;
 import com.google.gwt.dom.client.Style.HasCssName;
-import com.google.gwt.dom.client.TextAreaElement;
 import com.google.gwt.query.client.css.CSS;
 import com.google.gwt.query.client.css.HasCssValue;
 import com.google.gwt.query.client.css.TakesCssValue;
@@ -54,6 +50,10 @@ import com.google.gwt.user.client.Event;
 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.Arrays;
+import java.util.HashMap;
+import java.util.List;
 import com.google.gwt.query.client.LazyBase;
 
 public interface LazyGQuery<T> extends LazyBase<T>{
@@ -277,6 +277,7 @@ public interface LazyGQuery<T> extends LazyBase<T>{
    * 
    * 
    * ex :
+   * 
    * <pre class="code">
    * $("#myDiv").css(CSS.TOP.with(Length.cm(15)));
    * $("#myDiv").css(CSS.BACKGROUND.with(RGBColor.SILVER, ImageValue.url(""),
@@ -295,6 +296,7 @@ public interface LazyGQuery<T> extends LazyBase<T>{
    * matched elements.
    * 
    * Example:
+   * 
    * <pre class="code">
    *  $(".item").css(Properties.create("color: 'red', background:'blue'"))
    * </pre>
@@ -955,16 +957,20 @@ public interface LazyGQuery<T> extends LazyBase<T>{
   LazyGQuery<T> removeData(String name);
 
   /**
-   * Replaces the element <code>elem</code> by the specified selector with the matched
-   * elements. This function is the complement to replaceWith() which does the
-   * same task with the parameters reversed.
+   * Replaces the element <code>elem</code> by the specified selector with the
+   * matched elements. This function is the complement to replaceWith() which
+   * does the same task with the parameters reversed.
+   * 
+   * @return a {@link GQuery} object containing the new elements.
    */
   LazyGQuery<T> replaceAll(Element elem);
 
   /**
-   * Replaces the elements matched by the target with the matched
-   * elements. This function is the complement to replaceWith() which does the
-   * same task with the parameters reversed.
+   * Replaces the elements matched by the target with the selected elements.
+   * This function is the complement to replaceWith() which does the same task
+   * with the parameters reversed.
+   * 
+   * @return a {@link GQuery} object containing the new elements.
    */
   LazyGQuery<T> replaceAll(GQuery target);
 
@@ -972,27 +978,33 @@ public interface LazyGQuery<T> extends LazyBase<T>{
    * Replaces the elements matched by the specified selector with the matched
    * elements. This function is the complement to replaceWith() which does the
    * same task with the parameters reversed.
+   * 
+   * @return a {@link GQuery} object containing the new elements.
    */
   LazyGQuery<T> replaceAll(String selector);
 
   /**
-   * Replaces all matched elements with the specified HTML or DOM elements. This
-   * returns the GQuery element that was just replaced, which has been removed
-   * from the DOM.
+   * Replaces all matched elements with the specified element.
+   * 
+   * @return the GQuery element that was just replaced, which has been removed
+   *         from the DOM and not the new element that has replaced it.
    */
   LazyGQuery<T> replaceWith(Element elem);
 
   /**
-   * Replaces all matched elements with the specified HTML or DOM elements. This
-   * returns the GQuery element that was just replaced, which has been removed
-   * from the DOM.
+   * Replaces all matched elements with elements selected by <code>target</code>
+   * .
+   * 
+   * @return the GQuery element that was just replaced, which has been removed
+   *         from the DOM and not the new element that has replaced it.
    */
-  LazyGQuery<T> replaceWith(GQuery query);
+  LazyGQuery<T> replaceWith(GQuery target);
 
   /**
-   * Replaces all matched elements with the specified HTML or DOM elements. This
-   * returns the GQuery element that was just replaced, which has been removed
-   * from the DOM.
+   * Replaces all matched elements with the specified HTML.
+   * 
+   * @return the GQuery element that was just replaced, which has been removed
+   *         from the DOM and not the new element that has replaced it.
    */
   LazyGQuery<T> replaceWith(String html);
 
index a9899f7cd6a05d771b4c8e3e3273768fee0afa92..19b349e5f16445f14e2adbaeff077ef7f0491814 100644 (file)
@@ -20,16 +20,14 @@ import static com.google.gwt.query.client.GQuery.$$;
 import static com.google.gwt.query.client.GQuery.document;
 import static com.google.gwt.query.client.plugins.Widgets.Widgets;
 
-import java.util.List;
-
-import junit.framework.Assert;
-
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.Node;
 import com.google.gwt.event.dom.client.ClickEvent;
 import com.google.gwt.event.dom.client.ClickHandler;
 import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.query.client.css.CSS;
+import com.google.gwt.query.client.css.RGBColor;
 import com.google.gwt.query.client.impl.SelectorEngineImpl;
 import com.google.gwt.query.client.impl.SelectorEngineSizzle;
 import com.google.gwt.query.client.js.JsNodeArray;
@@ -41,6 +39,10 @@ import com.google.gwt.user.client.ui.Label;
 import com.google.gwt.user.client.ui.RootPanel;
 import com.google.gwt.user.client.ui.TextArea;
 
+import junit.framework.Assert;
+
+import java.util.List;
+
 /**
  * Test class for testing gwtquery-core api.
  */
@@ -56,19 +58,17 @@ public class GQueryCoreTest extends GWTTestCase {
 
   protected static String iExplorerFixHtml(Object s) {
     // IE shows all tags upper-case
-    // IE adds \r \n 
+    // IE adds \r \n
     // IE does not put quotes to some attributes
-    // Investigate: IE in method find puts the attribute $h="4" 
+    // Investigate: IE in method find puts the attribute $h="4"
     // Investigate: IE in method filter adds the attrib added="null"
-    return s.toString().trim().toLowerCase().replaceAll(
-            "[\r\n]", "").replaceAll(
-            " ([\\w]+)=[\"']([^\"']+)[\"']", " $1=$2").replaceAll(
-            "\\s+\\$h=\"[^\"]+\"", "").replaceAll(
-            " added=[^ >]+", "");
+    return s.toString().trim().toLowerCase().replaceAll("[\r\n]", "").replaceAll(
+        " ([\\w]+)=[\"']([^\"']+)[\"']", " $1=$2").replaceAll(
+        "\\s+\\$h=\"[^\"]+\"", "").replaceAll(" added=[^ >]+", "");
   }
 
   int done = 0;
-  
+
   public String getModuleName() {
     return "com.google.gwt.query.Query";
   }
@@ -83,17 +83,17 @@ public class GQueryCoreTest extends GWTTestCase {
       e.setInnerHTML("");
     }
   }
-  
+
   public void testAttributeMethods() {
 
     $(e).html("<p class=\"a1\">Content</p>");
     GQuery gq = $("p", e);
-    
+
     // attr()
     gq.attr($$("attr1: 'a', attr2: 'b'"));
     assertEquals("a", gq.attr("attr1"));
     assertEquals("b", gq.attr("attr2"));
-    
+
     gq.attr("attr3", new Function() {
       public String f(Element e, int i) {
         return e.getInnerText();
@@ -121,7 +121,7 @@ public class GQueryCoreTest extends GWTTestCase {
     assertTrue(gq.hasClass("b2"));
     assertFalse(gq.hasClass("c1"));
     assertFalse(gq.hasClass("c2"));
-    
+
     // toggleClass()
     gq.toggleClass("b2");
     assertTrue(gq.hasClass("b1"));
@@ -133,7 +133,7 @@ public class GQueryCoreTest extends GWTTestCase {
     assertTrue(gq.hasClass("b2"));
     gq.toggleClass("b2", false);
     assertFalse(gq.hasClass("b2"));
-    
+
     // css()
     String content = "<p style='color:red;'>Test Paragraph.</p>";
     $(e).html(content);
@@ -143,8 +143,8 @@ public class GQueryCoreTest extends GWTTestCase {
 
     // css() properties
     $(e).html("<p>Test Paragraph.</p>");
-    $("p", e).css(Properties.create(
-        "color: 'red', 'font-weight': 'bold', background: 'blue'"));
+    $("p", e).css(
+        Properties.create("color: 'red', 'font-weight': 'bold', background: 'blue'"));
     assertEquals("red", $("p", e).css("color"));
     assertEquals("bold", $("p", e).css("font-weight"));
     assertEquals("blue", $("p", e).css("background-color"));
@@ -168,49 +168,50 @@ public class GQueryCoreTest extends GWTTestCase {
   public void testCleanMethod() {
     $(e).html("").append($("<tr/>"));
     assertHtmlEquals("<tr></tr>", $(e).html());
-    
+
     $(e).html("").append($("<td/>"));
     assertHtmlEquals("<td></td>", $(e).html());
 
     $(e).html("").append($("<th/>"));
     assertHtmlEquals("<th></th>", $(e).html());
   }
-  
+
   public void testDomManip() {
     String content = "<span class='branchA'><span class='target'>branchA target</span></span>"
-      + "<span class='branchB'><span class='target'>branchB target</span></span>";
-    
+        + "<span class='branchB'><span class='target'>branchB target</span></span>";
+
     $(e).html("");
     $(e).append(content);
     assertEquals(4, $("span", e).size());
     assertEquals(2, $("span.target", e).size());
     assertHtmlEquals(content, $(e).html());
-    
+
     $(e).html("<span>a</span><span>b</span>");
     $("span").append("<div>c</div>");
-    assertHtmlEquals("<span>a<div>c</div></span><span>b<div>c</div></span>", $(e).html());
+    assertHtmlEquals("<span>a<div>c</div></span><span>b<div>c</div></span>", $(
+        e).html());
   }
-  
+
   public void testEach() {
     $(e).html("<p>Content 1</p><p>Content 2</p><p>Content 3</p>");
     $("p", e).each(new Function() {
       public void f(Element e) {
         $(e).text(".");
-      }      
+      }
     });
     assertHtmlEquals("<p>.</p><p>.</p><p>.</p>", $("p", e));
     $("p", e).each(new Function() {
       public String f(Element e, int i) {
         $(e).text("" + i);
         return "";
-      }      
+      }
     });
     assertHtmlEquals("<p>0</p><p>1</p><p>2</p>", $("p", e));
   }
 
   public void testIFrameManipulation() {
     $(e).html("<iframe name='miframe' id='miframe' src=\"javascript:''\">");
-    // FF has to call empty to open and close the document before 
+    // FF has to call empty to open and close the document before
     // accessing the recently created iframe content
     Document d = $("#miframe").contents().empty().get(0).cast();
     assertNotNull(d);
@@ -276,7 +277,7 @@ public class GQueryCoreTest extends GWTTestCase {
     gq = $("select", e);
     assertEquals(0, gq.vals().length);
     assertEquals("", gq.val());
-    
+
     $(e).html(
         "<select name='n' multiple='multiple'><option value='v1'>1</option><option value='v2' selected='selected'>2</option><option value='v3'>3</option></select>");
     gq = $("select", e);
@@ -287,9 +288,9 @@ public class GQueryCoreTest extends GWTTestCase {
     assertEquals("v1", gq.vals()[0]);
     assertEquals("v3", gq.vals()[1]);
     // FIXME: fix in IE
-//    gq.val("v1");
-//    assertEquals(1, gq.vals().length);
-//    assertEquals("v1", gq.val());
+    // gq.val("v1");
+    // assertEquals(1, gq.vals().length);
+    // assertEquals("v1", gq.val());
 
     // input radio
     $(e).html(
@@ -309,12 +310,13 @@ public class GQueryCoreTest extends GWTTestCase {
     gq.val("v1");
     assertEquals("v1", gq.val());
   }
-  
+
   public void testIssue23() {
-    $(e).html("<table><tr><td><input type='radio' name='n' value='v1'>1</input><input type='radio' name='n' value='v2' checked='checked'>2</input></td><td><button>Click</button></tr><td></table>");
+    $(e).html(
+        "<table><tr><td><input type='radio' name='n' value='v1'>1</input><input type='radio' name='n' value='v2' checked='checked'>2</input></td><td><button>Click</button></tr><td></table>");
     $("button").click(new Function() {
       public boolean f(Event ev) {
-       done = 0;
+        done = 0;
         $("table > tbody > tr > td > input:checked", e).each(new Function() {
           public void f(Element e) {
             done++;
@@ -336,7 +338,7 @@ public class GQueryCoreTest extends GWTTestCase {
     $(e).html(pTxt);
     $("p", e).append(bTxt);
     assertHtmlEquals(expected, $(e).html());
-    
+
     $(e).html(pTxt);
     $("p", e).append($(bTxt).get(0));
     assertHtmlEquals(expected, $(e).html());
@@ -424,40 +426,39 @@ 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>"; 
+
+    // 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);
+    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());    
+    assertEquals(expected, g2.toString());
   }
-  
+
   public void testOpacity() {
-    $(e)
-    .html(
+    $(e).html(
         "<p id='id1' style='opacity: 0.6; filter: alpha(opacity=60)'>Content 1</p>");
     GQuery g = $("#id1");
     assertEquals("0.6", g.css("opacity", false));
@@ -469,26 +470,27 @@ public class GQueryCoreTest extends GWTTestCase {
     assertEquals("0.4", g.css("opacity", false));
     assertEquals("0.4", g.css("opacity", true));
   }
-  
-  public void testPosition(){
-    $(e).html("<div style='top:25px; left:25px; padding:20px; position:relative;'><div id='child' style='margin:30px'>test</div></div> ");
+
+  public void testPosition() {
+    $(e).html(
+        "<div style='top:25px; left:25px; padding:20px; position:relative;'><div id='child' style='margin:30px'>test</div></div> ");
     GQuery g = $("#child");
     assertEquals(20, g.position().left);
     assertEquals(20, g.position().top);
-    
-    $(e).html("<div style='top:25px; left:25px; position:relative;'><div id='child' style='position:relative; top:15px; left:35px;'>test</div></div> ");
+
+    $(e).html(
+        "<div style='top:25px; left:25px; position:relative;'><div id='child' style='position:relative; top:15px; left:35px;'>test</div></div> ");
     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);
     assertNotNull(p.get("border"));
-    
+
     p = $$("({border:'1px solid black'})");
     assertEquals(1, p.keys().length);
     assertNotNull(p.get("border"));
@@ -529,8 +531,7 @@ public class GQueryCoreTest extends GWTTestCase {
     assertHtmlEquals(expected, $("p", e).parent());
 
     // parent()
-    content
-        = "<div><p>Hello</p></div><div class=\"selected\"><p>Hello Again</p></div>";
+    content = "<div><p>Hello</p></div><div class=\"selected\"><p>Hello Again</p></div>";
     expected = "<div class=\"selected\"><p>Hello Again</p></div>";
     $(e).html(content);
     assertHtmlEquals(expected, $("p", e).parent(".selected"));
@@ -541,7 +542,8 @@ public class GQueryCoreTest extends GWTTestCase {
     assertEquals(2, $("span", e).size());
     assertTrue(3 < $("span", e).parents().size());
     assertEquals(1, $("span", e).parents().filter("body").size());
-    $("span", e).parents().filter("body").toString().trim().toLowerCase().contains(content.toLowerCase());
+    $("span", e).parents().filter("body").toString().trim().toLowerCase().contains(
+        content.toLowerCase());
 
     // is()
     content = "<form><input type=\"checkbox\"></form>";
@@ -563,20 +565,19 @@ public class GQueryCoreTest extends GWTTestCase {
     assertHtmlEquals(next2, $("p", e).next().get(1).getString());
 
     // next()
-    content
-        = "<p>Hello</p><p class=\"selected\">Hello Again</p><div><span>And Again</span></div>";
+    content = "<p>Hello</p><p class=\"selected\">Hello Again</p><div><span>And Again</span></div>";
     expected = "<p class=\"selected\">Hello Again</p>";
     $(e).html(content);
     assertEquals(1, $("p", e).next(".selected").size());
     assertHtmlEquals(expected, $("p", e).next(".selected").get(0).getString());
-    
+
     // nextAll()
     content = "<ul><li>i1</li><li>i2</li><li class='third-item'>i3</li><li>i4</li><li>i5</li></ul>";
     expected = "<li>i4</li><li>i5</li>";
     $(e).html(content);
     assertEquals(2, $("li.third-item", e).nextAll().size());
     assertHtmlEquals(expected, $("li.third-item", e).nextAll());
-    
+
     // andSelf()
     content = "<ul><li>i1</li><li>i2</li><li class=\"third-item\">i3</li><li>i4</li><li>i5</li></ul>";
     expected = "<li>i4</li><li>i5</li><li class=\"third-item\">i3</li>";
@@ -592,8 +593,7 @@ public class GQueryCoreTest extends GWTTestCase {
     assertHtmlEquals(expected, $("p", e).prev().get(0).getString());
 
     // prev()
-    content
-        = "<div><span>Hello</span></div><p class=\"selected\">Hello Again</p><p>And Again</p>";
+    content = "<div><span>Hello</span></div><p class=\"selected\">Hello Again</p><p>And Again</p>";
     expected = "<p class=\"selected\">Hello Again</p>";
     $(e).html(content);
     assertEquals(2, $("p", e).prev().size());
@@ -610,12 +610,12 @@ public class GQueryCoreTest extends GWTTestCase {
     assertHtmlEquals(next2, $("#mdiv", e).siblings().get(1).getString());
 
     // siblings()
-    content
-        = "<div><span>Hello</span></div><p class=\"selected\">Hello Again</p><p>And Again</p>";
+    content = "<div><span>Hello</span></div><p class=\"selected\">Hello Again</p><p>And Again</p>";
     expected = "<p class=\"selected\">Hello Again</p>";
     $(e).html(content);
     assertEquals(1, $("p", e).siblings(".selected").size());
-    assertHtmlEquals(expected, $("p", e).siblings(".selected").get(0).getString());
+    assertHtmlEquals(expected,
+        $("p", e).siblings(".selected").get(0).getString());
 
     // children()
     content = "<p>Hello</p><div id='mdiv'><span>Hello Again</span></div><p>And Again</p>";
@@ -624,8 +624,7 @@ public class GQueryCoreTest extends GWTTestCase {
     assertHtmlEquals(expected, $("#mdiv", e).children());
 
     // children()
-    content
-        = "<div id='mdiv'><span>Hello</span><p class=\"selected\">Hello Again</p><p>And Again</p></div>";
+    content = "<div id='mdiv'><span>Hello</span><p class=\"selected\">Hello Again</p><p>And Again</p></div>";
     expected = "<p class=\"selected\">Hello Again</p>";
     $(e).html(content);
     assertHtmlEquals(expected, $("#mdiv", e).children(".selected"));
@@ -636,28 +635,64 @@ public class GQueryCoreTest extends GWTTestCase {
     $(e).html(content);
     assertHtmlEquals(expected, $("p", e).contains("test"));
   }
-  
+
+  public void testReplaceMethods() {
+    String content = "<div><div class='inner first'>Hello</div><div class='inner second'>And</div><div class='inner third'>Goodbye</div></div>";
+
+    $(e).html(content);
+    GQuery $inner = $(".inner").replaceWith("<h3>blop</h3>");
+    String expectedHtml = "<div><h3>blop</h3><h3>blop</h3><h3>blop</h3></div>";
+    assertEquals(expectedHtml, $(e).html());
+
+    // the returned gquery object should be the original with the div elements
+    // bug in filter method : wrap $inner in a div to use filter method on it
+    final GQuery outer = $("<div class=\"outer\"></div>");
+    for (Element e : $inner.elements()) {
+      outer.append(e);
+    }
+    assertEquals(3, $inner.filter("div.inner").length());
+
+    $(e).html(content);
+    // the css part below allows us to check if the objects returned by the
+    // replaceAll method are the new inserted elements
+    $("<h3>blop</h3>").replaceAll($(".inner")).css(CSS.COLOR.with(RGBColor.RED));
+    // $(e) must content 3 h3
+    assertEquals(3, $("h3", e).length());
+    // the objects returned by the replaceAll method should be the 3 inserted h3
+    assertEquals("red", $("h3", e).css(CSS.COLOR));
+
+
+    $(e).html(content);
+    $(".third").replaceWith($(".first"));
+    expectedHtml = "<div><div class=\"inner second\">And</div><div class=\"inner first\">Hello</div></div>";
+    assertEquals($(e).html(), expectedHtml);
+
+    $(e).html(content);
+    $(".first").replaceAll(".third");
+    assertEquals($(e).html(), expectedHtml);
+
+  }
+
   public void testShowHide() {
-    $(e)
-    .html(
+    $(e).html(
         "<p id='id1' style='display: inline'>Content 1</p><p id='id2'>Content 2</p><p id='id3'>Content 3</p>");
 
     final GQuery sectA = $("#id1");
     final GQuery sectB = $("#id2");
     final GQuery sectC = $("#id3");
-    
+
     // hide()
     sectA.hide();
     assertEquals("none", sectA.css("display"));
     sectB.hide();
     assertEquals("none", sectB.css("display"));
-    
+
     // show()
     sectA.show();
     assertEquals("inline", sectA.css("display"));
     sectB.show();
     assertEquals("", sectB.css("display"));
-    
+
     // toggle()
     assertEquals("", sectC.css("display"));
     sectC.toggle();
@@ -665,7 +700,7 @@ public class GQueryCoreTest extends GWTTestCase {
     sectC.toggle();
     assertEquals("block", sectC.css("display"));
   }
-  
+
   public void testSliceMethods() {
     String content = "<p>This is just a test.</p><p>So is this</p>";
     $(e).html(content);
@@ -686,7 +721,7 @@ public class GQueryCoreTest extends GWTTestCase {
     assertEquals(2, $("p", e).slice(0, -1).size());
     assertEquals(0, $("p", e).slice(3, 2).size());
   }
-  
+
   public void testUnique() {
     SelectorEngineImpl selSizz = new SelectorEngineSizzle();
     GQuery g = $(e).html("<div><p></p><p></p><span></span><p></p>");
@@ -695,11 +730,11 @@ public class GQueryCoreTest extends GWTTestCase {
     assertEquals(3, a.getLength());
     a.addNode(a.getNode(0));
     a.addNode(a.getNode(3));
-    assertEquals(5 , a.getLength());
+    assertEquals(5, a.getLength());
     a = g.unique(a);
     assertEquals(3, a.getLength());
   }
-  
+
   public void testUtilsEq() {
     assertTrue(JsUtils.eq("a", "a"));
     assertTrue(JsUtils.eq(true, true));
@@ -709,16 +744,16 @@ public class GQueryCoreTest extends GWTTestCase {
     assertTrue(JsUtils.eq(0.45, 0.45));
     assertTrue(JsUtils.eq(0.45d, 0.45d));
     assertTrue(JsUtils.eq(0.45f, 0.45f));
-    
+
     assertFalse(JsUtils.eq("a", ""));
     assertFalse(JsUtils.eq(true, false));
     assertFalse(JsUtils.eq(45, 42));
     assertFalse(JsUtils.eq("", null));
     assertFalse(JsUtils.eq(0.45, 0.451));
-    
-//    assertEquals("a", GQUtils.or("a", ""));
+
+    // assertEquals("a", GQUtils.or("a", ""));
   }
-  
+
   public void testUtilsTruth() {
     assertTrue(JsUtils.truth("a"));
     assertTrue(JsUtils.truth(this));
@@ -737,10 +772,9 @@ public class GQueryCoreTest extends GWTTestCase {
     assertFalse(JsUtils.truth(null));
     assertFalse(JsUtils.truth(""));
   }
-  
+
   public void testWidthHeight() {
-    $(e)
-    .html(
+    $(e).html(
         "<div style='border: 1px solid red; padding: 10px; margin:10px; width: 100px; height: 100px'>Content 1</div>");
     GQuery g = $("div", e);
     assertEquals(100, g.width());
@@ -759,10 +793,9 @@ public class GQueryCoreTest extends GWTTestCase {
     assertEquals(122, g.outerWidth());
     assertEquals(142, g.outerHeight(true));
     assertEquals(142, g.outerWidth(true));
-    
-    
+
   }
-  
+
   public void testWrapMethod() {
     String content = "<p>Test Paragraph.</p>";
     String wrapper = "<div id=\"content\">Content</div>";
@@ -778,47 +811,47 @@ public class GQueryCoreTest extends GWTTestCase {
     $("*", e).wrap("<b></b>");
     assertHtmlEquals(expected, $(e).html());
   }
-  
+
   public void testFilterBody() {
     GQuery myNewElement = $("<div>my new div</div>");
     boolean isAttachedToTheDOM = myNewElement.parents().filter("body").size() > 0;
     assertEquals(false, isAttachedToTheDOM);
-    
+
     myNewElement.appendTo(document);
     isAttachedToTheDOM = myNewElement.parents().filter("body").size() > 0;
     assertEquals(true, isAttachedToTheDOM);
   }
-  
+
   public void testGQueryWidgets() {
     final Button b1 = new Button("click-me");
     RootPanel.get().add(b1);
-    
+
     GQuery g = $(b1);
     Button b2 = g.widget();
     assertEquals(b1, b2);
-    
-    b2 = $("<button>Click-me</button>").appendTo(document).as(Widgets).button();
+
+    b2 = $("<button>Click-me</button>").appendTo(document).as(Widgets).button().widget();
     b2.addClickHandler(new ClickHandler() {
       public void onClick(ClickEvent event) {
         $(b1).css("color", "red");
       }
     });
-    
+
     (b2).click();
     assertEquals("red", $(b1).css("color"));
-    
+
     $("<button>Click-me</button>").appendTo(document);
     assertEquals(3, $("button").size());
     assertEquals(2, $("button").widgets(Button.class).size());
     assertEquals(2, $($("button").widgets(Button.class)).size());
-    
+
     assertEquals(2, $(new Label(""), new TextArea()).size());
   }
-  
+
   public void testGQueryMap() {
     String content = "<p id='1'/><p/><p id='2'/><p id='4'/>";
     $(e).html(content);
-    
+
     List<String> s = $("p", e).map(new Function() {
       public Object f(Element e, int i) {
         return null;
@@ -830,33 +863,32 @@ public class GQueryCoreTest extends GWTTestCase {
     s = $("p", e).map(new Function() {
       public Object f(Element e, int i) {
         String id = $(e).attr("id");
-        return id.isEmpty() ? null: id;
+        return id.isEmpty() ? null : id;
       }
     });
     assertEquals(3, s.size());
     assertEquals("1", s.get(0));
     assertEquals("2", s.get(1));
     assertEquals("4", s.get(2));
-    
 
     List<Element> a = $("p", e).map(new Function() {
       public Object f(Element e, int i) {
         String id = $(e).attr("id");
-        return id.isEmpty() ? null: e;
+        return id.isEmpty() ? null : e;
       }
     });
     assertEquals(3, a.size());
     assertEquals(3, $(a).size());
-    
+
   }
-  
+
   public void testNulls() {
-    Assert.assertEquals(0, $((Node)null).size());
-    Assert.assertEquals(0, $((Element)null).size());
-    Assert.assertEquals(0, $((String)null).size());
-    Assert.assertNull($((String)null).get(0));
-    Assert.assertNull($((String)null).get(-1));
-    Assert.assertEquals(0, $((String)null).eq(0).size());
+    Assert.assertEquals(0, $((Node) null).size());
+    Assert.assertEquals(0, $((Element) null).size());
+    Assert.assertEquals(0, $((String) null).size());
+    Assert.assertNull($((String) null).get(0));
+    Assert.assertNull($((String) null).get(-1));
+    Assert.assertEquals(0, $((String) null).eq(0).size());
   }
 
 }