@uselazuli/cli 0.0.0-stage → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +120 -0
- package/LICENSE +21 -0
- package/README.md +202 -2
- package/agents/lazuli-design-reviewer.md +30 -0
- package/agents/lazuli-first-look.md +22 -0
- package/agents/lazuli-task-tester.md +25 -0
- package/bin/lazuli.mjs +747 -0
- package/package.json +11 -4
- package/rules/accessibility/HYPOTHESES.md +8 -0
- package/rules/accessibility/_area.yaml +7 -0
- package/rules/accessibility/operable.yaml +100 -0
- package/rules/accessibility/perceivable.yaml +153 -0
- package/rules/accessibility/robust.yaml +24 -0
- package/rules/accessibility/understandable.yaml +72 -0
- package/rules/color/HYPOTHESES.md +27 -0
- package/rules/color/_area.yaml +10 -0
- package/rules/color/adapt.yaml +183 -0
- package/rules/color/create.yaml +259 -0
- package/rules/color/review.yaml +215 -0
- package/rules/color/signal.yaml +196 -0
- package/rules/content/HYPOTHESES.md +19 -0
- package/rules/content/_area.yaml +8 -0
- package/rules/content/review.yaml +378 -0
- package/rules/content/voice.yaml +144 -0
- package/rules/content/writing.yaml +202 -0
- package/rules/data/HYPOTHESES.md +11 -0
- package/rules/data/_area.yaml +7 -0
- package/rules/data/charts.yaml +189 -0
- package/rules/data/dashboards.yaml +141 -0
- package/rules/data/review.yaml +159 -0
- package/rules/graphic/HYPOTHESES.md +18 -0
- package/rules/graphic/_area.yaml +8 -0
- package/rules/graphic/identity.yaml +141 -0
- package/rules/graphic/pieces.yaml +164 -0
- package/rules/graphic/review.yaml +271 -0
- package/rules/graphic/symbol.yaml +141 -0
- package/rules/motion/HYPOTHESES.md +19 -0
- package/rules/motion/_area.yaml +7 -0
- package/rules/motion/micro.yaml +143 -0
- package/rules/motion/review.yaml +347 -0
- package/rules/motion/transitions.yaml +169 -0
- package/rules/typography/HYPOTHESES.md +12 -0
- package/rules/typography/_area.yaml +8 -0
- package/rules/typography/adapt.yaml +45 -0
- package/rules/typography/create.yaml +126 -0
- package/rules/typography/review.yaml +127 -0
- package/rules/ui/HYPOTHESES.md +23 -0
- package/rules/ui/_area.yaml +9 -0
- package/rules/ui/compose.yaml +246 -0
- package/rules/ui/grid.yaml +75 -0
- package/rules/ui/review.yaml +400 -0
- package/rules/ui/states.yaml +257 -0
- package/rules/ux/HYPOTHESES.md +13 -0
- package/rules/ux/_area.yaml +8 -0
- package/rules/ux/flows.yaml +164 -0
- package/rules/ux/forms.yaml +142 -0
- package/rules/ux/review.yaml +186 -0
- package/skills/lazuli/SKILL.md +183 -0
- package/skills/lazuli/procedures/create.md +29 -0
- package/skills/lazuli/procedures/explain-decisions.md +22 -0
- package/skills/lazuli/procedures/improve.md +30 -0
- package/skills/lazuli/procedures/remember-this.md +41 -0
- package/skills/lazuli/procedures/review-design.md +36 -0
- package/skills/lazuli/procedures/show-options.md +33 -0
- package/skills/lazuli-accessibility-check/SKILL.md +68 -0
- package/skills/lazuli-color-context-and-culture/SKILL.md +53 -0
- package/skills/lazuli-color-context-and-culture/references/sectors.md +26 -0
- package/skills/lazuli-color-palette-by-intent/SKILL.md +54 -0
- package/skills/lazuli-color-palette-by-intent/references/intent-chords.md +51 -0
- package/skills/lazuli-color-screen-review/SKILL.md +51 -0
- package/skills/lazuli-color-states-and-signals/SKILL.md +53 -0
- package/skills/lazuli-content-interface-writing/SKILL.md +53 -0
- package/skills/lazuli-content-voice/SKILL.md +40 -0
- package/skills/lazuli-data-charts-and-dashboards/SKILL.md +59 -0
- package/skills/lazuli-graphic-derived-pieces/SKILL.md +40 -0
- package/skills/lazuli-graphic-derived-pieces/references/piece-template.html +24 -0
- package/skills/lazuli-graphic-identity/SKILL.md +45 -0
- package/skills/lazuli-motion-micro-and-transitions/SKILL.md +49 -0
- package/skills/lazuli-typography-choose-and-pair/SKILL.md +50 -0
- package/skills/lazuli-typography-screen-review/SKILL.md +51 -0
- package/skills/lazuli-ui-composition/SKILL.md +42 -0
- package/skills/lazuli-ui-composition/references/option-template.html +34 -0
- package/skills/lazuli-ui-layout-review/SKILL.md +47 -0
- package/skills/lazuli-ui-states-and-system/SKILL.md +54 -0
- package/skills/lazuli-ux-flows-and-forms/SKILL.md +51 -0
- package/skills/lazuli-ux-task-test/SKILL.md +44 -0
- package/src/browser.mjs +162 -0
- package/src/commands/capture.mjs +313 -0
- package/src/commands/catalog.mjs +37 -0
- package/src/commands/check-design.mjs +463 -0
- package/src/commands/check-goals.mjs +57 -0
- package/src/commands/compare-designs.mjs +160 -0
- package/src/commands/draft-memo.mjs +162 -0
- package/src/commands/hooks.mjs +345 -0
- package/src/commands/install-hooks.mjs +120 -0
- package/src/commands/install.mjs +132 -0
- package/src/commands/list-rules.mjs +49 -0
- package/src/commands/map-page.mjs +594 -0
- package/src/commands/record-answers.mjs +79 -0
- package/src/commands/record-log.mjs +325 -0
- package/src/commands/record-task.mjs +250 -0
- package/src/commands/render-options.mjs +227 -0
- package/src/commands/start-project.mjs +32 -0
- package/src/measure/a11y.mjs +391 -0
- package/src/measure/asset.mjs +206 -0
- package/src/measure/at-rest.mjs +49 -0
- package/src/measure/cases.mjs +566 -0
- package/src/measure/chart-read.mjs +401 -0
- package/src/measure/chart.mjs +264 -0
- package/src/measure/coherence.mjs +145 -0
- package/src/measure/color.mjs +86 -0
- package/src/measure/content.mjs +82 -0
- package/src/measure/cvd.mjs +24 -0
- package/src/measure/extreme.mjs +241 -0
- package/src/measure/forms.mjs +150 -0
- package/src/measure/in-page/a11y.js +522 -0
- package/src/measure/in-page/color.js +348 -0
- package/src/measure/in-page/composition.js +1496 -0
- package/src/measure/in-page/contrast.js +15 -0
- package/src/measure/in-page/cvd.js +173 -0
- package/src/measure/in-page/extreme.js +377 -0
- package/src/measure/in-page/frames.js +781 -0
- package/src/measure/in-page/look.js +28 -0
- package/src/measure/in-page/motion-probe.js +171 -0
- package/src/measure/in-page/style.js +107 -0
- package/src/measure/in-page/svg-strokes.js +112 -0
- package/src/measure/in-page/svg-uses.js +50 -0
- package/src/measure/in-page/symbol.js +123 -0
- package/src/measure/in-page/type.js +269 -0
- package/src/measure/in-page/weight.js +207 -0
- package/src/measure/layout.mjs +100 -0
- package/src/measure/motion.mjs +509 -0
- package/src/measure/piece.mjs +195 -0
- package/src/measure/registry.mjs +253 -0
- package/src/measure/rhythm.mjs +81 -0
- package/src/measure/signature.mjs +65 -0
- package/src/measure/states.mjs +240 -0
- package/src/measure/style.mjs +13 -0
- package/src/measure/symbol.mjs +268 -0
- package/src/measure/terms.mjs +153 -0
- package/src/measure/tiles.mjs +20 -0
- package/src/measure/timeline.mjs +621 -0
- package/src/measure/type.mjs +167 -0
- package/src/measure/weight.mjs +18 -0
- package/src/private-strings.mjs +81 -0
- package/src/project/blind.mjs +70 -0
- package/src/project/edits.mjs +154 -0
- package/src/project/gate.mjs +168 -0
- package/src/project/lazuli-md.mjs +521 -0
- package/src/project/log.mjs +335 -0
- package/src/project/match.mjs +200 -0
- package/src/project/records.mjs +380 -0
- package/src/project/tasks.mjs +153 -0
- package/src/report/evaluate.mjs +27 -0
- package/src/report/evaluators/a11y.mjs +227 -0
- package/src/report/evaluators/chart.mjs +151 -0
- package/src/report/evaluators/coherence.mjs +103 -0
- package/src/report/evaluators/color.mjs +133 -0
- package/src/report/evaluators/content.mjs +112 -0
- package/src/report/evaluators/extreme.mjs +87 -0
- package/src/report/evaluators/forms.mjs +116 -0
- package/src/report/evaluators/layout.mjs +103 -0
- package/src/report/evaluators/motion.mjs +280 -0
- package/src/report/evaluators/piece.mjs +69 -0
- package/src/report/evaluators/rhythm.mjs +99 -0
- package/src/report/evaluators/states.mjs +39 -0
- package/src/report/evaluators/style.mjs +35 -0
- package/src/report/evaluators/symbol.mjs +110 -0
- package/src/report/evaluators/terms.mjs +49 -0
- package/src/report/evaluators/type.mjs +166 -0
- package/src/report/evaluators/weight.mjs +51 -0
- package/src/report/evidence.mjs +4 -0
- package/src/report/finding.mjs +11 -0
- package/src/report/format.mjs +215 -0
- package/src/rules/hypotheses.mjs +28 -0
- package/src/rules/load.mjs +274 -0
- package/src/rules/public-text.mjs +56 -0
- package/src/rules/schema.mjs +220 -0
- package/templates/LAZULI.md +88 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Evaluated inside the page; injected as source into composition.js (its second argument) and into the
|
|
2
|
+
// states measurer. Plain script, no imports. It evaluates to the function
|
|
3
|
+
// (el, props, parts) => { "<part> <property>": value }, with part "self" always and, with parts true,
|
|
4
|
+
// "::before" and "::after" (when their content is not none) and "child<i>" for the first
|
|
5
|
+
// LOOK_DESCENDANTS descendant elements in document order. Values are computed values, trimmed.
|
|
6
|
+
// The function carries the limit as look.descendants, so the components reader notes when it cuts.
|
|
7
|
+
(() => {
|
|
8
|
+
// Sampling limit, not a rule cutoff: descendant elements read per control when parts are read.
|
|
9
|
+
const LOOK_DESCENDANTS = 20;
|
|
10
|
+
const read = (s, props, part, out) => {
|
|
11
|
+
for (const p of props) out[`${part} ${p}`] = s.getPropertyValue(p).trim();
|
|
12
|
+
};
|
|
13
|
+
const look = (el, props, parts) => {
|
|
14
|
+
const out = {};
|
|
15
|
+
read(getComputedStyle(el), props, "self", out);
|
|
16
|
+
if (!parts) return out;
|
|
17
|
+
for (const pseudo of ["::before", "::after"]) {
|
|
18
|
+
const s = getComputedStyle(el, pseudo);
|
|
19
|
+
if (s.content !== "none") read(s, props, pseudo, out);
|
|
20
|
+
}
|
|
21
|
+
const kids = el.querySelectorAll("*");
|
|
22
|
+
const n = Math.min(kids.length, LOOK_DESCENDANTS);
|
|
23
|
+
for (let i = 0; i < n; i++) read(getComputedStyle(kids[i]), props, `child${i}`, out);
|
|
24
|
+
return out;
|
|
25
|
+
};
|
|
26
|
+
look.descendants = LOOK_DESCENDANTS;
|
|
27
|
+
return look;
|
|
28
|
+
})()
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// An init script, evaluated in every frame before any page script as a function expression:
|
|
2
|
+
// (cfg) => void, installed as `(${source})(${JSON.stringify(cfg)})`. Plain script, no imports. Only the
|
|
3
|
+
// main frame's recording is read; frames of the page (iframes) record their own and are never read.
|
|
4
|
+
// cfg = { maxAnimations, moveProps }: MAX_ANIMATIONS and MOVE_PROPS of src/measure/timeline.mjs.
|
|
5
|
+
// It never acts on the page: it reads document.getAnimations() once per animation frame (and on
|
|
6
|
+
// animationstart and transitionrun, capture phase, so an animation shorter than a busy frame is still
|
|
7
|
+
// seen), and observes long frames and layout shifts. Records are plain data; targets holds the
|
|
8
|
+
// elements, read back only through the composition reader "motion".
|
|
9
|
+
(cfg) => {
|
|
10
|
+
if (window.__lazuliMotion) return;
|
|
11
|
+
const moveProps = cfg.moveProps;
|
|
12
|
+
const recorded = new WeakSet();
|
|
13
|
+
const indexOf = new Map();
|
|
14
|
+
const kebab = (key) => key.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
|
|
15
|
+
const NOT_PROPS = new Set(["offset", "computedOffset", "easing", "composite"]);
|
|
16
|
+
const state = {
|
|
17
|
+
step: "load",
|
|
18
|
+
stepEl: null,
|
|
19
|
+
targets: [],
|
|
20
|
+
animations: [],
|
|
21
|
+
cut: 0,
|
|
22
|
+
longFrames: [],
|
|
23
|
+
longFrameApi: null,
|
|
24
|
+
shifts: [],
|
|
25
|
+
timeOrigin: performance.timeOrigin,
|
|
26
|
+
};
|
|
27
|
+
// A text node counts as its parent element; a pseudo-element records its host.
|
|
28
|
+
const targetOf = (node) => {
|
|
29
|
+
const el = node && node.nodeType === 3 ? node.parentElement : node;
|
|
30
|
+
if (!el || el.nodeType !== 1) return null;
|
|
31
|
+
if (!indexOf.has(el)) {
|
|
32
|
+
indexOf.set(el, state.targets.length);
|
|
33
|
+
state.targets.push(el);
|
|
34
|
+
}
|
|
35
|
+
return indexOf.get(el);
|
|
36
|
+
};
|
|
37
|
+
const valuesAt = (frame) => {
|
|
38
|
+
const out = {};
|
|
39
|
+
if (!frame) return out;
|
|
40
|
+
for (const [key, value] of Object.entries(frame)) {
|
|
41
|
+
const prop = kebab(key);
|
|
42
|
+
if (prop === "opacity" || moveProps.includes(prop)) out[prop] = String(value);
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
};
|
|
46
|
+
const record = (a) => {
|
|
47
|
+
if (!a || recorded.has(a)) return;
|
|
48
|
+
recorded.add(a);
|
|
49
|
+
if (state.animations.length >= cfg.maxAnimations) {
|
|
50
|
+
state.cut += 1;
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const effect = a.effect;
|
|
54
|
+
const kind = typeof CSSAnimation === "function" && a instanceof CSSAnimation
|
|
55
|
+
? "css-animation"
|
|
56
|
+
: typeof CSSTransition === "function" && a instanceof CSSTransition ? "css-transition" : "web-animation";
|
|
57
|
+
const timing = effect && effect.getTiming ? effect.getTiming() : {};
|
|
58
|
+
const computed = effect && effect.getComputedTiming ? effect.getComputedTiming() : {};
|
|
59
|
+
let frames = [];
|
|
60
|
+
try {
|
|
61
|
+
frames = effect && effect.getKeyframes ? effect.getKeyframes() : [];
|
|
62
|
+
} catch {
|
|
63
|
+
frames = [];
|
|
64
|
+
}
|
|
65
|
+
const props = [];
|
|
66
|
+
for (const f of frames) {
|
|
67
|
+
for (const key of Object.keys(f)) {
|
|
68
|
+
if (NOT_PROPS.has(key)) continue;
|
|
69
|
+
const prop = kebab(key);
|
|
70
|
+
if (!props.includes(prop)) props.push(prop);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const keyframeEasings = frames.map((f) => f.easing);
|
|
74
|
+
// The easing of the last keyframe applies to no interval, so it is left out of the sameness test.
|
|
75
|
+
const used = keyframeEasings.length > 1 ? keyframeEasings.slice(0, -1) : keyframeEasings;
|
|
76
|
+
let easing = timing.easing ?? "linear";
|
|
77
|
+
if (easing === "linear" && used.length > 0 && used.every((e) => e === used[0])) easing = used[0];
|
|
78
|
+
const iterations = timing.iterations === Infinity ? "infinite" : timing.iterations ?? 1;
|
|
79
|
+
const pseudo = effect && effect.pseudoElement ? effect.pseudoElement : null;
|
|
80
|
+
// Whether the animated element is the element the current step acted on, holds it or sits inside it
|
|
81
|
+
// null when the step acted on no element (load, scroll).
|
|
82
|
+
const el = effect && effect.target ? effect.target : null;
|
|
83
|
+
const on = state.stepEl;
|
|
84
|
+
const related = on ? !!el && (el === on || on.contains(el) || el.contains(on)) : null;
|
|
85
|
+
state.animations.push({
|
|
86
|
+
step: state.step,
|
|
87
|
+
startMs: Math.round(typeof a.startTime === "number" ? a.startTime : performance.now()),
|
|
88
|
+
kind,
|
|
89
|
+
name: kind === "css-animation" ? a.animationName : kind === "css-transition" ? a.transitionProperty : a.id || "",
|
|
90
|
+
target: effect ? targetOf(effect.target) : null,
|
|
91
|
+
...(pseudo ? { pseudo } : {}),
|
|
92
|
+
delayMs: Number(timing.delay) || 0,
|
|
93
|
+
durationMs: typeof computed.duration === "number" ? computed.duration : Number(timing.duration) || 0,
|
|
94
|
+
iterations,
|
|
95
|
+
easing,
|
|
96
|
+
properties: kind === "css-transition" ? [a.transitionProperty] : props,
|
|
97
|
+
keyframeEasings,
|
|
98
|
+
from: valuesAt(frames[0]),
|
|
99
|
+
to: valuesAt(frames[frames.length - 1]),
|
|
100
|
+
related,
|
|
101
|
+
// A scroll or view timeline: the scroll position is the curve, not the timing function.
|
|
102
|
+
scrollLinked: !!a.timeline && a.timeline !== document.timeline,
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
const scan = () => {
|
|
106
|
+
for (const a of document.getAnimations()) record(a);
|
|
107
|
+
};
|
|
108
|
+
const tick = () => {
|
|
109
|
+
scan();
|
|
110
|
+
requestAnimationFrame(tick);
|
|
111
|
+
};
|
|
112
|
+
requestAnimationFrame(tick);
|
|
113
|
+
const fromEvent = (e) => {
|
|
114
|
+
const el = e.target;
|
|
115
|
+
if (el && typeof el.getAnimations === "function") {
|
|
116
|
+
for (const a of el.getAnimations({ subtree: true })) record(a);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
document.addEventListener("animationstart", fromEvent, true);
|
|
120
|
+
document.addEventListener("transitionrun", fromEvent, true);
|
|
121
|
+
|
|
122
|
+
const types = (typeof PerformanceObserver === "function" && PerformanceObserver.supportedEntryTypes) || [];
|
|
123
|
+
const api = types.includes("long-animation-frame") ? "long-animation-frame" : types.includes("longtask") ? "longtask" : null;
|
|
124
|
+
state.longFrameApi = api;
|
|
125
|
+
if (api) {
|
|
126
|
+
new PerformanceObserver((list) => {
|
|
127
|
+
for (const e of list.getEntries()) state.longFrames.push({ startMs: Math.round(e.startTime), durationMs: Math.round(e.duration) });
|
|
128
|
+
}).observe({ type: api, buffered: true });
|
|
129
|
+
}
|
|
130
|
+
if (types.includes("layout-shift")) {
|
|
131
|
+
new PerformanceObserver((list) => {
|
|
132
|
+
for (const e of list.getEntries()) {
|
|
133
|
+
const sources = [];
|
|
134
|
+
for (const s of e.sources || []) {
|
|
135
|
+
const i = targetOf(s.node);
|
|
136
|
+
if (i !== null && !sources.includes(i)) sources.push(i);
|
|
137
|
+
}
|
|
138
|
+
state.shifts.push({ startMs: Math.round(e.startTime), value: e.value, hadRecentInput: e.hadRecentInput, sources });
|
|
139
|
+
}
|
|
140
|
+
}).observe({ type: "layout-shift", buffered: true });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
window.__lazuliMotion = {
|
|
144
|
+
get step() {
|
|
145
|
+
return state.step;
|
|
146
|
+
},
|
|
147
|
+
targets: state.targets,
|
|
148
|
+
animations: state.animations,
|
|
149
|
+
longFrames: state.longFrames,
|
|
150
|
+
get longFrameApi() {
|
|
151
|
+
return state.longFrameApi;
|
|
152
|
+
},
|
|
153
|
+
shifts: state.shifts,
|
|
154
|
+
timeOrigin: state.timeOrigin,
|
|
155
|
+
get cut() {
|
|
156
|
+
return state.cut;
|
|
157
|
+
},
|
|
158
|
+
// Every later record carries this step (and, through related, whether it runs on el, the element the
|
|
159
|
+
// step acts on); returns the page time of the mark.
|
|
160
|
+
mark(label, el) {
|
|
161
|
+
scan();
|
|
162
|
+
state.step = String(label);
|
|
163
|
+
state.stepEl = el && el.nodeType === 1 ? el : null;
|
|
164
|
+
return performance.now();
|
|
165
|
+
},
|
|
166
|
+
running() {
|
|
167
|
+
scan();
|
|
168
|
+
return document.getAnimations().filter((a) => a.playState === "running").length;
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Evaluated inside the page as a function expression: (rules, maxOffenders) => { rules, notes }.
|
|
2
|
+
// Plain script, no imports. Each rule is { id, threshold: { selector, property, forbid?, min?, max? } }.
|
|
3
|
+
(ruleList, maxOffenders) => {
|
|
4
|
+
const out = {};
|
|
5
|
+
const notes = [];
|
|
6
|
+
|
|
7
|
+
const selectorOf = (el) => {
|
|
8
|
+
const tag = el.tagName.toLowerCase();
|
|
9
|
+
if (el.id) return `${tag}#${CSS.escape(el.id)}`;
|
|
10
|
+
const first = (el.getAttribute("class") || "").split(/\s+/).find(Boolean);
|
|
11
|
+
return first ? `${tag}.${CSS.escape(first)}` : tag;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// Not display:none, so every property resolves; kept out of sight and out of layout.
|
|
15
|
+
const probe = document.createElement("div");
|
|
16
|
+
probe.setAttribute("aria-hidden", "true");
|
|
17
|
+
probe.style.cssText = "position:absolute;visibility:hidden;pointer-events:none;width:0;height:0;overflow:hidden";
|
|
18
|
+
(document.body || document.documentElement).appendChild(probe);
|
|
19
|
+
|
|
20
|
+
// A forbidden value is read back through a probe element, so #000, black and rgb(0, 0, 0) are one value.
|
|
21
|
+
const normalize = (property, value) => {
|
|
22
|
+
probe.style.removeProperty(property);
|
|
23
|
+
probe.style.setProperty(property, String(value));
|
|
24
|
+
if (probe.style.getPropertyValue(property) === "") return null;
|
|
25
|
+
return getComputedStyle(probe).getPropertyValue(property).trim();
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
for (const rule of ruleList) {
|
|
30
|
+
const t = rule.threshold;
|
|
31
|
+
const bad = (what) => notes.push(`${rule.id}: ${what}; rule not checked`);
|
|
32
|
+
if (!t || typeof t !== "object") {
|
|
33
|
+
bad("the threshold is missing");
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (typeof t.selector !== "string" || t.selector.trim() === "") {
|
|
37
|
+
bad("the threshold has no selector");
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (typeof t.property !== "string" || t.property.trim() === "") {
|
|
41
|
+
bad("the threshold has no property");
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const hasForbid = Array.isArray(t.forbid) && t.forbid.length > 0;
|
|
45
|
+
const hasMin = typeof t.min === "number" && Number.isFinite(t.min);
|
|
46
|
+
const hasMax = typeof t.max === "number" && Number.isFinite(t.max);
|
|
47
|
+
if (!hasForbid && !hasMin && !hasMax) {
|
|
48
|
+
bad("the threshold needs forbid, min or max");
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (!CSS.supports(t.property, "initial")) {
|
|
52
|
+
bad(`${t.property} is not a CSS property`);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
let els;
|
|
56
|
+
try {
|
|
57
|
+
els = [...document.querySelectorAll(t.selector)];
|
|
58
|
+
} catch {
|
|
59
|
+
bad(`${t.selector} is not a valid selector`);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const forbidden = hasForbid ? new Set(t.forbid.map((v) => normalize(t.property, v) ?? String(v).trim())) : null;
|
|
63
|
+
|
|
64
|
+
let checked = 0;
|
|
65
|
+
let compared = 0;
|
|
66
|
+
let nonNumeric = 0;
|
|
67
|
+
let offending = 0;
|
|
68
|
+
const offenders = [];
|
|
69
|
+
for (const el of els) {
|
|
70
|
+
const r = el.getBoundingClientRect();
|
|
71
|
+
if (!r.width || !r.height) continue;
|
|
72
|
+
const cs = getComputedStyle(el);
|
|
73
|
+
if (cs.visibility === "hidden") continue;
|
|
74
|
+
checked += 1;
|
|
75
|
+
const value = cs.getPropertyValue(t.property).trim();
|
|
76
|
+
let hit = false;
|
|
77
|
+
let didCompare = false;
|
|
78
|
+
if (forbidden) {
|
|
79
|
+
didCompare = true;
|
|
80
|
+
if (forbidden.has(value)) hit = true;
|
|
81
|
+
}
|
|
82
|
+
if (hasMin || hasMax) {
|
|
83
|
+
const n = parseFloat(value);
|
|
84
|
+
if (Number.isFinite(n)) {
|
|
85
|
+
didCompare = true;
|
|
86
|
+
if (hasMin && n < t.min) hit = true;
|
|
87
|
+
if (hasMax && n > t.max) hit = true;
|
|
88
|
+
} else {
|
|
89
|
+
nonNumeric += 1;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (didCompare) compared += 1;
|
|
93
|
+
if (hit) {
|
|
94
|
+
offending += 1;
|
|
95
|
+
if (offenders.length < maxOffenders) offenders.push({ selector: selectorOf(el), value });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (nonNumeric > 0) {
|
|
99
|
+
notes.push(`${rule.id}: ${nonNumeric} ${nonNumeric === 1 ? "element has" : "elements have"} a non-numeric ${t.property}; not compared`);
|
|
100
|
+
}
|
|
101
|
+
out[rule.id] = { checked, compared, offending, offenders };
|
|
102
|
+
}
|
|
103
|
+
} finally {
|
|
104
|
+
probe.remove();
|
|
105
|
+
}
|
|
106
|
+
return { rules: out, notes };
|
|
107
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// Evaluated inside the page as a function expression: (svgText, ref, listed) => { parsed, strokes, liveText,
|
|
2
|
+
// external }; every stroke is returned, with a selector on the first listed ones and the thinnest.
|
|
3
|
+
// Plain script, no imports. The SVG text is parsed with DOMParser (nothing in it runs) and sanitized before it
|
|
4
|
+
// is attached: script, foreignObject and animation elements are dropped, every on* attribute is removed, and
|
|
5
|
+
// every reference that is not a #fragment (href, xlink:href, src, url(...) in an attribute or a style, and
|
|
6
|
+
// @import) is removed and counted in external. The copy is then inlined at ref px on its longer side, and each
|
|
7
|
+
// rendered stroke's width is read from geometry: strokeWidth * sqrt(|a*d - b*c|) of getScreenCTM(), or
|
|
8
|
+
// strokeWidth with fixed true under vector-effect: non-scaling-stroke.
|
|
9
|
+
(svgText, ref, listed) => {
|
|
10
|
+
const doc = new DOMParser().parseFromString(svgText, "image/svg+xml");
|
|
11
|
+
const root = doc.documentElement;
|
|
12
|
+
if (!root || root.localName !== "svg" || doc.getElementsByTagName("parsererror").length > 0) {
|
|
13
|
+
return { parsed: false, strokes: [], liveText: 0, external: 0 };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let external = 0;
|
|
17
|
+
const DROP = new Set(["script", "foreignObject", "animate", "set", "animateTransform", "animateMotion"]);
|
|
18
|
+
// Elements outside the SVG namespace (an HTML iframe, editor metadata) are dropped too.
|
|
19
|
+
for (const el of [...root.querySelectorAll("*")]) {
|
|
20
|
+
if (DROP.has(el.localName) || el.namespaceURI !== "http://www.w3.org/2000/svg") el.remove();
|
|
21
|
+
}
|
|
22
|
+
const localRef = (v) => v.trim().startsWith("#");
|
|
23
|
+
// A data: URL is inside the file (the <img> render draws it): removed from the copy, never counted.
|
|
24
|
+
const inside = (v) => /^data:/i.test(v.trim());
|
|
25
|
+
// url(...) and @import references that are not #fragments, removed from a CSS text and counted.
|
|
26
|
+
const cleanCss = (css) => {
|
|
27
|
+
let text = css.replace(/@import[^;]*;?/gi, () => {
|
|
28
|
+
external += 1;
|
|
29
|
+
return "";
|
|
30
|
+
});
|
|
31
|
+
text = text.replace(/url\(\s*(['"]?)([^'")]*)\1\s*\)/gi, (whole, q, target) => {
|
|
32
|
+
if (localRef(target)) return whole;
|
|
33
|
+
if (!inside(target)) external += 1;
|
|
34
|
+
return "none";
|
|
35
|
+
});
|
|
36
|
+
return text;
|
|
37
|
+
};
|
|
38
|
+
for (const el of [root, ...root.querySelectorAll("*")]) {
|
|
39
|
+
if (el.localName === "style") el.textContent = cleanCss(el.textContent);
|
|
40
|
+
for (const attr of [...el.attributes]) {
|
|
41
|
+
const name = attr.name.toLowerCase();
|
|
42
|
+
const local = attr.localName.toLowerCase();
|
|
43
|
+
if (name.startsWith("on")) {
|
|
44
|
+
el.removeAttributeNode(attr);
|
|
45
|
+
} else if (local === "href" || local === "src") {
|
|
46
|
+
if (!localRef(attr.value)) {
|
|
47
|
+
if (attr.value.trim() !== "" && !inside(attr.value)) external += 1;
|
|
48
|
+
el.removeAttributeNode(attr);
|
|
49
|
+
}
|
|
50
|
+
} else if (name === "style" || /url\(/i.test(attr.value)) {
|
|
51
|
+
const cleaned = cleanCss(attr.value);
|
|
52
|
+
if (cleaned !== attr.value) attr.value = cleaned;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const liveText = [...root.querySelectorAll("text")].filter((t) => !t.parentElement.closest("text")).length;
|
|
58
|
+
|
|
59
|
+
// Inline the sanitized copy at ref px on its longer side, top left of the page.
|
|
60
|
+
const svg = document.importNode(root, true);
|
|
61
|
+
if (!svg.getAttribute("viewBox")) {
|
|
62
|
+
const w = parseFloat(svg.getAttribute("width"));
|
|
63
|
+
const h = parseFloat(svg.getAttribute("height"));
|
|
64
|
+
if (w > 0 && h > 0) svg.setAttribute("viewBox", `0 0 ${w} ${h}`);
|
|
65
|
+
}
|
|
66
|
+
svg.setAttribute("width", String(ref));
|
|
67
|
+
svg.setAttribute("height", String(ref));
|
|
68
|
+
const box = document.createElement("div");
|
|
69
|
+
box.style.cssText = `position:absolute;left:0;top:0;width:${ref}px;height:${ref}px;overflow:hidden`;
|
|
70
|
+
box.appendChild(svg);
|
|
71
|
+
document.body.appendChild(box);
|
|
72
|
+
|
|
73
|
+
const HIDDEN = "defs, clipPath, mask, symbol, marker, pattern";
|
|
74
|
+
const SHAPES = "path, rect, circle, ellipse, line, polyline, polygon, text";
|
|
75
|
+
const selectorOf = (el) => {
|
|
76
|
+
const steps = [];
|
|
77
|
+
for (let e = el; e && e !== svg; e = e.parentElement) {
|
|
78
|
+
const same = [...e.parentElement.children].filter((c) => c.localName === e.localName);
|
|
79
|
+
steps.unshift(e.id ? `${e.localName}#${e.id}` : `${e.localName}:nth-of-type(${same.indexOf(e) + 1})`);
|
|
80
|
+
}
|
|
81
|
+
return ["svg", ...steps].join(" > ");
|
|
82
|
+
};
|
|
83
|
+
const strokes = [];
|
|
84
|
+
const els = [];
|
|
85
|
+
for (const el of svg.querySelectorAll(SHAPES)) {
|
|
86
|
+
if (el.closest(HIDDEN)) continue;
|
|
87
|
+
const s = getComputedStyle(el);
|
|
88
|
+
if (s.display === "none" || s.visibility === "hidden") continue;
|
|
89
|
+
if (!s.stroke || s.stroke === "none") continue;
|
|
90
|
+
const width = parseFloat(s.strokeWidth);
|
|
91
|
+
if (!(width > 0) || !(parseFloat(s.strokeOpacity) > 0)) continue;
|
|
92
|
+
const fixed = s.vectorEffect === "non-scaling-stroke";
|
|
93
|
+
const m = el.getScreenCTM();
|
|
94
|
+
const scale = m ? Math.sqrt(Math.abs(m.a * m.d - m.b * m.c)) : 1;
|
|
95
|
+
strokes.push({ selector: null, refPx: +(fixed ? width : width * scale).toFixed(3), fixed });
|
|
96
|
+
els.push(el);
|
|
97
|
+
}
|
|
98
|
+
// A selector costs a walk over siblings, so it is built only for the strokes the report shows: the first
|
|
99
|
+
// listed ones and, per kind (fixed or scaling), the first of the thinnest, which is the thinnest at any size.
|
|
100
|
+
const first = (fixed) => {
|
|
101
|
+
let best = -1;
|
|
102
|
+
strokes.forEach((s, i) => {
|
|
103
|
+
if (s.fixed === fixed && (best < 0 || s.refPx < strokes[best].refPx)) best = i;
|
|
104
|
+
});
|
|
105
|
+
return best;
|
|
106
|
+
};
|
|
107
|
+
const shown = new Set([first(true), first(false)]);
|
|
108
|
+
for (let i = 0; i < Math.min(listed, strokes.length); i++) shown.add(i);
|
|
109
|
+
for (const i of shown) if (i >= 0) strokes[i].selector = selectorOf(els[i]);
|
|
110
|
+
box.remove();
|
|
111
|
+
return { parsed: true, strokes, liveText, external };
|
|
112
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Evaluated inside the page as a function expression: (svgText, cap) => { parsed, copies }.
|
|
2
|
+
// Plain script, no imports. The SVG text is parsed with DOMParser (nothing in it runs or is drawn) and the
|
|
3
|
+
// elements its <use> references add once drawn are counted: each use adds a copy of the element it points to
|
|
4
|
+
// (#fragment only), with that element's own use copies. Each element is counted once (memoized), a reference
|
|
5
|
+
// cycle adds nothing, and the counts stop at cap + 1, so the count itself stays linear in the file.
|
|
6
|
+
(svgText, cap) => {
|
|
7
|
+
const doc = new DOMParser().parseFromString(svgText, "image/svg+xml");
|
|
8
|
+
const root = doc.documentElement;
|
|
9
|
+
if (!root || root.localName !== "svg" || doc.getElementsByTagName("parsererror").length > 0) {
|
|
10
|
+
return { parsed: false, copies: 0 };
|
|
11
|
+
}
|
|
12
|
+
const byId = new Map();
|
|
13
|
+
for (const el of root.querySelectorAll("[id]")) if (!byId.has(el.id)) byId.set(el.id, el);
|
|
14
|
+
const target = (use) => {
|
|
15
|
+
const ref = (use.getAttribute("href") ?? use.getAttributeNS("http://www.w3.org/1999/xlink", "href") ?? "").trim();
|
|
16
|
+
return ref.startsWith("#") ? byId.get(ref.slice(1)) ?? null : null;
|
|
17
|
+
};
|
|
18
|
+
const limit = cap + 1;
|
|
19
|
+
const add = (a, b) => Math.min(limit, a + b);
|
|
20
|
+
// added(el): elements the use references inside el add; drawn(el): el, its descendants and those copies.
|
|
21
|
+
const addedOf = new Map();
|
|
22
|
+
const drawnOf = new Map();
|
|
23
|
+
const busy = new Set();
|
|
24
|
+
const walk = (el) => {
|
|
25
|
+
if (drawnOf.has(el) || busy.has(el)) return;
|
|
26
|
+
busy.add(el);
|
|
27
|
+
let added = 0;
|
|
28
|
+
let drawn = 1;
|
|
29
|
+
for (const c of el.children) {
|
|
30
|
+
walk(c);
|
|
31
|
+
added = add(added, addedOf.get(c) ?? 0);
|
|
32
|
+
drawn = add(drawn, drawnOf.get(c) ?? 0);
|
|
33
|
+
}
|
|
34
|
+
if (el.localName === "use") {
|
|
35
|
+
const t = target(el);
|
|
36
|
+
if (t) {
|
|
37
|
+
walk(t);
|
|
38
|
+
// A target still being walked is an ancestor: a cycle, which draws nothing.
|
|
39
|
+
const copy = drawnOf.get(t) ?? 0;
|
|
40
|
+
added = add(added, copy);
|
|
41
|
+
drawn = add(drawn, copy);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
busy.delete(el);
|
|
45
|
+
addedOf.set(el, added);
|
|
46
|
+
drawnOf.set(el, drawn);
|
|
47
|
+
};
|
|
48
|
+
walk(root);
|
|
49
|
+
return { parsed: true, copies: addedOf.get(root) };
|
|
50
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// Evaluated inside the page as a function expression: (input, contrast) => result, where contrast is the value
|
|
2
|
+
// of in-page/contrast.js ({ lin, lum, ratio }). Plain script, no imports, nothing fetched.
|
|
3
|
+
// input = { tiles: { size, file, url }[] (PNG data URLs, each size by size px, transparent around the symbol),
|
|
4
|
+
// ref (the largest size of the main file), main (its file label), fileRefs { [file]: largest size },
|
|
5
|
+
// backgrounds { name, hex }[], maxParts }.
|
|
6
|
+
// Returns { silhouette: { size, overlap }[] | null, empty, opaque, parts, colors, contrast }.
|
|
7
|
+
async (input, contrast) => {
|
|
8
|
+
// Definition: a pixel belongs to the symbol when its alpha is at least half of 255.
|
|
9
|
+
const SYMBOL_ALPHA = 128;
|
|
10
|
+
const { ratio } = contrast;
|
|
11
|
+
|
|
12
|
+
const decode = async (url) => {
|
|
13
|
+
const img = new Image();
|
|
14
|
+
img.src = url;
|
|
15
|
+
await img.decode();
|
|
16
|
+
const canvas = document.createElement("canvas");
|
|
17
|
+
canvas.width = img.naturalWidth;
|
|
18
|
+
canvas.height = img.naturalHeight;
|
|
19
|
+
const g = canvas.getContext("2d", { willReadFrequently: true });
|
|
20
|
+
g.drawImage(img, 0, 0);
|
|
21
|
+
return { w: canvas.width, h: canvas.height, px: g.getImageData(0, 0, canvas.width, canvas.height).data };
|
|
22
|
+
};
|
|
23
|
+
const tiles = [];
|
|
24
|
+
for (const t of input.tiles) tiles.push({ ...t, ...(await decode(t.url)) });
|
|
25
|
+
const at = (file, size) => tiles.find((t) => t.file === file && t.size === size);
|
|
26
|
+
const isSymbol = (t, x, y) => t.px[(y * t.w + x) * 4 + 3] >= SYMBOL_ALPHA;
|
|
27
|
+
const hex = (r, g, b) => "#" + [r, g, b].map((v) => Math.round(v).toString(16).padStart(2, "0")).join("");
|
|
28
|
+
const keyOf = (px, i) => ((px[i] >> 4) << 8) | ((px[i + 1] >> 4) << 4) | (px[i + 2] >> 4);
|
|
29
|
+
|
|
30
|
+
// ---- silhouette: overlap of the symbol pixels at ref and at each size mapped onto the ref grid ----
|
|
31
|
+
const big = at(input.main, input.ref);
|
|
32
|
+
let opaque = true;
|
|
33
|
+
let inA = 0;
|
|
34
|
+
for (let i = 3; i < big.px.length; i += 4) {
|
|
35
|
+
if (big.px[i] < SYMBOL_ALPHA) opaque = false;
|
|
36
|
+
else inA += 1;
|
|
37
|
+
}
|
|
38
|
+
const empty = inA === 0;
|
|
39
|
+
let silhouette = null;
|
|
40
|
+
if (!opaque && !empty) {
|
|
41
|
+
silhouette = [];
|
|
42
|
+
const R = input.ref;
|
|
43
|
+
for (const t of tiles) {
|
|
44
|
+
let both = 0;
|
|
45
|
+
let either = 0;
|
|
46
|
+
for (let y = 0; y < R; y++) {
|
|
47
|
+
const ys = Math.floor((y * t.size) / R);
|
|
48
|
+
for (let x = 0; x < R; x++) {
|
|
49
|
+
const a = isSymbol(big, x, y);
|
|
50
|
+
const b = isSymbol(t, Math.floor((x * t.size) / R), ys);
|
|
51
|
+
if (a && b) both += 1;
|
|
52
|
+
if (a || b) either += 1;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
silhouette.push({ size: t.size, overlap: +(both / either).toFixed(3) });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ---- parts: color buckets of each file's own ref render, kept at a size when one symbol pixel falls in it ----
|
|
60
|
+
const parts = [];
|
|
61
|
+
const colors = {};
|
|
62
|
+
for (const [file, fref] of Object.entries(input.fileRefs)) {
|
|
63
|
+
const t = at(file, fref);
|
|
64
|
+
const buckets = new Map();
|
|
65
|
+
let total = 0;
|
|
66
|
+
for (let i = 0; i < t.px.length; i += 4) {
|
|
67
|
+
if (t.px[i + 3] < SYMBOL_ALPHA) continue;
|
|
68
|
+
const key = keyOf(t.px, i);
|
|
69
|
+
let b = buckets.get(key);
|
|
70
|
+
if (!b) buckets.set(key, (b = { key, n: 0, r: 0, g: 0, b: 0 }));
|
|
71
|
+
b.n += 1;
|
|
72
|
+
b.r += t.px[i];
|
|
73
|
+
b.g += t.px[i + 1];
|
|
74
|
+
b.b += t.px[i + 2];
|
|
75
|
+
total += 1;
|
|
76
|
+
}
|
|
77
|
+
colors[file] = buckets.size;
|
|
78
|
+
const kept = [...buckets.values()].sort((p, q) => q.n - p.n || p.key - q.key).slice(0, input.maxParts);
|
|
79
|
+
const own = tiles.filter((x) => x.file === file);
|
|
80
|
+
const present = own.map((x) => {
|
|
81
|
+
const keys = new Set();
|
|
82
|
+
for (let i = 0; i < x.px.length; i += 4) if (x.px[i + 3] >= SYMBOL_ALPHA) keys.add(keyOf(x.px, i));
|
|
83
|
+
return { size: x.size, keys };
|
|
84
|
+
});
|
|
85
|
+
for (const b of kept) {
|
|
86
|
+
parts.push({
|
|
87
|
+
file,
|
|
88
|
+
hex: hex(b.r / b.n, b.g / b.n, b.b / b.n),
|
|
89
|
+
share: +(b.n / total).toFixed(3),
|
|
90
|
+
keptAt: present.filter((p) => p.keys.has(b.key)).map((p) => p.size),
|
|
91
|
+
lostAt: present.filter((p) => !p.keys.has(b.key)).map((p) => p.size),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ---- edge contrast: each edge pixel composited over the background, its ratio to the background ----
|
|
97
|
+
const rgb = (h) => ({ r: parseInt(h.slice(1, 3), 16), g: parseInt(h.slice(3, 5), 16), b: parseInt(h.slice(5, 7), 16) });
|
|
98
|
+
const out = [];
|
|
99
|
+
for (const t of tiles) {
|
|
100
|
+
const edge = [];
|
|
101
|
+
for (let y = 0; y < t.h; y++) {
|
|
102
|
+
for (let x = 0; x < t.w; x++) {
|
|
103
|
+
if (!isSymbol(t, x, y)) continue;
|
|
104
|
+
const outside = (nx, ny) => nx < 0 || ny < 0 || nx >= t.w || ny >= t.h || !isSymbol(t, nx, ny);
|
|
105
|
+
if (outside(x - 1, y) || outside(x + 1, y) || outside(x, y - 1) || outside(x, y + 1)) edge.push((y * t.w + x) * 4);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (const bg of input.backgrounds) {
|
|
109
|
+
const b = rgb(bg.hex);
|
|
110
|
+
const ratios = edge
|
|
111
|
+
.map((i) => {
|
|
112
|
+
const a = t.px[i + 3] / 255;
|
|
113
|
+
const c = { r: a * t.px[i] + (1 - a) * b.r, g: a * t.px[i + 1] + (1 - a) * b.g, b: a * t.px[i + 2] + (1 - a) * b.b };
|
|
114
|
+
return ratio(c, b);
|
|
115
|
+
})
|
|
116
|
+
.sort((p, q) => p - q);
|
|
117
|
+
const n = ratios.length;
|
|
118
|
+
const median = n === 0 ? null : +(n % 2 ? ratios[(n - 1) / 2] : (ratios[n / 2 - 1] + ratios[n / 2]) / 2).toFixed(2);
|
|
119
|
+
out.push({ size: t.size, background: bg.name, hex: bg.hex, median, edgePixels: n });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return { silhouette, empty, opaque, parts, colors, contrast: out };
|
|
123
|
+
}
|