/** * Get a set of elements containing the unique parents of the matched set of elements. */ public GQuery parent() { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { Element p = e.getParentElement(); if (p != null) { result.addNode(p); } } return new GQuery(unique(result)); }
/** * Get a set of elements containing all of the unique immediate children of each of the matched * set of elements. Also note: while parents() will look at all ancestors, children() will only * consider immediate child elements. */ public GQuery children() { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { allNextSiblingElements(e.getFirstChildElement(), result, null, null, null); } return new GQuery(unique(result)); }
/** * Get a set of elements containing the unique previous siblings of each of the matched set of * elements. Only the immediately previous sibling is returned, not all previous siblings. */ public GQuery prev() { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { Element next = getPreviousSiblingElement(e); if (next != null) { result.addNode(next); } } return new GQuery(unique(result)); }
/** * Get a set of elements containing all of the unique siblings of each of the matched set of * elements. */ public GQuery siblings() { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { allNextSiblingElements(e.getParentElement().getFirstChildElement(), result, e, null, null); } return new GQuery(unique(result)); }
/** * Searches for all elements that match the specified css expression. This method is a good way to * find additional descendant elements with which to process. * * Provide a comma-separated list of expressions to apply multiple filters at once. */ public GQuery find(String... filters) { JsNodeArray array = JsNodeArray.create(); for (String selector : filters) { for (Element e : elements) { for (Element c : $(selector, e).elements) { array.addNode(c); } } } return pushStack(unique(array), "find", filters[0]); }
/** * Get all following siblings of each element in the set of matched elements, filtered by a * selector. */ public GQuery nextAll(String filter) { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { allNextSiblingElements(e.getNextSiblingElement(), result, null, null, filter); } return pushStack(unique(result), "nextAll", getSelector()); }
/** * Get all preceding siblings of each element matching the <code>filter</code> up to but not * including the element matched by the <code>until</code> element. * */ public GQuery prevUntil(GQuery until, String filter) { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { allPreviousSiblingElements(getPreviousSiblingElement(e), result, until, filter); } return pushStack(unique(result), "prevUntil", getSelector()); }
/** * Get all following siblings of each element up to but not including the element matched by the * GQuery object, filtered by a selector. * * @return */ public GQuery nextUntil(GQuery until, String filter) { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { allNextSiblingElements(e.getNextSiblingElement(), result, null, until, filter); } return pushStack(unique(result), "nextUntil", getSelector()); }
/** * Get all preceding siblings of each element in the set of matched elements filtered by a * selector. */ public GQuery prevAll(String selector) { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { allPreviousSiblingElements(getPreviousSiblingElement(e), result, null, selector); } return pushStack(unique(result), "prevAll", getSelector()); }
/** * Get a set of elements containing the unique next siblings of each of the given set of elements. * next only returns the very next sibling for each element, not all next siblings see {#nextAll}. */ public GQuery next() { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { Element next = e.getNextSiblingElement(); if (next != null) { result.addNode(next); } } return pushStack(unique(result), "next", getSelector()); }
/** * Get a set of elements containing the unique previous siblings of each of the matched set of * elements filtered by selector. Only the immediately previous sibling is returned, not all * previous siblings. */ public GQuery prev(String... selectors) { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { Element next = getPreviousSiblingElement(e); if (next != null) { result.addNode(next); } } return new GQuery(unique(result)).filter(selectors); }
private GQuery parentsUntil(Predicate predicate) { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { int i = 0; Node par = e.getParentNode(); while (par != null && par != document) { if (predicate.f(par, i)) { break; } result.addNode(par); par = par.getParentNode(); i++; } } return new GQuery(unique(result)).setPreviousObject(this); }
/** * Find all the child nodes inside the matched elements (including text nodes), or the content * document, if the element is an iframe. */ public GQuery contents() { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { if (JsUtils.isWindow(e) || "iframe".equalsIgnoreCase(e.getTagName())) { result.addNode(getStyleImpl().getContentDocument(e)); } else { NodeList<Node> children = e.getChildNodes(); for (int i = 0, l = children.getLength(); i < l; i++) { result.addNode(children.getItem(i)); } } } return new GQuery(unique(result)); }
/** * Get the first ancestor element that matches the selector (for each matched element), beginning * at the current element and progressing up through the DOM tree until reach the * <code>context</code> node. * * If no context is passed in then the context of the gQuery object will be used instead. * */ public GQuery closest(String selector, Node context) { assert selector != null; if (context == null) { context = currentContext; } GQuery pos = posRegex.test(selector) ? $(selector, context) : null; JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { Element current = e; while (current != null && current.getOwnerDocument() != null && current != context) { boolean match = pos != null ? pos.index(current) > -1 : $(current).is(selector); if (match) { result.addNode(current); break; } else { current = current.getParentElement(); } } } return $(unique(result)); }