/** * Revert the most recent 'destructive' operation, changing the set of matched elements to its * previous state (right before the destructive operation). */ public GQuery end() { return previousObject != null ? previousObject : new GQuery(); }
/** * Wrap a GQuery around an existing element. */ public static GQuery $(Element element) { return new GQuery(element); }
/** * Wrap a GQuery around existing Elements. */ public static GQuery $(NodeList<Element> elms) { return new GQuery(elms); }
/** * Create an empty GQuery object. */ public static GQuery $() { return new GQuery(JsNodeArray.create()); }
protected GQuery pushStack(JsNodeArray elts, String name, String selector) { GQuery g = new GQuery(elts); g.setPreviousObject(this); g.setSelector(selector); g.currentContext = currentContext; return g; }
/** * This function accepts a string containing a CSS selector which is then used to match a set of * elements, or it accepts raw HTML creating a GQuery element containing those elements. The * second parameter is the context to use for the selector, or the document where the new elements * will be created. * * Xpath selector is supported in browsers with native xpath engine. */ public static GQuery $(String selectorOrHtml, Node ctx) { String selector = null; if (selectorOrHtml == null || (selector = selectorOrHtml.trim()).length() == 0) { return $(); } if (selector.startsWith("<")) { return innerHtml(selectorOrHtml, JsUtils.getOwnerDocument(ctx)); } return new GQuery().select(selectorOrHtml, ctx); }
/** * This function accepts a string containing a CSS selector which is then used to match a set of * elements, or it accepts raw HTML creating a GQuery element containing those elements. The * second parameter is the context to use for the selector. The third parameter is the class * plugin to use. * * Xpath selector is supported in browsers with native xpath engine. */ @SuppressWarnings("unchecked") public static <T extends GQuery> T $(String selector, Node context, Class<T> plugin) { try { if (plugins != null) { T gquery = (T) plugins.get(plugin).init(new GQuery().select(selector, context)); return gquery; } throw new RuntimeException("No plugin for class " + plugin); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Clone matched DOM Elements and select the clones. This is useful for moving copies of the * elements to another location in the DOM. */ public GQuery clone() { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { result.addNode(e.cloneNode(true)); } GQuery ret = new GQuery(result); ret.currentContext = currentContext; ret.currentSelector = currentSelector; return ret; }
/** * 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 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 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)); }
/** * Selects a subset of the matched elements. */ public GQuery slice(int start, int end) { JsNodeArray slice = JsNodeArray.create(); int l = size(); if (end == -1 || end > l) { end = l; } for (int i = start; i < end; i++) { slice.addNode(get(i)); } return new GQuery(slice); }
/** * 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)); }
/** * Returns a GQuery collection with the positioned parent of the first matched element. This is * the first parent of the element that has position (as in relative or absolute). This method * only works with visible elements. */ public GQuery offsetParent() { if (isEmpty()) { return $(); } Element offParent = JsUtils.or(get(0).getOffsetParent(), body); while (offParent != null && !"body".equalsIgnoreCase(offParent.getTagName()) && !"html".equalsIgnoreCase(offParent.getTagName()) && "static".equals(getStyleImpl().curCSS(offParent, "position", true))) { offParent = offParent.getOffsetParent(); } return new GQuery(offParent); }
/** * 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); }
/** * Create a new GQuery given a list of {@link com.google.gwt.dom.client.Node} or * {@link com.google.gwt.user.client.ui.IsWidget}. */ public static GQuery $(List<?> nodesOrWidgets) { JsNodeArray elms = JsNodeArray.create(); if (nodesOrWidgets != null) { for (Object o : nodesOrWidgets) { if (o instanceof Node) { elms.addNode((Node) o); } else if (o instanceof IsWidget) { elms.addNode(((IsWidget) o).asWidget().getElement()); } } } return new GQuery(elms); }
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)); }