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
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
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
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
return new GQuery(JsNodeArray.create());\r
}\r
\r
- \r
/**\r
* Wrap a GQuery around an existing element.\r
*/\r
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
/**\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
* \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
* matched elements.\r
* \r
* Example:\r
+ * \r
* <pre class="code">\r
* $(".item").css(Properties.create("color: 'red', background:'blue'"))\r
* </pre>\r
}\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
*/\r
@SuppressWarnings("unchecked")\r
public <W extends Widget> W widget() {\r
- return (W)widget(0);\r
+ return (W) widget(0);\r
}\r
\r
/**\r
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
}\r
domManip(g.clone(), func, e);\r
}\r
+\r
return this;\r
}\r
\r
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;
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.
*/
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";
}
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();
assertTrue(gq.hasClass("b2"));
assertFalse(gq.hasClass("c1"));
assertFalse(gq.hasClass("c2"));
-
+
// toggleClass()
gq.toggleClass("b2");
assertTrue(gq.hasClass("b1"));
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);
// 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"));
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);
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);
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(
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++;
$(e).html(pTxt);
$("p", e).append(bTxt);
assertHtmlEquals(expected, $(e).html());
-
+
$(e).html(pTxt);
$("p", e).append($(bTxt).get(0));
assertHtmlEquals(expected, $(e).html());
$(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));
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"));
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"));
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>";
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>";
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());
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>";
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"));
$(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();
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);
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>");
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));
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));
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());
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>";
$("*", 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;
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());
}
}