summaryrefslogtreecommitdiffstats
path: root/server/src/org/jsoup/select/Elements.java
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/org/jsoup/select/Elements.java')
-rw-r--r--server/src/org/jsoup/select/Elements.java442
1 files changed, 305 insertions, 137 deletions
diff --git a/server/src/org/jsoup/select/Elements.java b/server/src/org/jsoup/select/Elements.java
index 8302da1e53..cddea67d96 100644
--- a/server/src/org/jsoup/select/Elements.java
+++ b/server/src/org/jsoup/select/Elements.java
@@ -1,17 +1,26 @@
package org.jsoup.select;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.ListIterator;
+
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.
- <p/>
- To get an Elements object, use the {@link Element#select(String)} method.
-
- @author Jonathan Hedley, jonathan@hedley.net */
+ * A list of {@link Element Elements}, with methods that act on every element in
+ * the list.
+ * <p/>
+ * To get an Elements object, use the {@link Element#select(String)} method.
+ *
+ * @author Jonathan Hedley, jonathan@hedley.net
+ */
public class Elements implements List<Element>, Cloneable {
private List<Element> contents;
@@ -26,59 +35,70 @@ public class Elements implements List<Element>, Cloneable {
public Elements(Collection<Element> elements) {
contents = new ArrayList<Element>(elements);
}
-
+
public Elements(List<Element> elements) {
contents = elements;
}
-
+
public Elements(Element... elements) {
this(Arrays.asList(elements));
}
-
+
@Override
- public Elements clone() {
- List<Element> elements = new ArrayList<Element>();
-
- for(Element e : contents)
- elements.add(e.clone());
-
-
- return new Elements(elements);
- }
-
- // attribute methods
- /**
- Get an attribute value from the first matched element that has the attribute.
- @param attributeKey The attribute key.
- @return The attribute value from the first matched element that has the attribute.. If no elements were matched (isEmpty() == true),
- or if the no elements have the attribute, returns empty string.
- @see #hasAttr(String)
+ public Elements clone() {
+ List<Element> elements = new ArrayList<Element>();
+
+ for (Element e : contents) {
+ elements.add(e.clone());
+ }
+
+ return new Elements(elements);
+ }
+
+ // attribute methods
+ /**
+ * Get an attribute value from the first matched element that has the
+ * attribute.
+ *
+ * @param attributeKey
+ * The attribute key.
+ * @return The attribute value from the first matched element that has the
+ * attribute.. If no elements were matched (isEmpty() == true), or
+ * if the no elements have the attribute, returns empty string.
+ * @see #hasAttr(String)
*/
public String attr(String attributeKey) {
for (Element element : contents) {
- if (element.hasAttr(attributeKey))
+ if (element.hasAttr(attributeKey)) {
return element.attr(attributeKey);
+ }
}
return "";
}
/**
- Checks if any of the matched elements have this attribute set.
- @param attributeKey attribute key
- @return true if any of the elements have the attribute; false if none do.
+ * Checks if any of the matched elements have this attribute set.
+ *
+ * @param attributeKey
+ * attribute key
+ * @return true if any of the elements have the attribute; false if none do.
*/
public boolean hasAttr(String attributeKey) {
for (Element element : contents) {
- if (element.hasAttr(attributeKey))
+ if (element.hasAttr(attributeKey)) {
return true;
+ }
}
return false;
}
/**
* Set an attribute on all matched elements.
- * @param attributeKey attribute key
- * @param attributeValue attribute value
+ *
+ * @param attributeKey
+ * attribute key
+ * @param attributeValue
+ * attribute value
* @return this
*/
public Elements attr(String attributeKey, String attributeValue) {
@@ -90,7 +110,9 @@ public class Elements implements List<Element>, Cloneable {
/**
* Remove an attribute from every matched element.
- * @param attributeKey The attribute to remove.
+ *
+ * @param attributeKey
+ * The attribute to remove.
* @return this (for chaining)
*/
public Elements removeAttr(String attributeKey) {
@@ -101,9 +123,11 @@ public class Elements implements List<Element>, Cloneable {
}
/**
- Add the class name to every matched element's {@code class} attribute.
- @param className class name to add
- @return this
+ * Add the class name to every matched element's {@code class} attribute.
+ *
+ * @param className
+ * class name to add
+ * @return this
*/
public Elements addClass(String className) {
for (Element element : contents) {
@@ -113,9 +137,12 @@ public class Elements implements List<Element>, Cloneable {
}
/**
- Remove the class name from every matched element's {@code class} attribute, if present.
- @param className class name to remove
- @return this
+ * Remove the class name from every matched element's {@code class}
+ * attribute, if present.
+ *
+ * @param className
+ * class name to remove
+ * @return this
*/
public Elements removeClass(String className) {
for (Element element : contents) {
@@ -125,9 +152,12 @@ public class Elements implements List<Element>, Cloneable {
}
/**
- Toggle the class name on every matched element's {@code class} attribute.
- @param className class name to add if missing, or remove if present, from every element.
- @return this
+ * Toggle the class name on every matched element's {@code class} attribute.
+ *
+ * @param className
+ * class name to add if missing, or remove if present, from every
+ * element.
+ * @return this
*/
public Elements toggleClass(String className) {
for (Element element : contents) {
@@ -137,69 +167,83 @@ public class Elements implements List<Element>, Cloneable {
}
/**
- Determine if any of the matched elements have this class name set in their {@code class} attribute.
- @param className class name to check for
- @return true if any do, false if none do
+ * Determine if any of the matched elements have this class name set in
+ * their {@code class} attribute.
+ *
+ * @param className
+ * class name to check for
+ * @return true if any do, false if none do
*/
public boolean hasClass(String className) {
for (Element element : contents) {
- if (element.hasClass(className))
+ if (element.hasClass(className)) {
return true;
+ }
}
return false;
}
-
+
/**
* Get the form element's value of the first matched element.
+ *
* @return The form element's value, or empty if not set.
* @see Element#val()
*/
public String val() {
- if (size() > 0)
+ if (size() > 0) {
return first().val();
- else
+ } else {
return "";
+ }
}
-
+
/**
* Set the form element's value in each of the matched elements.
- * @param value The value to set into each matched element
+ *
+ * @param value
+ * The value to set into each matched element
* @return this (for chaining)
*/
public Elements val(String value) {
- for (Element element : contents)
+ for (Element element : contents) {
element.val(value);
+ }
return this;
}
-
+
/**
* Get the combined text of all the matched elements.
* <p>
- * 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.
+ * 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)
+ if (sb.length() != 0) {
sb.append(" ");
+ }
sb.append(element.text());
}
return sb.toString();
}
public boolean hasText() {
- for (Element element: contents) {
- if (element.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()
@@ -207,15 +251,17 @@ public class Elements implements List<Element>, Cloneable {
public String html() {
StringBuilder sb = new StringBuilder();
for (Element element : contents) {
- if (sb.length() != 0)
+ 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()
@@ -223,27 +269,33 @@ public class Elements implements List<Element>, Cloneable {
public String outerHtml() {
StringBuilder sb = new StringBuilder();
for (Element element : contents) {
- if (sb.length() != 0)
+ 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()}.
+ * 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()
*/
+ @Override
public String toString() {
return outerHtml();
}
/**
- * Update the tag name of each matched element. For example, to change each {@code <i>} to a {@code <em>}, do
- * {@code doc.select("i").tagName("em");}
- * @param tagName the new tag name
+ * Update the tag name of each matched element. For example, to change each
+ * {@code <i>} to a {@code <em>}, do {@code doc.select("i").tagName("em");}
+ *
+ * @param tagName
+ * the new tag name
* @return this, for chaining
* @see Element#tagName(String)
*/
@@ -253,10 +305,12 @@ public class Elements implements List<Element>, Cloneable {
}
return this;
}
-
+
/**
* Set the inner HTML of each matched element.
- * @param html HTML to parse and set into each matched element.
+ *
+ * @param html
+ * HTML to parse and set into each matched element.
* @return this, for chaining
* @see Element#html(String)
*/
@@ -266,10 +320,12 @@ public class Elements implements List<Element>, Cloneable {
}
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
+ *
+ * @param html
+ * HTML to add inside each element, before the existing HTML
* @return this, for chaining
* @see Element#prepend(String)
*/
@@ -279,10 +335,12 @@ public class Elements implements List<Element>, Cloneable {
}
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
+ *
+ * @param html
+ * HTML to add inside each element, after the existing HTML
* @return this, for chaining
* @see Element#append(String)
*/
@@ -292,10 +350,12 @@ public class Elements implements List<Element>, Cloneable {
}
return this;
}
-
+
/**
* Insert the supplied HTML before each matched element's outer HTML.
- * @param html HTML to insert before each element
+ *
+ * @param html
+ * HTML to insert before each element
* @return this, for chaining
* @see Element#before(String)
*/
@@ -305,10 +365,12 @@ public class Elements implements List<Element>, Cloneable {
}
return this;
}
-
+
/**
* Insert the supplied HTML after each matched element's outer HTML.
- * @param html HTML to insert after each element
+ *
+ * @param html
+ * HTML to insert after each element
* @return this, for chaining
* @see Element#after(String)
*/
@@ -320,13 +382,16 @@ public class Elements implements List<Element>, Cloneable {
}
/**
- Wrap the supplied HTML around each matched elements. For example, with HTML
- {@code <p><b>This</b> is <b>Jsoup</b></p>},
- <code>doc.select("b").wrap("&lt;i&gt;&lt;/i&gt;");</code>
- becomes {@code <p><i><b>This</b></i> is <i><b>jsoup</b></i></p>}
- @param html HTML to wrap around each element, e.g. {@code <div class="head"></div>}. Can be arbitrarily deep.
- @return this (for chaining)
- @see Element#wrap
+ * Wrap the supplied HTML around each matched elements. For example, with
+ * HTML {@code <p><b>This</b> is <b>Jsoup</b></p>},
+ * <code>doc.select("b").wrap("&lt;i&gt;&lt;/i&gt;");</code> becomes
+ * {@code <p><i><b>This</b></i> is <i><b>jsoup</b></i></p>}
+ *
+ * @param html
+ * HTML to wrap around each element, e.g.
+ * {@code <div class="head"></div>}. Can be arbitrarily deep.
+ * @return this (for chaining)
+ * @see Element#wrap
*/
public Elements wrap(String html) {
Validate.notEmpty(html);
@@ -337,15 +402,18 @@ public class Elements implements List<Element>, Cloneable {
}
/**
- * Removes the matched elements from the DOM, and moves their children up into their parents. This has the effect of
- * dropping the elements but keeping their children.
+ * Removes the matched elements from the DOM, and moves their children up
+ * into their parents. This has the effect of dropping the elements but
+ * keeping their children.
* <p/>
- * This is useful for e.g removing unwanted formatting elements but keeping their contents.
+ * This is useful for e.g removing unwanted formatting elements but keeping
+ * their contents.
* <p/>
- * E.g. with HTML: {@code <div><font>One</font> <font><a href="/">Two</a></font></div>}<br/>
+ * E.g. with HTML:
+ * {@code <div><font>One</font> <font><a href="/">Two</a></font></div>}<br/>
* {@code doc.select("font").unwrap();}<br/>
* HTML = {@code <div>One <a href="/">Two</a></div>}
- *
+ *
* @return this (for chaining)
* @see Node#unwrap
*/
@@ -357,12 +425,16 @@ public class Elements implements List<Element>, Cloneable {
}
/**
- * Empty (remove all child nodes from) each matched element. This is similar to setting the inner HTML of each
- * element to nothing.
+ * Empty (remove all child nodes from) each matched element. This is similar
+ * to setting the inner HTML of each element to nothing.
* <p>
* E.g. HTML: {@code <div><p>Hello <b>there</b></p> <p>now</p></div>}<br>
* <code>doc.select("p").empty();</code><br>
- * HTML = {@code <div><p></p> <p></p></div>}
+ * HTML = {@code <div>
+ * <p></p>
+ * <p></p>
+ * </div>}
+ *
* @return this, for chaining
* @see Element#empty()
* @see #remove()
@@ -375,13 +447,16 @@ public class Elements implements List<Element>, Cloneable {
}
/**
- * Remove each matched element from the DOM. This is similar to setting the outer HTML of each element to nothing.
+ * Remove each matched element from the DOM. This is similar to setting the
+ * outer HTML of each element to nothing.
* <p>
* E.g. HTML: {@code <div><p>Hello</p> <p>there</p> <img /></div>}<br>
* <code>doc.select("p").remove();</code><br>
* HTML = {@code <div> <img /></div>}
* <p>
- * Note that this method should not be used to clean user-submitted HTML; rather, use {@link org.jsoup.safety.Cleaner} to clean HTML.
+ * 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()
@@ -392,12 +467,14 @@ public class Elements implements List<Element>, Cloneable {
}
return this;
}
-
+
// filters
-
+
/**
* Find matching elements within this element list.
- * @param query A {@link Selector} query
+ *
+ * @param query
+ * A {@link Selector} query
* @return the filtered list of elements, or an empty list if none match.
*/
public Elements select(String query) {
@@ -411,28 +488,37 @@ public class Elements implements List<Element>, Cloneable {
* <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
+ *
+ * @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 <i>nth</i> matched element as an Elements object.
* <p>
* 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.
+ *
+ * @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();
+ 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
+ *
+ * @param query
+ * A selector
* @return true if at least one element in the list matches the query.
*/
public boolean is(String query) {
@@ -442,11 +528,12 @@ public class Elements implements List<Element>, Cloneable {
/**
* 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() {
HashSet<Element> combo = new LinkedHashSet<Element>();
- for (Element e: contents) {
+ for (Element e : contents) {
combo.addAll(e.parents());
}
return new Elements(combo);
@@ -454,16 +541,20 @@ public class Elements implements List<Element>, Cloneable {
// list-like methods
/**
- Get the first matched element.
- @return The first matched element, or <code>null</code> if contents is empty;
+ * Get the first matched element.
+ *
+ * @return The first matched element, or <code>null</code> 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 <code>null</code> if contents is empty.
+ * Get the last matched element.
+ *
+ * @return The last matched element, or <code>null</code> if contents is
+ * empty.
*/
public Element last() {
return contents.isEmpty() ? null : contents.get(contents.size() - 1);
@@ -471,66 +562,143 @@ public class Elements implements List<Element>, Cloneable {
/**
* Perform a depth-first traversal on each of the selected elements.
- * @param nodeVisitor the visitor callbacks to perform on each node
+ *
+ * @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) {
+ for (Element el : contents) {
traversor.traverse(el);
}
return this;
}
// implements List<Element> delegates:
- public int size() {return contents.size();}
+ @Override
+ public int size() {
+ return contents.size();
+ }
- public boolean isEmpty() {return contents.isEmpty();}
+ @Override
+ public boolean isEmpty() {
+ return contents.isEmpty();
+ }
- public boolean contains(Object o) {return contents.contains(o);}
+ @Override
+ public boolean contains(Object o) {
+ return contents.contains(o);
+ }
- public Iterator<Element> iterator() {return contents.iterator();}
+ @Override
+ public Iterator<Element> iterator() {
+ return contents.iterator();
+ }
- public Object[] toArray() {return contents.toArray();}
+ @Override
+ public Object[] toArray() {
+ return contents.toArray();
+ }
- public <T> T[] toArray(T[] a) {return contents.toArray(a);}
+ @Override
+ public <T> T[] toArray(T[] a) {
+ return contents.toArray(a);
+ }
- public boolean add(Element element) {return contents.add(element);}
+ @Override
+ public boolean add(Element element) {
+ return contents.add(element);
+ }
- public boolean remove(Object o) {return contents.remove(o);}
+ @Override
+ public boolean remove(Object o) {
+ return contents.remove(o);
+ }
- public boolean containsAll(Collection<?> c) {return contents.containsAll(c);}
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ return contents.containsAll(c);
+ }
- public boolean addAll(Collection<? extends Element> c) {return contents.addAll(c);}
+ @Override
+ public boolean addAll(Collection<? extends Element> c) {
+ return contents.addAll(c);
+ }
- public boolean addAll(int index, Collection<? extends Element> c) {return contents.addAll(index, c);}
+ @Override
+ public boolean addAll(int index, Collection<? extends Element> c) {
+ return contents.addAll(index, c);
+ }
- public boolean removeAll(Collection<?> c) {return contents.removeAll(c);}
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ return contents.removeAll(c);
+ }
- public boolean retainAll(Collection<?> c) {return contents.retainAll(c);}
+ @Override
+ public boolean retainAll(Collection<?> c) {
+ return contents.retainAll(c);
+ }
- public void clear() {contents.clear();}
+ @Override
+ public void clear() {
+ contents.clear();
+ }
- public boolean equals(Object o) {return contents.equals(o);}
+ @Override
+ public boolean equals(Object o) {
+ return contents.equals(o);
+ }
- public int hashCode() {return contents.hashCode();}
+ @Override
+ public int hashCode() {
+ return contents.hashCode();
+ }
- public Element get(int index) {return contents.get(index);}
+ @Override
+ public Element get(int index) {
+ return contents.get(index);
+ }
- public Element set(int index, Element element) {return contents.set(index, element);}
+ @Override
+ public Element set(int index, Element element) {
+ return contents.set(index, element);
+ }
- public void add(int index, Element element) {contents.add(index, element);}
+ @Override
+ public void add(int index, Element element) {
+ contents.add(index, element);
+ }
- public Element remove(int index) {return contents.remove(index);}
+ @Override
+ public Element remove(int index) {
+ return contents.remove(index);
+ }
- public int indexOf(Object o) {return contents.indexOf(o);}
+ @Override
+ public int indexOf(Object o) {
+ return contents.indexOf(o);
+ }
- public int lastIndexOf(Object o) {return contents.lastIndexOf(o);}
+ @Override
+ public int lastIndexOf(Object o) {
+ return contents.lastIndexOf(o);
+ }
- public ListIterator<Element> listIterator() {return contents.listIterator();}
+ @Override
+ public ListIterator<Element> listIterator() {
+ return contents.listIterator();
+ }
- public ListIterator<Element> listIterator(int index) {return contents.listIterator(index);}
+ @Override
+ public ListIterator<Element> listIterator(int index) {
+ return contents.listIterator(index);
+ }
- public List<Element> subList(int fromIndex, int toIndex) {return contents.subList(fromIndex, toIndex);}
+ @Override
+ public List<Element> subList(int fromIndex, int toIndex) {
+ return contents.subList(fromIndex, toIndex);
+ }
}