Skip to content

Commit 63023f8

Browse files
committed
Added Elements.deselectAll(), like clear() but no DOM impact
Fixes #2100 Also added see references
1 parent aa6e0d9 commit 63023f8

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
* `Element.cssSelector()` will prefer to return shorter selectors by using ancestor IDs when available and unique. E.g.
1010
`#id > div > p` instead of `html > body > div > div > p` [#2283](https://github.com/jhy/jsoup/pull/2283).
11-
* Added `Elements.deselect(int index)` and `Elements.deselect(Object o)` methods to remove elements from the `Elements`
11+
* Added `Elements.deselect(int index)`, `Elements.deselect(Object o)`, and `Elements.deselectAll()` methods to remove
12+
elements from the `Elements`
1213
list without affecting the DOM. And added `Elements.asList()` method to get a modifiable list of elements without
1314
affecting the DOM. (Each Element is still connected to the DOM.) [#2100](https://github.com/jhy/jsoup/issues/2100).
1415

src/main/java/org/jsoup/select/Elements.java

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,10 @@ private <T extends Node> List<T> childNodesOfType(Class<T> tClass) {
732732

733733
/**
734734
Remove the Element at the specified index in this ist, and from the DOM.
735-
* @param index the index of the element to be removed
736-
* @return the old element at this index
737-
* @since 1.17.1
735+
@param index the index of the element to be removed
736+
@return the old element at this index
737+
@see #deselect(int)
738+
@since 1.17.1
738739
*/
739740
@Override public Element remove(int index) {
740741
Element old = super.remove(index);
@@ -744,9 +745,10 @@ private <T extends Node> List<T> childNodesOfType(Class<T> tClass) {
744745

745746
/**
746747
Remove the specified Element from this list, and from the DOM.
747-
* @param o element to be removed from this list, if present
748-
* @return if this list contained the Element
749-
* @since 1.17.1
748+
@param o element to be removed from this list, if present
749+
@return if this list contained the Element
750+
@see #deselect(Object)
751+
@since 1.17.1
750752
*/
751753
@Override public boolean remove(Object o) {
752754
int index = super.indexOf(o);
@@ -759,35 +761,46 @@ private <T extends Node> List<T> childNodesOfType(Class<T> tClass) {
759761
}
760762

761763
/**
762-
* Remove the Element at the specified index in this list, but not from the DOM.
763-
* @param index the index of the element to be removed
764-
* @return the old element at this index
765-
* @since 1.19.2
764+
Remove the Element at the specified index in this list, but not from the DOM.
765+
@param index the index of the element to be removed
766+
@return the old element at this index
767+
@see #remove(int)
768+
@since 1.19.2
766769
*/
767770
public Element deselect(int index) {
768771
return super.remove(index);
769772
}
770773

771774
/**
772-
* Remove the specified Element from this list, but not from the DOM.
773-
* @param o element to be removed from this list, if present
774-
* @return if this list contained the Element
775-
* @since 1.19.2
775+
Remove the specified Element from this list, but not from the DOM.
776+
@param o element to be removed from this list, if present
777+
@return if this list contained the Element
778+
@see #remove(Object)
779+
@since 1.19.2
776780
*/
777781
public boolean deselect(Object o) {
778782
return super.remove(o);
779783
}
780784

781785
/**
782786
Removes all the elements from this list, and each of them from the DOM.
783-
* @since 1.17.1
784-
* @see #remove()
787+
@since 1.17.1
788+
@see #deselectAll()
785789
*/
786790
@Override public void clear() {
787791
remove();
788792
super.clear();
789793
}
790794

795+
/**
796+
Like {@link #clear()}, removes all the elements from this list, but not from the DOM.
797+
@see #clear()
798+
@since 1.19.2
799+
*/
800+
public void deselectAll() {
801+
super.clear();
802+
}
803+
791804
/**
792805
Removes from this list, and from the DOM, each of the elements that are contained in the specified collection and
793806
are in this list.

src/test/java/org/jsoup/nodes/ElementTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3097,4 +3097,15 @@ public void deselect() {
30973097
assertEquals(1, els.size());
30983098
assertEquals(3, parent.childrenSize());
30993099
}
3100+
3101+
@Test
3102+
public void deselectAll() {
3103+
Document doc = Jsoup.parse("<div><p>One</p><p>Two</p><p>Three</p></div>");
3104+
Elements els = doc.select("p");
3105+
Element parent = doc.expectFirst("div");
3106+
3107+
els.deselectAll();
3108+
assertEquals(0, els.size());
3109+
assertEquals(3, parent.childrenSize());
3110+
}
31003111
}

0 commit comments

Comments
 (0)