File size: 2,024 Bytes
2daf8de | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | import { afterEach, describe, expect, it } from 'vitest'
import { captureDOMSelection } from './captureDOMSelection.js'
const createContent = () => {
const oldParent = document.createElement('div')
const newParent = document.createElement('div')
const content = document.createElement('div')
const text = document.createTextNode('Alpha')
content.appendChild(text)
oldParent.appendChild(content)
document.body.append(oldParent, newParent)
return { content, newParent, text }
}
const selectInside = (node: Node, offset: number) => {
const selection = document.getSelection()!
selection.removeAllRanges()
const range = document.createRange()
range.setStart(node, offset)
range.collapse(true)
selection.addRange(range)
}
afterEach(() => {
document.getSelection()?.removeAllRanges()
document.body.innerHTML = ''
})
describe('captureDOMSelection', () => {
it('restores a selection that lives inside the element', () => {
const { content, newParent, text } = createContent()
selectInside(text, 3)
const restore = captureDOMSelection(content)
newParent.appendChild(content)
// jsdom keeps the selection on a move, browsers drop it. Fake that here.
document.getSelection()?.removeAllRanges()
restore?.()
const selection = document.getSelection()!
expect(restore).toBeTypeOf('function')
expect(selection.anchorNode).toBe(text)
expect(selection.anchorOffset).toBe(3)
})
it('returns null when there is no selection', () => {
const { content } = createContent()
document.getSelection()?.removeAllRanges()
expect(captureDOMSelection(content)).toBeNull()
})
it('returns null when the selection is outside of the element', () => {
const { content } = createContent()
const outside = document.createElement('div')
outside.appendChild(document.createTextNode('Beta'))
document.body.appendChild(outside)
selectInside(outside.firstChild!, 2)
expect(captureDOMSelection(content)).toBeNull()
})
})
|