package org.jsoup.select; import org.jsoup.helper.Validate; import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; import java.util.*; /** A list of {@link Element Elements}, with methods that act on every element in the list.
To get an Elements object, use the {@link Element#select(String)} method. @author Jonathan Hedley, jonathan@hedley.net */ public class Elements implements List
* Note that it is possible to get repeats if the matched elements contain both parent elements and their own
* children, as the Element.text() method returns the combined text of a parent and all its children.
* @return string of all text: unescaped and no HTML.
* @see Element#text()
*/
public String text() {
StringBuilder sb = new StringBuilder();
for (Element element : contents) {
if (sb.length() != 0)
sb.append(" ");
sb.append(element.text());
}
return sb.toString();
}
public boolean hasText() {
for (Element element: contents) {
if (element.hasText())
return true;
}
return false;
}
/**
* Get the combined inner HTML of all matched elements.
* @return string of all element's inner HTML.
* @see #text()
* @see #outerHtml()
*/
public String html() {
StringBuilder sb = new StringBuilder();
for (Element element : contents) {
if (sb.length() != 0)
sb.append("\n");
sb.append(element.html());
}
return sb.toString();
}
/**
* Get the combined outer HTML of all matched elements.
* @return string of all element's outer HTML.
* @see #text()
* @see #html()
*/
public String outerHtml() {
StringBuilder sb = new StringBuilder();
for (Element element : contents) {
if (sb.length() != 0)
sb.append("\n");
sb.append(element.outerHtml());
}
return sb.toString();
}
/**
* Get the combined outer HTML of all matched elements. Alias of {@link #outerHtml()}.
* @return string of all element's outer HTML.
* @see #text()
* @see #html()
*/
public String toString() {
return outerHtml();
}
/**
* Update the tag name of each matched element. For example, to change each {@code } to a {@code }, do
* {@code doc.select("i").tagName("em");}
* @param tagName the new tag name
* @return this, for chaining
* @see Element#tagName(String)
*/
public Elements tagName(String tagName) {
for (Element element : contents) {
element.tagName(tagName);
}
return this;
}
/**
* Set the inner HTML of each matched element.
* @param html HTML to parse and set into each matched element.
* @return this, for chaining
* @see Element#html(String)
*/
public Elements html(String html) {
for (Element element : contents) {
element.html(html);
}
return this;
}
/**
* Add the supplied HTML to the start of each matched element's inner HTML.
* @param html HTML to add inside each element, before the existing HTML
* @return this, for chaining
* @see Element#prepend(String)
*/
public Elements prepend(String html) {
for (Element element : contents) {
element.prepend(html);
}
return this;
}
/**
* Add the supplied HTML to the end of each matched element's inner HTML.
* @param html HTML to add inside each element, after the existing HTML
* @return this, for chaining
* @see Element#append(String)
*/
public Elements append(String html) {
for (Element element : contents) {
element.append(html);
}
return this;
}
/**
* Insert the supplied HTML before each matched element's outer HTML.
* @param html HTML to insert before each element
* @return this, for chaining
* @see Element#before(String)
*/
public Elements before(String html) {
for (Element element : contents) {
element.before(html);
}
return this;
}
/**
* Insert the supplied HTML after each matched element's outer HTML.
* @param html HTML to insert after each element
* @return this, for chaining
* @see Element#after(String)
*/
public Elements after(String html) {
for (Element element : contents) {
element.after(html);
}
return this;
}
/**
Wrap the supplied HTML around each matched elements. For example, with HTML
{@code This is Jsoup This is jsoup
* E.g. HTML: {@code Hello there now
* E.g. HTML: {@code Hello there
* Note that this method should not be used to clean user-submitted HTML; rather, use {@link org.jsoup.safety.Cleaner} to clean HTML.
* @return this, for chaining
* @see Element#empty()
* @see #empty()
*/
public Elements remove() {
for (Element element : contents) {
element.remove();
}
return this;
}
// filters
/**
* 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);
}
/**
* Remove elements from this list that match the {@link Selector} query.
*
* E.g. HTML: {@code
* @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);
}
/**
* Get the nth matched element as an Elements object.
*
* See also {@link #get(int)} to retrieve an Element.
* @param index the (zero-based) index of the element in the list to retain
* @return Elements containing only the specified element, or, if that element did not exist, an empty list.
*/
public Elements eq(int index) {
return contents.size() > index ? new Elements(get(index)) : new Elements();
}
/**
* Test if any of the matched elements match the supplied query.
* @param query A selector
* @return true if at least one element in the list matches the query.
*/
public boolean is(String query) {
Elements children = select(query);
return !children.isEmpty();
}
/**
* Get all of the parents and ancestor elements of the matched elements.
* @return all of the parents and ancestor elements of the matched elements
*/
public Elements parents() {
HashSetdoc.select("b").wrap("<i></i>");
becomes {@code
* {@code doc.select("font").unwrap();}
* HTML = {@code
* doc.select("p").empty();
* HTML = {@code
* doc.select("p").remove();
* HTML = {@code
* Elements divs = doc.select("div").not("#logo");
* Result: {@code divs: [null
if contents is empty;
*/
public Element first() {
return contents.isEmpty() ? null : contents.get(0);
}
/**
Get the last matched element.
@return The last matched element, or null
if contents is empty.
*/
public Element last() {
return contents.isEmpty() ? null : contents.get(contents.size() - 1);
}
/**
* Perform a depth-first traversal on each of the selected elements.
* @param nodeVisitor the visitor callbacks to perform on each node
* @return this, for chaining
*/
public Elements traverse(NodeVisitor nodeVisitor) {
Validate.notNull(nodeVisitor);
NodeTraversor traversor = new NodeTraversor(nodeVisitor);
for (Element el: contents) {
traversor.traverse(el);
}
return this;
}
// implements List