Permalink
Browse files

Fixed parsing of group-or commas in CSS selectors.

Fixes #179
  • Loading branch information...
jhy committed Apr 3, 2012
1 parent c744b9a commit 249e5a8aae9324cc6c8caf5303a607056b15a9da
View
@@ -1,5 +1,9 @@
jsoup changelog
+*** Release 1.6.3 [PENDING]
+ * Fixed parsing of group-or commas in CSS selectors, to correctly handle sub-queries containing commas.
+ <https://github.com/jhy/jsoup/issues/179>
+
*** Release 1.6.2 [2011-Mar-27]
* Added a simplified XML parsing mode, which can usefully parse valid and invalid XML, but does not enforce any HTML
document structure or special tag behaviour.
@@ -61,6 +61,10 @@ public String toString() {
this.evaluators.addAll(evaluators);
}
+ Or() {
+ super();
+ }
+
public void add(Evaluator e) {
evaluators.add(e);
}
@@ -55,15 +55,7 @@ Evaluator parse() {
// hierarchy and extras
boolean seenWhite = tq.consumeWhitespace();
- if (tq.matchChomp(",")) { // group or
- CombiningEvaluator.Or or = new CombiningEvaluator.Or(evals);
- evals.clear();
- evals.add(or);
- while (!tq.isEmpty()) {
- String subQuery = tq.chompTo(",");
- or.add(parse(subQuery));
- }
- } else if (tq.matchesAny(combinators)) {
+ if (tq.matchesAny(combinators)) {
combinator(tq.consume());
} else if (seenWhite) {
combinator(' ');
@@ -98,6 +90,18 @@ else if (combinator == '+')
evals.add(new CombiningEvaluator.And(f, new StructuralEvaluator.ImmediatePreviousSibling(e)));
else if (combinator == '~')
evals.add(new CombiningEvaluator.And(f, new StructuralEvaluator.PreviousSibling(e)));
+ else if (combinator == ',') { // group or
+ CombiningEvaluator.Or or;
+ if (e instanceof CombiningEvaluator.Or) {
+ or = (CombiningEvaluator.Or) e;
+ or.add(f);
+ } else {
+ or = new CombiningEvaluator.Or();
+ or.add(e);
+ or.add(f);
+ }
+ evals.add(or);
+ }
else
throw new Selector.SelectorParseException("Unknown combinator: " + combinator);
}
@@ -326,10 +326,9 @@
Document doc = Jsoup.parse(h);
Elements els = doc.select(".foo > ol, ol > li + li");
- assertEquals(3, els.size());
- assertEquals("ol", els.get(0).tagName());
- assertEquals("Two", els.get(1).text());
- assertEquals("Three", els.get(2).text());
+ assertEquals(2, els.size());
+ assertEquals("li", els.get(0).tagName());
+ assertEquals("Three", els.get(1).text());
}
@Test public void generalSiblings() {
@@ -585,4 +584,17 @@
assertEquals(1, el1.size());
assertEquals("1", el1.first().id());
}
+
+ @Test public void handlesCommasInSelector() {
+ Document doc = Jsoup.parse("<p name='1,2'>One</p><div>Two</div><ol><li>123</li><li>Text</li></ol>");
+
+ Elements ps = doc.select("[name=1,2]");
+ assertEquals(1, ps.size());
+
+ Elements containers = doc.select("div, li:matches([0-9,]+)");
+ assertEquals(2, containers.size());
+ assertEquals("div", containers.get(0).tagName());
+ assertEquals("li", containers.get(1).tagName());
+ assertEquals("123", containers.get(1).text());
+ }
}

0 comments on commit 249e5a8

Please sign in to comment.