// side.js behaviour, run under Node against a minimal DOM: just enough // elements, attribute selectors, focus and event dispatch for the // script's own selectors. It has no CSS, so what is visible is the // browser test's (side_browser_test.go, make test-browser). import assert from "node:assert/strict"; import fs from "node:fs"; import test from "node:test"; import vm from "node:vm"; const src = fs.readFileSync(new URL("./side.js", import.meta.url), "utf8"); // A compound selector: tag, [attr] and [attr="value"], comma-separated, // and :hover, which the fake answers from el.hovered. function matches(el, selector) { return selector.split(",").some((part) => { const m = part.trim().match(/^([a-z]*)((?:\[[^\]]+\])*)(:hover)?$/); if (!m) throw new Error("unsupported selector " + part); if (m[1] && el.tag !== m[1]) return false; if (m[3] && !el.hovered) return false; for (const [, name, value] of m[2].matchAll(/\[([^=\]]+)(?:="([^"]*)")?\]/g)) { if (!el.attrs.has(name)) return false; if (value !== undefined && el.attrs.get(name) !== value) return false; } return true; }); } let doc; class El { constructor(tag, attrs = {}, children = []) { this.tag = tag; this.attrs = new Map(Object.entries(attrs)); this.children = children; this.parent = null; this.listeners = []; this.hovered = false; this.disabled = "disabled" in attrs; for (const c of children) c.parent = this; } hasAttribute(n) { return this.attrs.has(n); } getAttribute(n) { return this.attrs.has(n) ? this.attrs.get(n) : null; } setAttribute(n, v) { this.attrs.set(n, String(v)); } removeAttribute(n) { this.attrs.delete(n); } get open() { return this.attrs.has("open"); } set open(v) { if (v) this.attrs.set("open", ""); else this.attrs.delete("open"); } get tabIndex() { return Number(this.attrs.get("tabindex") ?? 0); } set tabIndex(v) { this.attrs.set("tabindex", String(v)); } addEventListener(type, fn) { this.listeners.push({ type, fn }); } fire(type) { for (const l of this.listeners.filter((l) => l.type === type)) l.fn({ type }); } focus() { doc.activeElement = this; this.fire("focus"); } click() { const e = { type: "click", defaultPrevented: false, preventDefault() { this.defaultPrevented = true; } }; for (const l of this.listeners.filter((l) => l.type === "click")) l.fn(e); return e; } matches(s) { return matches(this, s); } contains(o) { for (let e = o; e; e = e.parent) if (e === this) return true; return false; } *walk() { for (const c of this.children) { yield c; yield* c.walk(); } } querySelectorAll(s) { return [...this.walk()].filter((e) => matches(e, s)); } querySelector(s) { return this.querySelectorAll(s)[0] ?? null; } closest(s) { for (let e = this; e; e = e.parent) if (e.tag && matches(e, s)) return e; return null; } } // page draws a side as the template does after Light(): Home holds the // page, which is lit on its own row under Pinned. fold: false draws it // with no fold (Side.Fold nil); frame: false draws no frame at all. function page(state, { wide = true, protocol = "https:", fold = true, frame = true } = {}) { const item = (attrs) => new El("a", { "estilo-side-item": "", ...attrs }, [new El("span", { "estilo-side-label": "" })]); const homeOpen = item({ "estilo-side-holds": "open", href: "/" }); const homeRail = item({ "estilo-side-holds": "rail", href: "/", "aria-current": "page", hidden: "" }); const search = item({ href: "/search" }); const pinned = item({ "estilo-side-page": "", href: "/p/a", "aria-current": "page" }); const hide = new El("button", { "estilo-side-item": "", "estilo-side-fold-to": "rail", "data-estilo-side-fold": "" }); const show = new El("button", { "estilo-side-item": "", "estilo-side-fold-to": "open", "data-estilo-side-fold": "" }); const form = new El("form", { "estilo-side-fold": "", "data-estilo-side-cookie": "app-side" }, [hide, show]); const foot = new El("div", { "estilo-side-foot": "" }, fold ? [form] : []); const side = new El("nav", { "estilo-side": "" }, [homeOpen, homeRail, search, pinned, foot]); const title = new El("summary", { "estilo-menubar-title": "", tabindex: "-1" }); const file = new El("summary", { "estilo-menubar-title": "", tabindex: "0" }); const row = new El("button", { "estilo-menu-item": "", "data-estilo-side-fold": "", disabled: "" }); const menu = new El("details", { open: "" }, [title, new El("div", { "estilo-menu": "" }, [row])]); const bar = new El("nav", { "estilo-menubar": "" }, [new El("details", {}, [file]), menu]); const framed = new El("div", { "estilo-side-frame": state }, [side]); const root = new El("", {}, frame ? [bar, framed] : [bar]); const listeners = []; doc = { readyState: "complete", cookie: "", activeElement: null, querySelector: (s) => root.querySelector(s), querySelectorAll: (s) => root.querySelectorAll(s), addEventListener: (type, fn) => listeners.push({ type, fn }), }; const window = { matchMedia: () => ({ matches: wide }) }; vm.runInNewContext(src, { document: doc, window, location: { protocol } }); const key = (init) => { const e = { type: "keydown", defaultPrevented: false, preventDefault() { this.defaultPrevented = true; }, ...init }; for (const l of listeners.filter((l) => l.type === "keydown")) l.fn(e); return e; }; return { doc, frame: framed, hide, show, row, menu, title, file, key, homeOpen, homeRail, search, pinned }; } test("the fold folds in place, sets the form's cookie and moves focus to the other button", () => { const p = page("open"); p.hide.focus(); const e = p.hide.click(); assert.equal(e.defaultPrevented, true); assert.equal(p.frame.getAttribute("estilo-side-frame"), "rail"); assert.equal(p.doc.cookie, "app-side=rail; path=/; max-age=31536000; samesite=lax; secure"); assert.equal(p.doc.activeElement, p.show); p.show.click(); assert.equal(p.frame.getAttribute("estilo-side-frame"), "open"); assert.equal(p.doc.activeElement, p.hide); }); test("with no state the width decides what is shown", () => { const wide = page("", { wide: true }); wide.hide.click(); assert.equal(wide.frame.getAttribute("estilo-side-frame"), "rail"); const narrow = page("", { wide: false, protocol: "http:" }); narrow.show.click(); assert.equal(narrow.frame.getAttribute("estilo-side-frame"), "open"); assert.equal(narrow.doc.cookie, "app-side=open; path=/; max-age=31536000; samesite=lax"); }); test("a menu row folds, is enabled, closes its menu and returns focus to its title", () => { const p = page("open"); assert.equal(p.row.disabled, false); p.row.focus(); p.row.click(); assert.equal(p.menu.open, false); assert.equal(p.frame.getAttribute("estilo-side-frame"), "rail"); assert.equal(p.doc.activeElement, p.title); assert.equal(p.title.getAttribute("tabindex"), "0"); assert.equal(p.file.getAttribute("tabindex"), "-1"); }); test("Cmd or Ctrl and backslash fold; other keys do not", () => { const p = page("rail"); assert.equal(p.key({ key: "\\", metaKey: true }).defaultPrevented, true); assert.equal(p.frame.getAttribute("estilo-side-frame"), "open"); p.key({ key: "\\", ctrlKey: true }); assert.equal(p.frame.getAttribute("estilo-side-frame"), "rail"); assert.equal(p.key({ key: "\\" }).defaultPrevented, false); assert.equal(p.frame.getAttribute("estilo-side-frame"), "rail"); }); test("the shortcut moves focus off a row the rail hides, and back", () => { const p = page("open"); p.pinned.focus(); p.key({ key: "\\", ctrlKey: true }); assert.equal(p.doc.activeElement, p.homeRail); p.key({ key: "\\", ctrlKey: true }); assert.equal(p.doc.activeElement, p.homeOpen); p.search.focus(); p.key({ key: "\\", ctrlKey: true }); assert.equal(p.doc.activeElement, p.search, "a row still shown keeps focus"); }); test("with no fold the shortcut does nothing and leaves the key alone", () => { const p = page("open", { fold: false }); const e = p.key({ key: "\\", ctrlKey: true }); assert.equal(e.defaultPrevented, false); assert.equal(p.frame.getAttribute("estilo-side-frame"), "open"); assert.equal(p.row.disabled, true, "a menu row stays as drawn"); }); test("with no frame the shortcut does nothing and leaves the key alone", () => { const p = page("open", { frame: false }); assert.equal(p.key({ key: "\\", metaKey: true }).defaultPrevented, false); assert.equal(p.row.disabled, true); }); test("Escape dismisses the tooltip of the focused and the hovered row until a new hover or focus", () => { const p = page("rail"); p.search.focus(); p.homeRail.hovered = true; const e = p.key({ key: "Escape" }); assert.equal(e.defaultPrevented, false); assert.equal(p.doc.activeElement, p.search); assert.equal(p.search.getAttribute("data-estilo-side-tip"), "off"); assert.equal(p.homeRail.getAttribute("data-estilo-side-tip"), "off"); assert.equal(p.pinned.getAttribute("data-estilo-side-tip"), null); p.homeRail.fire("pointerenter"); assert.equal(p.homeRail.getAttribute("data-estilo-side-tip"), null); p.search.focus(); assert.equal(p.search.getAttribute("data-estilo-side-tip"), null); }); test("with no fold Escape still dismisses the rail's tooltip", () => { const p = page("rail", { fold: false }); p.search.focus(); p.key({ key: "Escape" }); assert.equal(p.search.getAttribute("data-estilo-side-tip"), "off"); p.search.focus(); assert.equal(p.search.getAttribute("data-estilo-side-tip"), null); });