Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement method replaceAll, instead of hiding non supported operations #1526

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/org/jsoup/select/Elements.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.ConcurrentModificationException;
import java.util.function.UnaryOperator;

/**
A list of {@link Element}s, with methods that act on every element in the list.
Expand Down Expand Up @@ -682,5 +685,27 @@ private <T extends Node> List<T> childNodesOfType(Class<T> tClass) {
}
return nodes;
}

/**
* Implement a method to replace tag. The idea comes from Arraylist.replaceAll()
* @param operator a lambda to do the replacement
* @see ArrayList#replaceAll(UnaryOperator)
*/
public void replaceAll(UnaryOperator<Element> operator) {
Objects.requireNonNull(operator);
final int expectedModCount = modCount;
final int size = this.size();

for(int i = 0; this.modCount == expectedModCount && i < size; i++)
{
Element element = this.get(i);
Element replacer = operator.apply(element);
element.replaceWith(replacer);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}

}
132 changes: 132 additions & 0 deletions src/test/java/org/jsoup/select/ElementsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jsoup.Jsoup;
import org.jsoup.TextUtil;
import org.jsoup.nodes.*;
import org.jsoup.parser.Tag;
import org.junit.jupiter.api.Test;

import java.util.List;
Expand All @@ -14,6 +15,137 @@

@author Jonathan Hedley, jonathan@hedley.net */
public class ElementsTest {
@Test public void replaceAlltest01() {
String html =
"<html>" +
"<head>" +
"<meta content=\"text/html\">" +
"<meta name=\"theme-color\">" +
"</head>" +
"<body>" +
"<h1>a head</h1>" +
"<p>a paragraph。</p>" +
"</body>" +
"</html>";
Document document = Jsoup.parse(html);
document.select("meta").replaceAll(element ->
{
return new Element("foo");
});
String expected =
"<html>\n" +
" <head>\n" +
" <foo></foo><foo></foo>\n" +
" </head>\n" +
" <body>\n" +
" <h1>a head</h1>\n" +
" <p>a paragraph。</p>\n" +
" </body>\n" +
"</html>";
assertEquals(expected, document.toString());

}

@Test public void replaceAlltest02() {
String html =
"<html>" +
"<head>" +
"<meta content=\"text/html\">" +
"<meta name=\"theme-color\">" +
"</head>" +
"<body>" +
"<h1>a head</h1>" +
"<p>a paragraph。</p>" +
"</body>" +
"</html>";
Document document = Jsoup.parse(html);
document.select("head").replaceAll(element ->
{
return new Element("foo");
});
String expected =
"<html>\n" +
" <foo></foo>\n" +
" <body>\n" +
" <h1>a head</h1>\n" +
" <p>a paragraph。</p>\n" +
" </body>\n" +
"</html>";
assertEquals(expected, document.toString());
}

@Test public void replaceAlltest03() {
String html =
"<html>" +
"<head>" +
"<meta content=\"text/html\">" +
"<meta name=\"theme-color\">" +
"</head>" +
"<body>" +
"<h1>a head</h1>" +
"<p>a paragraph。</p>" +
"</body>" +
"</html>";
Document document = Jsoup.parse(html);
document.select("body").replaceAll(element ->
{
return new Element("foo");
});
String expected =
"<html>\n" +
" <head>\n" +
" <meta content=\"text/html\">\n" +
" <meta name=\"theme-color\">\n" +
" </head><foo></foo>\n" +
"</html>";
assertEquals(expected, document.toString());
}

@Test public void replaceAlltest04() {
String html =
"<html>" +
"<head>" +
"<meta content=\"text/html\">" +
"<meta name=\"theme-color\">" +
"</head>" +
"<body>" +
"<h1>a head</h1>" +
"<p>a paragraph。</p>" +
"</body>" +
"</html>";
Document document = Jsoup.parse(html);
document.select("html").replaceAll(element ->
{
return new Element("foo");
});
String expected = "<foo></foo>";
assertEquals(expected, document.toString());
}

@Test public void replaceAlltest05() {
String html =
"<html>" +
"<head>" +
"<meta content=\"text/html\">" +
"<meta name=\"theme-color\">" +
"</head>" +
"<body>" +
"<h1>a head</h1>" +
"<p>a paragraph。</p>" +
"</body>" +
"</html>";
Document document = Jsoup.parse(html);
document.select("html").replaceAll(element ->
{
Attributes miao = new Attributes();
miao.add("attribute","value");
return new Element(Tag.valueOf("foo"), "", miao);
});
String expected = "<foo attribute=\"value\"></foo>";
System.out.println(document.toString());
assertEquals(expected, document.toString());
}

@Test public void filter() {
String h = "<p>Excl</p><div class=headline><p>Hello</p><p>There</p></div><div class=headline><h1>Headline</h1></div>";
Document doc = Jsoup.parse(h);
Expand Down