diff --git a/src/main/java/org/codelibs/fess/helper/ViewHelper.java b/src/main/java/org/codelibs/fess/helper/ViewHelper.java index 3435ad51834e280751fb8fb1b2a0254a7a89c314..27babdcbb94c451f822f1dcfb6182f6d12927cbb 100644 --- a/src/main/java/org/codelibs/fess/helper/ViewHelper.java +++ b/src/main/java/org/codelibs/fess/helper/ViewHelper.java @@ -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) { diff --git a/src/test/java/org/codelibs/fess/helper/ViewHelperTest.java b/src/test/java/org/codelibs/fess/helper/ViewHelperTest.java index c12ac368ef00317da7896b51f0e1c7ddaadd88ba..d2166006c096794f1fd990ad60c976e4dfd09a6a 100644 --- a/src/test/java/org/codelibs/fess/helper/ViewHelperTest.java +++ b/src/test/java/org/codelibs/fess/helper/ViewHelperTest.java @@ -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)); + } }