Skip to content

Commit 3b84a76

Browse files
committed
fix(highlight): Validate (optional) text when highlighting range
1 parent 72423e0 commit 3b84a76

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,47 @@ ke The <br> World Go Round`)
353353
})
354354
})
355355

356+
describe('highlightRange() - text validation', function () {
357+
358+
it('returns -1 when text does not match the range', function () {
359+
setupHighlightEnv(this, 'People Make The World Go Round')
360+
const result = this.highlightRange('wrong text', 'myId', 0, 6)
361+
expect(result).to.equal(-1)
362+
})
363+
364+
it('does not apply a highlight when text does not match', function () {
365+
setupHighlightEnv(this, 'People Make The World Go Round')
366+
this.highlightRange('wrong text', 'myId', 0, 6)
367+
expect(this.extractWithoutNativeRange()).to.equal(undefined)
368+
})
369+
370+
it('does not notify the dispatcher when text does not match', function () {
371+
setupHighlightEnv(this, 'People Make The World Go Round')
372+
let called = 0
373+
const dispatcher = {notify: () => called++}
374+
this.highlightRange('wrong text', 'myId', 0, 6, dispatcher)
375+
expect(called).to.equal(0)
376+
})
377+
378+
it('skips validation when text is undefined', function () {
379+
setupHighlightEnv(this, 'People Make The World Go Round')
380+
const result = this.highlightRange(undefined, 'myId', 0, 6)
381+
expect(result).to.equal(0)
382+
})
383+
384+
it('skips validation when text is null', function () {
385+
setupHighlightEnv(this, 'People Make The World Go Round')
386+
const result = this.highlightRange(null, 'myId', 0, 6)
387+
expect(result).to.equal(0)
388+
})
389+
390+
it('skips validation when text is empty string', function () {
391+
setupHighlightEnv(this, 'People Make The World Go Round')
392+
const result = this.highlightRange('', 'myId', 0, 6)
393+
expect(result).to.equal(0)
394+
})
395+
})
396+
356397
describe('highlightRange() - with formatted text', function () {
357398

358399
it('handles highlights surrounding <span> tags', function () {
@@ -482,7 +523,7 @@ ke The <br> World Go Round`)
482523

483524
it('extracts a readable text', function () {
484525
setupHighlightEnv(this, '๐Ÿ˜ Make&nbsp;The \r\n ๐ŸŒ Go \n๐Ÿ”„')
485-
this.highlightRange('๐Ÿ˜ Makeย The ๐ŸŒ Go ๐Ÿ”„', 'myId', 0, 23)
526+
this.highlightRange('๐Ÿ˜ Makeย The \n ๐ŸŒ Go \n๐Ÿ”„', 'myId', 0, 23)
486527
const expectedRanges = {
487528
myId: {
488529
text: '๐Ÿ˜ Makeย The \n ๐ŸŒ Go \n๐Ÿ”„',
@@ -499,7 +540,7 @@ ke The <br> World Go Round`)
499540
setupHighlightEnv(this, '๐Ÿ˜ Make&nbsp;The \r\n ๐ŸŒ Go \n๐Ÿ”„')
500541
let called = 0
501542
const dispatcher = {notify: () => called++}
502-
this.highlightRange('๐Ÿ˜ Makeย The ๐ŸŒ Go ๐Ÿ”„', 'myId', 0, 20, dispatcher)
543+
this.highlightRange('๐Ÿ˜ Makeย The \n ๐ŸŒ Go \n๐Ÿ”„', 'myId', 0, 23, dispatcher)
503544

504545
expect(called).to.equal(1)
505546
})
@@ -508,7 +549,7 @@ ke The <br> World Go Round`)
508549
setupHighlightEnv(this, '๐Ÿ˜ Make&nbsp;The \r\n ๐ŸŒ Go \n๐Ÿ”„')
509550
let called = 0
510551
const dispatcher = {notify: () => called++}
511-
this.highlightRange('๐Ÿ˜ Makeย The ๐ŸŒ Go ๐Ÿ”„', 'myId', 0, 20)
552+
this.highlightRange('๐Ÿ˜ Makeย The \n ๐ŸŒ Go \n๐Ÿ”„', 'myId', 0, 23)
512553
this.removeHighlight('first', dispatcher)
513554

514555
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)