Bounty
The bounty will go to the fastest solution, as demonstrated by jsPerf, across the latest release versions of Firefox, Chrome and Internet Explorer at time of testing or the answer most useful in creating such a solution at my discretion. Mwahahaha!
I'll be mostly satisfied with a solution that takes all of the offsets and an unprocessed <span>
and adds the highlighting to that, so that parent.textContent = parent.textContent
followed by running the solution on an updated list of offsets will re-highlight, but this has unfavourable time complexity so is not preferred.
Related questions
- Overlapping of one string into another string highlighting issue
(doesn't feature overlapping) - How to get the parent element in an HTML string at a given offset?
(evolved from a variant of this question)
I have an element containing nothing but text, which I would like to highlight. I also have an array of [startline, startcol, endline, endcol]
which, knowing the lengths of each line from .textContent
, I can normalise to [startoffset, endoffset]
. How can I highlight between each pair of offsets?
This problem is harder than it seems because:
- the content is not guaranteed to have no repeats (so no find / replace), and
- highlighting must ultimately be performed on already highlighted text, sometimes intersecting with text that has already been highlighted, and
- highlighting must be performed based on the index of the parent element's
.textContent
property.
Definitions
- highlight: to place a subset of the text from an element's
textContent
in one or more<span class="highlighted">
without changing the parent element'stextContent
value, such that text that is highlighted n times is within n nested<span class="highlighted">
elements. - offset: a non-negative integer representing the number of characters before a certain point (which is between two characters).
- character: an instance of whatever JavaScript gives you as the value at a given index of a
.textContent
string (including whitespace).
MCVE
function highlight(parent, startoff, endoff) {
// Erm...
parent.textContent;
}
// Test cases
var starts = [
5, 44, 0, 50, 6, 100, 99, 50, 51, 52
];
var ends = [
20, 62, 4, 70, 10, 100, 101, 54, 53, 53
];
for (var i = 0; i < 10; i += 1) {
highlight(document.getElementById("target"),
starts[i], ends[i]);
}
#target {
white-space: pre-wrap;
}
<span id="target">
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.
"Beware the Jabberwock, my son!
The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
The frumious Bandersnatch!"
He took his vorpal sword in hand:
Long time the manxome foe he sought --
So rested he by the Tumtum tree,
And stood awhile in thought.
And, as in uffish thought he stood,
The Jabberwock, with eyes of flame,
Came whiffling through the tulgey wood,
And burbled as it came!
One, two! One, two! And through and through
The vorpal blade went snicker-snack!
He left it dead, and with its head
He went galumphing back.
"And, has thou slain the Jabberwock?
Come to my arms, my beamish boy!
O frabjous day! Callooh! Callay!'
He chortled in his joy.
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe;
All mimsy were the borogoves,
And the mome raths outgrabe.
</span>