Skip to content

Commit ef5c307

Browse files
committed
fix(content): Fix isExactSelection for textNodes and hidden content
1 parent 5a8d32f commit ef5c307

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

spec/content.spec.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,26 @@ describe('Content', function () {
486486
expect(exact).toEqual(true)
487487
})
488488

489+
it('is true if the selection is a text node', function () {
490+
// <div>|b|<em>b</em></div>
491+
const host = createElement('<div>b<em>b</em></div>')
492+
this.range.setStart(host.firstChild, 0)
493+
this.range.setEnd(host.firstChild, 1)
494+
495+
const exact = content.isExactSelection(this.range, host.firstChild)
496+
expect(exact).toEqual(true)
497+
})
498+
499+
it('is true if the selection contains an invisible node', function () {
500+
// <div>|b<script>console.log("foo")</script>|</div>
501+
const host = createElement('<div>hello<script>console.log("foo")</script> world</div>')
502+
this.range.setStart(host, 0)
503+
this.range.setEnd(host, 3)
504+
505+
const exact = content.isExactSelection(this.range, host)
506+
expect(exact).toEqual(true)
507+
})
508+
489509
it('is false if the selection goes beyond the tag', function () {
490510
// <div>|a<em>b</em>|</div>
491511
const host = createElement('<div>a<em>b</em></div>')
@@ -499,7 +519,7 @@ describe('Content', function () {
499519
it('is false if the selection is only partial', function () {
500520
// <div><em>a|b|</em></div>
501521
const host = createElement('<div><em>ab</em></div>')
502-
this.range.setEnd(host.querySelector('em').firstChild, 1)
522+
this.range.setStart(host.querySelector('em').firstChild, 1)
503523
this.range.setEnd(host.querySelector('em').firstChild, 2)
504524

505525
const exact = content.isExactSelection(this.range, host.querySelector('em'))
@@ -509,7 +529,7 @@ describe('Content', function () {
509529
it('is false for a collapsed this.range', function () {
510530
// <div><em>a|b</em></div>
511531
const host = createElement('<div><em>ab</em></div>')
512-
this.range.setEnd(host.querySelector('em').firstChild, 1)
532+
this.range.setStart(host.querySelector('em').firstChild, 1)
513533
this.range.setEnd(host.querySelector('em').firstChild, 1)
514534

515535
const exact = content.isExactSelection(this.range, host.querySelector('em'))
@@ -519,17 +539,17 @@ describe('Content', function () {
519539
it('is false for a collapsed this.range in an empty tag', function () {
520540
// <div><em>|</em></div>
521541
const host = createElement('<div><em></em></div>')
522-
this.range.setEnd(host.querySelector('em'), 0)
542+
this.range.setStart(host.querySelector('em'), 0)
523543
this.range.setEnd(host.querySelector('em'), 0)
524544

525545
const exact = content.isExactSelection(this.range, host.querySelector('em'))
526546
expect(exact).toEqual(false)
527547
})
528548

529-
it('is false if this.range and elem do not overlap but have the same content', function () {
549+
it('is false if selection and elem do not overlap but have the same content', function () {
530550
// <div>|b|<em>b</em></div>
531551
const host = createElement('<div>b<em>b</em></div>')
532-
this.range.setEnd(host.firstChild, 0)
552+
this.range.setStart(host.firstChild, 0)
533553
this.range.setEnd(host.firstChild, 1)
534554

535555
const exact = content.isExactSelection(this.range, host.querySelector('em'))

src/content.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export function isExactSelection (range, elem, visible) {
234234
if (!range.intersectsRange(elemRange)) return false
235235

236236
let rangeText = range.toString()
237-
let elemText = (elem.jquery ? elem[0] : elem).innerText
237+
let elemText = (elem.jquery ? elem[0] : elem).textContent
238238

239239
if (visible) {
240240
rangeText = string.trim(rangeText)

0 commit comments

Comments
 (0)