Skip to content

Commit 4f70700

Browse files
committed
Implemented Element.is()
1 parent 2c58e97 commit 4f70700

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ jsoup changelog
44
* Improved startup time, particularly on Android, by reducing garbage generation and CPU execution time when loading
55
the HTML entity files. About 1.72x faster in this area.
66

7+
* Added Element.is(query) to check if an element matches this CSS query.
8+
79
* Bugfix: a "SYSTEM" flag in doctype tags would be incorrectly removed.
810
<https://github.com/jhy/jsoup/issues/408>
911

src/main/java/org/jsoup/nodes/Element.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.jsoup.select.Evaluator;
1111
import org.jsoup.select.NodeTraversor;
1212
import org.jsoup.select.NodeVisitor;
13+
import org.jsoup.select.QueryParser;
1314
import org.jsoup.select.Selector;
1415

1516
import java.io.IOException;
@@ -286,6 +287,19 @@ public List<DataNode> dataNodes() {
286287
public Elements select(String cssQuery) {
287288
return Selector.select(cssQuery, this);
288289
}
290+
291+
/**
292+
* Check if this element matches the given {@link Selector} CSS query.
293+
* @param cssQuery a {@link Selector} CSS query
294+
* @return if this element matches the query
295+
*/
296+
public boolean is(String cssQuery) {
297+
return is(QueryParser.parse(cssQuery));
298+
}
299+
300+
public boolean is(Evaluator evaluator) {
301+
return evaluator.matches(this.ownerDocument(), this);
302+
}
289303

290304
/**
291305
* Add a node child node to this element.

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,12 @@ public Elements eq(int index) {
440440
* @return true if at least one element in the list matches the query.
441441
*/
442442
public boolean is(String query) {
443-
Elements children = select(query);
444-
return !children.isEmpty();
443+
Evaluator eval = QueryParser.parse(query);
444+
for (Element e : this) {
445+
if (e.is(eval))
446+
return true;
447+
}
448+
return false;
445449
}
446450

447451
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Parses a CSS selector into an Evaluator tree.
1414
*/
15-
class QueryParser {
15+
public class QueryParser {
1616
private final static String[] combinators = {",", ">", "+", "~", " "};
1717
private static final String[] AttributeEvals = new String[]{"=", "!=", "^=", "$=", "*=", "~="};
1818

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,4 +972,27 @@ public void testChainedRemoveAttributes() {
972972
.removeAttr("five");
973973
assertEquals("<a>Text</a>", a.outerHtml());
974974
}
975+
976+
@Test
977+
public void testIs() {
978+
String html = "<div><p>One <a class=big>Two</a> Three</p><p>Another</p>";
979+
Document doc = Jsoup.parse(html);
980+
Element p = doc.select("p").first();
981+
982+
assertTrue(p.is("p"));
983+
assertFalse(p.is("div"));
984+
assertTrue(p.is("p:has(a)"));
985+
assertTrue(p.is("p:first-child"));
986+
assertFalse(p.is("p:last-child"));
987+
assertTrue(p.is("*"));
988+
assertTrue(p.is("div p"));
989+
990+
Element q = doc.select("p").last();
991+
assertTrue(q.is("p"));
992+
assertTrue(q.is("p ~ p"));
993+
assertTrue(q.is("p + p"));
994+
assertTrue(q.is("p:last-child"));
995+
assertFalse(q.is("p a"));
996+
assertFalse(q.is("a"));
997+
}
975998
}

0 commit comments

Comments
 (0)