-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend Elements to update the backing DOM on set(), remove(), et al
- Loading branch information
Showing
3 changed files
with
294 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,14 +14,17 @@ | |
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.function.Predicate; | ||
import java.util.function.UnaryOperator; | ||
|
||
/** | ||
A list of {@link Element}s, with methods that act on every element in the list. | ||
<p> | ||
To get an {@code Elements} object, use the {@link Element#select(String)} method. | ||
</p> | ||
<p>To get an {@code Elements} object, use the {@link Element#select(String)} method.</p> | ||
<p>Methods that {@link #set(int, Element) set}, {@link #remove(int) remove}, or {@link #replaceAll(UnaryOperator) | ||
replace} Elements in the list will also act on the underlying {@link org.jsoup.nodes.Document DOM}.</p> | ||
@author Jonathan Hedley, [email protected] */ | ||
public class Elements extends ArrayList<Element> { | ||
|
@@ -431,6 +434,7 @@ public Elements empty() { | |
|
||
/** | ||
* Remove each matched element from the DOM. This is similar to setting the outer HTML of each element to nothing. | ||
* <p>The elements will still be retained in this list, in case further processing of them is desired.</p> | ||
* <p> | ||
* E.g. HTML: {@code <div><p>Hello</p> <p>there</p> <img /></div>}<br> | ||
* <code>doc.select("p").remove();</code><br> | ||
|
@@ -440,6 +444,7 @@ public Elements empty() { | |
* @return this, for chaining | ||
* @see Element#empty() | ||
* @see #empty() | ||
* @see #clear() | ||
*/ | ||
public Elements remove() { | ||
for (Element element : this) { | ||
|
@@ -683,4 +688,121 @@ private <T extends Node> List<T> childNodesOfType(Class<T> tClass) { | |
return nodes; | ||
} | ||
|
||
// list methods that update the DOM: | ||
|
||
/** | ||
Replace the Element at the specified index in this list, and in the DOM. | ||
* @param index index of the element to replace | ||
* @param element element to be stored at the specified position | ||
* @return the old Element at this index | ||
* @since 1.17.1 | ||
*/ | ||
@Override public Element set(int index, Element element) { | ||
Validate.notNull(element); | ||
Element old = super.set(index, element); | ||
old.replaceWith(element); | ||
return old; | ||
} | ||
|
||
/** | ||
Remove the Element at the specified index in this ist, and from the DOM. | ||
* @param index the index of the element to be removed | ||
* @return the old element at this index | ||
* @since 1.17.1 | ||
*/ | ||
@Override public Element remove(int index) { | ||
Element old = super.remove(index); | ||
old.remove(); | ||
return old; | ||
} | ||
|
||
/** | ||
Remove the specified Element from this list, and from th DOM | ||
* @param o element to be removed from this list, if present | ||
* @return if this list contained the Element | ||
* @since 1.17.1 | ||
*/ | ||
@Override public boolean remove(Object o) { | ||
int index = super.indexOf(o); | ||
if (index == -1) { | ||
return false; | ||
} else { | ||
remove(index); | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
Removes all the elements from this list, and each of them from the DOM. | ||
* @since 1.17.1 | ||
* @see #remove() | ||
*/ | ||
@Override public void clear() { | ||
remove(); | ||
super.clear(); | ||
} | ||
|
||
/** | ||
Removes from this list, and from the DOM, each of the elements that are contained in the specified collection and | ||
are in this list. | ||
* @param c collection containing elements to be removed from this list | ||
* @return {@code true} if elements were removed from this list | ||
* @since 1.17.1 | ||
*/ | ||
@Override public boolean removeAll(Collection<?> c) { | ||
boolean anyRemoved = false; | ||
for (Object o : c) { | ||
anyRemoved |= this.remove(o); | ||
} | ||
return anyRemoved; | ||
} | ||
|
||
/** | ||
Retain in this list, and in the DOM, only the elements that are in the specified collection and are in this list. | ||
In other words, remove elements from this list and the DOM any item that is in this list but not in the specified | ||
collection. | ||
* @param c collection containing elements to be retained in this list | ||
* @return {@code true} if elements were removed from this list | ||
* @since 1.17.1 | ||
*/ | ||
@Override public boolean retainAll(Collection<?> c) { | ||
boolean anyRemoved = false; | ||
for (Iterator<Element> it = this.iterator(); it.hasNext(); ) { | ||
Element el = it.next(); | ||
if (!c.contains(el)) { | ||
it.remove(); | ||
anyRemoved = true; | ||
} | ||
} | ||
return anyRemoved; | ||
} | ||
|
||
/** | ||
Remove from the list, and from the DOM, all elements in this list that mach the given filter. | ||
* @param filter a predicate which returns {@code true} for elements to be removed | ||
* @return {@code true} if elements were removed from this list | ||
* @since 1.17.1 | ||
*/ | ||
@Override public boolean removeIf(Predicate<? super Element> filter) { | ||
boolean anyRemoved = false; | ||
for (Iterator<Element> it = this.iterator(); it.hasNext(); ) { | ||
Element el = it.next(); | ||
if (filter.test(el)) { | ||
it.remove(); | ||
anyRemoved = true; | ||
} | ||
} | ||
return anyRemoved; | ||
} | ||
|
||
/** | ||
Replace each element in this list with the result of the operator, and update the DOM. | ||
* @param operator the operator to apply to each element | ||
* @since 1.17.1 | ||
*/ | ||
@Override public void replaceAll(UnaryOperator<Element> operator) { | ||
for (int i = 0; i < this.size(); i++) { | ||
this.set(i, operator.apply(this.get(i))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters