markText endStyle appearing multiple times

I’m trying to use the startStyle/endStyle options for the markText() function. However, when have overlapping marks, it appears that the endStyle is being placed on multiple spans for the mark.

I expected the rendered code to be something like this:

<span class="mark1 mark1-start">XXX</span>
<span class="mark1 mark2">YYY<span>
<span class="mark1 mark1-end">ZZZ</span>

But I’m getting the following instead:

<span class="mark1 mark1-start mark1-end">XXX</span>
<span class="mark1 mark2">YYY<span>
<span class="mark1 mark1-end">ZZZ</span>

The following fiddle shows this in action:

Is this a bug or am I misunderstanding the endStyle option?

Start and end styling is handled per mark, not per style. So yes, if you have overlapping marks with start/end style, you’ll get the effect you see.

Thanks for the quick reply. I’m not sure if I understand completely so I’ll provide a little more detail.

I have text XXXYYYZZZ
There are two marks:
mark1 which covers all the text XXXYYYZZZ
mark2 which covers text in the middle YYY

CodeMirror renders that as:
<span class="mark1">XXX</span><span class="mark1 mark2">YYY<span><span class="mark1">ZZZ</span>

On mark1 I add startStyle:“mark1-start” and endStyle:"mark1-end"
On mark2 I am not using start or end styles.

According to the docs endStyle would be “applied to the rightmost span that is part of the marker”, so I was assuming that it would appear on ZZZ, which is the rightmost span for mark1. Instead the endStyle is appearing on the first span XXX, not on the middle span YYY, and again on the last span ZZZ.

So in this case I can’t style the right side of the marked text, because the style is being applied in other places.

My tests aren’t producing the result you describe. Could you give me the actual code that creates the markers?

Yes - the code to create the markers is found here:

Argh, you already gave a link. Sorry about that. I figured out the issue – the test for whether a given span was the end of the mark was made before all other marks had been considered, and thus sometimes came out wrong. (Note the bug didn’t occur if you added the marks in another order.)

This patch seems to fix the issue.

1 Like

Thanks for the fix - works like a charm now!