///
import { readFileSync } from 'node:fs'
// The element stylesheet for layout backstops. Vitest stubs CSS imports, so
// the file is read from disk (vitest runs from the plugin root). happy-dom
// has no layout engine: the backstops check the layout contract instead.
export const css = readFileSync(`${process.cwd()}/assets/src/elements/styles.css`, 'utf8')
/** The declarations of every rule (outside @media) whose selector list names selector. */
export function rule(selector: string, sheet = css): Record {
const out: Record = {}
const body = sheet.replace(/\/\*[\s\S]*?\*\//g, '').replace(/@media[^{]*\{(?:[^{}]*\{[^{}]*\})*[^{}]*\}/g, '')
for (const match of body.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
const selectors = match[1].split(',').map((s) => s.trim())
if (!selectors.includes(selector)) {
continue
}
for (const decl of match[2].split(';')) {
const colon = decl.indexOf(':')
if (colon > 0) {
out[decl.slice(0, colon).trim()] = decl.slice(colon + 1).trim()
}
}
}
return out
}