/** * Find matching elements within this element list. * @param query A {@link Selector} query * @return the filtered list of elements, or an empty list if none match. */ public Elements select(String query) { return Selector.select(query, this); }
/** * Find elements that match the {@link Selector} CSS query, with this element as the starting context. Matched elements * may include this element, or any of its children. * <p> * This method is generally more powerful to use than the DOM-type {@code getElementBy*} methods, because * multiple filters can be combined, e.g.: * </p> * <ul> * <li>{@code el.select("a[href]")} - finds links ({@code a} tags with {@code href} attributes) * <li>{@code el.select("a[href*=example.com]")} - finds links pointing to example.com (loosely) * </ul> * <p> * See the query syntax documentation in {@link org.jsoup.select.Selector}. * </p> * * @param cssQuery a {@link Selector} CSS-like query * @return elements that match the query (empty if none match) * @see org.jsoup.select.Selector * @throws Selector.SelectorParseException (unchecked) on an invalid CSS query. */ public Elements select(String cssQuery) { return Selector.select(cssQuery, this); }
/** * Remove elements from this list that match the {@link Selector} query. * <p> * E.g. HTML: {@code <div class=logo>One</div> <div>Two</div>}<br> * <code>Elements divs = doc.select("div").not(".logo");</code><br> * Result: {@code divs: [<div>Two</div>]} * <p> * @param query the selector query whose results should be removed from these elements * @return a new elements list that contains only the filtered results */ public Elements not(String query) { Elements out = Selector.select(query, this); return Selector.filterOut(this, out); }
/** * Find elements matching selector. * * @param query CSS selector * @param root root element to descend into * @return matching elements, empty if none * @throws Selector.SelectorParseException (unchecked) on an invalid CSS query. */ public static Elements select(String query, Element root) { Validate.notEmpty(query); return select(QueryParser.parse(query), root); }
/** * Find elements matching selector. * * @param query CSS selector * @param roots root elements to descend into * @return matching elements, empty if none */ public static Elements select(String query, Iterable<Element> roots) { Validate.notEmpty(query); Validate.notNull(roots); Evaluator evaluator = QueryParser.parse(query); ArrayList<Element> elements = new ArrayList<>(); IdentityHashMap<Element, Boolean> seenElements = new IdentityHashMap<>(); // dedupe elements by identity, not equality for (Element root : roots) { final Elements found = select(evaluator, root); for (Element el : found) { if (!seenElements.containsKey(el)) { elements.add(el); seenElements.put(el, Boolean.TRUE); } } } return new Elements(elements); }
for (Element element : Selector.select("[style]", body)) { //$NON-NLS-1$ String style = element.attr("style"); //$NON-NLS-1$
for (Element element : Selector.select("[style], font, span", body)) { //$NON-NLS-1$ String style = element.attr("style"); //$NON-NLS-1$