Skip to content

Commit f1d3535

Browse files
committed
fix: highlights get only applied to editableHost
1 parent a95e4c5 commit f1d3535

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

spec/highlighting.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,17 @@ describe('Highlighting', function () {
286286
expect(highlightSpan.length).toEqual(0)
287287
})
288288

289+
it('skips if the range exceeds the content length', () => {
290+
const result = this.editable.highlight({
291+
editableHost: this.$div[0],
292+
highlightId: 'myId',
293+
textRange: { foo: 3, bar: 32 }
294+
})
295+
const highlightSpan = this.$div.find('[data-word-id="myId"]')
296+
expect(highlightSpan.length).toEqual(0)
297+
expect(result).toEqual(-1)
298+
})
299+
289300
it('skips and warns if the range object represents a cursor', () => {
290301
this.editable.highlight({
291302
editableHost: this.$div[0],

src/highlight-support.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import * as content from './content'
44
import highlightText from './highlight-text'
55
import TextHighlighting from './plugins/highlighting/text-highlighting'
66

7+
function isInHost (el, host) {
8+
if (!el.closest) {
9+
el = el.parentNode
10+
}
11+
return el.closest('[data-editable]:not([data-word-id])') === host
12+
}
13+
714
const highlightSupport = {
815

916
highlightText (editableHost, text, highlightId) {
@@ -28,14 +35,18 @@ const highlightSupport = {
2835
if (this.hasHighlight(editableHost, highlightId)) {
2936
this.removeHighlight(editableHost, highlightId)
3037
}
38+
const range = rangy.createRange()
39+
range.selectCharacters(editableHost, startIndex, endIndex)
40+
41+
if (!isInHost(range.commonAncestorContainer, editableHost)) {
42+
return -1
43+
}
3144

3245
const marker = highlightSupport.createMarkerNode(
3346
'<span class="highlight-comment" data-word-id="' + highlightId + '"></span>',
3447
'highlight',
3548
this.win
3649
)
37-
const range = rangy.createRange()
38-
range.selectCharacters(editableHost, startIndex, endIndex)
3950
const fragment = range.extractContents()
4051
marker.appendChild(fragment)
4152
range.deleteContents()

0 commit comments

Comments
 (0)