Skip to content

Commit

Permalink
Allow combinators at start of selector query
Browse files Browse the repository at this point in the history
Closes jhy#13
  • Loading branch information
jhy committed Feb 24, 2010
1 parent c4b96b3 commit 8b1abce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -7,6 +7,9 @@ jsoup changelog
* Relative links are resolved to absolute when cleaning, to normalize
output and to verify safe protocol. (Were previously discarded.)
<http://github.com/jhy/jsoup/issues/issue/12>

* Allow combinators at start of selector query, for query refinements
<http://github.com/jhy/jsoup/issues/issue/13>

*** Release 0.3.1 (2010-Feb-20)
* New features: supports Elements#html(), html(String),
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org/jsoup/select/Selector.java
Expand Up @@ -87,9 +87,16 @@ public static Elements select(String query, Iterable<Element> roots) {

private Elements select() {
tq.consumeWhitespace();
addElements(findElements()); // chomp first matcher off queue

if (tq.matchesAny(combinators)) { // if starts with a combinator, use root as elements
elements.add(root);
combinator(tq.consume().toString());
} else {
addElements(findElements()); // chomp first element matcher off queue
}

while (!tq.isEmpty()) {
// hierarchy and extras (todo: implement +, ~)
// hierarchy and extras
boolean seenWhite = tq.consumeWhitespace();

if (tq.matchChomp(",")) { // group or
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/jsoup/select/SelectorTest.java
Expand Up @@ -290,4 +290,21 @@ public class SelectorTest {
Element el4 = doc.select(".b2-qux_bif").first();
assertEquals("Two", el4.text());
}

// for http://github.com/jhy/jsoup/issues#issue/13
@Test public void testSupportsLeadingCombinator() {
String h = "<div><p><span>One</span><span>Two</span></p></div>";
Document doc = Jsoup.parse(h);

Element p = doc.select("div > p").first();
Elements spans = p.select("> span");
assertEquals(2, spans.size());
assertEquals("One", spans.first().text());

// make sure doesn't get nested
h = "<div id=1><div id=2><div id=3></div></div></div>";
doc = Jsoup.parse(h);
Element div = doc.select("div").select(" > div").first();
assertEquals("2", div.id());
}
}

0 comments on commit 8b1abce

Please sign in to comment.