String (@NotNull String textToMarkup, @Nullable String filter, Color textColor, Color backgroundColor) { if (filter == null || filter.length() == 0) { return textToMarkup; } int bodyStart = textToMarkup.indexOf("<body>"); final int bodyEnd = textToMarkup.indexOf("</body>"); final String head; final String foot; if (bodyStart >= 0) { bodyStart += "<body>".length(); head = textToMarkup.substring(0, bodyStart); if (bodyEnd >= 0) { foot = textToMarkup.substring(bodyEnd); } else { foot = ""; } textToMarkup = textToMarkup.substring(bodyStart, bodyEnd); } else { foot = ""; head = ""; } final Pattern insideHtmlTagPattern = Pattern.compile("[<[^<>]*>]*<[^<>]*"); final SearchableOptionsRegistrar registrar = SearchableOptionsRegistrar.getInstance(); final HashSet<String> quoted = new HashSet<>(); filter = processFilter(quoteStrictOccurrences(textToMarkup, filter), quoted); final Set<String> options = registrar.getProcessedWords(filter); final Set<String> words = registrar.getProcessedWords(textToMarkup); for (String option : options) { if (words.contains(option)) { textToMarkup = markup(textToMarkup, insideHtmlTagPattern, option, textColor, backgroundColor); } } for (String stripped : quoted) { if (registrar.isStopWord(stripped)) { continue; } textToMarkup = markup(textToMarkup, insideHtmlTagPattern, stripped, textColor, backgroundColor); } return head + textToMarkup + foot; }