Fail the gate on a stylesheet feature the browser floor lacks, unless an @supports guards it
CI / full-gate (push) Successful in 4m45s
CI / full-gate (push) Successful in 4m45s
This commit is contained in:
@@ -1,16 +1,71 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readdirSync, readFileSync } from "node:fs";
|
||||
import { globSync, readFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { test } from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { floorViolations } from "./stylesheet-floor.ts";
|
||||
|
||||
const cssDir = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "public", "css");
|
||||
const root = join(dirname(fileURLToPath(import.meta.url)), "..", "..");
|
||||
const cssDir = join(root, "public", "css");
|
||||
|
||||
// A custom property holding oklch() passes unchecked on an engine below Chromium 111, so the
|
||||
// var() that reads it is invalid at computed-value time: white text on no background.
|
||||
for (const file of readdirSync(cssDir).filter((f) => f.endsWith(".css"))) {
|
||||
test(`${file} declares no oklch() colour`, () => {
|
||||
assert.ok(!/oklch\(/i.test(readFileSync(join(cssDir, file), "utf8")));
|
||||
test("floorViolations flags a feature the floor lacks, with its line and the first version that has it", () => {
|
||||
assert.deepEqual(floorViolations(".a { color: red; }\n.b:has(.c) { color: red; }"), [
|
||||
"line 2: :has needs chrome 105, safari 15.4, firefox 121 (css.selectors.has)",
|
||||
]);
|
||||
assert.deepEqual(floorViolations(".a { min-height: 100dvh; }"), [
|
||||
"line 1: dvh needs chrome 108, safari 15.4 (css.types.length.viewport_percentage_units_dynamic)",
|
||||
]);
|
||||
assert.deepEqual(floorViolations(".a { accent-color: red; }"), [
|
||||
"line 1: accent-color needs chrome 93, safari 15.4 (css.properties.accent-color)",
|
||||
]);
|
||||
assert.deepEqual(floorViolations(".a { overflow-wrap: anywhere; }"), [
|
||||
"line 1: overflow-wrap: anywhere needs safari 15.4 (css.properties.overflow-wrap.anywhere)",
|
||||
]);
|
||||
assert.equal(floorViolations(".a { & .b { color: red; } }").length, 1);
|
||||
});
|
||||
|
||||
test("floorViolations checks a custom property's value, since the var() reading it fails unseen", () => {
|
||||
assert.deepEqual(floorViolations(":root { --accent: oklch(0.5 0.1 250); }"), [
|
||||
"line 1: oklch() needs chrome 111, safari 15.4 (css.types.color.oklch)",
|
||||
]);
|
||||
});
|
||||
|
||||
test("floorViolations allows a feature inside an @supports that tests it, and only then", () => {
|
||||
assert.deepEqual(floorViolations("@supports selector(:has(a)) { .b:has(.c) { color: red; } }"), []);
|
||||
assert.deepEqual(floorViolations("@media (min-width: 1px) { @supports (min-height: 100dvh) { .a { min-height: 100dvh; } } }"), []);
|
||||
assert.equal(floorViolations("@supports not selector(:has(a)) { .b:has(.c) { color: red; } }").length, 1);
|
||||
assert.equal(floorViolations("@supports (display: grid) and (not selector(:has(a))) { .b:has(.c) { color: red; } }").length, 1);
|
||||
assert.equal(floorViolations("@supports (min-height: 100dvh) { .b:has(.c) { color: red; } }").length, 1);
|
||||
assert.equal(floorViolations("@supports (position-anchor: auto) { .a { top: anchor(bottom); } }").length, 1);
|
||||
});
|
||||
|
||||
test("floorViolations passes what the floor has, a vendor prefix and a comment", () => {
|
||||
const css = `/* .x:has(y) { min-height: 100dvh } */
|
||||
@media (prefers-color-scheme: dark) { :root { color-scheme: dark; } }
|
||||
.a::before, .a:focus-within { content: "{ :has( }"; display: grid; gap: 4px; inset: 0;
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr)); width: min(86vw, 20rem); transition: .15s ease;
|
||||
background: rgba(0,0,0,.4) url("data:image/svg+xml;utf8,<svg/>"); transform: rotate(45deg); }
|
||||
.b { -webkit-font-smoothing: antialiased; -webkit-user-select: none; }
|
||||
summary::-webkit-details-marker { display: none; }`;
|
||||
assert.deepEqual(floorViolations(css), []);
|
||||
});
|
||||
|
||||
test("floorViolations refuses what MDN has no entry for, so a typo or a new feature can't pass unchecked", () => {
|
||||
assert.deepEqual(floorViolations(".a { colr: red; }"), ["line 1: colr has no MDN compat entry"]);
|
||||
assert.equal(floorViolations(".a { width: 3zz; }").length, 1);
|
||||
assert.equal(floorViolations(".a:hovr { color: red; }").length, 1);
|
||||
});
|
||||
|
||||
const stylesheets = globSync(["public/**/*.css", "examples/**/*.css"], { cwd: root, exclude: ["**/node_modules/**"] });
|
||||
|
||||
test("the floor check finds the stylesheets", () => {
|
||||
assert.ok(stylesheets.includes("public/css/styles.css"));
|
||||
assert.ok(stylesheets.includes("examples/plugins/scheduling/public/scheduling.css"));
|
||||
});
|
||||
|
||||
for (const file of stylesheets) {
|
||||
test(`${file} uses nothing the browser floor lacks outside an @supports guarding it`, () => {
|
||||
assert.deepEqual(floorViolations(readFileSync(join(root, file), "utf8")), []);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -31,8 +86,8 @@ function blockEnd(start: number): number {
|
||||
// Chromium below 105 and Firefox below 121 have no `:has()`, and nothing else reveals a folded
|
||||
// branch there, so the fold may only exist where `:has()` works.
|
||||
test("styles.css folds .nav-children only inside @supports selector(:has(a))", () => {
|
||||
const open = stylesheet.indexOf("@supports selector(:has(a)) {");
|
||||
assert.ok(open >= 0, "no @supports selector(:has(a)) block");
|
||||
const open = stylesheet.lastIndexOf("@supports selector(:has(a)) {", stylesheet.indexOf(".nav-node > .nav-children"));
|
||||
assert.ok(open >= 0, "no @supports selector(:has(a)) block before the fold rule");
|
||||
const close = blockEnd(open);
|
||||
const outside = stylesheet.slice(0, open) + stylesheet.slice(close + 1);
|
||||
assert.ok(!/\.nav-children\s*\{[^}]*display:\s*none/.test(outside), "a .nav-children rule hides it outside the guard");
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
// README → "Two browser floors", checked against MDN's compat data: a feature one of FLOOR's engines
|
||||
// lacks passes only inside an `@supports` whose condition names it. Checked: at-rules, media features,
|
||||
// pseudo-classes and -elements, properties, and in a value its functions, its units and the keywords
|
||||
// MDN lists for that property. A partial implementation counts as support. A vendor-prefixed name is
|
||||
// one engine's by design and is skipped, as are `font-family` names: an engine skips a family it lacks.
|
||||
// Not checked: the body of an at-rule other than @media and @supports, and syntax MDN has no entry for,
|
||||
// such as media range syntax.
|
||||
import bcd from "@mdn/browser-compat-data" with { type: "json" };
|
||||
import type { CompatStatement, Identifier, SimpleSupportStatement } from "@mdn/browser-compat-data";
|
||||
|
||||
const FLOOR = { chrome: "87", safari: "15", firefox: "115" };
|
||||
|
||||
interface Feature {
|
||||
label: string;
|
||||
path: string | undefined;
|
||||
// Matches the feature in an `@supports` condition that tests for it.
|
||||
guard: RegExp;
|
||||
offset: number;
|
||||
}
|
||||
|
||||
interface Sheet {
|
||||
css: string;
|
||||
out: string[];
|
||||
}
|
||||
|
||||
export function floorViolations(css: string): string[] {
|
||||
const sheet = { css: blank(css), out: [] };
|
||||
walk(sheet, 0, sheet.css.length, []);
|
||||
return [...new Set(sheet.out)];
|
||||
}
|
||||
|
||||
// Comments, string contents and url() contents become spaces, so nothing inside one reads as syntax
|
||||
// and every offset keeps its line.
|
||||
function blank(css: string): string {
|
||||
return css.replace(/\/\*[\s\S]*?\*\/|"(?:[^"\\\n]|\\.)*"|'(?:[^'\\\n]|\\.)*'|url\([^)]*\)/g, (m) => {
|
||||
if (m.startsWith("/*")) return m.replace(/[^\n]/g, " ");
|
||||
const keep = m.startsWith("url(") ? 4 : 1;
|
||||
return m.slice(0, keep) + " ".repeat(m.length - keep - 1) + m.slice(-1);
|
||||
});
|
||||
}
|
||||
|
||||
function closing(text: string, open: number): number {
|
||||
const [opener, closer] = text[open] === "(" ? ["(", ")"] : ["{", "}"];
|
||||
let depth = 0;
|
||||
for (let i = open; i < text.length; i++) {
|
||||
if (text[i] === opener) depth++;
|
||||
if (text[i] === closer && --depth === 0) return i;
|
||||
}
|
||||
throw new Error(`unbalanced ${opener} at offset ${open}`);
|
||||
}
|
||||
|
||||
function walk(sheet: Sheet, start: number, end: number, guards: string[]): void {
|
||||
let i = start;
|
||||
while (i < end) {
|
||||
const stop = sheet.css.slice(i, end).search(/[{;]/);
|
||||
if (stop < 0) return;
|
||||
const at = i + stop;
|
||||
const raw = sheet.css.slice(i, at);
|
||||
const prelude = raw.trim();
|
||||
const offset = i + raw.length - raw.trimStart().length;
|
||||
const blockEnd = sheet.css[at] === "{" ? closing(sheet.css, at) : at;
|
||||
if (prelude.startsWith("@")) atRule(sheet, prelude, offset, guards, [at + 1, blockEnd]);
|
||||
else styleRule(sheet, prelude, offset, guards, [at + 1, blockEnd]);
|
||||
i = blockEnd + 1;
|
||||
}
|
||||
}
|
||||
|
||||
function atRule(sheet: Sheet, prelude: string, offset: number, guards: string[], [from, to]: [number, number]): void {
|
||||
const name = /^@([\w-]+)/.exec(prelude)?.[1]?.toLowerCase() ?? "";
|
||||
if (name.startsWith("-")) return;
|
||||
check(sheet, guards, { label: `@${name}`, path: `css.at-rules.${name}`, guard: never, offset });
|
||||
const condition = prelude.slice(name.length + 1);
|
||||
if (name === "media") {
|
||||
for (const m of condition.matchAll(/\(\s*(?:min-|max-)?([a-z][\w-]*)\s*[:)]/gi)) {
|
||||
check(sheet, guards, { label: `(${m[1]})`, path: `css.at-rules.media.${m[1]}`, guard: never, offset });
|
||||
}
|
||||
}
|
||||
if (name === "supports" && /(?<![\w-])selector\(/i.test(condition)) {
|
||||
check(sheet, guards, { label: "selector()", path: "css.at-rules.supports.selector", guard: never, offset });
|
||||
}
|
||||
if (name === "media" || name === "supports") {
|
||||
walk(sheet, from, to, name === "supports" ? [...guards, positive(condition)] : guards);
|
||||
}
|
||||
}
|
||||
|
||||
// The parts of an @supports condition that hold inside it: a `not (…)` group holds only outside.
|
||||
function positive(condition: string): string {
|
||||
let rest = condition;
|
||||
for (let at = rest.search(/(?<![\w-])not\s/i); at >= 0; at = rest.search(/(?<![\w-])not\s/i)) {
|
||||
const open = rest.indexOf("(", at);
|
||||
if (open < 0) return rest.slice(0, at);
|
||||
rest = rest.slice(0, at) + rest.slice(closing(rest, open) + 1);
|
||||
}
|
||||
return rest;
|
||||
}
|
||||
|
||||
function styleRule(sheet: Sheet, selector: string, offset: number, guards: string[], [from, to]: [number, number]): void {
|
||||
for (const m of selector.matchAll(/::?([a-z][\w-]*)/gi)) {
|
||||
const name = (m[1] ?? "").toLowerCase();
|
||||
check(sheet, guards, { label: `:${name}`, path: `css.selectors.${name}`, guard: new RegExp(`:${name}(?![\\w-])`, "i"), offset: offset + (m.index ?? 0) });
|
||||
}
|
||||
const body = sheet.css.slice(from, to);
|
||||
if (body.includes("{")) {
|
||||
check(sheet, guards, { label: "a nested rule", path: "css.selectors.nesting", guard: never, offset: from });
|
||||
return;
|
||||
}
|
||||
let at = from;
|
||||
for (const declaration of body.split(";")) {
|
||||
const colon = declaration.indexOf(":");
|
||||
if (colon > 0) property(sheet, guards, declaration.slice(0, colon).trim().toLowerCase(), declaration.slice(colon + 1), at + declaration.search(/\S/));
|
||||
at += declaration.length + 1;
|
||||
}
|
||||
}
|
||||
|
||||
function property(sheet: Sheet, guards: string[], name: string, value: string, offset: number): void {
|
||||
if (name.startsWith("-") && !name.startsWith("--")) return;
|
||||
const custom = name.startsWith("--");
|
||||
const known = custom || check(sheet, guards, { label: name, path: `css.properties.${name}`, guard: word(name), offset });
|
||||
for (const m of value.matchAll(/(?<![\w-])([a-z][\w-]*)\(/gi)) {
|
||||
const fn = m[1] ?? "";
|
||||
check(sheet, guards, { label: `${fn}()`, path: functionPath(name, fn), guard: new RegExp(`(?<![\\w-])${fn}\\(`, "i"), offset });
|
||||
}
|
||||
for (const m of value.replace(/#[\da-f]+/gi, "").matchAll(/(?<![\w.])\d*\.?\d+([a-z]+|%)/gi)) {
|
||||
const unit = (m[1] ?? "").toLowerCase();
|
||||
check(sheet, guards, { label: unit, path: unitPath(unit), guard: new RegExp(`\\d${unit}(?![\\w-])`, "i"), offset });
|
||||
}
|
||||
if (custom || !known || name === "font-family") return;
|
||||
for (const m of value.matchAll(/(?<![\w-])([a-z][\w-]*)(?![\w(-])/gi)) {
|
||||
const path = `css.properties.${name}.${(m[1] ?? "").toLowerCase()}`;
|
||||
if (entry(path)) check(sheet, guards, { label: `${name}: ${m[1]}`, path, guard: word(m[1] ?? ""), offset });
|
||||
}
|
||||
}
|
||||
|
||||
const never = /(?!)/;
|
||||
const word = (name: string): RegExp => new RegExp(`(?<![\\w-])${name}(?![\\w-])`, "i");
|
||||
|
||||
// A name two types share, like rect() in <shape> and <basic-shape>, passes where the floor has either.
|
||||
function functionPath(property: string, fn: string): string | undefined {
|
||||
const name = fn === "rgba" ? "rgb" : fn; // MDN files rgba() under rgb(), its alias
|
||||
const types = Object.keys(bcd.css.types ?? {}).map((type) => `css.types.${type}.${name}`);
|
||||
const paths = [`css.properties.${property}.${name}`, `css.types.${name}`, ...types].filter((path) => entry(path));
|
||||
return paths.find((path) => lacking(entry(path)).length === 0) ?? paths[0];
|
||||
}
|
||||
|
||||
const VIEWPORT: Record<string, string> = { d: "dynamic", s: "small", l: "large" };
|
||||
const UNIT_TYPES: Record<string, string> = {
|
||||
px: "css.types.length",
|
||||
"%": "css.types.percentage",
|
||||
fr: "css.types.flex",
|
||||
s: "css.types.time",
|
||||
ms: "css.types.time",
|
||||
};
|
||||
|
||||
function unitPath(unit: string): string | undefined {
|
||||
const viewport = /^([dsl])v(?:h|w|i|b|min|max)$/.exec(unit)?.[1];
|
||||
if (viewport) return `css.types.length.viewport_percentage_units_${VIEWPORT[viewport]}`;
|
||||
return UNIT_TYPES[unit] ?? ["length", "angle", "resolution"].map((type) => `css.types.${type}.${unit}`).find((path) => entry(path));
|
||||
}
|
||||
|
||||
function entry(path: string | undefined): CompatStatement | undefined {
|
||||
if (!path) return undefined;
|
||||
let node: Identifier | undefined = bcd as unknown as Identifier;
|
||||
for (const key of path.split(".")) node = node && Object.hasOwn(node, key) ? node[key] : undefined;
|
||||
return node?.__compat;
|
||||
}
|
||||
|
||||
// Reports the feature unless a guard names it; true when it is fine as written.
|
||||
function check(sheet: Sheet, guards: string[], feature: Feature): boolean {
|
||||
if (guards.some((condition) => feature.guard.test(condition))) return true;
|
||||
const line = sheet.css.slice(0, feature.offset).split("\n").length;
|
||||
const compat = entry(feature.path);
|
||||
if (!compat) {
|
||||
sheet.out.push(`line ${line}: ${feature.label} has no MDN compat entry`);
|
||||
return false;
|
||||
}
|
||||
const missing = lacking(compat);
|
||||
if (missing.length > 0) sheet.out.push(`line ${line}: ${feature.label} needs ${missing.join(", ")} (${feature.path})`);
|
||||
return missing.length === 0;
|
||||
}
|
||||
|
||||
// Each floor engine without the feature, with the release that brought it, as "safari 15.4".
|
||||
function lacking(compat: CompatStatement | undefined): string[] {
|
||||
return Object.entries(FLOOR).flatMap(([browser, floor]) => {
|
||||
const support = compat?.support[browser as keyof typeof compat.support];
|
||||
const statements = [support ?? []].flat().filter((s: SimpleSupportStatement) => !s.prefix && !s.alternative_name && !s.flags);
|
||||
const has = (s: SimpleSupportStatement): boolean => {
|
||||
const added = release(s.version_added);
|
||||
return added !== undefined && !newer(added, floor) && !(s.version_removed && !newer(s.version_removed, floor));
|
||||
};
|
||||
if (statements.some(has)) return [];
|
||||
const releases = statements.flatMap((s) => release(s.version_added) ?? []).sort((x, y) => (newer(x, y) ? 1 : -1));
|
||||
return [`${browser} ${releases[0] ?? "(none yet)"}`];
|
||||
});
|
||||
}
|
||||
|
||||
// A shipped release number; "≤79" counts as 79, "preview" and false as none.
|
||||
function release(version: string | false): string | undefined {
|
||||
return typeof version === "string" && /^≤?\d/.test(version) ? version.replace("≤", "") : undefined;
|
||||
}
|
||||
|
||||
function newer(version: string, than: string): boolean {
|
||||
const [a, b] = [version, than].map((v) => v.split(".").map(Number));
|
||||
for (let i = 0; i < Math.max(a?.length ?? 0, b?.length ?? 0); i++) {
|
||||
if ((a?.[i] ?? 0) !== (b?.[i] ?? 0)) return (a?.[i] ?? 0) > (b?.[i] ?? 0);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user