Skip to content

Commit 3558b83

Browse files
committed
fix(highlight): Validate (optional) text when highlighting range
1 parent 4d1fcd1 commit 3558b83

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

โ€Žspec/highlight-support.spec.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ ke The <br> World Go Round`)
339339

340340
it('always selects the first character if values are too small', function () {
341341
setupHighlightEnv(this, 'ab')
342-
this.highlightRange('ab', 'myId', 0, 0)
342+
const highlightText = undefined // Avoid validation
343+
this.highlightRange(highlightText, 'myId', 0, 0)
343344
const expectedHtml = this.formatHtml(
344345
`<span class="highlight-comment" data-editable="ui-unwrap" data-highlight="comment" data-word-id="myId">a</span>b`
345346
)
@@ -349,7 +350,8 @@ ke The <br> World Go Round`)
349350

350351
it('always selects the last character if values are too large', function () {
351352
setupHighlightEnv(this, 'ab')
352-
this.highlightRange('ab', 'myId', 2, 5)
353+
const highlightText = undefined // Avoid validation
354+
this.highlightRange(highlightText, 'myId', 2, 5)
353355
const expectedHtml = this.formatHtml(
354356
`a<span class="highlight-comment" data-editable="ui-unwrap" data-highlight="comment" data-word-id="myId">b</span>`
355357
)
@@ -512,7 +514,7 @@ ke The <br> World Go Round`)
512514

513515
it('extracts a readable text', function () {
514516
setupHighlightEnv(this, '๐Ÿ˜ Make&nbsp;The \r\n ๐ŸŒ Go \n๐Ÿ”„')
515-
this.highlightRange('๐Ÿ˜ Makeย The ๐ŸŒ Go ๐Ÿ”„', 'myId', 0, 23)
517+
this.highlightRange('๐Ÿ˜ Makeย The \n ๐ŸŒ Go \n๐Ÿ”„', 'myId', 0, 23)
516518
const expectedRanges = {
517519
myId: {
518520
text: '๐Ÿ˜ Makeย The \n ๐ŸŒ Go \n๐Ÿ”„',
@@ -529,7 +531,7 @@ ke The <br> World Go Round`)
529531
setupHighlightEnv(this, '๐Ÿ˜ Make&nbsp;The \r\n ๐ŸŒ Go \n๐Ÿ”„')
530532
let called = 0
531533
const dispatcher = {notify: () => called++}
532-
this.highlightRange('๐Ÿ˜ Makeย The ๐ŸŒ Go ๐Ÿ”„', 'myId', 0, 20, dispatcher)
534+
this.highlightRange('๐Ÿ˜ Makeย The \n ๐ŸŒ Go \n๐Ÿ”„', 'myId', 0, 23, dispatcher)
533535

534536
expect(called).to.equal(1)
535537
})
@@ -538,7 +540,7 @@ ke The <br> World Go Round`)
538540
setupHighlightEnv(this, '๐Ÿ˜ Make&nbsp;The \r\n ๐ŸŒ Go \n๐Ÿ”„')
539541
let called = 0
540542
const dispatcher = {notify: () => called++}
541-
this.highlightRange('๐Ÿ˜ Makeย The ๐ŸŒ Go ๐Ÿ”„', 'myId', 0, 20)
543+
this.highlightRange('๐Ÿ˜ Makeย The \n ๐ŸŒ Go \n๐Ÿ”„', 'myId', 0, 23)
542544
this.removeHighlight('first', dispatcher)
543545

544546
expect(called).to.equal(1)

โ€Žsrc/highlight-support.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ const highlightSupport = {
4848
const actualStartIndex = startIndex
4949
const actualEndIndex = endIndex
5050

51+
// If text is provided then validate that it matches
52+
if (text) {
53+
const tempElement = document.createElement('div')
54+
tempElement.innerHTML = text
55+
if (tempElement.textContent !== blockText.slice(actualStartIndex, actualEndIndex)) {
56+
return -1
57+
}
58+
}
59+
5160
highlightText.highlightMatches(editableHost, [{
5261
startIndex: actualStartIndex,
5362
endIndex: actualEndIndex,

0 commit comments

Comments
ย (0)