fix #1702 ignore case in title

This commit is contained in:
Shinsuke Sugaya 2018-06-10 07:44:24 +09:00
parent a0105659db
commit cd16bf831c
2 changed files with 63 additions and 11 deletions

View file

@ -60,6 +60,7 @@ import org.codelibs.fess.mylasta.direction.FessConfig;
import org.codelibs.fess.util.ComponentUtil;
import org.codelibs.fess.util.DocumentUtil;
import org.codelibs.fess.util.ResourceUtil;
import org.dbflute.optional.OptionalThing;
import org.lastaflute.taglib.function.LaFunctions;
import org.lastaflute.web.response.ActionResponse;
import org.lastaflute.web.response.StreamResponse;
@ -150,19 +151,30 @@ public class ViewHelper {
title = StringUtils.abbreviate(title, size);
}
final String value = LaFunctions.h(title);
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();
}
return t;
}).orElse(value);
}
protected OptionalThing<Set<String>> getQuerySet() {
return LaRequestUtil.getOptionalRequest().map(req -> {
@SuppressWarnings("unchecked")
final Set<String> querySet = (Set<String>) req.getAttribute(Constants.HIGHLIGHT_QUERIES);
if (querySet != null) {
String t = value;
for (final String query : querySet) {
final String target = LaFunctions.h(query);
t = t.replace(target, highlightTagPre + target + highlightTagPost);
}
return t;
}
return value;
}).orElse(value);
return querySet;
}).filter(s -> s != null);
}
public String getContentDescription(final Map<String, Object> document) {

View file

@ -18,7 +18,9 @@ package org.codelibs.fess.helper;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.codelibs.core.io.FileUtil;
import org.codelibs.core.misc.DynamicProperties;
@ -26,6 +28,7 @@ import org.codelibs.fess.es.config.exentity.PathMapping;
import org.codelibs.fess.mylasta.direction.FessConfig;
import org.codelibs.fess.unit.UnitFessTestCase;
import org.codelibs.fess.util.ComponentUtil;
import org.dbflute.optional.OptionalThing;
public class ViewHelperTest extends UnitFessTestCase {
public ViewHelper viewHelper;
@ -209,7 +212,7 @@ public class ViewHelperTest extends UnitFessTestCase {
}
public void test_escapeHighlight() {
viewHelper = new ViewHelper();
ViewHelper viewHelper = new ViewHelper();
viewHelper.init();
String text = "";
@ -285,4 +288,41 @@ public class ViewHelperTest extends UnitFessTestCase {
docMap.put(fieldName, urlLink);
assertEquals(sitePath, viewHelper.getSitePath(docMap));
}
public void test_getContentTitle() {
final Set<String> querySet = new HashSet<>();
ViewHelper viewHelper = new ViewHelper() {
@Override
protected OptionalThing<Set<String>> getQuerySet() {
return OptionalThing.of(querySet);
}
};
viewHelper.init();
querySet.add("aaa");
final Map<String, Object> document = new HashMap<>();
document.put("title", "");
assertEquals("", viewHelper.getContentTitle(document));
document.put("title", "111");
assertEquals("111", viewHelper.getContentTitle(document));
document.put("title", "aaa");
assertEquals("<strong>aaa</strong>", viewHelper.getContentTitle(document));
document.put("title", "AAA");
assertEquals("<strong>AAA</strong>", viewHelper.getContentTitle(document));
document.put("title", "111AaA222bbb");
assertEquals("111<strong>AaA</strong>222bbb", viewHelper.getContentTitle(document));
document.put("title", "aaaAAA");
assertEquals("<strong>aaa</strong><strong>AAA</strong>", viewHelper.getContentTitle(document));
querySet.add("BBB");
document.put("title", "111AaA222bbb");
assertEquals("111<strong>AaA</strong>222<strong>bbb</strong>", viewHelper.getContentTitle(document));
}
}