I am trying to add highlight to search words in an HTML String in Java.
I am parsing the HTML using JSoup and iterating over each text node to find the search words, then adding mark tag around the word.
As I am parsing each node sequentially, there is a case which is getting missed out.
If a text is getting split by a tag, then the word definitely cannot be found by current logic.
Example :
<div><span class="A">search sear<span class="B">ch</span></span></div>
If search word is "search", then first one will get highlighted, but the second one will miss out.
Is there a way I can highlight sear<span class="B">ch</span>
without any major change in HTML structure?
Code Logic :
private static void doHighlighting(Node body, List<String> searchWordList, String highlightRgb[]) {
String startTag = "<mark style=\"background-color:rgb("+ highlightRgb[0] + "," + highlightRgb[1] + "," + highlightRgb[2] + ")\">";
String endTag = "</mark>";
for (Node node : body.childNodes()) {
if (node instanceof TextNode && !((TextNode) node).isBlank()) {
String nodeText = ((TextNode) node).text();
Map<Integer, Integer> indexMap = new HashMap<Integer, Integer>();
for (String searchWord : searchWordList){
indexMap = StringUtil.patternIndexSearch(searchWord.toLowerCase(), nodeText.toLowerCase(), indexMap);
}
//indexMap stores startPosition and endPosition of the searchwords in a String
//Then adds mark tag here and updates the html
} else {
doHighlighting(node, searchWordList, highlightRgb);
}
}
}