Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for match with quoted pattern #1536

Merged
merged 2 commits into from Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main/java/org/jsoup/parser/TokenQueue.java
Expand Up @@ -264,6 +264,7 @@ public String chompBalanced(char open, char close) {
char last = 0;
boolean inSingleQuote = false;
boolean inDoubleQuote = false;
boolean inRegexQE = false; // regex \Q .. \E escapes from Pattern.quote()

do {
if (isEmpty()) break;
Expand All @@ -273,8 +274,10 @@ public String chompBalanced(char open, char close) {
inSingleQuote = !inSingleQuote;
else if (c == '"' && c != open && !inSingleQuote)
inDoubleQuote = !inDoubleQuote;
if (inSingleQuote || inDoubleQuote)
if (inSingleQuote || inDoubleQuote || inRegexQE){
last = c;
continue;
}

if (c == open) {
depth++;
Expand All @@ -283,6 +286,10 @@ else if (c == '"' && c != open && !inSingleQuote)
}
else if (c == close)
depth--;
} else if (c == 'Q') {
inRegexQE = true;
} else if (c == 'E') {
inRegexQE = false;
}

if (depth > 0 && last != 0)
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jsoup/select/Selector.java
Expand Up @@ -73,6 +73,8 @@
* <tr><td><code>:empty</code></td><td>elements that have no children at all</td><td></td></tr>
* </table>
*
* <p>A word on using regular expressions in these selectors: depending on the content of the regex, you will need to quote the pattern using <b><code>Pattern.quote("regex")</code></b> for it to parse correclty through both the selector parser and the regex parser. E.g. <code>String query = "div:matches(" + Pattern.quote(regex) + ");"</code>.</p>
*
* @author Jonathan Hedley, jonathan@hedley.net
* @see Element#select(String)
*/
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/jsoup/parser/TokenQueueTest.java
@@ -1,8 +1,11 @@
package org.jsoup.parser;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.jupiter.api.Test;

import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -95,4 +98,12 @@ public void chompBalancedThrowIllegalArgumentException() {
assertEquals("Did not find balanced marker at 'something(or another)) else'", expected.getMessage());
}
}

@Test
public void testQuotedPattern() {
final Document doc = Jsoup.parse("<div>\\) foo1</div><div>( foo2</div><div>1) foo3</div>");
assertEquals("\n\\) foo1",doc.select("div:matches(" + Pattern.quote("\\)") + ")").get(0).childNode(0).toString());
assertEquals("\n( foo2",doc.select("div:matches(" + Pattern.quote("(") + ")").get(0).childNode(0).toString());
assertEquals("\n1) foo3",doc.select("div:matches(" + Pattern.quote("1)") + ")").get(0).childNode(0).toString());
}
}