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,666 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-tap fixes (`CheckFix` in `system-types.ts`): rewrite the literals of a page that equal a design-system token
|
|
3
|
+
* exactly, so that they follow the token. A pure function of the page's HTML: no browser, no files.
|
|
4
|
+
*
|
|
5
|
+
* It is careful because the page is untrusted text and a wrong rewrite breaks a design. It reads the page with a small
|
|
6
|
+
* scanner, not with patterns on the whole text: comments, scripts, strings, and `url()` are skipped; only `<style>`
|
|
7
|
+
* elements and `style` attributes are edited; only a declaration of a property that the finding names; only a literal
|
|
8
|
+
* that is a whole value token (a color, a `px` length, a whole `font-family` list); never a `var()` (nor its fallback),
|
|
9
|
+
* a custom property, `@font-face`, or `@keyframes`. A rewrite happens only where the place is certain (see `certain`).
|
|
10
|
+
*/
|
|
11
|
+
import { colorObjectCss, parseColor } from "./color.ts";
|
|
12
|
+
import type { CheckFinding, CheckFix, DesignSystem } from "./system-types.ts";
|
|
13
|
+
|
|
14
|
+
export interface FixResult {
|
|
15
|
+
/** The page, with the literals replaced by `var(--token)`. */
|
|
16
|
+
content: string;
|
|
17
|
+
/** How many literals were replaced (one finding can have several places). */
|
|
18
|
+
applied: number;
|
|
19
|
+
/** A reason for each finding that was not fixed, by the finding's id. */
|
|
20
|
+
skipped: { id: string; reason: string }[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A place that holds a declaration: a `<style>` element's text, or a `style` attribute's value. */
|
|
24
|
+
interface Region {
|
|
25
|
+
start: number;
|
|
26
|
+
end: number;
|
|
27
|
+
/** For an attribute: the element's `data-pid`, and whether another element has it too. */
|
|
28
|
+
pid?: string;
|
|
29
|
+
inline: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** One declaration in a region, with the offsets of its value in the page. */
|
|
33
|
+
interface Declaration {
|
|
34
|
+
property: string;
|
|
35
|
+
/** The value text with comments and the insides of strings and `url()` blanked, so that the scanner can read it. */
|
|
36
|
+
masked: string;
|
|
37
|
+
start: number;
|
|
38
|
+
end: number;
|
|
39
|
+
/** The rule's selector, when it is one plain rule (not nested, not inline). */
|
|
40
|
+
selector?: string;
|
|
41
|
+
inline: boolean;
|
|
42
|
+
pid?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** One place where a finding's literal stands. */
|
|
46
|
+
interface Match {
|
|
47
|
+
start: number;
|
|
48
|
+
end: number;
|
|
49
|
+
declaration: Declaration;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const MAX_CONTENT = 2_000_000;
|
|
53
|
+
const SKIPPED_AT_RULES =
|
|
54
|
+
/^@(-webkit-)?(font-face|keyframes|property|page|counter-style|font-feature-values)\b/i;
|
|
55
|
+
const RAW_TEXT = new Set(["script", "style", "textarea", "title", "xmp", "noscript"]);
|
|
56
|
+
const COLOR_FUNCTIONS = /^(rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)$/i;
|
|
57
|
+
|
|
58
|
+
/** The page's HTML as regions that hold CSS, and how many elements have each `data-pid`. */
|
|
59
|
+
function regions(content: string): { regions: Region[]; pids: Map<string, number> } {
|
|
60
|
+
const found: Region[] = [];
|
|
61
|
+
const pids = new Map<string, number>();
|
|
62
|
+
const lower = content.toLowerCase();
|
|
63
|
+
let at = 0;
|
|
64
|
+
|
|
65
|
+
while (at < content.length) {
|
|
66
|
+
const open = content.indexOf("<", at);
|
|
67
|
+
|
|
68
|
+
if (open === -1) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (content.startsWith("<!--", open)) {
|
|
73
|
+
const close = content.indexOf("-->", open + 4);
|
|
74
|
+
|
|
75
|
+
at = close === -1 ? content.length : close + 3;
|
|
76
|
+
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (content.startsWith("<![CDATA[", open)) {
|
|
81
|
+
const close = content.indexOf("]]>", open + 9);
|
|
82
|
+
|
|
83
|
+
at = close === -1 ? content.length : close + 3;
|
|
84
|
+
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const named = /^<([a-zA-Z][a-zA-Z0-9:-]*)/.exec(content.slice(open, open + 40));
|
|
89
|
+
|
|
90
|
+
if (named === null) {
|
|
91
|
+
// `</tag>`, `<!doctype>`, `<?x?>`, or a lone `<`.
|
|
92
|
+
const close = content.indexOf(">", open + 1);
|
|
93
|
+
|
|
94
|
+
at = close === -1 ? content.length : close + 1;
|
|
95
|
+
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const name = named[1]!.toLowerCase();
|
|
100
|
+
let index = open + named[0].length;
|
|
101
|
+
let styleValue: { start: number; end: number } | undefined;
|
|
102
|
+
let pid: string | undefined;
|
|
103
|
+
|
|
104
|
+
// The attributes, with quotes.
|
|
105
|
+
for (;;) {
|
|
106
|
+
while (index < content.length && /[\s/]/.test(content[index]!)) {
|
|
107
|
+
index++;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (index >= content.length || content[index] === ">") {
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const attribute = /^[^\s=>/]+/.exec(content.slice(index, index + 200));
|
|
115
|
+
|
|
116
|
+
if (attribute === null) {
|
|
117
|
+
index++;
|
|
118
|
+
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const attributeName = attribute[0].toLowerCase();
|
|
123
|
+
|
|
124
|
+
index += attribute[0].length;
|
|
125
|
+
|
|
126
|
+
while (index < content.length && /\s/.test(content[index]!)) {
|
|
127
|
+
index++;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (content[index] !== "=") {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
index++;
|
|
135
|
+
|
|
136
|
+
while (index < content.length && /\s/.test(content[index]!)) {
|
|
137
|
+
index++;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const quote = content[index];
|
|
141
|
+
let start = index;
|
|
142
|
+
let end: number;
|
|
143
|
+
|
|
144
|
+
if (quote === '"' || quote === "'") {
|
|
145
|
+
start = index + 1;
|
|
146
|
+
end = content.indexOf(quote, start);
|
|
147
|
+
end = end === -1 ? content.length : end;
|
|
148
|
+
index = end + 1;
|
|
149
|
+
} else {
|
|
150
|
+
end = start;
|
|
151
|
+
|
|
152
|
+
while (end < content.length && !/[\s>]/.test(content[end]!)) {
|
|
153
|
+
end++;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
index = end;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (attributeName === "style") {
|
|
160
|
+
styleValue = { start, end };
|
|
161
|
+
} else if (attributeName === "data-pid") {
|
|
162
|
+
pid = decode(content.slice(start, end));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const tagEnd = index >= content.length ? content.length : index + 1;
|
|
167
|
+
|
|
168
|
+
if (pid !== undefined) {
|
|
169
|
+
pids.set(pid, (pids.get(pid) ?? 0) + 1);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (styleValue !== undefined) {
|
|
173
|
+
found.push({ ...styleValue, pid, inline: true });
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
at = tagEnd;
|
|
177
|
+
|
|
178
|
+
if (RAW_TEXT.has(name)) {
|
|
179
|
+
const close = lower.indexOf(`</${name}`, tagEnd);
|
|
180
|
+
const end = close === -1 ? content.length : close;
|
|
181
|
+
|
|
182
|
+
if (name === "style") {
|
|
183
|
+
found.push({ start: tagEnd, end, inline: false });
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
at = end;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return { regions: found, pids };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The few entities that matter in a `style` attribute or a `data-pid`. */
|
|
194
|
+
function decode(text: string): string {
|
|
195
|
+
return text
|
|
196
|
+
.replace(/"/g, '"')
|
|
197
|
+
.replace(/'|'/g, "'")
|
|
198
|
+
.replace(/</g, "<")
|
|
199
|
+
.replace(/>/g, ">")
|
|
200
|
+
.replace(/&/g, "&");
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** The same text with comments, the insides of strings, and unquoted `url()` blanked to `x` (or a space): same length. */
|
|
204
|
+
function mask(text: string): string {
|
|
205
|
+
const out = text.split("");
|
|
206
|
+
let i = 0;
|
|
207
|
+
|
|
208
|
+
while (i < text.length) {
|
|
209
|
+
const ch = text[i]!;
|
|
210
|
+
|
|
211
|
+
if (ch === "/" && text[i + 1] === "*") {
|
|
212
|
+
const close = text.indexOf("*/", i + 2);
|
|
213
|
+
const end = close === -1 ? text.length : close + 2;
|
|
214
|
+
|
|
215
|
+
for (let j = i; j < end; j++) {
|
|
216
|
+
out[j] = " ";
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
i = end;
|
|
220
|
+
} else if (ch === '"' || ch === "'") {
|
|
221
|
+
let j = i + 1;
|
|
222
|
+
|
|
223
|
+
while (j < text.length && text[j] !== ch && text[j] !== "\n") {
|
|
224
|
+
if (text[j] === "\\") {
|
|
225
|
+
out[j] = "x";
|
|
226
|
+
j++;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (j < text.length) {
|
|
230
|
+
out[j] = "x";
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
j++;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
i = j + 1;
|
|
237
|
+
} else if (
|
|
238
|
+
/^url\(/i.test(text.slice(i, i + 4)) &&
|
|
239
|
+
text[i + 4] !== '"' &&
|
|
240
|
+
text[i + 4] !== "'"
|
|
241
|
+
) {
|
|
242
|
+
const close = text.indexOf(")", i + 4);
|
|
243
|
+
const end = close === -1 ? text.length : close;
|
|
244
|
+
|
|
245
|
+
for (let j = i + 4; j < end; j++) {
|
|
246
|
+
out[j] = "x";
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
i = end;
|
|
250
|
+
} else {
|
|
251
|
+
i++;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return out.join("");
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** The declarations of one region, with the selector of the rule that holds each. */
|
|
259
|
+
function declarations(content: string, region: Region): Declaration[] {
|
|
260
|
+
// In a `style` attribute a quote may be an entity: `"` stands for `"` (same length, so that offsets stay true).
|
|
261
|
+
const text = region.inline
|
|
262
|
+
? content
|
|
263
|
+
.slice(region.start, region.end)
|
|
264
|
+
.replace(/"/g, `"${" ".repeat(5)}`)
|
|
265
|
+
.replace(/'|'/g, (entity) => `'${" ".repeat(entity.length - 1)}`)
|
|
266
|
+
: content.slice(region.start, region.end);
|
|
267
|
+
const masked = mask(text);
|
|
268
|
+
const out: Declaration[] = [];
|
|
269
|
+
const stack: string[] = [];
|
|
270
|
+
// The same rules' selectors as written (the masked text hides the insides of strings, such as `[data-pid="x"]`).
|
|
271
|
+
const written: string[] = [];
|
|
272
|
+
let segment = 0;
|
|
273
|
+
let depth = 0;
|
|
274
|
+
|
|
275
|
+
const declaration = (from: number, to: number) => {
|
|
276
|
+
const colon = masked.indexOf(":", from);
|
|
277
|
+
|
|
278
|
+
if (colon === -1 || colon >= to) {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const property = masked.slice(from, colon).trim().toLowerCase();
|
|
283
|
+
|
|
284
|
+
if (property === "" || /[\s{}@]/.test(property) || property.startsWith("--")) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (stack.some((each) => SKIPPED_AT_RULES.test(each))) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
let end = to;
|
|
293
|
+
const important = /!\s*important\s*$/i.exec(masked.slice(colon + 1, to));
|
|
294
|
+
|
|
295
|
+
if (important !== null) {
|
|
296
|
+
end = colon + 1 + important.index;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const plain = written.filter((_, index) => !stack[index]!.startsWith("@"));
|
|
300
|
+
const selector =
|
|
301
|
+
region.inline || plain.length !== 1 || /&/.test(plain[0]!)
|
|
302
|
+
? undefined
|
|
303
|
+
: normalizeSelector(plain[0]!);
|
|
304
|
+
|
|
305
|
+
out.push({
|
|
306
|
+
property,
|
|
307
|
+
masked: masked.slice(colon + 1, end),
|
|
308
|
+
start: region.start + colon + 1,
|
|
309
|
+
end: region.start + end,
|
|
310
|
+
selector,
|
|
311
|
+
inline: region.inline,
|
|
312
|
+
pid: region.pid,
|
|
313
|
+
});
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
for (let i = 0; i < masked.length; i++) {
|
|
317
|
+
const ch = masked[i]!;
|
|
318
|
+
|
|
319
|
+
if (ch === "(") {
|
|
320
|
+
depth++;
|
|
321
|
+
} else if (ch === ")") {
|
|
322
|
+
depth = Math.max(0, depth - 1);
|
|
323
|
+
} else if (depth === 0 && ch === "{") {
|
|
324
|
+
stack.push(masked.slice(segment, i).trim());
|
|
325
|
+
written.push(
|
|
326
|
+
text
|
|
327
|
+
.slice(segment, i)
|
|
328
|
+
.replace(/\/\*[^]*?\*\//g, " ")
|
|
329
|
+
.trim(),
|
|
330
|
+
);
|
|
331
|
+
segment = i + 1;
|
|
332
|
+
} else if (depth === 0 && ch === "}") {
|
|
333
|
+
declaration(segment, i);
|
|
334
|
+
stack.pop();
|
|
335
|
+
written.pop();
|
|
336
|
+
segment = i + 1;
|
|
337
|
+
} else if (depth === 0 && ch === ";") {
|
|
338
|
+
declaration(segment, i);
|
|
339
|
+
segment = i + 1;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
declaration(segment, masked.length);
|
|
344
|
+
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** A selector without its extra spaces, for comparing the page's with a finding's. */
|
|
349
|
+
function normalizeSelector(selector: string): string {
|
|
350
|
+
return selector
|
|
351
|
+
.replace(/\s+/g, " ")
|
|
352
|
+
.replace(/\s*([>+~,])\s*/g, "$1")
|
|
353
|
+
.trim();
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** The top-level tokens of a value that can hold a color, a `px` length, or neither: with their offsets. */
|
|
357
|
+
function tokens(masked: string): { text: string; start: number; end: number }[] {
|
|
358
|
+
const out: { text: string; start: number; end: number }[] = [];
|
|
359
|
+
let i = 0;
|
|
360
|
+
|
|
361
|
+
while (i < masked.length) {
|
|
362
|
+
const ch = masked[i]!;
|
|
363
|
+
|
|
364
|
+
if (/\s|,|\//.test(ch)) {
|
|
365
|
+
i++;
|
|
366
|
+
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
let end = i;
|
|
371
|
+
|
|
372
|
+
// A word, a number with a unit, or a hex color; a function runs to its closing parenthesis.
|
|
373
|
+
while (end < masked.length && /[^\s,/(]/.test(masked[end]!)) {
|
|
374
|
+
end++;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (masked[end] === "(") {
|
|
378
|
+
let depth = 0;
|
|
379
|
+
const name = masked.slice(i, end);
|
|
380
|
+
|
|
381
|
+
for (; end < masked.length; end++) {
|
|
382
|
+
if (masked[end] === "(") {
|
|
383
|
+
depth++;
|
|
384
|
+
} else if (masked[end] === ")" && --depth === 0) {
|
|
385
|
+
end++;
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Only a color function is a value of its own; any other (`var()`, `calc()`, gradients) is left alone.
|
|
391
|
+
if (COLOR_FUNCTIONS.test(name)) {
|
|
392
|
+
out.push({ text: masked.slice(i, end), start: i, end });
|
|
393
|
+
}
|
|
394
|
+
} else if (end > i) {
|
|
395
|
+
out.push({ text: masked.slice(i, end), start: i, end });
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
i = Math.max(end, i + 1);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
return out;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const sameColor = (
|
|
405
|
+
a: { r: number; g: number; b: number; a: number },
|
|
406
|
+
b: { r: number; g: number; b: number; a: number },
|
|
407
|
+
) =>
|
|
408
|
+
Math.abs(a.r - b.r) <= 0.5 / 255 &&
|
|
409
|
+
Math.abs(a.g - b.g) <= 0.5 / 255 &&
|
|
410
|
+
Math.abs(a.b - b.b) <= 0.5 / 255 &&
|
|
411
|
+
Math.abs(a.a - b.a) <= 0.006;
|
|
412
|
+
|
|
413
|
+
/** The token of a fix, checked against the system again: it exists and still holds the literal in the mode. */
|
|
414
|
+
function verify(fix: CheckFix, system: DesignSystem): string | undefined {
|
|
415
|
+
const token = system.tokens.find(
|
|
416
|
+
(each) => each.name === fix.token && each.cssVar === fix.cssVar,
|
|
417
|
+
);
|
|
418
|
+
const entry = token?.modes[fix.mode];
|
|
419
|
+
|
|
420
|
+
if (token === undefined || entry === undefined) {
|
|
421
|
+
return "The token is not in the design system any more.";
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (fix.kind === "color") {
|
|
425
|
+
const css = entry.css.startsWith("var(")
|
|
426
|
+
? typeof entry.value === "string"
|
|
427
|
+
? entry.value
|
|
428
|
+
: (() => {
|
|
429
|
+
const made = colorObjectCss(entry.value);
|
|
430
|
+
|
|
431
|
+
return "css" in made ? made.css : undefined;
|
|
432
|
+
})()
|
|
433
|
+
: entry.css;
|
|
434
|
+
const color = css === undefined ? undefined : parseColor(css);
|
|
435
|
+
const literal = parseColor(fix.literal);
|
|
436
|
+
|
|
437
|
+
if (color === undefined || literal === undefined || !sameColor(color, literal)) {
|
|
438
|
+
return `The token ${fix.token} is not ${fix.literal} in ${fix.mode} any more.`;
|
|
439
|
+
}
|
|
440
|
+
} else if (fix.kind === "length") {
|
|
441
|
+
const value = entry.value;
|
|
442
|
+
const px =
|
|
443
|
+
typeof value === "object" &&
|
|
444
|
+
value !== null &&
|
|
445
|
+
(value as { unit?: unknown }).unit === "px"
|
|
446
|
+
? (value as { value?: unknown }).value
|
|
447
|
+
: typeof value === "string" && /^-?[\d.]+px$/.test(value)
|
|
448
|
+
? Number.parseFloat(value)
|
|
449
|
+
: undefined;
|
|
450
|
+
|
|
451
|
+
// A `rem` or other unit is trusted from the check (the browser resolved it); a `px` one is compared.
|
|
452
|
+
if (px !== undefined && px !== Number.parseFloat(fix.literal)) {
|
|
453
|
+
return `The token ${fix.token} is not ${fix.literal} in ${fix.mode} any more.`;
|
|
454
|
+
}
|
|
455
|
+
} else {
|
|
456
|
+
const wanted = families(fix.literal);
|
|
457
|
+
const list = Array.isArray(entry.value)
|
|
458
|
+
? entry.value.map((each) => String(each).toLowerCase())
|
|
459
|
+
: typeof entry.value === "string"
|
|
460
|
+
? families(entry.value)
|
|
461
|
+
: [];
|
|
462
|
+
|
|
463
|
+
if (wanted.length === 0 || wanted.join(",") !== list.join(",")) {
|
|
464
|
+
return `The token ${fix.token} is not ${fix.literal} in ${fix.mode} any more.`;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return undefined;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/** A font family list as names: lower case, no quotes. */
|
|
472
|
+
function families(text: string): string[] {
|
|
473
|
+
return decode(text)
|
|
474
|
+
.split(",")
|
|
475
|
+
.map((name) =>
|
|
476
|
+
name
|
|
477
|
+
.trim()
|
|
478
|
+
.replace(/^["']|["']$/g, "")
|
|
479
|
+
.toLowerCase(),
|
|
480
|
+
)
|
|
481
|
+
.filter((name) => name !== "");
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/** The places in `declaration` where the fix's literal stands. */
|
|
485
|
+
function placesIn(
|
|
486
|
+
content: string,
|
|
487
|
+
declaration: Declaration,
|
|
488
|
+
fix: CheckFix,
|
|
489
|
+
): { start: number; end: number }[] {
|
|
490
|
+
const base = declaration.start;
|
|
491
|
+
|
|
492
|
+
if (fix.kind === "font") {
|
|
493
|
+
// The whole value is the list, from its first to its last character that is not a space (read in the original).
|
|
494
|
+
const original = content.slice(declaration.start, declaration.end);
|
|
495
|
+
const trimmed = original.trim();
|
|
496
|
+
const lead = original.length - original.trimStart().length;
|
|
497
|
+
|
|
498
|
+
return trimmed === "" ? [] : [{ start: base + lead, end: base + lead + trimmed.length }];
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
const found: { start: number; end: number }[] = [];
|
|
502
|
+
const literal = fix.kind === "color" ? parseColor(fix.literal) : undefined;
|
|
503
|
+
const pixels = fix.kind === "length" ? Number.parseFloat(fix.literal) : Number.NaN;
|
|
504
|
+
let first = true;
|
|
505
|
+
|
|
506
|
+
for (const token of tokens(declaration.masked)) {
|
|
507
|
+
if (fix.kind === "color") {
|
|
508
|
+
const color = literal === undefined ? undefined : parseColor(token.text);
|
|
509
|
+
|
|
510
|
+
if (color !== undefined && literal !== undefined && sameColor(color, literal)) {
|
|
511
|
+
found.push({ start: base + token.start, end: base + token.end });
|
|
512
|
+
}
|
|
513
|
+
} else if (/^\d*\.?\d+px$/i.test(token.text)) {
|
|
514
|
+
// In the `font` shorthand only the first length is the size; the next, after a slash, is the line height.
|
|
515
|
+
if (
|
|
516
|
+
Number.parseFloat(token.text) === pixels &&
|
|
517
|
+
(declaration.property !== "font" || first)
|
|
518
|
+
) {
|
|
519
|
+
found.push({ start: base + token.start, end: base + token.end });
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
first = false;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
return found;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Replace the literals that equal a token exactly (see the file's comment). A finding is fixed where the place is
|
|
531
|
+
* certain: an inline style of the one element that has the finding's `data-pid`; a rule whose selector is the finding's;
|
|
532
|
+
* or the only place in the page that sets the property to the literal. Where several places match and none is certain,
|
|
533
|
+
* a finding is fixed only when the findings that were given are at least as many as the places (they are the same
|
|
534
|
+
* defect: fix them together); a single finding is skipped, and the reason says so.
|
|
535
|
+
*/
|
|
536
|
+
export function applyExactFixes(
|
|
537
|
+
content: string,
|
|
538
|
+
findings: CheckFinding[],
|
|
539
|
+
system: DesignSystem,
|
|
540
|
+
): FixResult {
|
|
541
|
+
const skipped: FixResult["skipped"] = [];
|
|
542
|
+
const edits = new Map<number, { start: number; end: number; text: string }>();
|
|
543
|
+
const skip = (finding: CheckFinding, reason: string) =>
|
|
544
|
+
skipped.push({ id: finding.id ?? "", reason });
|
|
545
|
+
|
|
546
|
+
if (typeof content !== "string" || content.length > MAX_CONTENT) {
|
|
547
|
+
for (const finding of findings) {
|
|
548
|
+
skip(finding, "The page is too large to rewrite.");
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
return { content, applied: 0, skipped };
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
const scanned = regions(content);
|
|
555
|
+
const all = scanned.regions.flatMap((region) => declarations(content, region));
|
|
556
|
+
const fixable = findings.filter((finding) => finding.fix !== undefined);
|
|
557
|
+
|
|
558
|
+
for (const finding of findings) {
|
|
559
|
+
const fix = finding.fix;
|
|
560
|
+
|
|
561
|
+
if (fix === undefined) {
|
|
562
|
+
skip(finding, "Not fixable: the value is not exactly a token's.");
|
|
563
|
+
|
|
564
|
+
continue;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const problem = verify(fix, system);
|
|
568
|
+
|
|
569
|
+
if (problem !== undefined) {
|
|
570
|
+
skip(finding, problem);
|
|
571
|
+
|
|
572
|
+
continue;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
const properties = new Set(fix.properties.map((property) => property.toLowerCase()));
|
|
576
|
+
const matches: Match[] = [];
|
|
577
|
+
|
|
578
|
+
for (const declaration of all) {
|
|
579
|
+
if (!properties.has(declaration.property)) {
|
|
580
|
+
continue;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
for (const place of placesIn(content, declaration, fix)) {
|
|
584
|
+
matches.push({ ...place, declaration });
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// A font list is a match only when the whole list equals the token's.
|
|
589
|
+
const real =
|
|
590
|
+
fix.kind === "font"
|
|
591
|
+
? matches.filter(
|
|
592
|
+
(match) =>
|
|
593
|
+
families(content.slice(match.start, match.end)).join(",") ===
|
|
594
|
+
families(fix.literal).join(","),
|
|
595
|
+
)
|
|
596
|
+
: matches;
|
|
597
|
+
|
|
598
|
+
if (real.length === 0) {
|
|
599
|
+
skip(
|
|
600
|
+
finding,
|
|
601
|
+
"The literal is not in a style element or a style attribute of the page: it may come from a script or a file.",
|
|
602
|
+
);
|
|
603
|
+
|
|
604
|
+
continue;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// A finding that stands for several elements (see `groupFindings`) knows all of them.
|
|
608
|
+
const where = finding.elements ?? [{ selector: finding.selector, pid: finding.pid }];
|
|
609
|
+
const owns = new Set(
|
|
610
|
+
where
|
|
611
|
+
.map((item) => item.pid)
|
|
612
|
+
.filter((pid): pid is string => pid !== undefined && scanned.pids.get(pid) === 1),
|
|
613
|
+
);
|
|
614
|
+
const wanted = new Set(
|
|
615
|
+
where
|
|
616
|
+
.filter((item) => item.selector !== "")
|
|
617
|
+
.map((item) => normalizeSelector(item.selector)),
|
|
618
|
+
);
|
|
619
|
+
const certain = real.filter(
|
|
620
|
+
(match) =>
|
|
621
|
+
(match.declaration.inline &&
|
|
622
|
+
match.declaration.pid !== undefined &&
|
|
623
|
+
owns.has(match.declaration.pid)) ||
|
|
624
|
+
(!match.declaration.inline &&
|
|
625
|
+
match.declaration.selector !== undefined &&
|
|
626
|
+
match.declaration.selector.split(",").some((each) => wanted.has(each))),
|
|
627
|
+
);
|
|
628
|
+
const together = fixable
|
|
629
|
+
.filter(
|
|
630
|
+
(other) =>
|
|
631
|
+
other.fix?.literal === fix.literal &&
|
|
632
|
+
other.fix.token === fix.token &&
|
|
633
|
+
other.fix.properties.some((property) => properties.has(property.toLowerCase())),
|
|
634
|
+
)
|
|
635
|
+
.reduce((total, other) => total + (other.elements?.length ?? 1), 0);
|
|
636
|
+
// Every place is accounted for when there are as many elements in the findings given as places (they are the same
|
|
637
|
+
// defect: fix them together); else the places that are certain; else the only place there is.
|
|
638
|
+
const chosen = together >= real.length || real.length === 1 ? real : certain;
|
|
639
|
+
|
|
640
|
+
if (chosen.length === 0) {
|
|
641
|
+
skip(
|
|
642
|
+
finding,
|
|
643
|
+
`${real.length} places set ${fix.properties[0]} to ${fix.literal} and none is certain to be this element's: fix them together.`,
|
|
644
|
+
);
|
|
645
|
+
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
for (const match of chosen) {
|
|
650
|
+
edits.set(match.start, {
|
|
651
|
+
start: match.start,
|
|
652
|
+
end: match.end,
|
|
653
|
+
text: `var(${fix.cssVar})`,
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
// Apply from the end, so that offsets stay true.
|
|
659
|
+
let result = content;
|
|
660
|
+
|
|
661
|
+
for (const edit of [...edits.values()].sort((a, b) => b.start - a.start)) {
|
|
662
|
+
result = result.slice(0, edit.start) + edit.text + result.slice(edit.end);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
return { content: result, applied: edits.size, skipped };
|
|
666
|
+
}
|