@@ -4,6 +4,13 @@ import * as content from './content'
44import highlightText from './highlight-text'
55import 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+
714const 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