diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 2dace444e1..3ba93875b4 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -2960,78 +2960,79 @@ public class HtmlHelper { return ssb; } - static Document highlightSearched(Context context, Document document, String query) { - int color = Helper.resolveColor(context, R.attr.colorHighlight); - query = Normalizer.normalize(query, Normalizer.Form.NFKD) - .replaceAll("[\\p{InCombiningDiacriticalMarks}]", ""); - - // TODO: fix highlighting pre processed text - - List word = new ArrayList<>(); - List plus = new ArrayList<>(); - for (String w : query.trim().split("\\s+")) - if (w.length() > 1 && (w.startsWith("+") || w.startsWith("-"))) { - if (w.startsWith("+")) - plus.add(w.substring(1)); - } else - word.add(w); - - int flags = Pattern.DOTALL | Pattern.CASE_INSENSITIVE; - List pat = new ArrayList<>(); - pat.add(Pattern.compile(".*?\\b(" + TextUtils.join("\\s+", word) + ")\\b.*?", flags)); - for (String w : plus) - pat.add(Pattern.compile(".*?\\b(" + w + ")\\b.*?", flags)); - - for (Pattern p : pat) - NodeTraversor.traverse(new NodeVisitor() { - @Override - public void head(Node node, int depth) { - if (node instanceof TextNode) - try { - TextNode tnode = (TextNode) node; - String text = Normalizer.normalize(tnode.getWholeText(), Normalizer.Form.NFKD) - .replaceAll("[\\p{InCombiningDiacriticalMarks}]", ""); + static void highlightSearched(Context context, Document document, String query) { + try { + int color = Helper.resolveColor(context, R.attr.colorHighlight); + query = Fts4DbHelper.preprocessText(query); + + // TODO breakText + // TODO: fix highlighting pre processed text + + List word = new ArrayList<>(); + List plus = new ArrayList<>(); + for (String w : query.trim().split("\\s+")) + if (w.length() > 1 && (w.startsWith("+") || w.startsWith("-"))) { + if (w.startsWith("+")) + plus.add(Pattern.quote(w.substring(1))); + } else + word.add(Pattern.quote(w)); - Matcher result = p.matcher(text); + int flags = Pattern.DOTALL | Pattern.CASE_INSENSITIVE; + List pat = new ArrayList<>(); + pat.add(Pattern.compile(".*?\\b(" + TextUtils.join("\\s+", word) + ")\\b.*?", flags)); + for (String w : plus) + pat.add(Pattern.compile(".*?\\b(" + w + ")\\b.*?", flags)); - int prev = 0; - Element holder = document.createElement("span"); - while (result.find()) { - int start = result.start(1); - int end = result.end(1); + for (Pattern p : pat) + NodeTraversor.traverse(new NodeVisitor() { + @Override + public void head(Node node, int depth) { + if (node instanceof TextNode) + try { + TextNode tnode = (TextNode) node; + String text = Fts4DbHelper.preprocessText(tnode.getWholeText()); + + Matcher result = p.matcher(text); + + int prev = 0; + Element holder = document.createElement("span"); + while (result.find()) { + int start = result.start(1); + int end = result.end(1); + + holder.appendText(text.substring(prev, start)); + + Element span = document.createElement("span"); + span.attr("style", mergeStyles( + span.attr("style"), + "font-size:larger; background-color:" + encodeWebColor(color) + )); + span.text(text.substring(start, end)); + holder.appendChild(span); + + prev = end; + } - holder.appendText(text.substring(prev, start)); + if (prev == 0) // No matches + return; - Element span = document.createElement("span"); - span.attr("style", mergeStyles( - span.attr("style"), - "font-size:larger; background-color:" + encodeWebColor(color) - )); - span.text(text.substring(start, end)); - holder.appendChild(span); + if (prev < text.length()) + holder.appendText(text.substring(prev)); - prev = end; + tnode.before(holder); + tnode.text(""); + } catch (Throwable ex) { + Log.e(ex); } + } - if (prev == 0) // No matches - return; - - if (prev < text.length()) - holder.appendText(text.substring(prev)); - - tnode.before(holder); - tnode.text(""); - } catch (Throwable ex) { - Log.e(ex); - } - } - - @Override - public void tail(Node node, int depth) { - } - }, document); - - return document; + @Override + public void tail(Node node, int depth) { + } + }, document); + } catch (Throwable ex) { + Log.e(ex); + } } static Document markText(Document document) {