/** * @vitest-environment happy-dom * * Custody of the selection: who owns it, when it is handed back, and the one * shortcut that both makes and unmakes a link. */ import { readFileSync } from 'fs' import { describe, it, expect, beforeAll, beforeEach } from 'vitest' import path from 'path' import { fileURLToPath } from 'url' import Alpine from 'alpinejs' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const SRC = readFileSync(path.join(__dirname, '../src/scripts/manifest.rich.text.js'), 'utf8') beforeAll(() => { window.Alpine = Alpine globalThis.Alpine = Alpine new Function(SRC)() Alpine.start() }) function mount(html) { const root = document.createElement('div') root.setAttribute('x-data', '{}') root.innerHTML = html Alpine.mutateDom(() => document.body.appendChild(root)) Alpine.initTree(root) return root } // happy-dom raises none of these itself, so each gesture is spelled out. const focusin = (el) => el.dispatchEvent(new window.FocusEvent('focusin', { bubbles: true })) const focusout = (el, relatedTarget = null) => el.dispatchEvent(new window.FocusEvent('focusout', { bubbles: true, relatedTarget })) const pointerdown = (el) => el.dispatchEvent(new window.Event('pointerdown', { bubbles: true })) function select(node, start, end) { const s = window.getSelection(), r = document.createRange() r.setStart(node, start); r.setEnd(node, end === undefined ? start : end) s.removeAllRanges(); s.addRange(r) document.dispatchEvent(new window.Event('selectionchange')) } // Every selection event an area emits, newest last. function watch(el) { const seen = [] el.addEventListener('rich-text:selection', (e) => seen.push(e.detail)) return seen } const text = (area) => area.querySelector('p').firstChild beforeEach(() => { document.body.replaceChildren() document.documentElement.removeAttribute('data-rich-text-selected') window.getSelection().removeAllRanges() }) describe('x-rich-text selection release', () => { it('releases when focus leaves for plain page text', () => { const root = mount('

hello world

elsewhere') const area = root.querySelector('#a') focusin(area) select(text(area), 0, 5) expect(area.hasAttribute('data-rich-text-selected')).toBe(true) expect(document.documentElement.hasAttribute('data-rich-text-selected')).toBe(true) const seen = watch(area) pointerdown(root.querySelector('#plain')) // plain text takes no focus focusout(area, null) expect(seen).toEqual([null]) expect(area.hasAttribute('data-rich-text-selected')).toBe(false) expect(document.documentElement.hasAttribute('data-rich-text-selected')).toBe(false) }) it('keeps the selection when a bound control is clicked', () => { const root = mount('

hello world

') const area = root.querySelector('#a') focusin(area) select(text(area), 0, 5) const seen = watch(area) pointerdown(root.querySelector('#b')) focusout(area, null) // a button never reports itself as relatedTarget expect(seen).toEqual([]) expect(area.hasAttribute('data-rich-text-selected')).toBe(true) focusout(area, root.querySelector('#b')) // nor when it does expect(seen).toEqual([]) expect(area.hasAttribute('data-rich-text-selected')).toBe(true) }) it('releases the area being left for another area', () => { const root = mount('

hello world

second area

') const [a, b] = [root.querySelector('#a'), root.querySelector('#b')] focusin(a) select(text(a), 0, 5) const seen = watch(a) pointerdown(b) focusout(a, b) focusin(b) select(text(b), 0, 6) expect(seen).toEqual([null]) expect(a.hasAttribute('data-rich-text-selected')).toBe(false) expect(b.hasAttribute('data-rich-text-selected')).toBe(true) expect(document.documentElement.hasAttribute('data-rich-text-selected')).toBe(true) }) it('releases on a selection that moves to another area without a focus change', () => { const root = mount('

hello world

second area

') const [a, b] = [root.querySelector('#a'), root.querySelector('#b')] focusin(a) select(text(a), 0, 5) const seen = watch(a) select(text(b), 0, 6) expect(seen).toEqual([null]) expect(a.hasAttribute('data-rich-text-selected')).toBe(false) expect(b.hasAttribute('data-rich-text-selected')).toBe(true) }) }) describe('x-rich-text links', () => { // Cmd/Ctrl+K is deliberately NOT bound: that chord belongs to site search // almost everywhere now. Links go through an `a` control or the unlink command. it('ignores Cmd+K (reserved for site search)', () => { const root = mount('

