oficina / estilo Public

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

Plain git — no account needed to clone.

Download

Download this file

1// side.js behaviour, run under Node against a minimal DOM: just enough
2// elements, attribute selectors, focus and event dispatch for the
3// script's own selectors. It has no CSS, so what is visible is the
4// browser test's (side_browser_test.go, make test-browser).
5import assert from "node:assert/strict";
6import fs from "node:fs";
7import test from "node:test";
8import vm from "node:vm";
9
10const src = fs.readFileSync(new URL("./side.js", import.meta.url), "utf8");
11
12// A compound selector: tag, [attr] and [attr="value"], comma-separated,
13// and :hover, which the fake answers from el.hovered.
14function matches(el, selector) {
15 return selector.split(",").some((part) => {
16 const m = part.trim().match(/^([a-z]*)((?:\[[^\]]+\])*)(:hover)?$/);
17 if (!m) throw new Error("unsupported selector " + part);
18 if (m[1] && el.tag !== m[1]) return false;
19 if (m[3] && !el.hovered) return false;
20 for (const [, name, value] of m[2].matchAll(/\[([^=\]]+)(?:="([^"]*)")?\]/g)) {
21 if (!el.attrs.has(name)) return false;
22 if (value !== undefined && el.attrs.get(name) !== value) return false;
23 }
24 return true;
25 });
26}
27
28let doc;
29class El {
30 constructor(tag, attrs = {}, children = []) {
31 this.tag = tag;
32 this.attrs = new Map(Object.entries(attrs));
33 this.children = children;
34 this.parent = null;
35 this.listeners = [];
36 this.hovered = false;
37 this.disabled = "disabled" in attrs;
38 for (const c of children) c.parent = this;
39 }
40 hasAttribute(n) { return this.attrs.has(n); }
41 getAttribute(n) { return this.attrs.has(n) ? this.attrs.get(n) : null; }
42 setAttribute(n, v) { this.attrs.set(n, String(v)); }
43 removeAttribute(n) { this.attrs.delete(n); }
44 get open() { return this.attrs.has("open"); }
45 set open(v) { if (v) this.attrs.set("open", ""); else this.attrs.delete("open"); }
46 get tabIndex() { return Number(this.attrs.get("tabindex") ?? 0); }
47 set tabIndex(v) { this.attrs.set("tabindex", String(v)); }
48 addEventListener(type, fn) { this.listeners.push({ type, fn }); }
49 fire(type) { for (const l of this.listeners.filter((l) => l.type === type)) l.fn({ type }); }
50 focus() { doc.activeElement = this; this.fire("focus"); }
51 click() {
52 const e = { type: "click", defaultPrevented: false, preventDefault() { this.defaultPrevented = true; } };
53 for (const l of this.listeners.filter((l) => l.type === "click")) l.fn(e);
54 return e;
55 }
56 matches(s) { return matches(this, s); }
57 contains(o) { for (let e = o; e; e = e.parent) if (e === this) return true; return false; }
58 *walk() { for (const c of this.children) { yield c; yield* c.walk(); } }
59 querySelectorAll(s) { return [...this.walk()].filter((e) => matches(e, s)); }
60 querySelector(s) { return this.querySelectorAll(s)[0] ?? null; }
61 closest(s) { for (let e = this; e; e = e.parent) if (e.tag && matches(e, s)) return e; return null; }
62}
63
64// page draws a side as the template does after Light(): Home holds the
65// page, which is lit on its own row under Pinned. fold: false draws it
66// with no fold (Side.Fold nil); frame: false draws no frame at all.
67function page(state, { wide = true, protocol = "https:", fold = true, frame = true } = {}) {
68 const item = (attrs) => new El("a", { "estilo-side-item": "", ...attrs }, [new El("span", { "estilo-side-label": "" })]);
69 const homeOpen = item({ "estilo-side-holds": "open", href: "/" });
70 const homeRail = item({ "estilo-side-holds": "rail", href: "/", "aria-current": "page", hidden: "" });
71 const search = item({ href: "/search" });
72 const pinned = item({ "estilo-side-page": "", href: "/p/a", "aria-current": "page" });
73 const hide = new El("button", { "estilo-side-item": "", "estilo-side-fold-to": "rail", "data-estilo-side-fold": "" });
74 const show = new El("button", { "estilo-side-item": "", "estilo-side-fold-to": "open", "data-estilo-side-fold": "" });
75 const form = new El("form", { "estilo-side-fold": "", "data-estilo-side-cookie": "app-side" }, [hide, show]);
76 const foot = new El("div", { "estilo-side-foot": "" }, fold ? [form] : []);
77 const side = new El("nav", { "estilo-side": "" }, [homeOpen, homeRail, search, pinned, foot]);
78 const title = new El("summary", { "estilo-menubar-title": "", tabindex: "-1" });
79 const file = new El("summary", { "estilo-menubar-title": "", tabindex: "0" });
80 const row = new El("button", { "estilo-menu-item": "", "data-estilo-side-fold": "", disabled: "" });
81 const menu = new El("details", { open: "" }, [title, new El("div", { "estilo-menu": "" }, [row])]);
82 const bar = new El("nav", { "estilo-menubar": "" }, [new El("details", {}, [file]), menu]);
83 const framed = new El("div", { "estilo-side-frame": state }, [side]);
84 const root = new El("", {}, frame ? [bar, framed] : [bar]);
85 const listeners = [];
86 doc = {
87 readyState: "complete",
88 cookie: "",
89 activeElement: null,
90 querySelector: (s) => root.querySelector(s),
91 querySelectorAll: (s) => root.querySelectorAll(s),
92 addEventListener: (type, fn) => listeners.push({ type, fn }),
93 };
94 const window = { matchMedia: () => ({ matches: wide }) };
95 vm.runInNewContext(src, { document: doc, window, location: { protocol } });
96 const key = (init) => {
97 const e = { type: "keydown", defaultPrevented: false, preventDefault() { this.defaultPrevented = true; }, ...init };
98 for (const l of listeners.filter((l) => l.type === "keydown")) l.fn(e);
99 return e;
100 };
101 return { doc, frame: framed, hide, show, row, menu, title, file, key, homeOpen, homeRail, search, pinned };
102}
103
104test("the fold folds in place, sets the form's cookie and moves focus to the other button", () => {
105 const p = page("open");
106 p.hide.focus();
107 const e = p.hide.click();
108 assert.equal(e.defaultPrevented, true);
109 assert.equal(p.frame.getAttribute("estilo-side-frame"), "rail");
110 assert.equal(p.doc.cookie, "app-side=rail; path=/; max-age=31536000; samesite=lax; secure");
111 assert.equal(p.doc.activeElement, p.show);
112 p.show.click();
113 assert.equal(p.frame.getAttribute("estilo-side-frame"), "open");
114 assert.equal(p.doc.activeElement, p.hide);
115});
116
117test("with no state the width decides what is shown", () => {
118 const wide = page("", { wide: true });
119 wide.hide.click();
120 assert.equal(wide.frame.getAttribute("estilo-side-frame"), "rail");
121 const narrow = page("", { wide: false, protocol: "http:" });
122 narrow.show.click();
123 assert.equal(narrow.frame.getAttribute("estilo-side-frame"), "open");
124 assert.equal(narrow.doc.cookie, "app-side=open; path=/; max-age=31536000; samesite=lax");
125});
126
127test("a menu row folds, is enabled, closes its menu and returns focus to its title", () => {
128 const p = page("open");
129 assert.equal(p.row.disabled, false);
130 p.row.focus();
131 p.row.click();
132 assert.equal(p.menu.open, false);
133 assert.equal(p.frame.getAttribute("estilo-side-frame"), "rail");
134 assert.equal(p.doc.activeElement, p.title);
135 assert.equal(p.title.getAttribute("tabindex"), "0");
136 assert.equal(p.file.getAttribute("tabindex"), "-1");
137});
138
139test("Cmd or Ctrl and backslash fold; other keys do not", () => {
140 const p = page("rail");
141 assert.equal(p.key({ key: "\\", metaKey: true }).defaultPrevented, true);
142 assert.equal(p.frame.getAttribute("estilo-side-frame"), "open");
143 p.key({ key: "\\", ctrlKey: true });
144 assert.equal(p.frame.getAttribute("estilo-side-frame"), "rail");
145 assert.equal(p.key({ key: "\\" }).defaultPrevented, false);
146 assert.equal(p.frame.getAttribute("estilo-side-frame"), "rail");
147});
148
149test("the shortcut moves focus off a row the rail hides, and back", () => {
150 const p = page("open");
151 p.pinned.focus();
152 p.key({ key: "\\", ctrlKey: true });
153 assert.equal(p.doc.activeElement, p.homeRail);
154 p.key({ key: "\\", ctrlKey: true });
155 assert.equal(p.doc.activeElement, p.homeOpen);
156 p.search.focus();
157 p.key({ key: "\\", ctrlKey: true });
158 assert.equal(p.doc.activeElement, p.search, "a row still shown keeps focus");
159});
160
161test("with no fold the shortcut does nothing and leaves the key alone", () => {
162 const p = page("open", { fold: false });
163 const e = p.key({ key: "\\", ctrlKey: true });
164 assert.equal(e.defaultPrevented, false);
165 assert.equal(p.frame.getAttribute("estilo-side-frame"), "open");
166 assert.equal(p.row.disabled, true, "a menu row stays as drawn");
167});
168
169test("with no frame the shortcut does nothing and leaves the key alone", () => {
170 const p = page("open", { frame: false });
171 assert.equal(p.key({ key: "\\", metaKey: true }).defaultPrevented, false);
172 assert.equal(p.row.disabled, true);
173});
174
175test("Escape dismisses the tooltip of the focused and the hovered row until a new hover or focus", () => {
176 const p = page("rail");
177 p.search.focus();
178 p.homeRail.hovered = true;
179 const e = p.key({ key: "Escape" });
180 assert.equal(e.defaultPrevented, false);
181 assert.equal(p.doc.activeElement, p.search);
182 assert.equal(p.search.getAttribute("data-estilo-side-tip"), "off");
183 assert.equal(p.homeRail.getAttribute("data-estilo-side-tip"), "off");
184 assert.equal(p.pinned.getAttribute("data-estilo-side-tip"), null);
185 p.homeRail.fire("pointerenter");
186 assert.equal(p.homeRail.getAttribute("data-estilo-side-tip"), null);
187 p.search.focus();
188 assert.equal(p.search.getAttribute("data-estilo-side-tip"), null);
189});
190
191test("with no fold Escape still dismisses the rail's tooltip", () => {
192 const p = page("rail", { fold: false });
193 p.search.focus();
194 p.key({ key: "Escape" });
195 assert.equal(p.search.getAttribute("data-estilo-side-tip"), "off");
196 p.search.focus();
197 assert.equal(p.search.getAttribute("data-estilo-side-tip"), null);
198});
199