Skip to content

Commit

Permalink
Merge branch 'pr/619'
Browse files Browse the repository at this point in the history
  • Loading branch information
jhy committed Aug 30, 2015
2 parents 658bb5c + b79afd3 commit 3bdad1f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
jsoup changelog

*** Release 1.8.4 [PENDING]
* Fixed an issue in the Parent selector where it would not match against the root element it was applied to.
<https://github.com/jhy/jsoup/pull/619>

*** Release 1.8.3 [2015-Aug-02]
* Added support for custom boolean attributes.
<https://github.com/jhy/jsoup/pull/555>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/jsoup/select/StructuralEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ public boolean matches(Element root, Element element) {
return false;

Element parent = element.parent();
while (parent != root) {
while (true) {
if (evaluator.matches(root, parent))
return true;
if (parent == root)
break;
parent = parent.parent();
}
return false;
Expand Down
19 changes: 15 additions & 4 deletions src/test/java/org/jsoup/select/SelectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,22 @@ public class SelectorTest {
@Test public void descendant() {
String h = "<div class=head><p class=first>Hello</p><p>There</p></div><p>None</p>";
Document doc = Jsoup.parse(h);
Elements els = doc.select(".head p");
Element root = doc.getElementsByClass("head").first();

Elements els = root.select(".head p");
assertEquals(2, els.size());
assertEquals("Hello", els.get(0).text());
assertEquals("There", els.get(1).text());

Elements p = doc.select("p.first");
Elements p = root.select("p.first");
assertEquals(1, p.size());
assertEquals("Hello", p.get(0).text());

Elements empty = doc.select("p .first"); // self, not descend, should not match
Elements empty = root.select("p .first"); // self, not descend, should not match
assertEquals(0, empty.size());

Elements aboveRoot = root.select("body div.head");
assertEquals(0, aboveRoot.size());
}

@Test public void and() {
Expand All @@ -236,10 +241,16 @@ public class SelectorTest {

@Test public void deeperDescendant() {
String h = "<div class=head><p><span class=first>Hello</div><div class=head><p class=first><span>Another</span><p>Again</div>";
Elements els = Jsoup.parse(h).select("div p .first");
Document doc = Jsoup.parse(h);
Element root = doc.getElementsByClass("head").first();

Elements els = root.select("div p .first");
assertEquals(1, els.size());
assertEquals("Hello", els.first().text());
assertEquals("span", els.first().tagName());

Elements aboveRoot = root.select("body p .first");
assertEquals(0, aboveRoot.size());
}

@Test public void parentChildElement() {
Expand Down

0 comments on commit 3bdad1f

Please sign in to comment.