/* This file is part of Ezra Bible App.
Copyright (C) 2019 - 2026 Ezra Bible App Development Team <contact@ezrabibleapp.net>
Ezra Bible App is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Ezra Bible App is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Ezra Bible App. See the file LICENSE.
If not, see <http://www.gnu.org/licenses/>. */
/**
* Highlights exact-phrase matches found in a verse's searchable text nodes.
*
* Match positions are calculated by VerseSearch against the concatenated text
* of those nodes. Each position is mapped back to one or more nodes, allowing
* a phrase to span inline markup without replacing the surrounding elements.
* The first segment of each match is marked for result navigation; subsequent
* segments remain highlighted but are not separate results.
*
* @category Component
*/
class ExactPhraseHighlighter {
/**
* @param {function(string, boolean): string} highlightFunction Returns highlight markup;
* the boolean indicates whether the segment starts a new occurrence.
*/
constructor(highlightFunction) {
this.highlightFunction = highlightFunction;
}
/**
* Replaces matched text nodes with wrappers containing highlighted segments.
* Other text and surrounding markup are preserved. The wrappers also allow
* the tab-search reset logic to remove the highlights.
*
* @param {Text[]} nodes Searchable text nodes in document order.
* @param {number[]} occurrences Ascending, non-overlapping match offsets in
* the concatenated node text, measured in UTF-16 code units.
* @param {number} searchLength Match length in UTF-16 code units.
*/
highlightOccurrences(nodes, occurrences, searchLength) {
let nodeStart = 0;
let occurrenceIndex = 0;
// Occurrence offsets refer to the concatenated searchable text, not individual nodes.
nodes.forEach((node) => {
const text = node.nodeValue;
const nodeEnd = nodeStart + text.length;
let textOffset = 0;
let wrapper = null;
while (occurrenceIndex < occurrences.length && occurrences[occurrenceIndex] < nodeEnd) {
const occurrenceStart = occurrences[occurrenceIndex];
const occurrenceEnd = occurrenceStart + searchLength;
const start = Math.max(occurrenceStart, nodeStart) - nodeStart;
const end = Math.min(occurrenceEnd, nodeEnd) - nodeStart;
if (end > start) {
if (wrapper == null) {
// The existing reset logic removes this wrapper when clearing highlights.
wrapper = document.createElement('span');
}
wrapper.appendChild(document.createTextNode(text.slice(textOffset, start)));
const highlight = document.createElement('span');
highlight.innerHTML = this.highlightFunction('', occurrenceStart >= nodeStart);
highlight.firstElementChild.textContent = text.slice(start, end);
wrapper.appendChild(highlight.firstElementChild);
textOffset = end;
}
if (occurrenceEnd > nodeEnd) {
break;
}
occurrenceIndex++;
}
if (wrapper != null) {
wrapper.appendChild(document.createTextNode(text.slice(textOffset)));
node.replaceWith(wrapper);
}
nodeStart = nodeEnd;
});
}
}
module.exports = ExactPhraseHighlighter;
Source