oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// font-families.test.mjs — the browser's family rule against the server's,
2// through the shared vectors (vectors/font-families.json, which
3// internal/slides/render/families_test.go reads too), and the section
4// reader the stage and the projector check a fetched slide with.
5import { test } from 'node:test'
6import assert from 'node:assert/strict'
7import { readFileSync } from 'node:fs'
8import { familiesOfView, familiesOfSection, familiesInStack, safeFontStack } from './font-families.mjs'
9
10const vectors = JSON.parse(readFileSync(new URL('./vectors/font-families.json', import.meta.url)))
11
12test('the shared vectors have cases to check', () => {
13 assert.ok(vectors.cases.length >= 10)
14})
15
16for (const c of vectors.cases) {
17 test(`familiesOfView: ${c.name}`, () => {
18 assert.deepEqual(familiesOfView(c.view, vectors.stacks), c.families)
19 })
20}
21
22test('a stack names a family wherever it appears, in any case, quoted or not', () => {
23 assert.deepEqual(familiesInStack('Arial, "Liberation Serif", serif'), ['serif'])
24 assert.deepEqual(familiesInStack("liberation mono, 'LIBERATION SANS'"), ['sans', 'mono'])
25 assert.deepEqual(familiesInStack('Liberation Sans Narrow, sans-serif'), [])
26 assert.deepEqual(familiesInStack(''), [])
27 assert.deepEqual(familiesInStack(null), [])
28})
29
30test('safeFontStack is render.go\'s allowlist', () => {
31 assert.equal(safeFontStack(' Georgia, serif '), 'Georgia, serif')
32 assert.equal(safeFontStack('url(x)'), '')
33 assert.equal(safeFontStack('a; b'), '')
34 assert.equal(safeFontStack('x'.repeat(201)), '')
35 assert.equal(safeFontStack(7), '')
36})
37
38// The reader with no DOM: the section's style attribute and whether it
39// holds code, the way sectionReader gets them from a <template>.
40const reader = {
41 styleOf: html => (/<section[^>]*\sstyle="([^"]*)"/.exec(html) || [])[1] || '',
42 hasCodeIn: html => /<(pre|code)[\s>]/.test(html)
43}
44const section = (style, inner = '') => `<section slides-slide data-layout="flow" style="${style}">${inner}</section>`
45
46test('a section names its heading and body families', () => {
47 const html = section('--slides-bg:#fff;--slides-font-heading:Liberation Serif, Georgia, serif;--slides-font-body:Liberation Sans, system-ui, sans-serif', '<h1>x</h1>')
48 assert.deepEqual(familiesOfSection(html, vectors.stacks, reader), ['sans', 'serif'])
49})
50
51test('a section with code in it brings in the code family', () => {
52 const style = '--slides-font-heading:system-ui;--slides-font-body:Georgia, serif'
53 assert.deepEqual(familiesOfSection(section(style, '<pre slides-block data-block="code"><code>x</code></pre>'), vectors.stacks, reader), ['mono'])
54 assert.deepEqual(familiesOfSection(section(style, '<p>run <code>go</code></p>'), vectors.stacks, reader), ['mono'])
55 assert.deepEqual(familiesOfSection(section(style, '<p>no code</p>'), vectors.stacks, reader), [])
56})
57
58test('only the two font properties are read off a section', () => {
59 const html = section('--slides-bg:Liberation Serif;--slides-font-body:Liberation Sans')
60 assert.deepEqual(familiesOfSection(html, vectors.stacks, reader), ['sans'])
61 assert.deepEqual(familiesOfSection('', vectors.stacks, reader), [])
62})
63