see site now

') const area = root.querySelector('#a') focusin(area) select(area.querySelector('a').firstChild, 2) const ev = new window.KeyboardEvent('keydown', { key: 'k', metaKey: true, bubbles: true, cancelable: true }) area.dispatchEvent(ev) expect(ev.defaultPrevented).toBe(false) expect(area.querySelector('a')).not.toBe(null) }) it('links a selection through the area url control', () => { const root = mount('

hello world

') const area = root.querySelector('#a'), field = root.querySelector('#u') focusin(area) select(text(area), 0, 5) field.value = 'https://example.com' field.dispatchEvent(new window.Event('change')) const link = area.querySelector('a') expect(link.getAttribute('href')).toBe('https://example.com') expect(link.textContent).toBe('hello') }) it('prefixes https:// on a bare domain and refuses junk', () => { const root = mount('

hello world

') const area = root.querySelector('#a'), field = root.querySelector('#u') focusin(area) select(text(area), 0, 5) field.value = 'example.com/page' field.dispatchEvent(new window.Event('change')) expect(area.querySelector('a').getAttribute('href')).toBe('https://example.com/page') const tail = area.querySelector('p').lastChild // " world" after the link select(tail, 1, 6) field.value = 'not a url' field.dispatchEvent(new window.Event('change')) expect(area.querySelectorAll('a').length).toBe(1) // junk did not link }) it('unlink removes the link at the caret', () => { const root = mount('

see site now

') const area = root.querySelector('#a'), btn = root.querySelector('#un') focusin(area) select(area.querySelector('a').firstChild, 2) btn.dispatchEvent(new window.PointerEvent('pointerdown', { bubbles: true, cancelable: true })) btn.click() expect(area.querySelector('a')).toBe(null) expect(area.textContent).toBe('see site now') }) }) describe('x-rich-text inline markdown as you type', () => { // Simulate typing one character: mutate the text node, place the caret // after it, and fire the input event the way the browser would. function typeChar(area, node, ch) { node.insertData(node.length, ch) select(node, node.length) area.dispatchEvent(new window.InputEvent('input', { bubbles: true, data: ch })) } it('converts **bold** the moment the closing marker is typed', () => { const root = mount('

make

') const area = root.querySelector('#a') focusin(area) const node = text(area) for (const ch of '**test*') typeChar(area, node, ch) expect(area.querySelector('strong')).toBe(null) // not yet — marker incomplete typeChar(area, node, '*') const strong = area.querySelector('strong') expect(strong && strong.textContent).toBe('test') expect(area.querySelector('p').textContent).toBe('make test') }) it('leaves half-typed markers alone and ignores code contexts', () => { const root = mount('

x

') const area = root.querySelector('#a') focusin(area) const node = text(area) for (const ch of '`lit`') typeChar(area, node, ch) const code = area.querySelector('code') expect(code && code.textContent).toBe('lit') // typing inside the element must stay literal (clicked into: the // keystroke right after a conversion leaves the mark instead) pointerdown(code) const inner = code.firstChild for (const ch of ' **no**') typeChar(area, inner, ch) expect(code.querySelector('strong')).toBe(null) expect(code.textContent).toBe('lit **no**') }) }) describe('x-rich-text inline conversion rule priority', () => { function typeChar(area, node, ch) { node.insertData(node.length, ch) select(node, node.length) area.dispatchEvent(new window.InputEvent('input', { bubbles: true, data: ch })) } it('![alt](src) converts to an image, not a link with a stray bang', () => { const root = mount('

pic

') const area = root.querySelector('#a') focusin(area) const node = text(area) for (const ch of '![alt](x.png)') typeChar(area, node, ch) const img = area.querySelector('img') expect(img && img.getAttribute('src')).toBe('x.png') expect(area.querySelector('a')).toBe(null) expect(area.querySelector('p').textContent).toBe('pic ') }) it('composition input never converts', () => { const root = mount('

x

') const area = root.querySelector('#a') focusin(area) const node = text(area) for (const ch of '**b**') { node.insertData(node.length, ch) select(node, node.length) area.dispatchEvent(new window.InputEvent('input', { bubbles: true, data: ch, isComposing: true })) } expect(area.querySelector('strong')).toBe(null) }) })