fix #1725 improve highlight in title
This commit is contained in:
parent
8d815456f1
commit
f939c1b7f2
2 changed files with 18 additions and 12 deletions
|
@ -35,6 +35,7 @@ import java.util.function.Consumer;
|
|||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
@ -156,19 +157,15 @@ public class ViewHelper {
|
|||
}
|
||||
return getQuerySet().map(
|
||||
querySet -> {
|
||||
String t = value;
|
||||
for (final String query : querySet) {
|
||||
final String target = LaFunctions.h(query);
|
||||
final Matcher matcher =
|
||||
Pattern.compile(target, Pattern.LITERAL | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(t);
|
||||
final StringBuffer buf = new StringBuffer(t.length() + 100);
|
||||
while (matcher.find()) {
|
||||
matcher.appendReplacement(buf, highlightTagPre + matcher.group(0) + highlightTagPost);
|
||||
}
|
||||
matcher.appendTail(buf);
|
||||
t = buf.toString();
|
||||
final Matcher matcher =
|
||||
Pattern.compile(querySet.stream().map(LaFunctions::h).map(Pattern::quote).collect(Collectors.joining("|")),
|
||||
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(value);
|
||||
final StringBuffer buf = new StringBuffer(value.length() + 100);
|
||||
while (matcher.find()) {
|
||||
matcher.appendReplacement(buf, highlightTagPre + matcher.group(0) + highlightTagPost);
|
||||
}
|
||||
return t;
|
||||
matcher.appendTail(buf);
|
||||
return buf.toString();
|
||||
}).orElse(value);
|
||||
}
|
||||
|
||||
|
|
|
@ -209,6 +209,10 @@ public class ViewHelperTest extends UnitFessTestCase {
|
|||
text = "1ABC2";
|
||||
queries = new String[] { "abc" };
|
||||
assertEquals("1<strong>abc</strong>2", viewHelper.replaceHighlightQueries(text, queries));
|
||||
|
||||
text = "abc on exy";
|
||||
queries = new String[] { "on" };
|
||||
assertEquals("abc <strong>on</strong> exy", viewHelper.replaceHighlightQueries(text, queries));
|
||||
}
|
||||
|
||||
public void test_escapeHighlight() {
|
||||
|
@ -324,5 +328,10 @@ public class ViewHelperTest extends UnitFessTestCase {
|
|||
|
||||
document.put("title", "111AaA222bbb");
|
||||
assertEquals("111<strong>AaA</strong>222<strong>bbb</strong>", viewHelper.getContentTitle(document));
|
||||
|
||||
querySet.add("on");
|
||||
document.put("title", "on 111 strong on aaaa");
|
||||
assertEquals("<strong>on</strong> 111 str<strong>on</strong>g <strong>on</strong> <strong>aaa</strong>a",
|
||||
viewHelper.getContentTitle(document));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue