pi-pocket 0.26.0 → 0.27.0-beta.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/CONTRIBUTING.md +1 -1
- package/docs/design/design-system.md +165 -0
- package/docs/design/ds-notes-b.md +61 -0
- package/docs/design/ds-notes-g.md +170 -0
- package/docs/design/ds-notes-t.md +303 -0
- package/docs/design/ds-notes-u.md +220 -0
- package/docs/design/ds-notes-v.md +186 -0
- package/docs/design/ds-rounds.md +160 -0
- package/docs/design/ds-u-bridge.patch +732 -0
- package/docs/examples/design-system/README.md +15 -0
- package/docs/examples/design-system/pocket/.pi/design/components/badge.html +112 -0
- package/docs/examples/design-system/pocket/.pi/design/components/button.html +203 -0
- package/docs/examples/design-system/pocket/.pi/design/components/card.html +125 -0
- package/docs/examples/design-system/pocket/.pi/design/components/code-block.html +155 -0
- package/docs/examples/design-system/pocket/.pi/design/components/input.html +156 -0
- package/docs/examples/design-system/pocket/.pi/design/components/menu-row.html +167 -0
- package/docs/examples/design-system/pocket/.pi/design/components/notice.html +171 -0
- package/docs/examples/design-system/pocket/.pi/design/components/segmented.html +120 -0
- package/docs/examples/design-system/pocket/.pi/design/components/switch.html +117 -0
- package/docs/examples/design-system/pocket/.pi/design/components/tool-step.html +163 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/components.css +730 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/names.md +495 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tailwind.css +1132 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.css +1126 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.dark.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.high-contrast.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.light.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.resolved.json +371 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.studio.json +5915 -0
- package/docs/examples/design-system/pocket/.pi/design/guidelines.md +81 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.dark.json +182 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.high-contrast.json +185 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.json +2034 -0
- package/docs/features.md +6 -2
- package/docs/index.md +1 -0
- package/docs/map.md +34 -25
- package/docs/release-notes.md +1 -1
- package/docs/releases.md +45 -0
- package/package.json +2 -1
- package/src/server/design/check-cli.ts +283 -0
- package/src/server/design/color.ts +654 -0
- package/src/server/design/components.ts +328 -0
- package/src/server/design/docsite-assets.ts +29 -0
- package/src/server/design/docsite-color-section.ts +366 -0
- package/src/server/design/docsite-color.ts +303 -0
- package/src/server/design/docsite-components.ts +89 -0
- package/src/server/design/docsite-core.ts +634 -0
- package/src/server/design/docsite-css-base.ts +397 -0
- package/src/server/design/docsite-css-content.ts +477 -0
- package/src/server/design/docsite-css-phone.ts +251 -0
- package/src/server/design/docsite-css-reference.ts +374 -0
- package/src/server/design/docsite-css-specimens.ts +465 -0
- package/src/server/design/docsite-js-code.ts +96 -0
- package/src/server/design/docsite-js-core.ts +72 -0
- package/src/server/design/docsite-js-demos.ts +24 -0
- package/src/server/design/docsite-js-links.ts +25 -0
- package/src/server/design/docsite-js-nav.ts +73 -0
- package/src/server/design/docsite-js-stages.ts +81 -0
- package/src/server/design/docsite-js-table.ts +81 -0
- package/src/server/design/docsite-markdown.ts +221 -0
- package/src/server/design/docsite-motion.ts +110 -0
- package/src/server/design/docsite-reference.ts +269 -0
- package/src/server/design/docsite-sanitize.ts +193 -0
- package/src/server/design/docsite-shadows.ts +45 -0
- package/src/server/design/docsite-space.ts +289 -0
- package/src/server/design/docsite-type.ts +167 -0
- package/src/server/design/docsite.ts +508 -0
- package/src/server/design/export.ts +86 -9
- package/src/server/design/fix.ts +666 -0
- package/src/server/design/guide.ts +137 -22
- package/src/server/design/index.ts +368 -15
- package/src/server/design/lint-color.ts +293 -0
- package/src/server/design/lint-page-checks.ts +204 -0
- package/src/server/design/lint-page-elements.ts +252 -0
- package/src/server/design/lint-page-focus.ts +165 -0
- package/src/server/design/lint-page-literals.ts +293 -0
- package/src/server/design/lint-page-setup.ts +102 -0
- package/src/server/design/lint-page-states.ts +121 -0
- package/src/server/design/lint-page-values.ts +329 -0
- package/src/server/design/lint-page.ts +101 -0
- package/src/server/design/lint-suggest.ts +340 -0
- package/src/server/design/lint.ts +1136 -0
- package/src/server/design/render.ts +369 -18
- package/src/server/design/scan-braces.ts +115 -0
- package/src/server/design/scan-categorize.ts +187 -0
- package/src/server/design/scan-channels.ts +113 -0
- package/src/server/design/scan-color-base.ts +209 -0
- package/src/server/design/scan-color-names.ts +403 -0
- package/src/server/design/scan-color-palette.ts +91 -0
- package/src/server/design/scan-color-roles.ts +565 -0
- package/src/server/design/scan-colors.ts +22 -0
- package/src/server/design/scan-facts-antd.ts +171 -0
- package/src/server/design/scan-facts-files.ts +300 -0
- package/src/server/design/scan-facts-lists.ts +538 -0
- package/src/server/design/scan-facts-sass.ts +113 -0
- package/src/server/design/scan-facts-theme.ts +400 -0
- package/src/server/design/scan-facts-use.ts +674 -0
- package/src/server/design/scan-facts-wordpress.ts +374 -0
- package/src/server/design/scan-facts.ts +285 -0
- package/src/server/design/scan-files.ts +384 -0
- package/src/server/design/scan-import-convert.ts +277 -0
- package/src/server/design/scan-import-eval.ts +583 -0
- package/src/server/design/scan-import-material.ts +745 -0
- package/src/server/design/scan-import-sd-config.ts +370 -0
- package/src/server/design/scan-import-sd.ts +381 -0
- package/src/server/design/scan-import-studio.ts +242 -0
- package/src/server/design/scan-import.ts +336 -0
- package/src/server/design/scan-infer.ts +313 -0
- package/src/server/design/scan-js-eval.ts +1267 -0
- package/src/server/design/scan-palette.ts +485 -0
- package/src/server/design/scan-parse.ts +1134 -0
- package/src/server/design/scan-prepare.ts +402 -0
- package/src/server/design/scan-render-pages.ts +367 -0
- package/src/server/design/scan-render-probe.ts +229 -0
- package/src/server/design/scan-render.ts +524 -0
- package/src/server/design/scan-review.ts +54 -0
- package/src/server/design/scan-roles.ts +259 -0
- package/src/server/design/scan-sass-colors.ts +275 -0
- package/src/server/design/scan-sass-eval.ts +269 -0
- package/src/server/design/scan-sass-expr.ts +911 -0
- package/src/server/design/scan-sass-modules.ts +353 -0
- package/src/server/design/scan-sass-run.ts +1003 -0
- package/src/server/design/scan-sass-stmt.ts +256 -0
- package/src/server/design/scan-sass-values.ts +886 -0
- package/src/server/design/scan-scales.ts +983 -0
- package/src/server/design/scan-tailwind.ts +34 -0
- package/src/server/design/scan-theme-shapes.ts +579 -0
- package/src/server/design/scan-tree.ts +290 -0
- package/src/server/design/scan-util.ts +44 -0
- package/src/server/design/scan-variables.ts +470 -0
- package/src/server/design/scan.ts +703 -0
- package/src/server/design/system-schema.ts +68 -0
- package/src/server/design/system-types.ts +279 -0
- package/src/server/design/system.ts +510 -82
- package/src/server/design/tokens-base.ts +123 -0
- package/src/server/design/tokens-compile.ts +440 -0
- package/src/server/design/tokens-dtcg.ts +354 -0
- package/src/server/design/tokens-io.ts +417 -0
- package/src/server/design/tokens-model.ts +1163 -0
- package/src/server/design/tokens-names.ts +112 -0
- package/src/server/design/tokens-ramps.ts +112 -0
- package/src/server/design/tokens-summary.ts +317 -0
- package/src/server/design/tokens-tailwind.ts +324 -0
- package/src/server/design/tokens-values.ts +921 -0
- package/src/server/design/tokens.ts +533 -0
- package/src/server/docs.ts +8 -0
- package/src/server/extensions/artifacts.ts +87 -2
- package/src/server/extensions/design/app.js +280 -0
- package/src/server/extensions/design/canvas.js +142 -17
- package/src/server/extensions/design/check.js +656 -0
- package/src/server/extensions/design/color.js +383 -0
- package/src/server/extensions/design/data.js +102 -7
- package/src/server/extensions/design/devices.js +26 -0
- package/src/server/extensions/design/docks.js +117 -3
- package/src/server/extensions/design/index.js +171 -219
- package/src/server/extensions/design/messages.js +49 -0
- package/src/server/extensions/design/parts.js +15 -1
- package/src/server/extensions/design/paths.js +4 -0
- package/src/server/extensions/design/picker.js +123 -0
- package/src/server/extensions/design/specimen.js +55 -0
- package/src/server/extensions/design/state.js +72 -7
- package/src/server/extensions/design/style.css +546 -0
- package/src/server/extensions/design/system-actions.js +214 -0
- package/src/server/extensions/design/system-api.ts +62 -0
- package/src/server/extensions/design/system-data.js +315 -0
- package/src/server/extensions/design/system-preview.js +41 -0
- package/src/server/extensions/design/system-routes.ts +1122 -0
- package/src/server/extensions/design/system-sections.js +1344 -0
- package/src/server/extensions/design/system-ui.js +32 -0
- package/src/server/extensions/design/system-view.js +1196 -0
- package/src/server/extensions/design/system-widgets.js +942 -0
- package/src/server/extensions/design/system.css +2313 -0
- package/src/server/extensions/design/system.js +1479 -0
- package/src/server/extensions/design/unsaved.js +20 -0
- package/src/server/extensions/design/views.js +2 -2
- package/src/server/extensions/design/workspace.js +200 -105
- package/src/server/extensions/design.ts +89 -6
- package/src/server/extensions/updates/index.js +31 -9
- package/src/server/extensions/updates/style.css +11 -0
- package/src/server/extensions/updates.ts +29 -1
- package/src/server/host.ts +4 -1
- package/src/server/http/artifact-inspect.ts +338 -24
- package/src/server/updates.ts +235 -53
- package/src/server/versions.ts +71 -0
- package/web/artifact-bridge.js +34 -1
- package/npm-shrinkwrap.json +0 -3162
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A part of the script that `lint.ts` runs inside a rendered design (see `lint-page.ts`): values that the page wrote as literals in its own CSS (with a system). The parts are one
|
|
3
|
+
* function body, in this order: setup, values, literals, states, elements, checks, focus. Plain JavaScript in a string.
|
|
4
|
+
*/
|
|
5
|
+
export const PAGE_LITERALS = String.raw`// ---------------------------------------------------------------------------------------------------------------------
|
|
6
|
+
// Authored literals. With a system, a color, length, or font written as a literal in the page's own CSS is hard-coded
|
|
7
|
+
// even when its value equals a token: it does not follow the token. The page's rules say which values were authored (a
|
|
8
|
+
// computed value cannot): the colors, lengths, and font families that they set without var().
|
|
9
|
+
|
|
10
|
+
const literalRules = [];
|
|
11
|
+
const KEEP_WORDS = new Set(['transparent', 'currentcolor', 'inherit', 'initial', 'unset', 'revert', 'revert-layer', 'none', 'auto', 'canvas', 'canvastext', 'buttonface', 'buttontext', 'buttonborder', 'field', 'fieldtext', 'highlight', 'highlighttext', 'linktext', 'visitedtext', 'activetext', 'graytext', 'mark', 'marktext', 'accentcolor', 'accentcolortext', 'selecteditem', 'selecteditemtext']);
|
|
12
|
+
const stripVars = (text) => {
|
|
13
|
+
let out = '';
|
|
14
|
+
|
|
15
|
+
for (let i = 0; i < text.length; ) {
|
|
16
|
+
if (text.startsWith('var(', i)) {
|
|
17
|
+
let depth = 0;
|
|
18
|
+
let j = i + 3;
|
|
19
|
+
|
|
20
|
+
for (; j < text.length; j++) {
|
|
21
|
+
if (text[j] === '(') depth++;
|
|
22
|
+
else if (text[j] === ')' && --depth === 0) break;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
out += ' ';
|
|
26
|
+
i = j + 1;
|
|
27
|
+
} else {
|
|
28
|
+
out += text[i++];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return out;
|
|
33
|
+
};
|
|
34
|
+
const colorWords = (text) => {
|
|
35
|
+
const found = [];
|
|
36
|
+
const pattern = /#[0-9a-f]{3,8}\b|\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\([^()]*\)|[a-z][a-z-]*/gi;
|
|
37
|
+
|
|
38
|
+
for (const match of stripVars(text).matchAll(pattern)) {
|
|
39
|
+
const word = match[0];
|
|
40
|
+
|
|
41
|
+
if (/^[a-z]/i.test(word) && !word.includes('(') && (KEEP_WORDS.has(word.toLowerCase()) || !CSS.supports('color', word))) continue;
|
|
42
|
+
|
|
43
|
+
found.push(word);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return found;
|
|
47
|
+
};
|
|
48
|
+
const lengthWords = (text) => [...stripVars(text).matchAll(/(-?\d*\.?\d+)(px|rem|em|pt)\b/gi)].map((m) => ({ value: Number(m[1]), unit: m[2].toLowerCase() }));
|
|
49
|
+
const splitDecls = (cssText) => {
|
|
50
|
+
const out = [];
|
|
51
|
+
let depth = 0;
|
|
52
|
+
let quote = '';
|
|
53
|
+
let start = 0;
|
|
54
|
+
|
|
55
|
+
for (let i = 0; i <= cssText.length; i++) {
|
|
56
|
+
const ch = cssText[i];
|
|
57
|
+
|
|
58
|
+
if (quote) {
|
|
59
|
+
if (ch === quote) quote = '';
|
|
60
|
+
} else if (ch === '"' || ch === "'") quote = ch;
|
|
61
|
+
else if (ch === '(') depth++;
|
|
62
|
+
else if (ch === ')') depth--;
|
|
63
|
+
else if ((ch === ';' && depth === 0) || i === cssText.length) {
|
|
64
|
+
const text = cssText.slice(start, i);
|
|
65
|
+
const colon = text.indexOf(':');
|
|
66
|
+
|
|
67
|
+
if (colon > 0) out.push([text.slice(0, colon).trim().toLowerCase(), text.slice(colon + 1).replace(/!important\s*$/i, '').trim()]);
|
|
68
|
+
|
|
69
|
+
start = i + 1;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return out;
|
|
74
|
+
};
|
|
75
|
+
const partsOf = (cssText) => {
|
|
76
|
+
const parts = { text: [], background: [], border: [], outline: [], 'font-size': [], space: [], radius: [], family: [] };
|
|
77
|
+
let any = false;
|
|
78
|
+
const put = (kind, list) => {
|
|
79
|
+
if (list.length > 0) {
|
|
80
|
+
parts[kind].push(...list);
|
|
81
|
+
any = true;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
for (const [prop, value] of splitDecls(cssText)) {
|
|
86
|
+
if (prop.startsWith('--')) continue;
|
|
87
|
+
|
|
88
|
+
if (prop === 'color' || prop === '-webkit-text-fill-color') put('text', colorWords(value));
|
|
89
|
+
else if (prop === 'background' || prop === 'background-color') put('background', colorWords(value));
|
|
90
|
+
else if (/^border(-(top|right|bottom|left|block|inline)(-(start|end))?)?(-color)?$/.test(prop)) put('border', colorWords(value));
|
|
91
|
+
else if (prop === 'outline' || prop === 'outline-color') put('outline', colorWords(value));
|
|
92
|
+
else if (prop === 'font-size') put('font-size', lengthWords(value));
|
|
93
|
+
else if (prop === 'font') put('font-size', lengthWords(value).slice(0, 1));
|
|
94
|
+
else if (/^(padding|margin)(-(top|right|bottom|left|block|inline)(-(start|end))?)?$/.test(prop) || /^(grid-)?(row-|column-)?gap$/.test(prop)) put('space', lengthWords(value));
|
|
95
|
+
else if (/^border(-(top|bottom)-(left|right))?-radius$/.test(prop)) put('radius', lengthWords(value));
|
|
96
|
+
else if (prop === 'font-family' && !value.includes('var(')) put('family', [value]);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return any ? parts : null;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// Every style rule of the page (not only those with literals): its selector, what it holds, and which properties it
|
|
103
|
+
// sets. With them, "did the author set this property on this element" has an answer: a rule that matches the element
|
|
104
|
+
// sets it, or its style attribute does. The rules are indexed by the id, a class, or the tag of the last compound of
|
|
105
|
+
// each selector, so that an element is tried only against the rules that could match it.
|
|
106
|
+
const allRules = [];
|
|
107
|
+
const byId = new Map();
|
|
108
|
+
const byClass = new Map();
|
|
109
|
+
const byTag = new Map();
|
|
110
|
+
const general = [];
|
|
111
|
+
let unreadableSheet = false;
|
|
112
|
+
let capHit = false;
|
|
113
|
+
const declaredNames = (cssText) => new Set(splitDecls(cssText).map(([prop]) => prop).filter((prop) => !prop.startsWith('--')));
|
|
114
|
+
const indexRule = (rule) => {
|
|
115
|
+
const keys = [];
|
|
116
|
+
|
|
117
|
+
for (const part of rule.selector.split(/,(?![^(\[]*[)\]])/)) {
|
|
118
|
+
const last = part.trim().split(/\s*[>+~]\s*|\s+(?![^(\[]*[)\]])/).pop() || '';
|
|
119
|
+
const id = /#([\w-]+)/.exec(last);
|
|
120
|
+
const klass = /\.([\w-]+)/.exec(last);
|
|
121
|
+
const tag = /^([a-z][\w-]*)/i.exec(last);
|
|
122
|
+
|
|
123
|
+
keys.push(part.includes('&') || /^[:*\[]/.test(last) && !id && !klass ? ['*'] : id ? ['#', id[1]] : klass ? ['.', klass[1]] : tag ? ['t', tag[1].toLowerCase()] : ['*']);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
for (const [kind, name] of keys) {
|
|
127
|
+
const map = kind === '#' ? byId : kind === '.' ? byClass : kind === 't' ? byTag : null;
|
|
128
|
+
|
|
129
|
+
if (!map) general.push(rule);
|
|
130
|
+
else {
|
|
131
|
+
if (!map.has(name)) map.set(name, []);
|
|
132
|
+
map.get(name).push(rule);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
if (input.literals === true) {
|
|
138
|
+
const walk = (rules) => {
|
|
139
|
+
for (const rule of rules) {
|
|
140
|
+
if (allRules.length >= 4000) {
|
|
141
|
+
capHit = true;
|
|
142
|
+
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (rule.type === 1) {
|
|
147
|
+
const entry = { selector: rule.selectorText, parts: partsOf(rule.style.cssText), declared: declaredNames(rule.style.cssText) };
|
|
148
|
+
|
|
149
|
+
allRules.push(entry);
|
|
150
|
+
indexRule(entry);
|
|
151
|
+
|
|
152
|
+
if (entry.parts) literalRules.push(entry);
|
|
153
|
+
} else if (rule.type === 3 && rule.styleSheet) {
|
|
154
|
+
try {
|
|
155
|
+
walk(rule.styleSheet.cssRules);
|
|
156
|
+
} catch {
|
|
157
|
+
unreadableSheet = true;
|
|
158
|
+
}
|
|
159
|
+
} else if (rule.cssRules && (rule.type === 4 ? win.matchMedia(rule.conditionText || rule.media.mediaText).matches : rule.type === 12 ? CSS.supports(rule.conditionText) : true)) {
|
|
160
|
+
walk(rule.cssRules);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
for (const rules of sheetRules) walk(rules);
|
|
166
|
+
|
|
167
|
+
// A sheet that could not be read at all (another site's): the rules are not all known.
|
|
168
|
+
unreadableSheet = sheetsUnreadable;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Whether the rules are all known: the answer of "did the author set it" then comes from them, not from the browser's defaults.
|
|
172
|
+
const declarationsKnown = input.literals === true && !unreadableSheet && !capHit;
|
|
173
|
+
const matchedCache = new Map();
|
|
174
|
+
const matchedRules = (el) => {
|
|
175
|
+
if (matchedCache.has(el)) return matchedCache.get(el);
|
|
176
|
+
|
|
177
|
+
const candidates = new Set(general);
|
|
178
|
+
|
|
179
|
+
for (const rule of byTag.get(el.localName) || []) candidates.add(rule);
|
|
180
|
+
|
|
181
|
+
if (el.id) for (const rule of byId.get(el.id) || []) candidates.add(rule);
|
|
182
|
+
|
|
183
|
+
for (const name of el.classList) for (const rule of byClass.get(name) || []) candidates.add(rule);
|
|
184
|
+
|
|
185
|
+
const matched = [...candidates].filter((rule) => {
|
|
186
|
+
try {
|
|
187
|
+
return el.matches(rule.selector);
|
|
188
|
+
} catch {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
matchedCache.set(el, matched);
|
|
194
|
+
|
|
195
|
+
return matched;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// The CSS properties that set one property (a camelCase one, as in computed styles): the longhand itself, the shorthands
|
|
199
|
+
// that carry it, and the logical forms of its side. A shorthand family is not enough: the author who sets margin-top
|
|
200
|
+
// has not set margin-bottom, and the paragraph's own margin there is the browser's.
|
|
201
|
+
const SIDES = { Top: ['top', 'block-start'], Bottom: ['bottom', 'block-end'], Left: ['left', rootStyle.direction === 'rtl' ? 'inline-end' : 'inline-start'], Right: ['right', rootStyle.direction === 'rtl' ? 'inline-start' : 'inline-end'] };
|
|
202
|
+
const CORNERS = { TopLeft: ['top-left', 'start-start'], TopRight: ['top-right', 'start-end'], BottomRight: ['bottom-right', 'end-end'], BottomLeft: ['bottom-left', 'end-start'] };
|
|
203
|
+
const kebab = (name) => name.replace(/([A-Z])/g, (m) => '-' + m.toLowerCase());
|
|
204
|
+
const settersCache = new Map();
|
|
205
|
+
const settersOf = (prop) => {
|
|
206
|
+
if (settersCache.has(prop)) return settersCache.get(prop);
|
|
207
|
+
|
|
208
|
+
const names = new Set(['all', kebab(prop)]);
|
|
209
|
+
let match;
|
|
210
|
+
|
|
211
|
+
if ((match = /^(padding|margin)(Top|Right|Bottom|Left)$/.exec(prop))) {
|
|
212
|
+
const [family, side] = [match[1], match[2]];
|
|
213
|
+
const [physical, logical] = SIDES[side];
|
|
214
|
+
const axis = side === 'Top' || side === 'Bottom' ? 'block' : 'inline';
|
|
215
|
+
|
|
216
|
+
for (const name of [family, family + '-' + physical, family + '-' + logical, family + '-' + axis]) names.add(name);
|
|
217
|
+
} else if ((match = /^border(Top|Right|Bottom|Left)(Color|Width|Style)$/.exec(prop))) {
|
|
218
|
+
const [side, kind] = [match[1], match[2].toLowerCase()];
|
|
219
|
+
const [physical, logical] = SIDES[side];
|
|
220
|
+
const axis = side === 'Top' || side === 'Bottom' ? 'block' : 'inline';
|
|
221
|
+
|
|
222
|
+
for (const base of ['border', 'border-' + physical, 'border-' + logical, 'border-' + axis]) {
|
|
223
|
+
names.add(base);
|
|
224
|
+
names.add(base + '-' + kind);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
names.add('border-' + kind);
|
|
228
|
+
} else if ((match = /^border(TopLeft|TopRight|BottomRight|BottomLeft)Radius$/.exec(prop))) {
|
|
229
|
+
for (const name of ['border-radius', 'border-' + CORNERS[match[1]][0] + '-radius', 'border-' + CORNERS[match[1]][1] + '-radius']) names.add(name);
|
|
230
|
+
} else if (prop === 'rowGap') {
|
|
231
|
+
for (const name of ['gap', 'row-gap', 'grid-gap', 'grid-row-gap']) names.add(name);
|
|
232
|
+
} else if (prop === 'columnGap') {
|
|
233
|
+
for (const name of ['gap', 'column-gap', 'grid-gap', 'grid-column-gap']) names.add(name);
|
|
234
|
+
} else if (prop === 'outlineColor') {
|
|
235
|
+
names.add('outline');
|
|
236
|
+
} else if (prop === 'backgroundColor') {
|
|
237
|
+
names.add('background');
|
|
238
|
+
} else if (prop === 'fontSize' || prop === 'fontFamily') {
|
|
239
|
+
names.add('font');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
settersCache.set(prop, names);
|
|
243
|
+
|
|
244
|
+
return names;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// Did a rule that matches the element, or its style attribute, set this property?
|
|
248
|
+
const declaredMemo = new Map();
|
|
249
|
+
const declaredOwn = (el, prop) => {
|
|
250
|
+
const memo = declaredMemo.get(el) || {};
|
|
251
|
+
|
|
252
|
+
if (prop in memo) return memo[prop];
|
|
253
|
+
|
|
254
|
+
const setters = settersOf(prop);
|
|
255
|
+
let found = matchedRules(el).some((rule) => [...rule.declared].some((name) => setters.has(name)));
|
|
256
|
+
|
|
257
|
+
if (!found && el.hasAttribute('style')) found = [...declaredNames(el.getAttribute('style'))].some((name) => setters.has(name));
|
|
258
|
+
|
|
259
|
+
declaredMemo.set(el, Object.assign(memo, { [prop]: found }));
|
|
260
|
+
|
|
261
|
+
return found;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const literalCache = new Map();
|
|
265
|
+
const literalsOf = (el) => {
|
|
266
|
+
if (literalCache.has(el)) return literalCache.get(el);
|
|
267
|
+
|
|
268
|
+
const merged = { text: [], background: [], border: [], outline: [], 'font-size': [], space: [], radius: [], family: [] };
|
|
269
|
+
const lists = matchedRules(el).filter((rule) => rule.parts).map((rule) => rule.parts);
|
|
270
|
+
const inline = el.hasAttribute('style') && input.literals === true ? partsOf(el.getAttribute('style')) : null;
|
|
271
|
+
|
|
272
|
+
for (const parts of inline ? [...lists, inline] : lists) {
|
|
273
|
+
for (const kind of Object.keys(merged)) merged[kind].push(...parts[kind]);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
literalCache.set(el, merged);
|
|
277
|
+
|
|
278
|
+
return merged;
|
|
279
|
+
};
|
|
280
|
+
const sameColor = (a, b) => Math.abs(a[0] - b[0]) <= 1.5 && Math.abs(a[1] - b[1]) <= 1.5 && Math.abs(a[2] - b[2]) <= 1.5 && Math.abs(a[3] - b[3]) <= 0.02;
|
|
281
|
+
const authoredColor = (el, role, rgba) => literalsOf(el)[role].some((css) => {
|
|
282
|
+
const other = toRgba(css);
|
|
283
|
+
|
|
284
|
+
return other && sameColor(other, rgba);
|
|
285
|
+
});
|
|
286
|
+
const rootPx = parseFloat(rootStyle.fontSize) || 16;
|
|
287
|
+
const authoredLength = (el, kind, px, style) => literalsOf(el)[kind].some((item) => {
|
|
288
|
+
const base = item.unit === 'px' ? 1 : item.unit === 'pt' ? 4 / 3 : item.unit === 'rem' ? rootPx : kind === 'font-size' ? parseFloat(getComputedStyle(el.parentElement || el).fontSize) || rootPx : parseFloat(style.fontSize) || rootPx;
|
|
289
|
+
|
|
290
|
+
return Math.abs(Math.abs(item.value) * base - px) <= 0.5;
|
|
291
|
+
});
|
|
292
|
+
const authoredFamily = (el, family) => literalsOf(el).family.some((list) => familyOf(list) === family);
|
|
293
|
+
`;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A part of the script that `lint.ts` runs inside a rendered design (see `lint-page.ts`): the first lines of the page script: the input, a few helpers, the motion scan (before the calm style hides it), and the calm style. The parts are one
|
|
3
|
+
* function body, in this order: setup, values, literals, states, elements, checks, focus. Plain JavaScript in a string.
|
|
4
|
+
*/
|
|
5
|
+
export const PAGE_SETUP = String.raw`(async (input) => {
|
|
6
|
+
const doc = document;
|
|
7
|
+
const win = window;
|
|
8
|
+
const root = doc.documentElement;
|
|
9
|
+
const body = doc.body;
|
|
10
|
+
const XHTML = 'http://www.w3.org/1999/xhtml';
|
|
11
|
+
const MAX_ELEMENTS = 4000;
|
|
12
|
+
const MAX_GROUPS = 300;
|
|
13
|
+
const round = (n) => Math.round(n * 100) / 100;
|
|
14
|
+
|
|
15
|
+
// The page's style sheets, as rules. A sheet linked from the bundle comes from the render's own server, and the sandbox
|
|
16
|
+
// makes that another origin, so the browser will not show its rules: read its text and parse it here instead. A sheet
|
|
17
|
+
// that cannot be read at all (another site's) makes "what the page's CSS says" unknown, and the checks that need it
|
|
18
|
+
// say less rather than guess.
|
|
19
|
+
const sheetRules = [];
|
|
20
|
+
let sheetsUnreadable = false;
|
|
21
|
+
|
|
22
|
+
for (const sheet of doc.styleSheets) {
|
|
23
|
+
if (sheet.disabled || (sheet.media && sheet.media.length > 0 && !win.matchMedia(sheet.media.mediaText).matches)) continue;
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
sheetRules.push(sheet.cssRules);
|
|
27
|
+
} catch {
|
|
28
|
+
try {
|
|
29
|
+
if (!sheet.href) throw new Error('no address');
|
|
30
|
+
|
|
31
|
+
const response = await fetch(sheet.href);
|
|
32
|
+
const text = await response.text();
|
|
33
|
+
|
|
34
|
+
if (!response.ok || text.length > 2000000) throw new Error('unusable');
|
|
35
|
+
|
|
36
|
+
const parsed = new CSSStyleSheet();
|
|
37
|
+
|
|
38
|
+
parsed.replaceSync(text);
|
|
39
|
+
sheetRules.push(parsed.cssRules);
|
|
40
|
+
} catch {
|
|
41
|
+
sheetsUnreadable = true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Motion first: the calm style below would hide it. A transition or animation that runs longer than 500 ms, or an
|
|
47
|
+
// animation that repeats forever, is motion that a person who asks for less (prefers-reduced-motion) should not get,
|
|
48
|
+
// unless the page's CSS has a prefers-reduced-motion rule.
|
|
49
|
+
const MOTION_MS = 500;
|
|
50
|
+
const motion = { count: 0, longest: 0, first: null, override: false, unreadable: false };
|
|
51
|
+
const toMs = (text) => text.split(',').map((part) => { const t = part.trim(); return t.endsWith('ms') ? parseFloat(t) : t.endsWith('s') ? parseFloat(t) * 1000 : 0; });
|
|
52
|
+
|
|
53
|
+
for (const el of [...(body ? body.querySelectorAll('*') : [])].slice(0, 3000)) {
|
|
54
|
+
const style = getComputedStyle(el);
|
|
55
|
+
|
|
56
|
+
if (style.display === 'none') continue;
|
|
57
|
+
|
|
58
|
+
const props = style.transitionProperty.split(',').map((part) => part.trim());
|
|
59
|
+
const transition = Math.max(0, ...toMs(style.transitionDuration).filter((ms, at) => props[at % props.length] !== 'none'));
|
|
60
|
+
const names = style.animationName.split(',').map((part) => part.trim());
|
|
61
|
+
const durations = toMs(style.animationDuration);
|
|
62
|
+
const repeats = style.animationIterationCount.split(',').map((part) => part.trim());
|
|
63
|
+
let animation = 0;
|
|
64
|
+
|
|
65
|
+
names.forEach((name, at) => {
|
|
66
|
+
const ms = durations[at % durations.length];
|
|
67
|
+
|
|
68
|
+
if (name === 'none' || !(ms > 0)) return;
|
|
69
|
+
|
|
70
|
+
animation = Math.max(animation, repeats[at % repeats.length] === 'infinite' ? Math.max(ms, MOTION_MS + 1) : ms);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const longest = Math.max(transition, animation);
|
|
74
|
+
|
|
75
|
+
if (longest > MOTION_MS) {
|
|
76
|
+
motion.count++;
|
|
77
|
+
motion.longest = Math.max(motion.longest, Math.round(longest));
|
|
78
|
+
if (!motion.first) motion.first = el;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const reducedRule = (rules) => {
|
|
83
|
+
for (const rule of rules) {
|
|
84
|
+
if (rule.media && /prefers-reduced-motion/.test(rule.media.mediaText || '')) return true;
|
|
85
|
+
if (rule.conditionText && /prefers-reduced-motion/.test(rule.conditionText)) return true;
|
|
86
|
+
if (rule.cssRules && reducedRule(rule.cssRules)) return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return false;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
for (const rules of sheetRules) {
|
|
93
|
+
if (reducedRule(rules)) motion.override = true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
motion.unreadable = sheetsUnreadable;
|
|
97
|
+
|
|
98
|
+
// A style that stops transitions and animations, so every computed value is the resting one.
|
|
99
|
+
const calm = doc.createElement('style');
|
|
100
|
+
calm.textContent = '*,*::before,*::after{transition:none!important;animation:none!important}';
|
|
101
|
+
doc.head.append(calm);
|
|
102
|
+
`;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A part of the script that `lint.ts` runs inside a rendered design (see `lint-page.ts`): the first pass of the walk: the backdrop behind each element, and what can paint over text. The parts are one
|
|
3
|
+
* function body, in this order: setup, values, literals, states, elements, checks, focus. Plain JavaScript in a string.
|
|
4
|
+
*/
|
|
5
|
+
export const PAGE_STATES = String.raw`// ---------------------------------------------------------------------------------------------------------------------
|
|
6
|
+
// The walk.
|
|
7
|
+
|
|
8
|
+
const SKIP = new Set(['HEAD', 'SCRIPT', 'STYLE', 'META', 'LINK', 'TITLE', 'NOSCRIPT', 'TEMPLATE', 'BASE', 'OPTION', 'OPTGROUP']);
|
|
9
|
+
const MEDIA = new Set(['IMG', 'VIDEO', 'CANVAS', 'SVG', 'PICTURE', 'IFRAME', 'OBJECT', 'EMBED']);
|
|
10
|
+
const nodes = body ? [root, body, ...body.querySelectorAll('*')].slice(0, MAX_ELEMENTS + 2) : [root];
|
|
11
|
+
const states = new Map();
|
|
12
|
+
const painters = [];
|
|
13
|
+
const boxes = new Map();
|
|
14
|
+
const rects = new Map();
|
|
15
|
+
let elements = 0;
|
|
16
|
+
const rectOf = (el) => {
|
|
17
|
+
let rect = rects.get(el);
|
|
18
|
+
|
|
19
|
+
if (!rect) {
|
|
20
|
+
rect = el.getBoundingClientRect();
|
|
21
|
+
rects.set(el, rect);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return rect;
|
|
25
|
+
};
|
|
26
|
+
const intersects = (a, b) => a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom;
|
|
27
|
+
|
|
28
|
+
// First pass: the resolved background behind each element (every ancestor's color, with opacity), whether it can be
|
|
29
|
+
// known at all, and the elements that paint over others (media, images, positioned fills).
|
|
30
|
+
for (const el of nodes) {
|
|
31
|
+
if (SKIP.has(el.tagName.toUpperCase()) || el.namespaceURI !== XHTML) {
|
|
32
|
+
if (el.localName === 'svg' && el.namespaceURI !== XHTML) {
|
|
33
|
+
const style = getComputedStyle(el);
|
|
34
|
+
|
|
35
|
+
if (style.display !== 'none' && painters.length < 300) {
|
|
36
|
+
const rect = rectOf(el);
|
|
37
|
+
|
|
38
|
+
if (rect.width > 0 && rect.height > 0) painters.push({ el, rect });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const style = getComputedStyle(el);
|
|
46
|
+
|
|
47
|
+
if (style.display === 'none') continue;
|
|
48
|
+
|
|
49
|
+
const parent = el.parentElement;
|
|
50
|
+
const before = parent ? states.get(parent) : undefined;
|
|
51
|
+
let back = before ? before.back : canvasRgb;
|
|
52
|
+
let cum = before ? before.cum : 1;
|
|
53
|
+
let uncertain = before ? before.uncertain : false;
|
|
54
|
+
let clip = before ? before.clip : false;
|
|
55
|
+
let fixed = before ? before.fixed : false;
|
|
56
|
+
const opacity = parseFloat(style.opacity);
|
|
57
|
+
|
|
58
|
+
cum *= Number.isNaN(opacity) ? 1 : opacity;
|
|
59
|
+
const bg = toRgba(style.backgroundColor) || [0, 0, 0, 0];
|
|
60
|
+
|
|
61
|
+
if (bg[3] > 0) back = mix(back, bg, bg[3] * cum);
|
|
62
|
+
|
|
63
|
+
const image = style.backgroundImage !== 'none';
|
|
64
|
+
|
|
65
|
+
if (image || style.filter !== 'none' || (style.backdropFilter || 'none') !== 'none' || style.mixBlendMode !== 'normal') uncertain = true;
|
|
66
|
+
|
|
67
|
+
// Partial opacity (a faded state, a transition caught half way) and an overlay drawn by ::before or ::after make
|
|
68
|
+
// what is behind the text uncertain; so does a clip or a mask.
|
|
69
|
+
if (cum < 0.99 || (style.clipPath !== 'none' && style.clipPath !== undefined) || (style.maskImage || style.webkitMaskImage || 'none') !== 'none') uncertain = true;
|
|
70
|
+
|
|
71
|
+
for (const pseudo of ['::before', '::after']) {
|
|
72
|
+
const made = getComputedStyle(el, pseudo);
|
|
73
|
+
|
|
74
|
+
if (made.content !== 'none' && made.content !== 'normal' && made.display !== 'none' && (made.position === 'absolute' || made.position === 'fixed') && (made.backgroundImage !== 'none' || (toRgba(made.backgroundColor) || [0, 0, 0, 0])[3] > 0)) uncertain = true;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (el !== root && el !== body && style.overflowX !== 'visible') clip = true;
|
|
78
|
+
|
|
79
|
+
if (style.position === 'fixed') fixed = true;
|
|
80
|
+
|
|
81
|
+
states.set(el, { back, cum, uncertain, clip, fixed, before: before ? before.back : canvasRgb });
|
|
82
|
+
|
|
83
|
+
if (painters.length < 300 && el !== root && el !== body) {
|
|
84
|
+
const rect = rectOf(el);
|
|
85
|
+
|
|
86
|
+
if (rect.width > 0 && rect.height > 0) {
|
|
87
|
+
const positioned = style.position === 'absolute' || style.position === 'fixed' || style.zIndex !== 'auto' || style.transform !== 'none';
|
|
88
|
+
|
|
89
|
+
if (MEDIA.has(el.tagName) || image || (positioned && bg[3] > 0)) painters.push({ el, rect });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const overlapped = (el, rect) => painters.some((item) => item.el !== el && !item.el.contains(el) && !el.contains(item.el) && intersects(rect, item.rect));
|
|
95
|
+
const hasText = (el) => {
|
|
96
|
+
for (const child of el.childNodes) {
|
|
97
|
+
if (child.nodeType === 3 && child.nodeValue.trim() !== '') return true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return el.localName === 'input' && /^(button|submit|reset)$/.test(el.type) && el.value !== '';
|
|
101
|
+
};
|
|
102
|
+
const familyOf = (list) => {
|
|
103
|
+
const first = list.split(',')[0].trim().replace(/^["']|["']$/g, '');
|
|
104
|
+
|
|
105
|
+
return first.toLowerCase();
|
|
106
|
+
};
|
|
107
|
+
const pxOf = (value) => parseFloat(value);
|
|
108
|
+
// What a pointer or finger can hit; the keyboard also reaches anything with a tabindex.
|
|
109
|
+
const pointed = 'a[href],button,input:not([type=hidden]),select,textarea,summary,[role=button],[role=link],[role=checkbox],[role=radio],[role=tab],[role=menuitem],[role=switch]';
|
|
110
|
+
const interactive = pointed + ',[tabindex]:not([tabindex="-1"])';
|
|
111
|
+
const OS_BLACK = 'rgba(0, 0, 0, 1)';
|
|
112
|
+
const targets = [];
|
|
113
|
+
const textFamilies = new Map();
|
|
114
|
+
const focusCandidates = new Map();
|
|
115
|
+
const viewWidth = root.clientWidth || win.innerWidth;
|
|
116
|
+
const offenders = [];
|
|
117
|
+
const fontInfo = new Map();
|
|
118
|
+
const spaceSeen = new Map();
|
|
119
|
+
const radiusSeen = new Map();
|
|
120
|
+
const scaleSides = [['top', 'Top'], ['right', 'Right'], ['bottom', 'Bottom'], ['left', 'Left']];
|
|
121
|
+
`;
|