*/\r
package com.google.gwt.query.client;\r
\r
+import static com.google.gwt.query.client.plugins.QueuePlugin.Queue;\r
+\r
import java.util.ArrayList;\r
import java.util.Arrays;\r
import java.util.List;\r
+import java.util.Map;\r
\r
import com.google.gwt.core.client.GWT;\r
import com.google.gwt.core.client.JavaScriptObject;\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.query.client.css.TakesCssValue.CssSetter;\r
import com.google.gwt.user.client.Window;\r
import com.google.gwt.user.client.ui.GqUi;\r
import com.google.gwt.user.client.ui.Widget;\r
-import static com.google.gwt.query.client.plugins.QueuePlugin.Queue;\r
\r
/**\r
* GwtQuery is a GWT clone of the popular jQuery library.\r
}\r
\r
private void allPreviousSiblingElements(Element firstChildElement,\r
- JsNodeArray result, String untilSelector) {\r
+ JsNodeArray result, GQuery until, String filterSelector) {\r
while (firstChildElement != null) {\r
- if (untilSelector != null && $(firstChildElement).is(untilSelector)){\r
+ if (until != null && until.index(firstChildElement) != -1){\r
return;\r
}\r
- result.addNode(firstChildElement);\r
+ \r
+ if(filterSelector == null || $(firstChildElement).is(filterSelector)){\r
+ result.addNode(firstChildElement);\r
+ }\r
+\r
firstChildElement = getPreviousSiblingElement(firstChildElement);\r
}\r
}\r
}\r
return new GQuery(unique(result)).filter(selectors);\r
}\r
- /**\r
- * Find all sibling elements in front of the current element.\r
- */\r
- public GQuery prevAll() {\r
- JsNodeArray result = JsNodeArray.create();\r
- for (Element e : elements) {\r
- allPreviousSiblingElements(getPreviousSiblingElement(e), result, null);\r
- }\r
- return pushStack(unique(result), "prevAll", getSelector());\r
- }\r
+ \r
+ /**\r
+ * Get all preceding siblings of each element in the set of matched\r
+ * elements.\r
+ */\r
+ public GQuery prevAll() {\r
+ return prevAll(null);\r
+ }\r
\r
- /**\r
- * Find all sibling elements in front of the current element.\r
- */\r
- public GQuery prevUntil(String selector) {\r
- JsNodeArray result = JsNodeArray.create();\r
- for (Element e : elements) {\r
- allPreviousSiblingElements(getPreviousSiblingElement(e), result, selector);\r
- }\r
- return pushStack(unique(result), "prevUntil", getSelector());\r
- }\r
+ /**\r
+ * Get all preceding siblings of each element in the set of matched elements\r
+ * filtered by a selector.\r
+ */\r
+ public GQuery prevAll(String selector) {\r
+ JsNodeArray result = JsNodeArray.create();\r
+ for (Element e : elements) {\r
+ allPreviousSiblingElements(getPreviousSiblingElement(e), result,\r
+ null, selector);\r
+ }\r
+ return pushStack(unique(result), "prevAll", getSelector());\r
+ }\r
+\r
+ /**\r
+ * Get all preceding siblings of each element up to but not including the\r
+ * element matched by the <code>selector</code>.\r
+ * \r
+ * The elements are returned in order from the closest sibling to the\r
+ * farthest.\r
+ */\r
+ public GQuery prevUntil(String selector) {\r
+ return prevUntil($(selector), null);\r
+ }\r
+\r
+ /**\r
+ * Get all preceding siblings of each element up to but not including the\r
+ * <code>until</code> element.\r
+ * \r
+ * The elements are returned in order from the closest sibling to the\r
+ * farthest.\r
+ */\r
+ public GQuery prevUntil(Element until) {\r
+ return prevUntil($(until), null);\r
+ }\r
+\r
+ /**\r
+ * Get all preceding siblings of each element up to but not including the\r
+ * <code>until</code> element.\r
+ * \r
+ * The elements are returned in order from the closest sibling to the\r
+ * farthest.\r
+ */\r
+ public GQuery prevUntil(GQuery until) {\r
+ return prevUntil(until, null);\r
+ }\r
+\r
+ /**\r
+ * Get all preceding siblings of each element matching the\r
+ * <code>filter</code> up to but not including the element matched by the\r
+ * <code>selector</code>.\r
+ * \r
+ * The elements are returned in order from the closest sibling to the\r
+ * farthest.\r
+ */\r
+ public GQuery prevUntil(String selector, String filter) {\r
+ return prevUntil($(selector), filter);\r
+ }\r
+\r
+ /**\r
+ * Get all preceding siblings of each element matching the\r
+ * <code>filter</code> up to but not including the <code>until</code>\r
+ * element.\r
+ * \r
+ */\r
+ public GQuery prevUntil(Element until, String filter) {\r
+ return prevUntil($(until), filter);\r
+ }\r
+\r
+ /**\r
+ * Get all preceding siblings of each element matching the\r
+ * <code>filter</code> up to but not including the element matched by the\r
+ * <code>until</code> element.\r
+ * \r
+ */\r
+ public GQuery prevUntil(GQuery until, String filter) {\r
+ JsNodeArray result = JsNodeArray.create();\r
+ for (Element e : elements) {\r
+ allPreviousSiblingElements(getPreviousSiblingElement(e), result,\r
+ until, filter);\r
+ }\r
+ return pushStack(unique(result), "prevUntil", getSelector());\r
+ }\r
\r
/**\r
* Accesses a boolean property on the first matched element.\r
$(e).html(content);
assertEquals(4, $("li.five-item", e).prevAll().size());
assertHtmlEquals(expected, $("li.five-item", e).prevAll());
+
+ expected = "<li class='third-item'>i3</li>";
+ assertEquals(1, $("li.five-item", e).prevAll(".third-item").size());
+ assertHtmlEquals(expected, $("li.five-item", e).prevAll(".third-item"));
+
// prevUntil()
- content = "<ul><li>i1</li><li>i2</li><li class='third-item'>i3</li><li>i4</li><li class='five-item'>i5</li></ul>";
- expected = "<li>i4</li>";
+ content = "<ul><li class='item'>i1</li><li class='second-item'>i2</li><li class='third-item'>i3</li><li class='item'>i4</li><li class='five-item'>i5</li></ul>";
+ expected = "<li class='item'>i4</li>";
$(e).html(content);
assertEquals(1, $("li.five-item", e).prevUntil(".third-item").size());
assertHtmlEquals(expected, $("li.five-item", e).prevUntil(".third-item"));
+ assertEquals(1, $("li.five-item", e).prevUntil($(".third-item")).size());
+ assertHtmlEquals(expected, $("li.five-item", e).prevUntil($(".third-item")));
+
+ Element until = $(".third-item").get(0);
+ assertEquals(1, $("li.five-item", e).prevUntil(until).size());
+ assertHtmlEquals(expected, $("li.five-item", e).prevUntil(until));
+
+
+ assertEquals(0, $("li.five-item", e).prevUntil(".third-item", ".fake-class").size());
+ assertEquals(1, $("li.five-item", e).prevUntil(".second-item", ".item").size());
+ assertHtmlEquals(expected, $("li.five-item", e).prevUntil(".second-item", ".item"));
+
+ assertEquals(0, $("li.five-item", e).prevUntil($(".third-item"), ".fake-class").size());
+ assertEquals(1, $("li.five-item", e).prevUntil($(".second-item"), ".item").size());
+ assertHtmlEquals(expected, $("li.five-item", e).prevUntil($(".second-item"), ".item"));
+
+ assertEquals(0, $("li.five-item", e).prevUntil(until, ".fake-class").size());
+
+ until = $(".second-item").get(0);
+ assertEquals(1, $("li.five-item", e).prevUntil(until, ".item").size());
+ assertHtmlEquals(expected, $("li.five-item", e).prevUntil(until, ".item"));
+
+
// siblings()
content = "<p>Hello</p><div id='mdiv'><span>Hello Again</span></div><p>And Again</p>";
next1 = "<p>Hello</p>";