Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
* upstream/master:
  Bump classgraph from 4.8.62 to 4.8.64 (#5954)
  Squashed 'src/main/resources/csl-styles/' changes from c531528..9e81857
  Fix not escaping special characters in search pattern (#5938)
  fix missing gson
  • Loading branch information
Siedlerchr committed Feb 18, 2020
2 parents ef4bf81 + 19bfe3e commit 859e826
Show file tree
Hide file tree
Showing 38 changed files with 2,678 additions and 81 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ dependencies {
}


testCompile 'io.github.classgraph:classgraph:4.8.62'
testCompile 'io.github.classgraph:classgraph:4.8.64'
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.0'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.6.0'
testCompile 'org.junit.platform:junit-platform-launcher:1.6.0'
Expand Down Expand Up @@ -590,6 +590,7 @@ jlink {
requires 'java.rmi'
requires 'java.xml'
requires 'com.sun.xml.txw2'
requires 'com.google.gson';
requires 'java.desktop'
requires 'java.security.jgss'
requires 'jdk.jsobject'
Expand Down
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::getJavaScriptPatternForWords);
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
46 changes: 45 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,25 @@

public class SearchQuery implements SearchMatcher {

/**
* Regex pattern for escaping special characters in javascript regular expressions
*/
public static final Pattern JAVASCRIPT_ESCAPED_CHARS_PATTERN = Pattern.compile("[\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\/]");

/**
* The mode of escaping special characters in regular expressions
*/
private enum EscapeMode {
/**
* using \Q and \E marks
*/
JAVA,
/**
* escaping all javascript regex special characters separately
*/
JAVASCRIPT
}

private final String query;
private final boolean caseSensitive;
private final boolean regularExpression;
Expand Down Expand Up @@ -124,6 +143,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(EscapeMode.JAVA);
}

// 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> getJavaScriptPatternForWords() {
return joinWordsToPattern(EscapeMode.JAVASCRIPT);
}

/** Returns a regular expression pattern in the form (w1)|(w2)| ... wi are escaped if no regular expression search is enabled
* @param escapeMode the mode of escaping special characters in wi
*/
private Optional<Pattern> joinWordsToPattern(EscapeMode escapeMode) {
List<String> words = getSearchWords();

if ((words == null) || words.isEmpty() || words.get(0).isEmpty()) {
Expand All @@ -133,7 +164,20 @@ 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));
if (regularExpression) {
joiner.add(word);
} else {
switch (escapeMode) {
case JAVA:
joiner.add(Pattern.quote(word));
break;
case JAVASCRIPT:
joiner.add(JAVASCRIPT_ESCAPED_CHARS_PATTERN.matcher(word).replaceAll("\\\\$0"));
break;
default:
throw new IllegalArgumentException("Unknown special characters escape mode: " + escapeMode);
}
}
}
String searchPattern = joiner.toString();

Expand Down
Loading

0 comments on commit 859e826

Please sign in to comment.