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
*/\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
}\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
* 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
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
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
* 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
* 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
* 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
* 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
}\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
* 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
* 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
*/\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
* 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
* 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
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
}\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
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
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
}\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
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;
// 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>";
$(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>";
$(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() {
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());