/* * The shadow root's stylesheet in parts, at build time (build.js): the rules the first render can * need, and for each group loaded later (the menus' panels, the live report's states, the help) the * rules only that group's code matches, which load with it (src/styles.js puts them back in their * place). A rule goes to a group when each of its selectors names a class that the group's code * writes and no other code does, the first load's or another group's (a group that loads only after * another one leaves their shared classes to it). Anything else stays first: an at-rule, a rule on an * element or a token, a class the code builds from parts, a class two unrelated groups write. So a rule the first render needs never waits for a group; one that could wait may stay * first, which costs bytes, not looks. * * What the scan of the code cannot see, and why it holds today: a class the code builds from "pk-" alone * (the table's header cells, pk-corner and its kind) counts as no class; a class named only inside * :not() counts like any other; the element's own code (src/) is not scanned. None of them names a * class only a group writes. test/build.test.js and the browser spec element-first-load.spec.js check * that the first render looks the same before and after the menus' rules arrive. * * The parts keep their order: runs of the first load alternate with runs of the groups, and each * group's runs go back into their places between the first load's ones, so the cascade is the same * as in one sheet. */ /** * The top-level rules of a stylesheet, in order, as their text: a rule with its block, an at-rule with * its block, or a statement that ends in ";". Joined they are the stylesheet again, to the last space. * @param {string} css * @returns {string[]} */ export function topRules(css) { const rules = []; let start = 0; let depth = 0; let quote = ""; for (let i = 0; i < css.length; i++) { const c = css[i]; if (quote) { if (c === "\\") { i++; } else if (c === quote) { quote = ""; } } else if (c === '"' || c === "'") { quote = c; } else if (c === "/" && css[i + 1] === "*") { const end = css.indexOf("*/", i + 2); i = end < 0 ? css.length : end + 1; } else if (c === "{") { depth++; } else if (c === "}") { depth--; if (depth === 0) { rules.push(css.slice(start, i + 1)); start = i + 1; } } else if (c === ";" && depth === 0) { rules.push(css.slice(start, i + 1)); start = i + 1; } } const rest = css.slice(start); if (rest.trim() || rules.length === 0) { rules.push(rest); } else { rules[rules.length - 1] += rest; } if (depth !== 0) { throw new Error("pivotkit-element: the stylesheet's braces do not match"); } return rules.filter((rule) => rule !== ""); } /** The class names a piece of code writes: "pk-chip", "pk-chip pk-over", ` pk-over`, `pk-card${…}`. */ const CLASS_WORD = /(?, prefixes: string[] }} */ export function classesIn(code) { const prefixes = new Set([...code.matchAll(CLASS_PREFIX)].map((m) => m[1]).filter((p) => p !== "pk-")); return { names: new Set(code.match(CLASS_WORD) ?? []), prefixes: [...prefixes] }; } /** * @typedef {{ names: Set, prefixes: string[] }} Classes * @typedef {{ name: string, classes: Classes, after?: string }} Group `after`: the group it loads after */ /** * The group a class belongs to, or null (it stays in the first load): the one group that writes it, * where the first load does not. Where a group and one that loads only after it (`after`) both write * it, it is the earlier one's: it is there whenever the later one is. * @param {string} name * @param {Classes} first * @param {Group[]} groups */ function groupOf(name, first, groups) { /** @param {Classes} code */ const writes = (code) => code.names.has(name) || code.prefixes.some((p) => name.startsWith(p)); if (writes(first)) { return null; } const by = groups.filter((g) => writes(g.classes)); const own = by.filter((g) => !by.some((o) => o.name === g.after)); return own.length === 1 ? own[0].name : null; } /** * The group whose code alone writes the classes of a rule, or null (the rule stays in the first * load): not an at-rule, and each selector names a class of that one group (groupOf). * @param {string} rule * @param {Classes} first the first load's classes * @param {Group[]} groups * @returns {string | null} */ function ownerOf(rule, first, groups) { if (rule.trimStart().startsWith("@")) { return null; } const selector = rule.slice(0, rule.indexOf("{")); const owners = selector.split(",").map((part) => (part.match(/\.(pk-[\w-]+)/g) ?? []).map((c) => groupOf(c.slice(1), first, groups)).find(Boolean) ?? null); return owners.every((o) => o !== null && o === owners[0]) ? owners[0] : null; } /** * The stylesheet in runs: the first load's (`first`), and between them the runs of the groups loaded * later, `slots[i]` naming the group whose next run goes after first[i]. Joined in that order * (joinStyles) they are the stylesheet again. * @param {string} css minified * @param {string} firstCode the code of the first load * @param {Record} groupCode each group's code, which only it * loads (the menus, a lazy chunk), and the group it loads after * @returns {{ first: string[], slots: string[], groups: Record }} first has one run more than slots */ export function splitStyles(css, firstCode, groupCode) { const first = classesIn(firstCode); /** @type {Group[]} */ const groups = Object.entries(groupCode).map(([name, { code, after }]) => ({ name, classes: classesIn(code), after })); const out = { first: [""], slots: /** @type {string[]} */ ([]), groups: /** @type {Record} */ (Object.fromEntries(groups.map((g) => [g.name, []]))) }; /** The group of the run being filled, or null (the first load's). */ let open = /** @type {string | null} */ (null); const last = (/** @type {string[]} */ runs, /** @type {string} */ rule) => { runs[runs.length - 1] += rule; }; for (const rule of topRules(css)) { const owner = ownerOf(rule, first, groups); if (owner !== open) { // a group's run ends with a run of the first load, an empty one between two groups if (open !== null) { out.first.push(""); } if (owner !== null) { out.slots.push(owner); out.groups[owner].push(""); } open = owner; } last(owner === null ? out.first : out.groups[owner], rule); } if (open !== null) { out.first.push(""); } return out; } /** * The stylesheet from its runs: the first load's, with the runs of each group that is here * (`loaded`) in their places; a group not loaded yet leaves its places empty. * @param {readonly string[]} first * @param {readonly string[]} slots * @param {Readonly>} loaded */ export function joinStyles(first, slots, loaded) { /** @type {Record} */ const next = {}; let css = first[0]; slots.forEach((group, i) => { const at = (next[group] = (next[group] ?? -1) + 1); css += (Object.hasOwn(loaded, group) ? loaded[group][at] : "") + first[i + 1]; }); return css; }