public GQuery children() {\r
JsNodeArray result = JsNodeArray.create();\r
for (Element e : elements()) {\r
- allNextSiblingElements(e.getFirstChildElement(), result, null);\r
+ allNextSiblingElements(e.getFirstChildElement(), result, null, null);\r
}\r
return new GQuery(unique(result));\r
}\r
return pushStack(result, "next", selectors[0]).filter(selectors);\r
}\r
\r
+ \r
/**\r
* Find all sibling elements after the current element.\r
*/\r
public GQuery nextAll() {\r
JsNodeArray result = JsNodeArray.create();\r
for (Element e : elements()) {\r
- allNextSiblingElements(e.getNextSiblingElement(), result, null);\r
+ allNextSiblingElements(e.getNextSiblingElement(), result, null, null);\r
}\r
return pushStack(unique(result), "nextAll", getSelector());\r
}\r
+ \r
+ /**\r
+ * Get all following siblings of each element up to but not including the\r
+ * element matched by the selector.\r
+ * \r
+ * @param selector\r
+ * @return\r
+ */\r
+ public GQuery nextUntil(String selector) {\r
+ JsNodeArray result = JsNodeArray.create();\r
+ for (Element e : elements()) {\r
+ allNextSiblingElements(e.getNextSiblingElement(), result, null, selector);\r
+ }\r
+ return pushStack(unique(result), "nextUntil", getSelector());\r
+ }\r
\r
/**\r
* Removes the specified Element from the set of matched elements. This method\r
* elements (except for the root element).\r
*/\r
public GQuery parents() {\r
- JsNodeArray result = JsNodeArray.create();\r
- for (Element e : elements()) {\r
- Node par = e.getParentNode();\r
- while (par != null && par != document) {\r
- result.addNode(par);\r
- par = par.getParentNode();\r
- }\r
- }\r
- return new GQuery(unique(result));\r
+ return parentsUntil(null);\r
}\r
\r
/**\r
public GQuery parents(String... filters) {\r
return parents().filter(filters);\r
}\r
+ \r
+ /**\r
+ * Get the ancestors of each element in the current set of matched elements,\r
+ * up to but not including the element matched by the selector.\r
+ *\r
+ */\r
+ public GQuery parentsUntil(String selector) {\r
+ JsNodeArray result = JsNodeArray.create();\r
+ for (Element e : elements()) {\r
+ Node par = e.getParentNode();\r
+ while (par != null && par != document) {\r
+ if (selector != null && $(par).is(selector)) {\r
+ break;\r
+ }\r
+ result.addNode(par);\r
+ par = par.getParentNode();\r
+ }\r
+ }\r
+ return new GQuery(unique(result));\r
+ }\r
+ \r
\r
/**\r
* Gets the top and left position of an element relative to its offset parent.\r
public GQuery prevAll() {\r
JsNodeArray result = JsNodeArray.create();\r
for (Element e : elements()) {\r
- allPreviousSiblingElements(getPreviousSiblingElement(e), result);\r
+ allPreviousSiblingElements(getPreviousSiblingElement(e), result, null);\r
}\r
return pushStack(unique(result), "prevAll", getSelector());\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
/**\r
* Put a {@link Function} at the end of the Effects queue.\r
JsNodeArray result = JsNodeArray.create();\r
for (Element e : elements()) {\r
allNextSiblingElements(e.getParentElement().getFirstChildElement(),\r
- result, e);\r
+ result, e, null);\r
}\r
return new GQuery(unique(result));\r
}\r
}\r
\r
private void allNextSiblingElements(Element firstChildElement,\r
- JsNodeArray result, Element elem) {\r
+ JsNodeArray result, Element elem, String untilSelector) {\r
while (firstChildElement != null) {\r
+ \r
+ if (untilSelector != null && $(firstChildElement).is(untilSelector)){\r
+ return;\r
+ }\r
+ \r
if (firstChildElement != elem) {\r
result.addNode(firstChildElement);\r
}\r
}\r
\r
private void allPreviousSiblingElements(Element firstChildElement,\r
- JsNodeArray result) {\r
+ JsNodeArray result, String untilSelector) {\r
while (firstChildElement != null) {\r
+ if (untilSelector != null && $(firstChildElement).is(untilSelector)){\r
+ return;\r
+ }\r
result.addNode(firstChildElement);\r
firstChildElement = getPreviousSiblingElement(firstChildElement);\r
}\r
assertEquals(1, $("span", e).parents().filter("body").size());
$("span", e).parents().filter("body").toString().trim().toLowerCase().contains(
content.toLowerCase());
+
+ //parentsUntil()
+ content = "<div id='mainDiv'><div id='subDiv1' class='subDiv'><div id='subSubDiv1'><p id='p1'>child1</p></div></div><div id='subDiv2' class='subDiv'><div id='subSubDiv2'><p id='p2'>child2</p></div></div></div>";
+ $(e).html(content);
+ $("p",e).parentsUntil("#mainDiv").css(CSS.COLOR.with(RGBColor.RED));
+ assertEquals("red", $("#subDiv1", e).css(CSS.COLOR));
+ assertEquals("red", $("#subSubDiv1", e).css(CSS.COLOR));
+ assertEquals("red", $("#subDiv2", e).css(CSS.COLOR));
+ assertEquals("red", $("#subSubDiv2", e).css(CSS.COLOR));
+ assertEquals("", $("#mainDiv", e).css(CSS.COLOR));
+ assertEquals("", $("#p1", e).css(CSS.COLOR));
+ assertEquals("", $("#p2", e).css(CSS.COLOR));
+
+ $("#p1",e).parentsUntil(".subDiv").css(CSS.COLOR.with(RGBColor.YELLOW));
+ assertEquals("red", $("#subDiv1", e).css(CSS.COLOR));
+ assertEquals("yellow", $("#subSubDiv1", e).css(CSS.COLOR));
// is()
content = "<form><input type=\"checkbox\"></form>";
assertEquals(2, $("li.third-item", e).nextAll().size());
assertHtmlEquals(expected, $("li.third-item", e).nextAll());
+ // nextUntil()
+ 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>";
+ $(e).html(content);
+ assertEquals(1, $("li.third-item", e).nextUntil(".five-item").size());
+ assertHtmlEquals(expected, $("li.third-item", e).nextUntil(".five-item"));
+
+
// 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>";
assertEquals(2, $("p", e).prev().size());
assertEquals(1, $("p", e).prev(".selected").size());
assertHtmlEquals(expected, $("p", e).prev(".selected").get(0).getString());
+
+ // prevAll()
+ 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><li class='third-item'>i3</li><li>i2</li><li>i1</li>";
+ $(e).html(content);
+ assertEquals(4, $("li.five-item", e).prevAll().size());
+ assertHtmlEquals(expected, $("li.five-item", e).prevAll());
+
+
+ // 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>";
+ $(e).html(content);
+ assertEquals(1, $("li.five-item", e).prevUntil(".third-item").size());
+ assertHtmlEquals(expected, $("li.five-item", e).prevUntil(".third-item"));
// siblings()
content = "<p>Hello</p><div id='mdiv'><span>Hello Again</span></div><p>And Again</p>";