oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// head.mjs: the inspector card's Format toggle (spec §2, §4, §4.4) —
2// its cookie, the old localStorage preference moved to it once, the
3// View menu row kept in step, cookies blocked, and focus in and back.
4import test from 'node:test'
5import assert from 'node:assert/strict'
6import { mountHead, INSPECTOR_KEY, uncheckedOfGroup } from './head.mjs'
7
8// A stand-in element: attributes, dataset, listeners, focus, and the
9// few selectors head.mjs asks of it. `is` lists the selectors it
10// matches; `closest` answers for itself and its parents.
11function el (attrs = {}, is = []) {
12 const e = {
13 attrs: new Map(Object.entries(attrs)),
14 dataset: {},
15 is: new Set(is),
16 parent: null,
17 kids: [],
18 listeners: [],
19 isConnected: true,
20 getAttribute: k => e.attrs.has(k) ? e.attrs.get(k) : null,
21 setAttribute: (k, v) => e.attrs.set(k, String(v)),
22 hasAttribute: k => e.attrs.has(k),
23 removeAttribute: k => e.attrs.delete(k),
24 matches: sel => sel.split(',').some(s => e.is.has(s.trim())),
25 closest (sel) {
26 for (let n = e; n; n = n.parent) if (n.matches(sel)) return n
27 return null
28 },
29 contains (n) {
30 for (; n; n = n.parent) if (n === e) return true
31 return false
32 },
33 add (...kids) { for (const k of kids) { k.parent = e; e.kids.push(k) } return e },
34 all () { return e.kids.flatMap(k => [k, ...k.all()]) },
35 querySelectorAll: sel => e.all().filter(n => n.matches(sel)),
36 querySelector: sel => e.querySelectorAll(sel)[0] || null,
37 addEventListener (type, fn, capture = false) { e.listeners.push({ type, fn, capture: !!capture }) },
38 // Drawn unless it, or a closed <details> it is in (its summary
39 // apart), is not: a browser's focus() does nothing to an element
40 // that is not drawn, and neither does this one.
41 visible: true,
42 checkVisibility () {
43 for (let n = e; n; n = n.parent) {
44 if (!n.visible) return false
45 if (n.parent && n.parent.is.has('details') && n.parent.open === false && !n.is.has(':scope > summary')) return false
46 }
47 return true
48 },
49 focus () { if (e.checkVisibility()) globalThis.document.activeElement = e }
50 }
51 for (const [k, v] of Object.entries(attrs)) {
52 if (k.startsWith('data-')) e.dataset[k.slice(5).replace(/-([a-z])/g, (m, c) => c.toUpperCase())] = v
53 }
54 return e
55}
56
57const TOGGLE = '[data-view-toggle="inspector"]'
58const ANY = ['[data-view-toggle]']
59const FOCUSABLE = 'input:not([type="hidden"]):not([disabled])'
60
61// The page head.mjs mounts on: the card, holding a hidden field and
62// then a title field; the bar's Format button; the View menu's row; a
63// field on the stage that had focus before.
64function page ({ state = 'open' } = {}) {
65 const root = el()
66 const card = el({ id: 'slides-inspector', 'data-state': state }, ['[slides-inspector]'])
67 const hiddenField = el({}, [FOCUSABLE])
68 hiddenField.closest = sel => sel === '[hidden]' ? hiddenField : null
69 const title = el({ name: 'deck-title' }, [FOCUSABLE])
70 card.add(hiddenField, title)
71 const format = el({ 'data-view-toggle': 'inspector', 'aria-expanded': String(state !== 'closed'), 'aria-controls': 'slides-inspector' }, [TOGGLE, '[data-view-toggle="inspector"][aria-expanded]', ...ANY])
72 const row = el({ 'data-view-toggle': 'inspector', 'aria-pressed': String(state !== 'closed') }, [TOGGLE, ...ANY])
73 // The View menu: a <details> whose summary is its button.
74 const menu = el({}, ['details'])
75 menu.open = false
76 const summary = el({}, [':scope > summary'])
77 menu.add(summary, row)
78 const elsewhere = el({}, [])
79 root.add(card, format, menu, elsewhere)
80 return { root, card, title, format, menu, summary, row, elsewhere }
81}
82
83// A click on target, through the root's capture then bubble listeners.
84// detail is 0 for a click from the keyboard, 1 from a pointer.
85function click (root, target, detail = 1) {
86 const ev = { type: 'click', target, detail, preventDefault () {} }
87 for (const pass of [true, false]) {
88 for (const l of root.listeners) if (l.type === 'click' && l.capture === pass) l.fn(ev)
89 }
90}
91
92function fakeStorage (init = {}) {
93 const m = new Map(Object.entries(init))
94 return {
95 m,
96 getItem: k => m.has(k) ? m.get(k) : null,
97 setItem: (k, v) => m.set(k, String(v)),
98 removeItem: k => m.delete(k)
99 }
100}
101
102// document.cookie as a browser has it: reading gives name=value pairs,
103// writing sets one. blocked: writes are refused with an exception.
104function installDocument ({ cookie = '', blocked = false } = {}) {
105 const jar = new Map()
106 for (const part of cookie.split(';')) {
107 const at = part.indexOf('=')
108 if (at > 0) jar.set(part.slice(0, at).trim(), part.slice(at + 1).trim())
109 }
110 const doc = {
111 activeElement: null,
112 body: null,
113 written: [],
114 get cookie () { return [...jar].map(([k, v]) => `${k}=${v}`).join('; ') },
115 set cookie (s) {
116 if (blocked) throw new Error('SecurityError: cookies are blocked')
117 doc.written.push(s)
118 const pair = s.split(';')[0]
119 const at = pair.indexOf('=')
120 jar.set(pair.slice(0, at), pair.slice(at + 1))
121 }
122 }
123 globalThis.document = doc
124 return doc
125}
126
127test.beforeEach(() => {
128 globalThis.CSS = { escape: s => s }
129 delete globalThis.location
130})
131
132const mount = (p, storage = fakeStorage()) => mountHead({ root: p.root, commands: {}, storage })
133
134test('Format writes the cookie the server reads, and flips the card and its toggles', () => {
135 const doc = installDocument()
136 const p = page()
137 const head = mount(p)
138 assert.equal(head.inspectorShown(), true)
139
140 click(p.root, p.format)
141 assert.equal(p.card.getAttribute('data-state'), 'closed')
142 assert.equal(p.format.getAttribute('aria-expanded'), 'false')
143 assert.equal(p.format.hasAttribute('aria-pressed'), false, 'the bar button has no aria-pressed')
144 assert.equal(p.row.getAttribute('aria-pressed'), 'false', 'the View row follows')
145 assert.equal(head.inspectorShown(), false)
146 assert.deepEqual(doc.written, ['slides-inspector=closed; Path=/; Max-Age=31536000; SameSite=Lax'])
147
148 click(p.root, p.format)
149 assert.equal(p.card.getAttribute('data-state'), 'open')
150 assert.equal(p.format.getAttribute('aria-expanded'), 'true')
151 assert.equal(p.row.getAttribute('aria-pressed'), 'true')
152 assert.equal(doc.written.at(-1), 'slides-inspector=open; Path=/; Max-Age=31536000; SameSite=Lax')
153})
154
155test('the View menu row toggles the card too, and keeps aria-pressed with the button', () => {
156 installDocument()
157 const p = page()
158 mount(p)
159 p.menu.open = true
160 click(p.root, p.row)
161 assert.equal(p.menu.open, false, 'the menu closes')
162 assert.equal(p.card.getAttribute('data-state'), 'closed')
163 assert.equal(p.row.getAttribute('aria-pressed'), 'false')
164 assert.equal(p.format.getAttribute('aria-expanded'), 'false')
165 assert.equal(p.row.hasAttribute('aria-expanded'), false, 'the row keeps aria-pressed, not aria-expanded')
166})
167
168test('the first paint is the server\'s: mounting writes nothing and changes nothing', () => {
169 const doc = installDocument({ cookie: 'slides-inspector=closed' })
170 const p = page({ state: 'closed' })
171 const head = mount(p)
172 assert.equal(head.inspectorShown(), false)
173 assert.equal(p.card.getAttribute('data-state'), 'closed')
174 assert.equal(p.format.getAttribute('aria-expanded'), 'false')
175 assert.equal(p.row.getAttribute('aria-pressed'), 'false')
176 assert.deepEqual(doc.written, [])
177})
178
179for (const [stored, state] of [['off', 'closed'], ['on', 'open']]) {
180 test(`an old localStorage '${stored}' with no cookie is applied once, moved to the cookie as ${state}, and removed`, () => {
181 const doc = installDocument()
182 const p = page({ state: stored === 'off' ? 'open' : 'closed' })
183 const storage = fakeStorage({ [INSPECTOR_KEY]: stored })
184 mount(p, storage)
185 assert.equal(p.card.getAttribute('data-state'), state)
186 assert.equal(p.format.getAttribute('aria-expanded'), String(state === 'open'))
187 assert.deepEqual(doc.written, [`slides-inspector=${state}; Path=/; Max-Age=31536000; SameSite=Lax`])
188 assert.equal(storage.m.has(INSPECTOR_KEY), false)
189
190 // Once: the next page has the cookie and no key, and is left alone.
191 const again = page({ state })
192 mount(again, storage)
193 assert.equal(again.card.getAttribute('data-state'), state)
194 assert.equal(doc.written.length, 1)
195 })
196}
197
198test('with a cookie already set, an old localStorage key is dropped and not applied', () => {
199 const doc = installDocument({ cookie: 'other=1; slides-inspector=open' })
200 const p = page()
201 const storage = fakeStorage({ [INSPECTOR_KEY]: 'off' })
202 mount(p, storage)
203 assert.equal(p.card.getAttribute('data-state'), 'open')
204 assert.deepEqual(doc.written, [])
205 assert.equal(storage.m.has(INSPECTOR_KEY), false)
206})
207
208test('with cookies blocked, Format still shows and hides the card for this page', () => {
209 installDocument({ blocked: true })
210 const p = page()
211 const head = mount(p)
212 click(p.root, p.format)
213 assert.equal(p.card.getAttribute('data-state'), 'closed')
214 assert.equal(p.format.getAttribute('aria-expanded'), 'false')
215 assert.equal(p.row.getAttribute('aria-pressed'), 'false')
216 click(p.root, p.format)
217 assert.equal(p.card.getAttribute('data-state'), 'open')
218 assert.equal(head.inspectorShown(), true)
219})
220
221test('with site data blocked (localStorage throws), the toggle still works', () => {
222 installDocument()
223 const p = page()
224 const storage = { getItem () { throw new Error('blocked') }, removeItem () { throw new Error('blocked') } }
225 mount(p, storage)
226 click(p.root, p.format)
227 assert.equal(p.card.getAttribute('data-state'), 'closed')
228})
229
230test('showInspector(true, {focus}) focuses the card\'s first showing control; hiding gives focus back', () => {
231 const doc = installDocument({ cookie: 'slides-inspector=closed' })
232 const p = page({ state: 'closed' })
233 const head = mount(p)
234 p.elsewhere.focus()
235 head.showInspector(true, { focus: true })
236 assert.equal(p.card.getAttribute('data-state'), 'open')
237 assert.equal(doc.activeElement, p.title, 'the first control that is not hidden')
238 head.showInspector(false)
239 assert.equal(p.card.getAttribute('data-state'), 'closed')
240 assert.equal(doc.activeElement, p.elsewhere)
241})
242
243test('Format from the keyboard moves focus into the card; from a pointer it does not', () => {
244 const doc = installDocument({ cookie: 'slides-inspector=closed' })
245 const p = page({ state: 'closed' })
246 mount(p)
247 p.format.focus()
248 click(p.root, p.format, 1)
249 assert.equal(doc.activeElement, p.format)
250 click(p.root, p.format, 1)
251 p.format.focus()
252 click(p.root, p.format, 0)
253 assert.equal(doc.activeElement, p.title)
254 // Dismissed from the keyboard: back to the button that opened it.
255 p.format.focus()
256 click(p.root, p.format, 0)
257 assert.equal(p.card.getAttribute('data-state'), 'closed')
258 assert.equal(doc.activeElement, p.format)
259})
260
261test('asking to delete a slide shows a hidden card, with focus', () => {
262 const doc = installDocument({ cookie: 'slides-inspector=closed' })
263 const p = page({ state: 'closed' })
264 const ask = el({ 'data-command': 'slide-delete-ask' }, ['[data-command="slide-delete-ask"]'])
265 p.root.add(ask)
266 mount(p)
267 click(p.root, ask)
268 assert.equal(p.card.getAttribute('data-state'), 'open')
269 assert.equal(p.format.getAttribute('aria-expanded'), 'true')
270 assert.equal(doc.activeElement, p.title)
271})
272
273test('on https the cookie is Secure', () => {
274 const doc = installDocument()
275 globalThis.location = { protocol: 'https:' }
276 const p = page()
277 mount(p)
278 click(p.root, p.format)
279 assert.equal(doc.written.at(-1), 'slides-inspector=closed; Path=/; Max-Age=31536000; SameSite=Lax; Secure')
280})
281
282test('opened from the View menu by keyboard, the card gives focus back to the menu\'s button, not its hidden row', () => {
283 const doc = installDocument({ cookie: 'slides-inspector=closed' })
284 const p = page({ state: 'closed' })
285 mount(p)
286 p.menu.open = true
287 p.row.focus()
288 assert.equal(doc.activeElement, p.row)
289 click(p.root, p.row, 0)
290 assert.equal(p.menu.open, false)
291 assert.equal(doc.activeElement, p.title, 'focus is in the card')
292 // Dismissed with Format from the keyboard.
293 p.format.focus()
294 click(p.root, p.format, 0)
295 assert.equal(p.card.getAttribute('data-state'), 'closed')
296 assert.equal(doc.activeElement, p.summary, 'back to the View menu\'s button')
297})
298
299test('dismissed from inside the card through the View row, focus goes to the menu\'s button', () => {
300 const doc = installDocument({ cookie: 'slides-inspector=closed' })
301 const p = page({ state: 'closed' })
302 mount(p)
303 p.menu.open = true
304 p.row.focus()
305 click(p.root, p.row, 0)
306 p.menu.open = true
307 p.row.focus()
308 click(p.root, p.row, 0)
309 assert.equal(p.card.getAttribute('data-state'), 'closed')
310 assert.equal(doc.activeElement, p.summary)
311})
312
313test('a return target that can no longer take focus falls back to Format', () => {
314 const doc = installDocument({ cookie: 'slides-inspector=closed' })
315 const p = page({ state: 'closed' })
316 const head = mount(p)
317 p.elsewhere.focus()
318 head.showInspector(true, { focus: true })
319 assert.equal(doc.activeElement, p.title)
320 p.elsewhere.visible = false
321 head.showInspector(false)
322 assert.equal(doc.activeElement, p.format)
323
324 p.elsewhere.visible = true
325 p.elsewhere.focus()
326 head.showInspector(true, { focus: true })
327 p.elsewhere.isConnected = false
328 head.showInspector(false)
329 assert.equal(doc.activeElement, p.format)
330})
331
332test('asking to delete from a strip item\'s menu returns focus to that menu\'s button', () => {
333 const doc = installDocument({ cookie: 'slides-inspector=closed' })
334 const p = page({ state: 'closed' })
335 const stripMenu = el({}, ['details'])
336 stripMenu.open = true
337 const kebab = el({}, [':scope > summary'])
338 const del = el({ 'data-slide-command': 'delete' }, ['[data-slide-command="delete"]'])
339 stripMenu.add(kebab, del)
340 p.root.add(stripMenu)
341 mount(p)
342 del.focus()
343 click(p.root, del, 0)
344 stripMenu.open = false // strip.mjs closes its menu
345 assert.equal(doc.activeElement, p.title)
346 p.format.focus()
347 click(p.root, p.format, 0)
348 assert.equal(doc.activeElement, kebab)
349})
350
351test('focus into the card reaches a radio group at its checked radio', () => {
352 const radio = (name, checked) => ({ type: 'radio', name, checked })
353 const flow = radio('slides-layout-pick', false)
354 const canvas = radio('slides-layout-pick', true)
355 const lone = radio('other', false)
356 const root = { querySelectorAll: () => [flow, canvas, lone] }
357 assert.equal(uncheckedOfGroup(flow, root), true, 'unchecked, its group has a checked one')
358 assert.equal(uncheckedOfGroup(canvas, root), false, 'the checked one')
359 assert.equal(uncheckedOfGroup(lone, root), false, 'a group with nothing checked: its first radio stands for it')
360 assert.equal(uncheckedOfGroup({ type: 'checkbox', checked: false, name: 'x' }, root), false, 'not a radio')
361})
362
363// Fires the listeners e holds for type, as a browser would.
364function fire (e, type, extra = {}) {
365 const ev = { type, target: e, preventDefault () {}, ...extra }
366 for (const l of e.listeners) if (l.type === type) l.fn(ev)
367}
368
369test('the bar\'s title is the one place the deck\'s title is edited: a change is commitTitle', () => {
370 const doc = installDocument()
371 const p = page()
372 const title = el({}, ['[slides-head-title]'])
373 title.value = 'Talk'
374 title.blur = () => { doc.activeElement = null }
375 p.root.add(title)
376 const sent = []
377 const head = mountHead({ root: p.root, commands: {}, storage: fakeStorage(), commitTitle: v => sent.push(v) })
378
379 title.value = ' A new talk '
380 fire(title, 'change')
381 assert.deepEqual(sent, ['A new talk'])
382 assert.equal(title.value, 'A new talk')
383
384 // Empty, or the same: nothing sent, the title put back.
385 title.value = ' '
386 fire(title, 'change')
387 title.value = 'A new talk'
388 fire(title, 'change')
389 assert.deepEqual(sent, ['A new talk'])
390 assert.equal(title.value, 'A new talk')
391
392 // A colleague's title shows, and is what Escape puts back.
393 head.setTitle('Their talk')
394 assert.equal(title.value, 'Their talk')
395 title.value = 'half typed'
396 fire(title, 'keydown', { key: 'Escape' })
397 assert.equal(title.value, 'Their talk')
398 assert.deepEqual(sent, ['A new talk'])
399})
400
401test('a menu row\'s dialog opens modal, and on close focus goes to the menu\'s title', () => {
402 const doc = installDocument()
403 const p = page()
404 const file = el({}, ['details'])
405 file.open = true
406 const title = el({}, [':scope > summary'])
407 const row = el({ 'data-open-dialog': 'slides-deck-dialog' }, ['[data-open-dialog]'])
408 file.add(title, row)
409 p.root.add(file)
410 const dialog = el({ id: 'slides-deck-dialog' })
411 dialog.open = false
412 dialog.showModal = () => { dialog.open = true; doc.activeElement = dialog }
413 doc.getElementById = id => id === 'slides-deck-dialog' ? dialog : null
414 mount(p)
415
416 row.focus()
417 click(p.root, row)
418 assert.equal(file.open, false, 'the menu closes')
419 assert.equal(dialog.open, true, 'the dialog is open')
420 // Escape or Close: the dialog's close event.
421 dialog.open = false
422 fire(dialog, 'close')
423 assert.equal(doc.activeElement, title)
424})
425
426test('an Insert row adds its kind through commands.addBlock, and closes the menu', () => {
427 installDocument()
428 const p = page()
429 const insert = el({}, ['details'])
430 insert.open = true
431 const row = el({ 'data-insert-block': 'shape' }, ['[data-insert-block]'])
432 insert.add(el({}, [':scope > summary']), row)
433 p.root.add(insert)
434 const added = []
435 mountHead({ root: p.root, commands: { addBlock: kind => added.push(kind) }, storage: fakeStorage() })
436 click(p.root, row)
437 assert.deepEqual(added, ['shape'])
438 assert.equal(insert.open, false)
439 // A disabled row (no slide) adds nothing.
440 row.disabled = true
441 click(p.root, row)
442 assert.deepEqual(added, ['shape'])
443})
444
445test('View → Outline opens the Outline under the View menu\'s title, and leaves the card alone', () => {
446 installDocument()
447 const p = page()
448 const row = el({ 'data-view-toggle': 'outline' }, ['[data-view-toggle]'])
449 p.menu.add(row)
450 p.menu.open = true
451 const opened = []
452 mountHead({ root: p.root, commands: {}, outline: { open: o => opened.push(o) }, storage: fakeStorage() })
453 click(p.root, row)
454 assert.equal(opened.length, 1)
455 assert.equal(opened[0].returnTo, p.summary, 'focus comes back to the menu\'s title, not its hidden row')
456 assert.equal(p.menu.open, false)
457 assert.equal(p.card.getAttribute('data-state'), 'open')
458})
459