/* 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/>. */
const { escapeRegExp } = require('../../helpers/ezra_helper.js');
class SingleWordHighlighter {
constructor(highlightFunction) {
this.highlightFunction = highlightFunction;
}
highlightOccurrences(verseElement, searchString, regexOptions, wordBoundaries=false) {
var verseHtml = verseElement.innerHTML;
// Replace all occurances with space. This is necessary in case of Strong's verse content.
// Otherwise we don't find the user's searchString if it contains spaces.
// Later we need to undo this!
verseHtml = verseHtml.replace(/ /g, ' ');
searchString = escapeRegExp(searchString);
// Create regex pattern with or without word boundaries
var regexPattern = searchString;
if (wordBoundaries) {
// Manual word boundary implementation for better control
regexPattern = '(^|[\\s.,;:!?()[\\]{}|<>"\'\/\\\\-])' + searchString + '($|[\\s.,;:!?()[\\]{}|<>"\'\/\\\\-])';
}
var regexSearchString = new RegExp(regexPattern, regexOptions);
var highlightedVerseText = verseHtml.replace(regexSearchString, (...args) => {
var match = args[0];
var offset, string, p1, p2;
if (wordBoundaries) {
p1 = args[1];
p2 = args[2];
offset = args[3];
string = args[4];
} else {
offset = args[1];
string = args[2];
}
if (this.isOccuranceValid(match, offset, string)) {
if (wordBoundaries) {
// If using word boundaries, we need to preserve the boundary characters
return p1 + this.highlightFunction(match.slice(p1.length, match.length - p2.length)) + p2;
} else {
return this.highlightFunction(match);
}
} else {
return match;
}
});
verseElement.innerHTML = highlightedVerseText;
}
isOccuranceValid(match, offset, string) {
if (match == null || offset == null || string == null) {
// If any of the parameters are null, we cannot validate the occurance, so we consider it invalid.
return false;
}
var offsetAfterMatch = offset + match.length;
var lengthAfterMatch = string.length - offset;
var foundOpeningAngleBracketIndex = -1;
var foundClosingAngleBracketIndex = -1;
var matchIsValid = true;
for (var i = offsetAfterMatch; i < (offsetAfterMatch + lengthAfterMatch); i++) {
var currentChar = string[i];
if (foundOpeningAngleBracketIndex == (i - 1)) {
// We found an opening angle bracket in the previous iteration
// Next we will be looking for '/'
if (currentChar == '/') {
// Found closing angle bracket - so essential we found '</' now.
foundClosingAngleBracketIndex = i;
} else if (currentChar == 'd') {
// Some other markup is starting (with a div), it's not a closing one, so the match is not surrounded by markup.
// In this case we cancel the search and the match is considered valid.
break;
}
}
// Disabled for now, because it has been causing issues with highlighting.
/*if (foundClosingAngleBracketIndex == (i - 1)) {
// We previously found a closing angle bracket ('</').
if (currentChar == 'd') {
// Now it's clear that the closing element is a div. (No other element starting with 'd')
// This means that the match is within special markup and must be ignored (invalid match)
// We're cancelling the search and are now done.
matchIsValid = false;
break;
}
}*/
if (currentChar == '<') {
foundOpeningAngleBracketIndex = i;
}
if (currentChar == '>' && foundOpeningAngleBracketIndex == -1) {
// If we find a closing angle bracket and have not found a opening angle bracket before,
// it's clear that the occurance is within special markup and is invalid.
// We're cancelling the search and are now done.
matchIsValid = false;
break;
}
}
return matchIsValid;
}
}
module.exports = SingleWordHighlighter;
Source