@adminiumjs/skin-kit 0.0.0-stage → 0.3.82
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/LICENSE +21 -0
- package/README.md +44 -2
- package/dist/contract.d.ts +90 -0
- package/dist/contract.d.ts.map +1 -0
- package/dist/contract.js +216 -0
- package/dist/contract.js.map +1 -0
- package/dist/css.d.ts +51 -0
- package/dist/css.d.ts.map +1 -0
- package/dist/css.js +690 -0
- package/dist/css.js.map +1 -0
- package/dist/diff.d.ts +11 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +122 -0
- package/dist/diff.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/loader.d.ts +92 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +199 -0
- package/dist/loader.js.map +1 -0
- package/dist/react.d.ts +87 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +346 -0
- package/dist/react.js.map +1 -0
- package/dist/testing.d.ts +39 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +107 -0
- package/dist/testing.js.map +1 -0
- package/dist/tokens.d.ts +149 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +367 -0
- package/dist/tokens.js.map +1 -0
- package/dist/validate.d.ts +154 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +621 -0
- package/dist/validate.js.map +1 -0
- package/dist/version.d.ts +22 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +41 -0
- package/dist/version.js.map +1 -0
- package/package.json +50 -4
package/dist/validate.js
ADDED
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
/**
|
|
3
|
+
* A SKIN PACKAGE'S `skin.json`, CHECKED AGAINST AN APP'S CONTRACT.
|
|
4
|
+
*
|
|
5
|
+
* The same function runs in the engine (when the package is attached and each
|
|
6
|
+
* time the shop's config is served) and in the shopper's browser (before a
|
|
7
|
+
* block is drawn), so Studio can say what the browser would refuse.
|
|
8
|
+
*
|
|
9
|
+
* What it holds a skin to:
|
|
10
|
+
* - the envelope: known keys only, the kit and contract versions it needs;
|
|
11
|
+
* - tokens: only the shared set, each value in its grammar, AA in both themes;
|
|
12
|
+
* - faces: a strict grammar, never a family the app owns;
|
|
13
|
+
* - blocks: nine types, each key typed; every `{hole}` names a prop the slot
|
|
14
|
+
* is given, of a kind that may go there (text into text nodes only, a
|
|
15
|
+
* picture only through `src`, a link target only through `to`);
|
|
16
|
+
* - hosts: only the slot's own, never repeated, never under `each` or `when`,
|
|
17
|
+
* every required group present;
|
|
18
|
+
* - limits on depth, size and count.
|
|
19
|
+
*
|
|
20
|
+
* Every problem names its place ("skin.json › home.featured › c.1: unknown
|
|
21
|
+
* prop `badge`", comp map CD26), so the owner can fix it.
|
|
22
|
+
*
|
|
23
|
+
* Pure.
|
|
24
|
+
*/
|
|
25
|
+
import { contractProblems, reservedFamilies, sameSpec, SLOT_ID_RE } from './contract.js';
|
|
26
|
+
import { aaFailures, COLOR_TOKENS, FAMILY_RE, familyKey, needsBothThemes, resolveTokens, SKIN_TOKENS, tokenValueProblem } from './tokens.js';
|
|
27
|
+
import { fitProblem, KIT_VERSION } from './version.js';
|
|
28
|
+
/** The nine block types. */
|
|
29
|
+
export const BLOCK_TYPES = ['stack', 'row', 'grid', 'heading', 'text', 'image', 'link', 'host', 'icon'];
|
|
30
|
+
/** Container tags a stack, row or grid may be. */
|
|
31
|
+
export const CONTAINER_TAGS = ['div', 'section', 'header', 'footer', 'nav', 'article', 'aside', 'address', 'ul', 'ol', 'li'];
|
|
32
|
+
/** Tags that may carry an accessible name (`label`). */
|
|
33
|
+
export const LABELLED_TAGS = new Set(['nav', 'section', 'aside', 'header', 'footer', 'article']);
|
|
34
|
+
/** Which keys each block type takes. */
|
|
35
|
+
const KEYS = {
|
|
36
|
+
stack: ['t', 'tag', 'cls', 'c', 'label', 'each', 'as', 'item', 'when', 'on'],
|
|
37
|
+
row: ['t', 'tag', 'cls', 'c', 'label', 'each', 'as', 'item', 'when', 'on'],
|
|
38
|
+
grid: ['t', 'tag', 'cls', 'c', 'label', 'each', 'as', 'item', 'when', 'on'],
|
|
39
|
+
heading: ['t', 'cls', 'text', 'parts', 'level', 'id', 'when'],
|
|
40
|
+
text: ['t', 'cls', 'text', 'parts', 'inline', 'when'],
|
|
41
|
+
image: ['t', 'cls', 'src', 'alt', 'decorative', 'when'],
|
|
42
|
+
link: ['t', 'cls', 'to', 'text', 'parts', 'c', 'label', 'when', 'on'],
|
|
43
|
+
host: ['t', 'name', 'area'],
|
|
44
|
+
icon: ['t', 'cls', 'name', 'when'],
|
|
45
|
+
};
|
|
46
|
+
/** Limits, shared by the engine and the browser. */
|
|
47
|
+
export const LIMITS = {
|
|
48
|
+
depth: 24,
|
|
49
|
+
blocksPerSlot: 2000,
|
|
50
|
+
blocksPerPackage: 6000,
|
|
51
|
+
children: 64,
|
|
52
|
+
parts: 16,
|
|
53
|
+
classes: 4,
|
|
54
|
+
text: 300,
|
|
55
|
+
faces: 8,
|
|
56
|
+
words: 200,
|
|
57
|
+
languages: 16,
|
|
58
|
+
drawnPerSlot: 5000,
|
|
59
|
+
eachDrawn: 200,
|
|
60
|
+
};
|
|
61
|
+
/** Package files: the data, CSS, fonts, pictures and licence texts. No script, no SVG, no HTML. */
|
|
62
|
+
export const ASSET_TYPES = {
|
|
63
|
+
json: 'application/json',
|
|
64
|
+
css: 'text/css',
|
|
65
|
+
woff2: 'font/woff2',
|
|
66
|
+
png: 'image/png',
|
|
67
|
+
jpg: 'image/jpeg',
|
|
68
|
+
jpeg: 'image/jpeg',
|
|
69
|
+
webp: 'image/webp',
|
|
70
|
+
avif: 'image/avif',
|
|
71
|
+
txt: 'text/plain',
|
|
72
|
+
};
|
|
73
|
+
/** Byte caps per file type, and for the whole package. */
|
|
74
|
+
export const SIZE_CAPS = { json: 512 * 1024, css: 64 * 1024, cssTotal: 256 * 1024, woff2: 2 * 1024 * 1024, image: 4 * 1024 * 1024, txt: 64 * 1024, files: 64, total: 20 * 1024 * 1024 };
|
|
75
|
+
/** A package path: lower-case folders, a file name, an allowed extension; no `..`. */
|
|
76
|
+
export const ASSET_PATH_RE = /^(?:[a-z0-9][a-z0-9_-]{0,31}\/){0,3}[A-Za-z0-9][A-Za-z0-9_-]{0,63}(?:\.[A-Za-z0-9_-]{1,16})*\.(json|css|woff2|png|jpg|jpeg|webp|avif|txt)$/;
|
|
77
|
+
/** The file type a package path has, or null when it isn't one a skin may carry. */
|
|
78
|
+
export function assetType(path) {
|
|
79
|
+
const m = ASSET_PATH_RE.exec(path);
|
|
80
|
+
return m === null ? null : (ASSET_TYPES[m[1] ?? ''] ?? null);
|
|
81
|
+
}
|
|
82
|
+
/** The cap for one file of this path, in bytes. */
|
|
83
|
+
export function sizeCap(path) {
|
|
84
|
+
const ext = path.slice(path.lastIndexOf('.') + 1).toLowerCase();
|
|
85
|
+
if (ext === 'json')
|
|
86
|
+
return SIZE_CAPS.json;
|
|
87
|
+
if (ext === 'css')
|
|
88
|
+
return SIZE_CAPS.css;
|
|
89
|
+
if (ext === 'woff2')
|
|
90
|
+
return SIZE_CAPS.woff2;
|
|
91
|
+
if (ext === 'txt')
|
|
92
|
+
return SIZE_CAPS.txt;
|
|
93
|
+
return SIZE_CAPS.image;
|
|
94
|
+
}
|
|
95
|
+
const ENVELOPE_KEYS = ['kit', 'of', 'name', 'defaultLanguage', 'tokens', 'faces', 'words', 'slots', 'styles'];
|
|
96
|
+
const CLASS_RE = /^[a-z][a-z0-9-]{0,31}$/;
|
|
97
|
+
const AREA_RE = /^[a-z][a-z0-9-]{0,23}$/;
|
|
98
|
+
const LOOP_RE = /^[a-z][a-zA-Z0-9]{0,15}$/;
|
|
99
|
+
const LANG_RE = /^[a-z]{2}(-[A-Z]{2})?$/;
|
|
100
|
+
const WORD_KEY_RE = /^[a-zA-Z][a-zA-Z0-9]{0,31}$/;
|
|
101
|
+
const SEG_RE = /^[a-zA-Z][a-zA-Z0-9]{0,31}$/;
|
|
102
|
+
// Control characters, and the bidi overrides a spoofed line would use.
|
|
103
|
+
// eslint-disable-next-line no-control-regex
|
|
104
|
+
const CONTROL_RE = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f--]/;
|
|
105
|
+
function isRecord(value) {
|
|
106
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
107
|
+
}
|
|
108
|
+
const ROOT = 'skin.json';
|
|
109
|
+
const at = (...parts) => [ROOT, ...parts.map(String)].join(' › ');
|
|
110
|
+
/** A template, read; a string saying what is wrong otherwise. */
|
|
111
|
+
export function parseTemplate(source) {
|
|
112
|
+
const out = [];
|
|
113
|
+
let literal = '';
|
|
114
|
+
for (let i = 0; i < source.length; i++) {
|
|
115
|
+
const ch = source[i];
|
|
116
|
+
if (ch === '{' && source[i + 1] === '{') {
|
|
117
|
+
literal += '{';
|
|
118
|
+
i++;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (ch !== '{') {
|
|
122
|
+
literal += ch;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const end = source.indexOf('}', i);
|
|
126
|
+
if (end < 0)
|
|
127
|
+
return 'has a "{" with no "}" (write "{{" for a brace)';
|
|
128
|
+
const path = source.slice(i + 1, end).split('.');
|
|
129
|
+
if (path.length > 6 || path.some((seg) => !SEG_RE.test(seg)))
|
|
130
|
+
return `has "{${source.slice(i + 1, end)}}", which is not a prop path`;
|
|
131
|
+
if (literal !== '')
|
|
132
|
+
out.push({ text: literal });
|
|
133
|
+
literal = '';
|
|
134
|
+
out.push({ hole: path });
|
|
135
|
+
i = end;
|
|
136
|
+
}
|
|
137
|
+
if (literal !== '')
|
|
138
|
+
out.push({ text: literal });
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
/** A whole-value hole (`"{a.b}"`), read; null when the value is anything else. */
|
|
142
|
+
export function wholeHole(value) {
|
|
143
|
+
const parsed = parseTemplate(value);
|
|
144
|
+
return Array.isArray(parsed) && parsed.length === 1 && 'hole' in parsed[0] ? parsed[0].hole : null;
|
|
145
|
+
}
|
|
146
|
+
/** The spec a path names in a scope, or why it names nothing. */
|
|
147
|
+
function resolve(scope, path) {
|
|
148
|
+
const [root, ...rest] = path;
|
|
149
|
+
if (root === undefined)
|
|
150
|
+
return 'is empty';
|
|
151
|
+
if (root === 't') {
|
|
152
|
+
if (rest.length !== 1 || !scope.words.has(rest[0] ?? ''))
|
|
153
|
+
return `names the app's word \`${rest.join('.')}\`, which this slot isn't given`;
|
|
154
|
+
return { kind: 'text' };
|
|
155
|
+
}
|
|
156
|
+
if (root === 'w') {
|
|
157
|
+
if (rest.length !== 1 || !scope.skinWords.has(rest[0] ?? ''))
|
|
158
|
+
return `names the skin's word \`${rest.join('.')}\`, which its words don't hold`;
|
|
159
|
+
return { kind: 'text' };
|
|
160
|
+
}
|
|
161
|
+
let spec = scope.loops[root] ?? scope.props[root];
|
|
162
|
+
if (spec === undefined)
|
|
163
|
+
return `unknown prop \`${path.join('.')}\``;
|
|
164
|
+
for (const seg of rest) {
|
|
165
|
+
if (spec.kind === 'image' && ['url', 'alt', 'w', 'h'].includes(seg))
|
|
166
|
+
return `reads \`${path.join('.')}\`: a picture is drawn whole, through src`;
|
|
167
|
+
if (spec.kind !== 'object')
|
|
168
|
+
return `unknown prop \`${path.join('.')}\``;
|
|
169
|
+
const next = spec.props?.[seg];
|
|
170
|
+
if (next === undefined)
|
|
171
|
+
return `unknown prop \`${path.join('.')}\``;
|
|
172
|
+
spec = next;
|
|
173
|
+
}
|
|
174
|
+
return spec;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The spec a hole names inside a slot (the renderer asks, to draw a figure in
|
|
178
|
+
* the app's own face); null when it names nothing.
|
|
179
|
+
*/
|
|
180
|
+
export function specAt(contract, slot, loops, path) {
|
|
181
|
+
const spec = contract.slots[slot];
|
|
182
|
+
if (spec === undefined)
|
|
183
|
+
return null;
|
|
184
|
+
const found = resolve({ props: spec.props, words: new Set(spec.words ?? []), skinWords: new Set(), loops }, path);
|
|
185
|
+
return typeof found === 'string' ? (path[0] === 'w' ? { kind: 'text' } : null) : found;
|
|
186
|
+
}
|
|
187
|
+
/** Kinds a hole may draw as text. */
|
|
188
|
+
const TEXT_KINDS = new Set(['text', 'figure', 'enum']);
|
|
189
|
+
/** Check one block and its children. `guarded` is true under `each` or `when`. */
|
|
190
|
+
function walk(w, b, path, scope, depth, guarded, parent) {
|
|
191
|
+
const here = at(w.slot, ...path);
|
|
192
|
+
const problem = (message) => w.problems.push({ path: here, message });
|
|
193
|
+
if (depth > LIMITS.depth) {
|
|
194
|
+
problem(`nests deeper than ${String(LIMITS.depth)}`);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (!isRecord(b)) {
|
|
198
|
+
problem('must be a block');
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
w.blocks++;
|
|
202
|
+
const t = b['t'];
|
|
203
|
+
if (!BLOCK_TYPES.includes(t)) {
|
|
204
|
+
problem(`block type "${String(t)}" isn't allowed`);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const type = t;
|
|
208
|
+
for (const key of Object.keys(b))
|
|
209
|
+
if (!KEYS[type].includes(key))
|
|
210
|
+
problem(`key "${key}" isn't allowed on ${type}`);
|
|
211
|
+
// A list item sits right under a list, and a list holds only items.
|
|
212
|
+
const tag = b['tag'];
|
|
213
|
+
if (tag !== undefined) {
|
|
214
|
+
if (!['stack', 'row', 'grid'].includes(type) || !CONTAINER_TAGS.includes(tag))
|
|
215
|
+
problem(`tag "${String(tag)}" isn't allowed`);
|
|
216
|
+
}
|
|
217
|
+
const parentList = parent !== null && (parent.tag === 'ul' || parent.tag === 'ol');
|
|
218
|
+
if (tag === 'li' && !parentList)
|
|
219
|
+
problem('an li sits directly in a ul or ol');
|
|
220
|
+
if (parentList && tag !== 'li')
|
|
221
|
+
problem('a ul or ol holds only li blocks');
|
|
222
|
+
if (b['cls'] !== undefined) {
|
|
223
|
+
const names = typeof b['cls'] === 'string' ? b['cls'].split(/\s+/).filter(Boolean) : null;
|
|
224
|
+
if (names === null || names.length > LIMITS.classes || names.some((n) => !CLASS_RE.test(n)))
|
|
225
|
+
problem(`cls must be up to ${String(LIMITS.classes)} names like "card" or "hero-pic"`);
|
|
226
|
+
}
|
|
227
|
+
if (b['label'] !== undefined && type !== 'link' && !LABELLED_TAGS.has(String(tag ?? 'div')))
|
|
228
|
+
problem('label only names a nav, section, aside, header, footer, article or link');
|
|
229
|
+
if (b['on'] !== undefined && b['on'] !== 'accent' && b['on'] !== 'accent-2')
|
|
230
|
+
problem('on is accent or accent-2');
|
|
231
|
+
if (b['level'] !== undefined && (typeof b['level'] !== 'number' || !Number.isInteger(b['level']) || b['level'] < 1 || b['level'] > 6))
|
|
232
|
+
problem('level is 1 to 6');
|
|
233
|
+
if (type === 'heading' && b['level'] === undefined)
|
|
234
|
+
problem('a heading needs a level');
|
|
235
|
+
if (b['inline'] !== undefined && typeof b['inline'] !== 'boolean')
|
|
236
|
+
problem('inline is true or false');
|
|
237
|
+
if (b['decorative'] !== undefined && b['decorative'] !== true)
|
|
238
|
+
problem('decorative is true');
|
|
239
|
+
// `when`: a path, `!path`, or `path=value`.
|
|
240
|
+
let whenGuard = false;
|
|
241
|
+
if (b['when'] !== undefined) {
|
|
242
|
+
whenGuard = true;
|
|
243
|
+
const m = typeof b['when'] === 'string' ? /^(!?)([a-zA-Z][a-zA-Z0-9.]*)(?:=([a-z0-9-]{1,32}))?$/.exec(b['when']) : null;
|
|
244
|
+
if (m === null)
|
|
245
|
+
problem('when is a prop path, "!path" or "path=value"');
|
|
246
|
+
else {
|
|
247
|
+
const spec = resolve(scope, (m[2] ?? '').split('.'));
|
|
248
|
+
if (typeof spec === 'string')
|
|
249
|
+
problem(`when ${spec}`);
|
|
250
|
+
else if (m[3] !== undefined && spec.kind === 'enum' && !(spec.values ?? []).includes(m[3]))
|
|
251
|
+
problem(`when compares with "${m[3]}", which ${m[2] ?? ''} never is`);
|
|
252
|
+
else if (m[3] !== undefined && !['enum', 'text'].includes(spec.kind))
|
|
253
|
+
problem('when compares only a text or an enum');
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
// Text: a template, or one hole naming rich text.
|
|
257
|
+
const template = (key, value, allowRich) => {
|
|
258
|
+
if (typeof value !== 'string') {
|
|
259
|
+
problem(`${key} must be text`);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (value.length > LIMITS.text)
|
|
263
|
+
problem(`${key} is longer than ${String(LIMITS.text)} characters`);
|
|
264
|
+
if (CONTROL_RE.test(value))
|
|
265
|
+
problem(`${key} holds a control character`);
|
|
266
|
+
const pieces = parseTemplate(value);
|
|
267
|
+
if (typeof pieces === 'string') {
|
|
268
|
+
problem(`${key} ${pieces}`);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
for (const piece of pieces) {
|
|
272
|
+
if (!('hole' in piece))
|
|
273
|
+
continue;
|
|
274
|
+
const spec = resolve(scope, piece.hole);
|
|
275
|
+
if (typeof spec === 'string')
|
|
276
|
+
problem(spec);
|
|
277
|
+
else if (spec.kind === 'rich') {
|
|
278
|
+
if (!allowRich || pieces.length !== 1)
|
|
279
|
+
problem(`{${piece.hole.join('.')}} is rich text: a text block's text, alone`);
|
|
280
|
+
}
|
|
281
|
+
else if (!TEXT_KINDS.has(spec.kind))
|
|
282
|
+
problem(`{${piece.hole.join('.')}} is ${spec.kind === 'image' ? 'a picture (use src)' : `a ${spec.kind}`}, not text`);
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
if (b['text'] !== undefined)
|
|
286
|
+
template('text', b['text'], type === 'text');
|
|
287
|
+
if (b['alt'] !== undefined)
|
|
288
|
+
template('alt', b['alt'], false);
|
|
289
|
+
if (b['label'] !== undefined)
|
|
290
|
+
template('label', b['label'], false);
|
|
291
|
+
if (b['parts'] !== undefined) {
|
|
292
|
+
if (!Array.isArray(b['parts']) || b['parts'].length === 0 || b['parts'].length > LIMITS.parts)
|
|
293
|
+
problem(`parts lists 1 to ${String(LIMITS.parts)} runs`);
|
|
294
|
+
else
|
|
295
|
+
b['parts'].forEach((part, i) => {
|
|
296
|
+
if (!isRecord(part) || Object.keys(part).some((k) => k !== 'text' && k !== 'mono') || (part['mono'] !== undefined && typeof part['mono'] !== 'boolean')) {
|
|
297
|
+
w.problems.push({ path: at(w.slot, ...path, `parts.${String(i)}`), message: 'a run is { text, mono? }' });
|
|
298
|
+
}
|
|
299
|
+
else
|
|
300
|
+
template(`parts.${String(i)}`, part['text'], false);
|
|
301
|
+
});
|
|
302
|
+
if (b['text'] !== undefined)
|
|
303
|
+
problem('text and parts don\'t go together');
|
|
304
|
+
}
|
|
305
|
+
if ((type === 'heading' || type === 'text') && b['text'] === undefined && b['parts'] === undefined)
|
|
306
|
+
problem(`a ${type} needs text or parts`);
|
|
307
|
+
// A picture: one image prop, or a declared asset.
|
|
308
|
+
if (type === 'image') {
|
|
309
|
+
const src = b['src'];
|
|
310
|
+
if (typeof src !== 'string')
|
|
311
|
+
problem('an image needs src');
|
|
312
|
+
else if (src.startsWith('asset:')) {
|
|
313
|
+
const file = src.slice('asset:'.length);
|
|
314
|
+
if (assetType(file)?.startsWith('image/') !== true)
|
|
315
|
+
problem(`src names "${file}", which is not a picture file`);
|
|
316
|
+
else if (w.files !== null && !w.files.has(file))
|
|
317
|
+
problem(`src names "${file}", which the package doesn't declare`);
|
|
318
|
+
if (b['alt'] === undefined && b['decorative'] !== true)
|
|
319
|
+
problem('a picture of the package needs alt, or decorative: true');
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
const hole = wholeHole(src);
|
|
323
|
+
const spec = hole === null ? 'src is "{prop}" naming a picture, or "asset:<file>"' : resolve(scope, hole);
|
|
324
|
+
if (typeof spec === 'string')
|
|
325
|
+
problem(spec);
|
|
326
|
+
else if (spec.kind !== 'image')
|
|
327
|
+
problem(`src {${hole?.join('.') ?? ''}} is a ${spec.kind}, not a picture`);
|
|
328
|
+
else if (spec.optional === true && !whenGuard)
|
|
329
|
+
problem(`src {${hole?.join('.') ?? ''}} may be empty: put the image under when`);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
// A link: a literal route id, or one prop naming a route, a target or an anchor.
|
|
333
|
+
if (type === 'link') {
|
|
334
|
+
const to = b['to'];
|
|
335
|
+
if (typeof to !== 'string')
|
|
336
|
+
problem('a link needs to');
|
|
337
|
+
else if (to.includes('{')) {
|
|
338
|
+
const hole = wholeHole(to);
|
|
339
|
+
const spec = hole === null ? 'to is a route id, or "{prop}" alone' : resolve(scope, hole);
|
|
340
|
+
if (typeof spec === 'string')
|
|
341
|
+
problem(spec);
|
|
342
|
+
else if (!['route', 'target', 'anchor'].includes(spec.kind))
|
|
343
|
+
problem(`to {${hole?.join('.') ?? ''}} is a ${spec.kind}, not a link target`);
|
|
344
|
+
}
|
|
345
|
+
else if (!w.contract.routes.includes(to))
|
|
346
|
+
problem(`to "${to}" is not a route this app has`);
|
|
347
|
+
if (b['text'] === undefined && b['parts'] === undefined && b['c'] === undefined)
|
|
348
|
+
problem('a link needs words: text, parts or blocks');
|
|
349
|
+
}
|
|
350
|
+
// An icon: a literal from the app's set, or an icon prop.
|
|
351
|
+
if (type === 'icon') {
|
|
352
|
+
const name = b['name'];
|
|
353
|
+
if (typeof name !== 'string')
|
|
354
|
+
problem('an icon needs name');
|
|
355
|
+
else if (name.includes('{')) {
|
|
356
|
+
const hole = wholeHole(name);
|
|
357
|
+
const spec = hole === null ? 'name is an icon id, or "{prop}" alone' : resolve(scope, hole);
|
|
358
|
+
if (typeof spec === 'string')
|
|
359
|
+
problem(spec);
|
|
360
|
+
else if (spec.kind !== 'icon')
|
|
361
|
+
problem(`name {${hole?.join('.') ?? ''}} is a ${spec.kind}, not an icon`);
|
|
362
|
+
}
|
|
363
|
+
else if (!(w.contract.icons ?? []).includes(name))
|
|
364
|
+
problem(`icon "${name}" is not one this app draws`);
|
|
365
|
+
}
|
|
366
|
+
// An anchor id on a heading.
|
|
367
|
+
if (b['id'] !== undefined) {
|
|
368
|
+
const id = b['id'];
|
|
369
|
+
if (typeof id !== 'string')
|
|
370
|
+
problem('id must be text');
|
|
371
|
+
else if (id.includes('{')) {
|
|
372
|
+
const hole = wholeHole(id);
|
|
373
|
+
const spec = hole === null ? 'id is a name, or "{prop}" alone' : resolve(scope, hole);
|
|
374
|
+
if (typeof spec === 'string')
|
|
375
|
+
problem(spec);
|
|
376
|
+
else if (spec.kind !== 'anchor')
|
|
377
|
+
problem(`id {${hole?.join('.') ?? ''}} is a ${spec.kind}, not an anchor`);
|
|
378
|
+
}
|
|
379
|
+
else if (!CLASS_RE.test(id))
|
|
380
|
+
problem('id is a name like "who-we-are"');
|
|
381
|
+
}
|
|
382
|
+
// A host: this slot's, once, never conditional.
|
|
383
|
+
if (type === 'host') {
|
|
384
|
+
const name = b['name'];
|
|
385
|
+
if (typeof name !== 'string' || !(w.spec.hosts ?? []).includes(name))
|
|
386
|
+
problem(`host "${String(name)}" can't be placed in ${w.slot}`);
|
|
387
|
+
else {
|
|
388
|
+
w.hosts.set(name, (w.hosts.get(name) ?? 0) + 1);
|
|
389
|
+
if ((w.hosts.get(name) ?? 0) > 1)
|
|
390
|
+
problem(`host "${name}" is placed twice`);
|
|
391
|
+
if (guarded)
|
|
392
|
+
problem(`host "${name}" sits under each or when: the app's own controls are always drawn`);
|
|
393
|
+
}
|
|
394
|
+
if (b['area'] !== undefined && (typeof b['area'] !== 'string' || !AREA_RE.test(b['area'])))
|
|
395
|
+
problem('area is a grid area name like "search"');
|
|
396
|
+
if (parent?.t === 'link')
|
|
397
|
+
problem('a host never sits inside a link');
|
|
398
|
+
}
|
|
399
|
+
// Children, and lists.
|
|
400
|
+
const hasEach = b['each'] !== undefined;
|
|
401
|
+
if (b['c'] !== undefined) {
|
|
402
|
+
if (!['stack', 'row', 'grid', 'link'].includes(type) || !Array.isArray(b['c']) || b['c'].length > LIMITS.children)
|
|
403
|
+
problem(`c lists up to ${String(LIMITS.children)} blocks`);
|
|
404
|
+
else if (hasEach)
|
|
405
|
+
problem('a block repeats item for each, or holds c, not both');
|
|
406
|
+
else
|
|
407
|
+
b['c'].forEach((child, i) => walk(w, child, [...path, `c.${String(i)}`], scope, depth + 1, guarded || whenGuard, b));
|
|
408
|
+
}
|
|
409
|
+
if (type === 'link' && Array.isArray(b['c']) && containsType(b['c'], 'link'))
|
|
410
|
+
problem('a link never holds another link');
|
|
411
|
+
if (hasEach) {
|
|
412
|
+
const each = b['each'];
|
|
413
|
+
const spec = typeof each === 'string' ? resolve(scope, each.split('.')) : 'each is a prop path';
|
|
414
|
+
const loop = b['as'] ?? 'item';
|
|
415
|
+
if (typeof spec === 'string')
|
|
416
|
+
problem(`each ${spec}`);
|
|
417
|
+
else if (spec.kind !== 'list' || spec.of === undefined)
|
|
418
|
+
problem(`each ${String(each)} is a ${spec.kind}, not a list`);
|
|
419
|
+
else if (typeof loop !== 'string' || !LOOP_RE.test(loop) || loop === 't' || loop === 'w' || loop in w.spec.props)
|
|
420
|
+
problem('as is a short loop name that is not a prop, t or w');
|
|
421
|
+
else {
|
|
422
|
+
const item = b['item'];
|
|
423
|
+
if (typeof item === 'string') {
|
|
424
|
+
const target = item.startsWith('@') ? item.slice(1) : '';
|
|
425
|
+
const reused = w.contract.slots[target];
|
|
426
|
+
if (reused?.reusable !== true)
|
|
427
|
+
problem(`item "${item}" is not a slot that can be reused`);
|
|
428
|
+
else if (target === w.slot)
|
|
429
|
+
problem(`item "${item}" draws the slot inside itself`);
|
|
430
|
+
else if (!sameSpec(reused.props['item'], spec.of))
|
|
431
|
+
problem(`item "${item}" is drawn from ${String(each)}, whose elements are not that slot's item`);
|
|
432
|
+
}
|
|
433
|
+
else if (item === undefined)
|
|
434
|
+
problem('each needs an item');
|
|
435
|
+
else
|
|
436
|
+
walk(w, item, [...path, 'item'], { ...scope, loops: { ...scope.loops, [loop]: spec.of } }, depth + 1, true, b);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
else if (b['item'] !== undefined || b['as'] !== undefined)
|
|
440
|
+
problem('item and as go with each');
|
|
441
|
+
}
|
|
442
|
+
function containsType(blocks, type) {
|
|
443
|
+
return blocks.some((b) => isRecord(b) && (b['t'] === type || (Array.isArray(b['c']) && containsType(b['c'], type)) || (isRecord(b['item']) && containsType([b['item']], type))));
|
|
444
|
+
}
|
|
445
|
+
/** Check a `skin.json` against an app's contract. */
|
|
446
|
+
export function validateSkin(input, contract, options = {}) {
|
|
447
|
+
const problems = [];
|
|
448
|
+
const problem = (path, message) => problems.push({ path, message });
|
|
449
|
+
const contractIssues = contractProblems(contract);
|
|
450
|
+
if (contractIssues.length > 0)
|
|
451
|
+
return { ok: false, problems: contractIssues.map((p) => ({ path: `contract › ${p.path}`, message: p.message })) };
|
|
452
|
+
if (!isRecord(input))
|
|
453
|
+
return { ok: false, problems: [{ path: ROOT, message: 'must be an object' }] };
|
|
454
|
+
for (const key of Object.keys(input))
|
|
455
|
+
if (!ENVELOPE_KEYS.includes(key))
|
|
456
|
+
problem(at(key), key === 'pages' ? 'pages arrive in a later kit (with Landing pages in the shop)' : 'unknown key');
|
|
457
|
+
// Versions first: a skin for another app or kit is refused before anything else is read.
|
|
458
|
+
const kitProblem = typeof input['kit'] === 'string' ? fitProblem('the skin\'s kit', KIT_VERSION, input['kit']) : 'kit is the kit version the skin is written for, such as "1.0"';
|
|
459
|
+
if (kitProblem !== null)
|
|
460
|
+
return { ok: false, problems: [{ path: at('kit'), message: kitProblem }] };
|
|
461
|
+
const of = input['of'];
|
|
462
|
+
if (!isRecord(of) || typeof of['app'] !== 'string' || typeof of['contract'] !== 'string')
|
|
463
|
+
return { ok: false, problems: [{ path: at('of'), message: 'of is { app, contract }' }] };
|
|
464
|
+
if (of['app'] !== contract.app)
|
|
465
|
+
return { ok: false, problems: [{ path: at('of', 'app'), message: `this skin is for "${of['app']}", not "${contract.app}"` }] };
|
|
466
|
+
const fit = fitProblem('the skin\'s contract', contract.contract, of['contract']);
|
|
467
|
+
if (fit !== null)
|
|
468
|
+
return { ok: false, problems: [{ path: at('of', 'contract'), message: fit }] };
|
|
469
|
+
const files = options.files === undefined ? null : new Set(options.files);
|
|
470
|
+
if (files !== null)
|
|
471
|
+
for (const file of files)
|
|
472
|
+
if (assetType(file) === null)
|
|
473
|
+
problem(at('files'), `"${file}" is not a file a skin may carry`);
|
|
474
|
+
if (typeof input['name'] !== 'string' || input['name'].trim() === '' || input['name'].length > 60 || CONTROL_RE.test(input['name']))
|
|
475
|
+
problem(at('name'), 'name is plain text, up to 60 characters');
|
|
476
|
+
// Words of the skin's own, per language.
|
|
477
|
+
const skinWords = new Set();
|
|
478
|
+
const words = input['words'];
|
|
479
|
+
if (words !== undefined) {
|
|
480
|
+
const lang = input['defaultLanguage'];
|
|
481
|
+
if (typeof lang !== 'string' || !LANG_RE.test(lang))
|
|
482
|
+
problem(at('defaultLanguage'), 'defaultLanguage is a language tag such as "en" or "de-DE"');
|
|
483
|
+
if (!isRecord(words) || Object.keys(words).length > LIMITS.languages)
|
|
484
|
+
problem(at('words'), 'words maps languages to { key: text }');
|
|
485
|
+
else {
|
|
486
|
+
for (const [language, entries] of Object.entries(words)) {
|
|
487
|
+
if (!LANG_RE.test(language))
|
|
488
|
+
problem(at('words', language), 'is not a language tag');
|
|
489
|
+
if (!isRecord(entries) || Object.keys(entries).length > LIMITS.words) {
|
|
490
|
+
problem(at('words', language), `maps up to ${String(LIMITS.words)} keys to text`);
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
for (const [key, value] of Object.entries(entries)) {
|
|
494
|
+
if (!WORD_KEY_RE.test(key))
|
|
495
|
+
problem(at('words', language, key), 'is not a word key');
|
|
496
|
+
if (typeof value !== 'string' || value.length > LIMITS.text || CONTROL_RE.test(value))
|
|
497
|
+
problem(at('words', language, key), `is plain text up to ${String(LIMITS.text)} characters`);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
const base = typeof lang === 'string' && isRecord(words[lang]) ? words[lang] : null;
|
|
501
|
+
if (base === null)
|
|
502
|
+
problem(at('words'), 'words needs the default language');
|
|
503
|
+
else
|
|
504
|
+
for (const key of Object.keys(base))
|
|
505
|
+
skinWords.add(key);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
else if (input['defaultLanguage'] !== undefined && (typeof input['defaultLanguage'] !== 'string' || !LANG_RE.test(input['defaultLanguage'])))
|
|
509
|
+
problem(at('defaultLanguage'), 'is a language tag');
|
|
510
|
+
// Faces: a strict grammar; never a family the app owns.
|
|
511
|
+
const reserved = reservedFamilies(contract);
|
|
512
|
+
const declared = new Set();
|
|
513
|
+
const faces = input['faces'];
|
|
514
|
+
if (faces !== undefined) {
|
|
515
|
+
if (!Array.isArray(faces) || faces.length > LIMITS.faces)
|
|
516
|
+
problem(at('faces'), `faces lists up to ${String(LIMITS.faces)} faces`);
|
|
517
|
+
else
|
|
518
|
+
faces.forEach((face, i) => {
|
|
519
|
+
const here = at('faces', i);
|
|
520
|
+
if (!isRecord(face) || Object.keys(face).some((k) => !['family', 'file', 'weight', 'style', 'unicodeRange'].includes(k))) {
|
|
521
|
+
problem(here, 'a face is { family, file, weight?, style?, unicodeRange? }');
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
const family = face['family'];
|
|
525
|
+
if (typeof family === 'string' && reserved.has(familyKey(family)))
|
|
526
|
+
problem(here, `"${family}" is a family the app owns: a skin may name it but never bring its own`);
|
|
527
|
+
else if (typeof family !== 'string' || !FAMILY_RE.test(family) || family.length > 40)
|
|
528
|
+
problem(here, 'family is letters, digits and single spaces');
|
|
529
|
+
else
|
|
530
|
+
declared.add(family);
|
|
531
|
+
const file = face['file'];
|
|
532
|
+
if (typeof file !== 'string' || !file.startsWith('fonts/') || !file.endsWith('.woff2') || assetType(file) === null)
|
|
533
|
+
problem(here, 'file is a fonts/*.woff2 file of the package');
|
|
534
|
+
else if (files !== null && !files.has(file))
|
|
535
|
+
problem(here, `file "${file}" is not declared by the package`);
|
|
536
|
+
if (face['weight'] !== undefined && (typeof face['weight'] !== 'string' || !/^[1-9]00( [1-9]00)?$/.test(face['weight'])))
|
|
537
|
+
problem(here, 'weight is "400" or a range such as "200 800"');
|
|
538
|
+
if (face['style'] !== undefined && face['style'] !== 'normal' && face['style'] !== 'italic')
|
|
539
|
+
problem(here, 'style is normal or italic');
|
|
540
|
+
if (face['unicodeRange'] !== undefined && (typeof face['unicodeRange'] !== 'string' || !/^U\+[0-9A-F?]{1,6}(-[0-9A-F]{1,6})?(, ?U\+[0-9A-F?]{1,6}(-[0-9A-F]{1,6})?){0,31}$/.test(face['unicodeRange'])))
|
|
541
|
+
problem(here, 'unicodeRange is a list such as "U+0000-00FF, U+0131"');
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
// Tokens: the shared set, typed, both themes, AA.
|
|
545
|
+
const tokens = input['tokens'];
|
|
546
|
+
let resolved;
|
|
547
|
+
if (!isRecord(tokens) || !isRecord(tokens['light']) || !isRecord(tokens['dark']) || Object.keys(tokens).length !== 2)
|
|
548
|
+
problem(at('tokens'), 'tokens is { light, dark }');
|
|
549
|
+
else {
|
|
550
|
+
for (const theme of ['light', 'dark']) {
|
|
551
|
+
const set = tokens[theme];
|
|
552
|
+
for (const [token, value] of Object.entries(set)) {
|
|
553
|
+
if (!SKIN_TOKENS.includes(token)) {
|
|
554
|
+
problem(at('tokens', theme, token), 'is not a token a skin may set');
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
const issue = tokenValueProblem(token, value, declared, reserved);
|
|
558
|
+
if (issue !== null)
|
|
559
|
+
problem(at('tokens', theme, token), issue);
|
|
560
|
+
}
|
|
561
|
+
for (const token of [...COLOR_TOKENS, '--shadow-color'])
|
|
562
|
+
if (set[token] === undefined)
|
|
563
|
+
problem(at('tokens', theme), `needs ${token}`);
|
|
564
|
+
}
|
|
565
|
+
for (const token of Object.keys(tokens['dark']))
|
|
566
|
+
if (!needsBothThemes(token) && tokens['light'][token] === undefined)
|
|
567
|
+
problem(at('tokens', 'dark', token), 'is set in dark only');
|
|
568
|
+
if (problems.length === 0) {
|
|
569
|
+
resolved = resolveTokens(tokens);
|
|
570
|
+
if (options.skipAa !== true) {
|
|
571
|
+
for (const pair of aaFailures(resolved, { pairs: contract.aaPairs ?? [], derived: contract.derived ?? {} })) {
|
|
572
|
+
problem(at('tokens', pair.theme), `${pair.fg} on ${pair.on} is ${pair.ratio.toFixed(2)}:1; it needs ${String(pair.need)}:1`);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
// Slots.
|
|
578
|
+
const slots = input['slots'];
|
|
579
|
+
let total = 0;
|
|
580
|
+
if (!isRecord(slots))
|
|
581
|
+
problem(at('slots'), 'slots maps slot ids to a block');
|
|
582
|
+
else
|
|
583
|
+
for (const [slot, tree] of Object.entries(slots)) {
|
|
584
|
+
const spec = contract.slots[slot];
|
|
585
|
+
if (!SLOT_ID_RE.test(slot) || spec === undefined) {
|
|
586
|
+
problem(at(slot), `"${slot}" is not a slot of ${contract.app}`);
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
const w = { contract, slot, spec, problems, files, blocks: 0, hosts: new Map(), skinWords };
|
|
590
|
+
const scope = { props: spec.props, words: new Set(spec.words ?? []), skinWords, loops: {} };
|
|
591
|
+
walk(w, tree, [], scope, 0, false, null);
|
|
592
|
+
if (w.blocks > LIMITS.blocksPerSlot)
|
|
593
|
+
problem(at(slot), `has ${String(w.blocks)} blocks; a slot holds up to ${String(LIMITS.blocksPerSlot)}`);
|
|
594
|
+
total += w.blocks;
|
|
595
|
+
for (const group of spec.requiredHosts ?? []) {
|
|
596
|
+
if (!group.some((host) => w.hosts.has(host)))
|
|
597
|
+
problem(at(slot), `needs the host ${group.map((h) => `"${h}"`).join(' or ')}`);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
if (total > LIMITS.blocksPerPackage)
|
|
601
|
+
problem(at('slots'), `has ${String(total)} blocks; a package holds up to ${String(LIMITS.blocksPerPackage)}`);
|
|
602
|
+
// Styles: one CSS file per slot the skin fills.
|
|
603
|
+
const styles = input['styles'];
|
|
604
|
+
if (styles !== undefined) {
|
|
605
|
+
if (!isRecord(styles))
|
|
606
|
+
problem(at('styles'), 'styles maps slot ids to css files');
|
|
607
|
+
else
|
|
608
|
+
for (const [slot, file] of Object.entries(styles)) {
|
|
609
|
+
if (!isRecord(slots) || !(slot in slots))
|
|
610
|
+
problem(at('styles', slot), 'styles a slot the skin doesn\'t fill');
|
|
611
|
+
if (typeof file !== 'string' || !file.endsWith('.css') || assetType(file) === null)
|
|
612
|
+
problem(at('styles', slot), 'is a .css file of the package');
|
|
613
|
+
else if (files !== null && !files.has(file))
|
|
614
|
+
problem(at('styles', slot), `"${file}" is not declared by the package`);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
if (problems.length > 0)
|
|
618
|
+
return { ok: false, problems };
|
|
619
|
+
return { ok: true, problems: [], skin: input, ...(resolved === undefined ? {} : { tokens: resolved }) };
|
|
620
|
+
}
|
|
621
|
+
//# sourceMappingURL=validate.js.map
|