// fixtures/pref-cookie.mjs — a per-person editor preference kept in a // cookie the server reads, so it renders the preference and the first // paint is the final one: the inspector card's state (head.mjs, // slides-inspector) and the notes card's state and height // (notes-card.mjs, slides-notes and slides-notes-height). Never document // state. // // Every cookie is Path=/; Max-Age=31536000; SameSite=Lax, and Secure // on https. Where cookies cannot be read or written (blocked), reading // is as if absent and writing does nothing: the preference holds for // the page. Tested in pref-cookie.test.mjs. /** The cookie's value, or null when it is absent or unreadable. */ export function readCookie (name, doc = globalThis.document) { try { const all = String(doc?.cookie || '') for (const part of all.split(';')) { const at = part.indexOf('=') if (at > 0 && part.slice(0, at).trim() === name) return part.slice(at + 1).trim() } } catch { /* unreadable: as if absent */ } return null } /** Writes the cookie for a year, site-wide; nothing where it cannot. */ export function writeCookie (name, value, doc = globalThis.document, loc = globalThis.location) { const secure = loc?.protocol === 'https:' ? '; Secure' : '' try { if (doc) doc.cookie = `${name}=${value}; Path=/; Max-Age=31536000; SameSite=Lax${secure}` } catch { /* cookies blocked: this page only */ } }