Skip to content

Commit

Permalink
Allow selectors to contain unbalanced braces
Browse files Browse the repository at this point in the history
Fixes #611
  • Loading branch information
jhy committed May 8, 2016
1 parent c090381 commit a229d73
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ jsoup changelog
* Fixed an issue where created XML documents would have an incorrect prolog.
<https://github.com/jhy/jsoup/issues/652>

* Fixed an issue where you could not use an attribute selector to find values containing unbalanced braces or
parentheses.
<https://github.com/jhy/jsoup/issues/611>

* Fixed an issue where namespaced tags (like <fb:comment>) would cause Element.cssSelector() to fail.
<https://github.com/jhy/jsoup/pull/677>

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/jsoup/parser/TokenQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public String chompToIgnoreCase(String seq) {

/**
* Pulls a balanced string off the queue. E.g. if queue is "(one (two) three) four", (,) will return "one (two) three",
* and leave " four" on the queue. Unbalanced openers and closers can be escaped (with \). Those escapes will be left
* and leave " four" on the queue. Unbalanced openers and closers can quoted (with ' or ") or escaped (with \). Those escapes will be left
* in the returned string, which is suitable for regexes (where we need to preserve the escape), but unsuitable for
* contains text strings; use unescape for that.
* @param open opener
Expand All @@ -262,11 +262,16 @@ public String chompBalanced(char open, char close) {
int end = -1;
int depth = 0;
char last = 0;
boolean inQuote = false;

do {
if (isEmpty()) break;
Character c = consume();
if (last == 0 || last != ESC) {
if (c.equals('\'') || c.equals('"') && c != open)
inQuote = !inQuote;
if (inQuote)
continue;
if (c.equals(open)) {
depth++;
if (start == -1)
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/jsoup/select/SelectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -661,4 +661,11 @@ public void selectClassWithSpace() {
Elements subSelect = els.select(":contains(one)");
assertEquals(2, subSelect.size());
}

@Test public void attributeWithBrackets() {
String html = "<div data='End]'>One</div> <div data='[Another)]]'>Two</div>";
Document doc = Jsoup.parse(html);
assertEquals("One", doc.select("div[data='End]'").first().text());
assertEquals("Two", doc.select("div[data='[Another)]]'").first().text());
}
}

0 comments on commit a229d73

Please sign in to comment.