Skip to content

Commit

Permalink
Fix not escaping special characters in search pattern
Browse files Browse the repository at this point in the history
fixes JabRef#5892

* add method to get search pattern for searched words with escaped
 javascript regexp special characters (for search without regular
 expressions)

* in preview viewer use search pattern with escaped javascript regexp
 special characters
  • Loading branch information
dawidm committed Feb 11, 2020
1 parent 259bca0 commit 2f68520
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/preview/PreviewViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class PreviewViewer extends ScrollPane implements InvalidationListener {
private boolean registered;

private ChangeListener<Optional<SearchQuery>> listener = (queryObservable, queryOldValue, queryNewValue) -> {
searchHighlightPattern = queryNewValue.flatMap(SearchQuery::getPatternForWords);
searchHighlightPattern = queryNewValue.flatMap(SearchQuery::getJsPatternForWords);
highlightSearchPattern();
};

Expand Down Expand Up @@ -131,7 +131,7 @@ public void setTheme(String theme) {

private void highlightSearchPattern() {
if (searchHighlightPattern.isPresent()) {
String pattern = searchHighlightPattern.get().pattern().replace("\\Q", "").replace("\\E", "");
String pattern = searchHighlightPattern.get().pattern();

previewView.getEngine().executeScript(
"var markInstance = new Mark(document.getElementById(\"content\"));" +
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/org/jabref/logic/search/SearchQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

public class SearchQuery implements SearchMatcher {

// regexp pattern for escaping special characters in javascript regex
public static final String JAVASCRIPT_ESCAPED_CHARS_PATTERN = "[\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\/]";

private final String query;
private final boolean caseSensitive;
private final boolean regularExpression;
Expand Down Expand Up @@ -124,6 +127,18 @@ public List<String> getSearchWords() {

// Returns a regular expression pattern in the form (w1)|(w2)| ... wi are escaped if no regular expression search is enabled
public Optional<Pattern> getPatternForWords() {
return joinWordsToPattern(false);
}

// Returns a regular expression pattern in the form (w1)|(w2)| ... wi are escaped for javascript if no regular expression search is enabled
public Optional<Pattern> getJsPatternForWords() {
return joinWordsToPattern(true);
}

/* Returns a regular expression pattern in the form (w1)|(w2)| ... wi are escaped if no regular expression search is enabled
* @param escapeSpecialCharsForJS whether to escape characters in wi for javascript regexp (escaping all special characters) or for java (using \Q and \E)
*/
private Optional<Pattern> joinWordsToPattern(boolean escapeSpecialCharsForJS) {
List<String> words = getSearchWords();

if ((words == null) || words.isEmpty() || words.get(0).isEmpty()) {
Expand All @@ -133,7 +148,7 @@ public Optional<Pattern> getPatternForWords() {
// compile the words to a regular expression in the form (w1)|(w2)|(w3)
StringJoiner joiner = new StringJoiner(")|(", "(", ")");
for (String word : words) {
joiner.add(regularExpression ? word : Pattern.quote(word));
joiner.add(regularExpression ? word : (escapeSpecialCharsForJS ? word.replaceAll(JAVASCRIPT_ESCAPED_CHARS_PATTERN, "\\\\$0") : Pattern.quote(word)));
}
String searchPattern = joiner.toString();

Expand Down

0 comments on commit 2f68520

Please sign in to comment.