// head.mjs: the inspector card's Format toggle (spec §2, §4, §4.4) — // its cookie, the old localStorage preference moved to it once, the // View menu row kept in step, cookies blocked, and focus in and back. import test from 'node:test' import assert from 'node:assert/strict' import { mountHead, INSPECTOR_KEY, uncheckedOfGroup } from './head.mjs' // A stand-in element: attributes, dataset, listeners, focus, and the // few selectors head.mjs asks of it. `is` lists the selectors it // matches; `closest` answers for itself and its parents. function el (attrs = {}, is = []) { const e = { attrs: new Map(Object.entries(attrs)), dataset: {}, is: new Set(is), parent: null, kids: [], listeners: [], isConnected: true, getAttribute: k => e.attrs.has(k) ? e.attrs.get(k) : null, setAttribute: (k, v) => e.attrs.set(k, String(v)), hasAttribute: k => e.attrs.has(k), removeAttribute: k => e.attrs.delete(k), matches: sel => sel.split(',').some(s => e.is.has(s.trim())), closest (sel) { for (let n = e; n; n = n.parent) if (n.matches(sel)) return n return null }, contains (n) { for (; n; n = n.parent) if (n === e) return true return false }, add (...kids) { for (const k of kids) { k.parent = e; e.kids.push(k) } return e }, all () { return e.kids.flatMap(k => [k, ...k.all()]) }, querySelectorAll: sel => e.all().filter(n => n.matches(sel)), querySelector: sel => e.querySelectorAll(sel)[0] || null, addEventListener (type, fn, capture = false) { e.listeners.push({ type, fn, capture: !!capture }) }, // Drawn unless it, or a closed
it is in (its summary // apart), is not: a browser's focus() does nothing to an element // that is not drawn, and neither does this one. visible: true, checkVisibility () { for (let n = e; n; n = n.parent) { if (!n.visible) return false if (n.parent && n.parent.is.has('details') && n.parent.open === false && !n.is.has(':scope > summary')) return false } return true }, focus () { if (e.checkVisibility()) globalThis.document.activeElement = e } } for (const [k, v] of Object.entries(attrs)) { if (k.startsWith('data-')) e.dataset[k.slice(5).replace(/-([a-z])/g, (m, c) => c.toUpperCase())] = v } return e } const TOGGLE = '[data-view-toggle="inspector"]' const ANY = ['[data-view-toggle]'] const FOCUSABLE = 'input:not([type="hidden"]):not([disabled])' // The page head.mjs mounts on: the card, holding a hidden field and // then a title field; the bar's Format button; the View menu's row; a // field on the stage that had focus before. function page ({ state = 'open' } = {}) { const root = el() const card = el({ id: 'slides-inspector', 'data-state': state }, ['[slides-inspector]']) const hiddenField = el({}, [FOCUSABLE]) hiddenField.closest = sel => sel === '[hidden]' ? hiddenField : null const title = el({ name: 'deck-title' }, [FOCUSABLE]) card.add(hiddenField, title) const format = el({ 'data-view-toggle': 'inspector', 'aria-expanded': String(state !== 'closed'), 'aria-controls': 'slides-inspector' }, [TOGGLE, '[data-view-toggle="inspector"][aria-expanded]', ...ANY]) const row = el({ 'data-view-toggle': 'inspector', 'aria-pressed': String(state !== 'closed') }, [TOGGLE, ...ANY]) // The View menu: a
whose summary is its button. const menu = el({}, ['details']) menu.open = false const summary = el({}, [':scope > summary']) menu.add(summary, row) const elsewhere = el({}, []) root.add(card, format, menu, elsewhere) return { root, card, title, format, menu, summary, row, elsewhere } } // A click on target, through the root's capture then bubble listeners. // detail is 0 for a click from the keyboard, 1 from a pointer. function click (root, target, detail = 1) { const ev = { type: 'click', target, detail, preventDefault () {} } for (const pass of [true, false]) { for (const l of root.listeners) if (l.type === 'click' && l.capture === pass) l.fn(ev) } } function fakeStorage (init = {}) { const m = new Map(Object.entries(init)) return { m, getItem: k => m.has(k) ? m.get(k) : null, setItem: (k, v) => m.set(k, String(v)), removeItem: k => m.delete(k) } } // document.cookie as a browser has it: reading gives name=value pairs, // writing sets one. blocked: writes are refused with an exception. function installDocument ({ cookie = '', blocked = false } = {}) { const jar = new Map() for (const part of cookie.split(';')) { const at = part.indexOf('=') if (at > 0) jar.set(part.slice(0, at).trim(), part.slice(at + 1).trim()) } const doc = { activeElement: null, body: null, written: [], get cookie () { return [...jar].map(([k, v]) => `${k}=${v}`).join('; ') }, set cookie (s) { if (blocked) throw new Error('SecurityError: cookies are blocked') doc.written.push(s) const pair = s.split(';')[0] const at = pair.indexOf('=') jar.set(pair.slice(0, at), pair.slice(at + 1)) } } globalThis.document = doc return doc } test.beforeEach(() => { globalThis.CSS = { escape: s => s } delete globalThis.location }) const mount = (p, storage = fakeStorage()) => mountHead({ root: p.root, commands: {}, storage }) test('Format writes the cookie the server reads, and flips the card and its toggles', () => { const doc = installDocument() const p = page() const head = mount(p) assert.equal(head.inspectorShown(), true) click(p.root, p.format) assert.equal(p.card.getAttribute('data-state'), 'closed') assert.equal(p.format.getAttribute('aria-expanded'), 'false') assert.equal(p.format.hasAttribute('aria-pressed'), false, 'the bar button has no aria-pressed') assert.equal(p.row.getAttribute('aria-pressed'), 'false', 'the View row follows') assert.equal(head.inspectorShown(), false) assert.deepEqual(doc.written, ['slides-inspector=closed; Path=/; Max-Age=31536000; SameSite=Lax']) click(p.root, p.format) assert.equal(p.card.getAttribute('data-state'), 'open') assert.equal(p.format.getAttribute('aria-expanded'), 'true') assert.equal(p.row.getAttribute('aria-pressed'), 'true') assert.equal(doc.written.at(-1), 'slides-inspector=open; Path=/; Max-Age=31536000; SameSite=Lax') }) test('the View menu row toggles the card too, and keeps aria-pressed with the button', () => { installDocument() const p = page() mount(p) p.menu.open = true click(p.root, p.row) assert.equal(p.menu.open, false, 'the menu closes') assert.equal(p.card.getAttribute('data-state'), 'closed') assert.equal(p.row.getAttribute('aria-pressed'), 'false') assert.equal(p.format.getAttribute('aria-expanded'), 'false') assert.equal(p.row.hasAttribute('aria-expanded'), false, 'the row keeps aria-pressed, not aria-expanded') }) test('the first paint is the server\'s: mounting writes nothing and changes nothing', () => { const doc = installDocument({ cookie: 'slides-inspector=closed' }) const p = page({ state: 'closed' }) const head = mount(p) assert.equal(head.inspectorShown(), false) assert.equal(p.card.getAttribute('data-state'), 'closed') assert.equal(p.format.getAttribute('aria-expanded'), 'false') assert.equal(p.row.getAttribute('aria-pressed'), 'false') assert.deepEqual(doc.written, []) }) for (const [stored, state] of [['off', 'closed'], ['on', 'open']]) { test(`an old localStorage '${stored}' with no cookie is applied once, moved to the cookie as ${state}, and removed`, () => { const doc = installDocument() const p = page({ state: stored === 'off' ? 'open' : 'closed' }) const storage = fakeStorage({ [INSPECTOR_KEY]: stored }) mount(p, storage) assert.equal(p.card.getAttribute('data-state'), state) assert.equal(p.format.getAttribute('aria-expanded'), String(state === 'open')) assert.deepEqual(doc.written, [`slides-inspector=${state}; Path=/; Max-Age=31536000; SameSite=Lax`]) assert.equal(storage.m.has(INSPECTOR_KEY), false) // Once: the next page has the cookie and no key, and is left alone. const again = page({ state }) mount(again, storage) assert.equal(again.card.getAttribute('data-state'), state) assert.equal(doc.written.length, 1) }) } test('with a cookie already set, an old localStorage key is dropped and not applied', () => { const doc = installDocument({ cookie: 'other=1; slides-inspector=open' }) const p = page() const storage = fakeStorage({ [INSPECTOR_KEY]: 'off' }) mount(p, storage) assert.equal(p.card.getAttribute('data-state'), 'open') assert.deepEqual(doc.written, []) assert.equal(storage.m.has(INSPECTOR_KEY), false) }) test('with cookies blocked, Format still shows and hides the card for this page', () => { installDocument({ blocked: true }) const p = page() const head = mount(p) click(p.root, p.format) assert.equal(p.card.getAttribute('data-state'), 'closed') assert.equal(p.format.getAttribute('aria-expanded'), 'false') assert.equal(p.row.getAttribute('aria-pressed'), 'false') click(p.root, p.format) assert.equal(p.card.getAttribute('data-state'), 'open') assert.equal(head.inspectorShown(), true) }) test('with site data blocked (localStorage throws), the toggle still works', () => { installDocument() const p = page() const storage = { getItem () { throw new Error('blocked') }, removeItem () { throw new Error('blocked') } } mount(p, storage) click(p.root, p.format) assert.equal(p.card.getAttribute('data-state'), 'closed') }) test('showInspector(true, {focus}) focuses the card\'s first showing control; hiding gives focus back', () => { const doc = installDocument({ cookie: 'slides-inspector=closed' }) const p = page({ state: 'closed' }) const head = mount(p) p.elsewhere.focus() head.showInspector(true, { focus: true }) assert.equal(p.card.getAttribute('data-state'), 'open') assert.equal(doc.activeElement, p.title, 'the first control that is not hidden') head.showInspector(false) assert.equal(p.card.getAttribute('data-state'), 'closed') assert.equal(doc.activeElement, p.elsewhere) }) test('Format from the keyboard moves focus into the card; from a pointer it does not', () => { const doc = installDocument({ cookie: 'slides-inspector=closed' }) const p = page({ state: 'closed' }) mount(p) p.format.focus() click(p.root, p.format, 1) assert.equal(doc.activeElement, p.format) click(p.root, p.format, 1) p.format.focus() click(p.root, p.format, 0) assert.equal(doc.activeElement, p.title) // Dismissed from the keyboard: back to the button that opened it. p.format.focus() click(p.root, p.format, 0) assert.equal(p.card.getAttribute('data-state'), 'closed') assert.equal(doc.activeElement, p.format) }) test('asking to delete a slide shows a hidden card, with focus', () => { const doc = installDocument({ cookie: 'slides-inspector=closed' }) const p = page({ state: 'closed' }) const ask = el({ 'data-command': 'slide-delete-ask' }, ['[data-command="slide-delete-ask"]']) p.root.add(ask) mount(p) click(p.root, ask) assert.equal(p.card.getAttribute('data-state'), 'open') assert.equal(p.format.getAttribute('aria-expanded'), 'true') assert.equal(doc.activeElement, p.title) }) test('on https the cookie is Secure', () => { const doc = installDocument() globalThis.location = { protocol: 'https:' } const p = page() mount(p) click(p.root, p.format) assert.equal(doc.written.at(-1), 'slides-inspector=closed; Path=/; Max-Age=31536000; SameSite=Lax; Secure') }) test('opened from the View menu by keyboard, the card gives focus back to the menu\'s button, not its hidden row', () => { const doc = installDocument({ cookie: 'slides-inspector=closed' }) const p = page({ state: 'closed' }) mount(p) p.menu.open = true p.row.focus() assert.equal(doc.activeElement, p.row) click(p.root, p.row, 0) assert.equal(p.menu.open, false) assert.equal(doc.activeElement, p.title, 'focus is in the card') // Dismissed with Format from the keyboard. p.format.focus() click(p.root, p.format, 0) assert.equal(p.card.getAttribute('data-state'), 'closed') assert.equal(doc.activeElement, p.summary, 'back to the View menu\'s button') }) test('dismissed from inside the card through the View row, focus goes to the menu\'s button', () => { const doc = installDocument({ cookie: 'slides-inspector=closed' }) const p = page({ state: 'closed' }) mount(p) p.menu.open = true p.row.focus() click(p.root, p.row, 0) p.menu.open = true p.row.focus() click(p.root, p.row, 0) assert.equal(p.card.getAttribute('data-state'), 'closed') assert.equal(doc.activeElement, p.summary) }) test('a return target that can no longer take focus falls back to Format', () => { const doc = installDocument({ cookie: 'slides-inspector=closed' }) const p = page({ state: 'closed' }) const head = mount(p) p.elsewhere.focus() head.showInspector(true, { focus: true }) assert.equal(doc.activeElement, p.title) p.elsewhere.visible = false head.showInspector(false) assert.equal(doc.activeElement, p.format) p.elsewhere.visible = true p.elsewhere.focus() head.showInspector(true, { focus: true }) p.elsewhere.isConnected = false head.showInspector(false) assert.equal(doc.activeElement, p.format) }) test('asking to delete from a strip item\'s menu returns focus to that menu\'s button', () => { const doc = installDocument({ cookie: 'slides-inspector=closed' }) const p = page({ state: 'closed' }) const stripMenu = el({}, ['details']) stripMenu.open = true const kebab = el({}, [':scope > summary']) const del = el({ 'data-slide-command': 'delete' }, ['[data-slide-command="delete"]']) stripMenu.add(kebab, del) p.root.add(stripMenu) mount(p) del.focus() click(p.root, del, 0) stripMenu.open = false // strip.mjs closes its menu assert.equal(doc.activeElement, p.title) p.format.focus() click(p.root, p.format, 0) assert.equal(doc.activeElement, kebab) }) test('focus into the card reaches a radio group at its checked radio', () => { const radio = (name, checked) => ({ type: 'radio', name, checked }) const flow = radio('slides-layout-pick', false) const canvas = radio('slides-layout-pick', true) const lone = radio('other', false) const root = { querySelectorAll: () => [flow, canvas, lone] } assert.equal(uncheckedOfGroup(flow, root), true, 'unchecked, its group has a checked one') assert.equal(uncheckedOfGroup(canvas, root), false, 'the checked one') assert.equal(uncheckedOfGroup(lone, root), false, 'a group with nothing checked: its first radio stands for it') assert.equal(uncheckedOfGroup({ type: 'checkbox', checked: false, name: 'x' }, root), false, 'not a radio') }) // Fires the listeners e holds for type, as a browser would. function fire (e, type, extra = {}) { const ev = { type, target: e, preventDefault () {}, ...extra } for (const l of e.listeners) if (l.type === type) l.fn(ev) } test('the bar\'s title is the one place the deck\'s title is edited: a change is commitTitle', () => { const doc = installDocument() const p = page() const title = el({}, ['[slides-head-title]']) title.value = 'Talk' title.blur = () => { doc.activeElement = null } p.root.add(title) const sent = [] const head = mountHead({ root: p.root, commands: {}, storage: fakeStorage(), commitTitle: v => sent.push(v) }) title.value = ' A new talk ' fire(title, 'change') assert.deepEqual(sent, ['A new talk']) assert.equal(title.value, 'A new talk') // Empty, or the same: nothing sent, the title put back. title.value = ' ' fire(title, 'change') title.value = 'A new talk' fire(title, 'change') assert.deepEqual(sent, ['A new talk']) assert.equal(title.value, 'A new talk') // A colleague's title shows, and is what Escape puts back. head.setTitle('Their talk') assert.equal(title.value, 'Their talk') title.value = 'half typed' fire(title, 'keydown', { key: 'Escape' }) assert.equal(title.value, 'Their talk') assert.deepEqual(sent, ['A new talk']) }) test('a menu row\'s dialog opens modal, and on close focus goes to the menu\'s title', () => { const doc = installDocument() const p = page() const file = el({}, ['details']) file.open = true const title = el({}, [':scope > summary']) const row = el({ 'data-open-dialog': 'slides-deck-dialog' }, ['[data-open-dialog]']) file.add(title, row) p.root.add(file) const dialog = el({ id: 'slides-deck-dialog' }) dialog.open = false dialog.showModal = () => { dialog.open = true; doc.activeElement = dialog } doc.getElementById = id => id === 'slides-deck-dialog' ? dialog : null mount(p) row.focus() click(p.root, row) assert.equal(file.open, false, 'the menu closes') assert.equal(dialog.open, true, 'the dialog is open') // Escape or Close: the dialog's close event. dialog.open = false fire(dialog, 'close') assert.equal(doc.activeElement, title) }) test('an Insert row adds its kind through commands.addBlock, and closes the menu', () => { installDocument() const p = page() const insert = el({}, ['details']) insert.open = true const row = el({ 'data-insert-block': 'shape' }, ['[data-insert-block]']) insert.add(el({}, [':scope > summary']), row) p.root.add(insert) const added = [] mountHead({ root: p.root, commands: { addBlock: kind => added.push(kind) }, storage: fakeStorage() }) click(p.root, row) assert.deepEqual(added, ['shape']) assert.equal(insert.open, false) // A disabled row (no slide) adds nothing. row.disabled = true click(p.root, row) assert.deepEqual(added, ['shape']) }) test('View → Outline opens the Outline under the View menu\'s title, and leaves the card alone', () => { installDocument() const p = page() const row = el({ 'data-view-toggle': 'outline' }, ['[data-view-toggle]']) p.menu.add(row) p.menu.open = true const opened = [] mountHead({ root: p.root, commands: {}, outline: { open: o => opened.push(o) }, storage: fakeStorage() }) click(p.root, row) assert.equal(opened.length, 1) assert.equal(opened[0].returnTo, p.summary, 'focus comes back to the menu\'s title, not its hidden row') assert.equal(p.menu.open, false) assert.equal(p.card.getAttribute('data-state'), 'open') })