@cagov/ca-ds 1.0.0 → 1.1.0
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/README.md +4 -2
- package/THIRD-PARTY-LICENSES.txt +690 -0
- package/ca-ds.js +48337 -0
- package/package.json +5 -31
- package/bin/ca-ds.js +0 -5
- package/src/cli/add.js +0 -134
- package/src/cli/build.js +0 -15
- package/src/cli/diff.js +0 -39
- package/src/cli/eject.js +0 -112
- package/src/cli/exit.js +0 -22
- package/src/cli/format.js +0 -52
- package/src/cli/init.js +0 -148
- package/src/cli/install.js +0 -178
- package/src/cli/items.js +0 -10
- package/src/cli/login.js +0 -331
- package/src/cli/main.js +0 -108
- package/src/cli/new.js +0 -230
- package/src/cli/outdated.js +0 -74
- package/src/cli/pack.js +0 -75
- package/src/cli/preview.js +0 -31
- package/src/cli/publish.js +0 -295
- package/src/cli/pull.js +0 -208
- package/src/cli/remote.js +0 -52
- package/src/cli/remove.js +0 -69
- package/src/cli/update.js +0 -223
- package/src/cli/validate.js +0 -131
- package/src/lib/browser.js +0 -12
- package/src/lib/build.js +0 -249
- package/src/lib/bundle.js +0 -148
- package/src/lib/catalog.js +0 -44
- package/src/lib/checks.js +0 -31
- package/src/lib/collection.js +0 -65
- package/src/lib/emblem.js +0 -29
- package/src/lib/fixtures.js +0 -59
- package/src/lib/floor.js +0 -79
- package/src/lib/glyph.js +0 -148
- package/src/lib/image.js +0 -138
- package/src/lib/json-text.js +0 -213
- package/src/lib/keys.js +0 -322
- package/src/lib/links.js +0 -321
- package/src/lib/markdown.js +0 -408
- package/src/lib/merge.js +0 -93
- package/src/lib/pack.js +0 -30
- package/src/lib/page-schema.js +0 -368
- package/src/lib/page.js +0 -372
- package/src/lib/patch.js +0 -9
- package/src/lib/regions.js +0 -173
- package/src/lib/registry.js +0 -371
- package/src/lib/release.js +0 -119
- package/src/lib/repoints.js +0 -120
- package/src/lib/resolve.js +0 -32
- package/src/lib/schema.js +0 -28
- package/src/lib/serial.js +0 -4
- package/src/lib/serialize.js +0 -444
- package/src/lib/structured.js +0 -72
- package/src/lib/system.js +0 -13
- package/src/lib/targets.js +0 -85
- package/src/lib/terms.js +0 -11
- package/src/lib/token-view.js +0 -289
- package/src/lib/tokens.js +0 -247
- package/src/lib/version.js +0 -34
- package/src/preview/frame.js +0 -75
- package/src/preview/pages.js +0 -128
- package/src/preview/screenshots.js +0 -72
- package/src/preview/server.js +0 -378
- package/src/preview/shell.js +0 -153
- package/src/schema/a11y.schema.json +0 -56
- package/src/schema/collection.lock.schema.json +0 -58
- package/src/schema/collection.schema.json +0 -65
- package/src/schema/composition.schema.json +0 -98
- package/src/schema/contract.schema.json +0 -116
- package/src/schema/interface.schema.json +0 -257
- package/src/schema/item.schema.json +0 -362
- package/src/schema/release.schema.json +0 -39
- package/src/schema/system.schema.json +0 -75
- package/src/schema/variants.schema.json +0 -60
- package/src/validate/collection.js +0 -19
- package/src/validate/collisions.js +0 -124
- package/src/validate/context.js +0 -357
- package/src/validate/facts.js +0 -253
- package/src/validate/files.js +0 -103
- package/src/validate/findings.js +0 -19
- package/src/validate/glyphs.js +0 -126
- package/src/validate/html.js +0 -215
- package/src/validate/imports.js +0 -29
- package/src/validate/index.js +0 -75
- package/src/validate/item.js +0 -47
- package/src/validate/links.js +0 -113
- package/src/validate/manifest.js +0 -147
- package/src/validate/media.js +0 -65
- package/src/validate/page.js +0 -74
- package/src/validate/prefixes.js +0 -58
- package/src/validate/relations.js +0 -310
- package/src/validate/release.js +0 -109
- package/src/validate/skill.js +0 -122
- package/src/validate/structured.js +0 -335
- package/src/validate/tasc.js +0 -80
- package/src/validate/template.js +0 -129
- package/src/validate/tokens.js +0 -154
- package/src/validate/zip.js +0 -40
- /package/{src/preview/preview.css → preview.css} +0 -0
package/src/lib/image.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
// read(at, length) answers up to length bytes from offset at. A JPEG's readers walk its segments a few
|
|
2
|
-
// bytes at a time and never read the whole file.
|
|
3
|
-
|
|
4
|
-
export const HEAD = 64;
|
|
5
|
-
|
|
6
|
-
const ascii = (bytes, at, length) => String.fromCharCode(...bytes.subarray(at, at + length));
|
|
7
|
-
const uint16 = (bytes, at) => bytes[at] | (bytes[at + 1] << 8);
|
|
8
|
-
const uint16be = (bytes, at) => (bytes[at] << 8) | bytes[at + 1];
|
|
9
|
-
const uint24 = (bytes, at) => bytes[at] | (bytes[at + 1] << 8) | (bytes[at + 2] << 16);
|
|
10
|
-
|
|
11
|
-
export const fromBytes = (bytes) => (at, length) => bytes.subarray(at, at + length);
|
|
12
|
-
|
|
13
|
-
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
|
14
|
-
|
|
15
|
-
export function pngSize(bytes) {
|
|
16
|
-
if (bytes.length < 24 || PNG_SIGNATURE.some((b, i) => bytes[i] !== b) || ascii(bytes, 12, 4) !== 'IHDR') return null;
|
|
17
|
-
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
18
|
-
return { width: view.getUint32(16), height: view.getUint32(20) };
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// VP8X and VP8L store each dimension less one. "VP8 " keeps an upscaling hint in each dimension's top two bits.
|
|
22
|
-
export function webpSize(bytes) {
|
|
23
|
-
if (bytes.length < 25 || ascii(bytes, 0, 4) !== 'RIFF' || ascii(bytes, 8, 4) !== 'WEBP') return null;
|
|
24
|
-
const at = 20;
|
|
25
|
-
switch (ascii(bytes, 12, 4)) {
|
|
26
|
-
case 'VP8X':
|
|
27
|
-
if (bytes.length < at + 10) return null;
|
|
28
|
-
return { width: uint24(bytes, at + 4) + 1, height: uint24(bytes, at + 7) + 1 };
|
|
29
|
-
case 'VP8L': {
|
|
30
|
-
if (bytes[at] !== 0x2f) return null;
|
|
31
|
-
const bits = (bytes[at + 1] | (bytes[at + 2] << 8) | (bytes[at + 3] << 16) | (bytes[at + 4] << 24)) >>> 0;
|
|
32
|
-
return { width: (bits & 0x3fff) + 1, height: ((bits >>> 14) & 0x3fff) + 1 };
|
|
33
|
-
}
|
|
34
|
-
case 'VP8 ':
|
|
35
|
-
if (bytes.length < at + 10 || bytes[at + 3] !== 0x9d || bytes[at + 4] !== 0x01 || bytes[at + 5] !== 0x2a) return null;
|
|
36
|
-
return { width: uint16(bytes, at + 6) & 0x3fff, height: uint16(bytes, at + 8) & 0x3fff };
|
|
37
|
-
default:
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// SOF0 to SOF15, less C4, C8, and CC, which are DHT, JPG, and DAC and carry no size.
|
|
43
|
-
const isFrame = (marker) => marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc;
|
|
44
|
-
|
|
45
|
-
// TEM and RST0 to RST7, which carry no length.
|
|
46
|
-
const isStandalone = (marker) => marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7);
|
|
47
|
-
|
|
48
|
-
// Keeps a file of empty segments from costing a read every four bytes. A real file needs a few dozen.
|
|
49
|
-
const MAX_MARKERS = 1024;
|
|
50
|
-
|
|
51
|
-
// Yields each segment ahead of SOS as { marker, at, length, head }, head its first 9 bytes.
|
|
52
|
-
function* segments(read) {
|
|
53
|
-
const soi = read(0, 2);
|
|
54
|
-
if (soi.length < 2 || soi[0] !== 0xff || soi[1] !== 0xd8) return;
|
|
55
|
-
let at = 2;
|
|
56
|
-
for (let step = 0; step < MAX_MARKERS; step++) {
|
|
57
|
-
const head = read(at, 9);
|
|
58
|
-
if (head.length < 2 || head[0] !== 0xff) return;
|
|
59
|
-
const marker = head[1];
|
|
60
|
-
if (marker === 0xff) {
|
|
61
|
-
at += 1;
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
if (isStandalone(marker)) {
|
|
65
|
-
at += 2;
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
// Below c0 only TEM is a marker, and ff 00 is a stuffed byte inside
|
|
69
|
-
// image data, so neither starts a segment here.
|
|
70
|
-
if (marker < 0xc0 || marker === 0xd8 || marker === 0xd9 || marker === 0xda || head.length < 4) return;
|
|
71
|
-
const length = uint16be(head, 2);
|
|
72
|
-
if (length < 2) return;
|
|
73
|
-
yield { marker, at, length, head };
|
|
74
|
-
at += 2 + length;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// A height of 0 is set later by a DNL marker, so it reads as no size.
|
|
79
|
-
export function jpegSize(read) {
|
|
80
|
-
for (const { marker, length, head } of segments(read)) {
|
|
81
|
-
if (!isFrame(marker)) continue;
|
|
82
|
-
if (head.length < 9 || length < 7) return null;
|
|
83
|
-
const size = { width: uint16be(head, 7), height: uint16be(head, 5) };
|
|
84
|
-
return size.width > 0 && size.height > 0 ? size : null;
|
|
85
|
-
}
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const MAX_ENTRIES = 256;
|
|
90
|
-
|
|
91
|
-
const ORIENTATION = 0x0112;
|
|
92
|
-
|
|
93
|
-
// Orientations 5 to 8 turn the image a quarter turn, so a browser shows it at its height by its width.
|
|
94
|
-
// IFD0's entries sit in ascending tag order, so the walk stops past the tag.
|
|
95
|
-
export function jpegOrientation(read) {
|
|
96
|
-
for (const { marker, at, length } of segments(read)) {
|
|
97
|
-
if (marker !== 0xe1) continue;
|
|
98
|
-
const head = read(at + 4, 14);
|
|
99
|
-
if (head.length < 14 || ascii(head, 0, 6) !== 'Exif\0\0') continue;
|
|
100
|
-
return tiffOrientation(read, at + 10, at + 2 + length, head.subarray(6));
|
|
101
|
-
}
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function tiffOrientation(read, tiff, end, header) {
|
|
106
|
-
const order = ascii(header, 0, 2);
|
|
107
|
-
if (order !== 'II' && order !== 'MM') return null;
|
|
108
|
-
const little = order === 'II';
|
|
109
|
-
const u16 = (b, i) => (little ? uint16(b, i) : uint16be(b, i));
|
|
110
|
-
const u32 = (b, i) => (little ? (uint16(b, i) | (uint16(b, i + 2) << 16)) >>> 0 : ((uint16be(b, i) << 16) | uint16be(b, i + 2)) >>> 0);
|
|
111
|
-
if (u16(header, 2) !== 42) return null;
|
|
112
|
-
const offset = u32(header, 4);
|
|
113
|
-
if (offset < 8 || tiff + offset + 2 > end) return null;
|
|
114
|
-
const ifd = tiff + offset;
|
|
115
|
-
const count = read(ifd, 2);
|
|
116
|
-
if (count.length < 2) return null;
|
|
117
|
-
for (let i = 0; i < Math.min(u16(count, 0), MAX_ENTRIES); i++) {
|
|
118
|
-
const at = ifd + 2 + i * 12;
|
|
119
|
-
if (at + 12 > end) return null;
|
|
120
|
-
const entry = read(at, 12);
|
|
121
|
-
if (entry.length < 12) return null;
|
|
122
|
-
const tag = u16(entry, 0);
|
|
123
|
-
if (tag > ORIENTATION) return null;
|
|
124
|
-
if (tag === ORIENTATION) {
|
|
125
|
-
const value = u16(entry, 8);
|
|
126
|
-
return u16(entry, 2) === 3 && value >= 1 && value <= 8 ? value : null;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const JPEG = { format: 'JPEG', size: jpegSize, orientation: jpegOrientation };
|
|
133
|
-
export const READERS = new Map([
|
|
134
|
-
['.png', { format: 'PNG', size: (read) => pngSize(read(0, HEAD)) }],
|
|
135
|
-
['.webp', { format: 'WebP', size: (read) => webpSize(read(0, HEAD)) }],
|
|
136
|
-
['.jpg', JPEG],
|
|
137
|
-
['.jpeg', JPEG],
|
|
138
|
-
]);
|
package/src/lib/json-text.js
DELETED
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
// The repo's manifests are formatted by hand, so the style is the closest one rule comes to them.
|
|
2
|
-
|
|
3
|
-
import { isDeepStrictEqual } from 'node:util';
|
|
4
|
-
|
|
5
|
-
export const WIDTH = 100;
|
|
6
|
-
|
|
7
|
-
function flat(value) {
|
|
8
|
-
if (Array.isArray(value)) return `[${value.map(flat).join(', ')}]`;
|
|
9
|
-
if (value && typeof value === 'object') return `{${Object.entries(value).map(([k, v]) => `${JSON.stringify(k)}: ${flat(v)}`).join(', ')}}`;
|
|
10
|
-
return JSON.stringify(value);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// lead and tail are the characters already on the line before and after the value, such as its key and comma.
|
|
14
|
-
function text(value, indent, lead, tail) {
|
|
15
|
-
const one = flat(value);
|
|
16
|
-
if (value === null || typeof value !== 'object' || indent + lead + one.length + tail <= WIDTH) return one;
|
|
17
|
-
const entries = Array.isArray(value) ? value.map((v) => [null, v]) : Object.entries(value);
|
|
18
|
-
if (entries.length === 0) return one;
|
|
19
|
-
return spread(entries, Array.isArray(value), indent);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function spread(entries, array, indent) {
|
|
23
|
-
const pad = ' '.repeat(indent + 2);
|
|
24
|
-
const lines = entries.map(([k, v], i) => {
|
|
25
|
-
const key = k === null ? '' : `${JSON.stringify(k)}: `;
|
|
26
|
-
return `${pad}${key}${text(v, indent + 2, key.length, i < entries.length - 1 ? 1 : 0)}`;
|
|
27
|
-
});
|
|
28
|
-
return `${array ? '[' : '{'}\n${lines.join(',\n')}\n${' '.repeat(indent)}${array ? ']' : '}'}`;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function formatJson(value) {
|
|
32
|
-
const entries = Object.entries(value);
|
|
33
|
-
return `${entries.length ? spread(entries, false, 0) : '{}'}\n`;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function members(text) {
|
|
37
|
-
if (!text.startsWith('{\n') || !text.endsWith('\n}\n')) return null;
|
|
38
|
-
const inner = text.slice(1, -3);
|
|
39
|
-
const chunks = [];
|
|
40
|
-
let depth = 0;
|
|
41
|
-
let quoted = false;
|
|
42
|
-
let start = 0;
|
|
43
|
-
for (let i = 0; i < inner.length; i += 1) {
|
|
44
|
-
const ch = inner[i];
|
|
45
|
-
if (quoted) {
|
|
46
|
-
if (ch === '\\') i += 1;
|
|
47
|
-
else if (ch === '"') quoted = false;
|
|
48
|
-
} else if (ch === '"') quoted = true;
|
|
49
|
-
else if (ch === '{' || ch === '[') depth += 1;
|
|
50
|
-
else if (ch === '}' || ch === ']') depth -= 1;
|
|
51
|
-
else if (ch === ',' && depth === 0) {
|
|
52
|
-
chunks.push(inner.slice(start, i));
|
|
53
|
-
start = i + 1;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
chunks.push(inner.slice(start));
|
|
57
|
-
const out = [];
|
|
58
|
-
for (const chunk of chunks) {
|
|
59
|
-
const m = /^\n {2}("(?:[^"\\]|\\.)*"): ([\s\S]*)$/.exec(chunk);
|
|
60
|
-
if (!m) return null;
|
|
61
|
-
try {
|
|
62
|
-
out.push({ key: JSON.parse(m[1]), value: JSON.parse(m[2]), chunk });
|
|
63
|
-
} catch {
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return out;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Keeps before's bytes for each unchanged top-level member.
|
|
71
|
-
export function rewrittenJson(before, value) {
|
|
72
|
-
const was = typeof before === 'string' ? members(before) : null;
|
|
73
|
-
if (!was || `{${was.map((m) => m.chunk).join(',')}\n}\n` !== before) return formatJson(value);
|
|
74
|
-
const kept = new Map(was.map((m) => [m.key, m]));
|
|
75
|
-
const entries = Object.entries(value);
|
|
76
|
-
if (entries.length === 0) return '{}\n';
|
|
77
|
-
const chunks = entries.map(([key, v], i) => {
|
|
78
|
-
const old = kept.get(key);
|
|
79
|
-
if (old && isDeepStrictEqual(old.value, v)) return old.chunk;
|
|
80
|
-
const lead = `${JSON.stringify(key)}: `;
|
|
81
|
-
return `\n ${lead}${text(v, 2, lead.length, i < entries.length - 1 ? 1 : 0)}`;
|
|
82
|
-
});
|
|
83
|
-
return `{${chunks.join(',')}\n}\n`;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Each node's spread is true when its brackets hold a line break; indent and close are columns.
|
|
87
|
-
function spans(text) {
|
|
88
|
-
let i = 0;
|
|
89
|
-
const fail = () => {
|
|
90
|
-
throw new SyntaxError(`The JSON text breaks at offset ${i}.`);
|
|
91
|
-
};
|
|
92
|
-
const space = () => {
|
|
93
|
-
while (i < text.length && ' \t\n\r'.includes(text[i])) i += 1;
|
|
94
|
-
};
|
|
95
|
-
const column = (at) => at - (text.lastIndexOf('\n', at - 1) + 1);
|
|
96
|
-
const string = () => {
|
|
97
|
-
const start = i;
|
|
98
|
-
i += 1;
|
|
99
|
-
while (i < text.length && text[i] !== '"') i += text[i] === '\\' ? 2 : 1;
|
|
100
|
-
if (i >= text.length) fail();
|
|
101
|
-
i += 1;
|
|
102
|
-
return JSON.parse(text.slice(start, i));
|
|
103
|
-
};
|
|
104
|
-
const value = () => {
|
|
105
|
-
space();
|
|
106
|
-
const start = i;
|
|
107
|
-
const ch = text[i];
|
|
108
|
-
if (ch === '{' || ch === '[') {
|
|
109
|
-
const array = ch === '[';
|
|
110
|
-
i += 1;
|
|
111
|
-
const entries = [];
|
|
112
|
-
space();
|
|
113
|
-
if (text[i] === (array ? ']' : '}')) {
|
|
114
|
-
i += 1;
|
|
115
|
-
return { value: array ? [] : {}, start, end: i, kind: array ? 'array' : 'object', entries, spread: text.slice(start, i).includes('\n'), indent: 0, close: column(i - 1) };
|
|
116
|
-
}
|
|
117
|
-
let indent = null;
|
|
118
|
-
for (;;) {
|
|
119
|
-
space();
|
|
120
|
-
if (indent === null) indent = column(i);
|
|
121
|
-
let key = null;
|
|
122
|
-
if (!array) {
|
|
123
|
-
if (text[i] !== '"') fail();
|
|
124
|
-
key = string();
|
|
125
|
-
space();
|
|
126
|
-
if (text[i] !== ':') fail();
|
|
127
|
-
i += 1;
|
|
128
|
-
}
|
|
129
|
-
entries.push({ key, node: value() });
|
|
130
|
-
space();
|
|
131
|
-
if (text[i] === ',') {
|
|
132
|
-
i += 1;
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
if (text[i] !== (array ? ']' : '}')) fail();
|
|
136
|
-
i += 1;
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
const out = array ? entries.map((e) => e.node.value) : Object.fromEntries(entries.map((e) => [e.key, e.node.value]));
|
|
140
|
-
return { value: out, start, end: i, kind: array ? 'array' : 'object', entries, spread: text.slice(start, i).includes('\n'), indent, close: column(i - 1) };
|
|
141
|
-
}
|
|
142
|
-
if (ch === '"') return { value: string(), start, end: i, kind: 'scalar' };
|
|
143
|
-
const m = /^(?:true|false|null|-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)/.exec(text.slice(i, i + 64));
|
|
144
|
-
if (!m) fail();
|
|
145
|
-
i += m[0].length;
|
|
146
|
-
return { value: JSON.parse(m[0]), start, end: i, kind: 'scalar' };
|
|
147
|
-
};
|
|
148
|
-
const root = value();
|
|
149
|
-
space();
|
|
150
|
-
if (i !== text.length) fail();
|
|
151
|
-
return root;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const kindOf = (value) => (Array.isArray(value) ? 'array' : value !== null && typeof value === 'object' ? 'object' : 'scalar');
|
|
155
|
-
|
|
156
|
-
const IDENTITY = ['id', 'name', 'attribute'];
|
|
157
|
-
const identityOf = (value) => (kindOf(value) === 'object' ? (IDENTITY.map((k) => value[k]).find((v) => typeof v === 'string') ?? null) : null);
|
|
158
|
-
|
|
159
|
-
// Matches by equal value, then by record name, then by index, so an edit inside one record keeps the rest of it.
|
|
160
|
-
function matchElements(entries, value) {
|
|
161
|
-
const used = new Set();
|
|
162
|
-
const take = (at) => {
|
|
163
|
-
if (at === -1 || used.has(at)) return null;
|
|
164
|
-
used.add(at);
|
|
165
|
-
return entries[at].node;
|
|
166
|
-
};
|
|
167
|
-
const out = value.map((v) => take(entries.findIndex((e, j) => !used.has(j) && isDeepStrictEqual(e.node.value, v))));
|
|
168
|
-
for (const [i, v] of value.entries()) {
|
|
169
|
-
const name = identityOf(v);
|
|
170
|
-
if (!out[i] && name !== null) out[i] = take(entries.findIndex((e, j) => !used.has(j) && identityOf(e.node.value) === name));
|
|
171
|
-
}
|
|
172
|
-
return out.map((node, i) => node ?? (i < entries.length ? take(i) : null));
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
function spreadText(value, indent) {
|
|
176
|
-
const entries = Array.isArray(value) ? value.map((v) => [null, v]) : Object.entries(value);
|
|
177
|
-
return entries.length ? spread(entries, Array.isArray(value), indent) : flat(value);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function edited(source, node, value, indent, lead, tail) {
|
|
181
|
-
if (node && isDeepStrictEqual(node.value, value)) return source.slice(node.start, node.end);
|
|
182
|
-
const kind = kindOf(value);
|
|
183
|
-
if (node && node.kind === kind && kind !== 'scalar' && !node.spread && node.entries.length > 0) return flat(value);
|
|
184
|
-
if (!node || node.kind !== kind || kind === 'scalar' || !node.spread || node.entries.length === 0) return text(value, indent, lead, tail);
|
|
185
|
-
const entries = kind === 'array' ? value.map((v) => [null, v]) : Object.entries(value);
|
|
186
|
-
if (entries.length === 0) return text(value, indent, lead, tail);
|
|
187
|
-
const originals = kind === 'array' ? matchElements(node.entries, value) : entries.map(([k]) => node.entries.find((e) => e.key === k)?.node ?? null);
|
|
188
|
-
const pad = ' '.repeat(node.indent);
|
|
189
|
-
// A record added to a list of spread records is spread too.
|
|
190
|
-
const sibling = (v) => node.entries.find((e) => e.node.kind === kindOf(v) && e.node.kind !== 'scalar')?.node ?? null;
|
|
191
|
-
const lines = entries.map(([k, v], i) => {
|
|
192
|
-
const key = k === null ? '' : `${JSON.stringify(k)}: `;
|
|
193
|
-
const original = originals[i];
|
|
194
|
-
const like = original ? null : sibling(v);
|
|
195
|
-
const body = like?.spread ? spreadText(v, node.indent) : edited(source, original, v, node.indent, key.length, i < entries.length - 1 ? 1 : 0);
|
|
196
|
-
return `${pad}${key}${body}`;
|
|
197
|
-
});
|
|
198
|
-
return `${kind === 'array' ? '[' : '{'}\n${lines.join(',\n')}\n${' '.repeat(node.close)}${kind === 'array' ? ']' : '}'}`;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// Keeps every unchanged value's bytes at any depth, and each array's or object's layout, so a one-field edit diffs as one line.
|
|
202
|
-
export function editedJson(before, value) {
|
|
203
|
-
let root;
|
|
204
|
-
try {
|
|
205
|
-
root = typeof before === 'string' ? spans(before) : null;
|
|
206
|
-
} catch {
|
|
207
|
-
root = null;
|
|
208
|
-
}
|
|
209
|
-
if (!root || root.kind !== 'object' || kindOf(value) !== 'object') return formatJson(value);
|
|
210
|
-
return `${before.slice(0, root.start)}${edited(before, root, value, 0, 0, 0)}${before.slice(root.end)}`;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export const withVersion = (text, version) => editedJson(text, { ...JSON.parse(text), version });
|
package/src/lib/keys.js
DELETED
|
@@ -1,322 +0,0 @@
|
|
|
1
|
-
// The content repo's standard/components/README.md § *Parts*, § *Remixes*, and § *Collisions* own the rules.
|
|
2
|
-
// A name is { kind: element|attribute|value|slot|class, element, name, value }. Knobs, markers, and events
|
|
3
|
-
// are no names: any rule may read a knob, the platform gives a marker its meaning, and an event selects nothing.
|
|
4
|
-
|
|
5
|
-
const UNWRAP = new Set(['is', 'where', 'matches', 'any']);
|
|
6
|
-
|
|
7
|
-
// Keeps a selector with many nested lists cheap. No reference stylesheet comes near it.
|
|
8
|
-
const MAX_ALTERNATIVES = 512;
|
|
9
|
-
|
|
10
|
-
export const nameId = ({ kind, element, name, value }) => `${kind}\0${element ?? ''}\0${name}\0${value ?? ''}`;
|
|
11
|
-
|
|
12
|
-
const literal = (v) => (typeof v === 'string' ? v : typeof v?.value === 'string' ? v.value : null);
|
|
13
|
-
|
|
14
|
-
export const ownedTags = (iface) => Object.entries(iface?.elements ?? {}).filter(([, el]) => !el.extends).map(([tag]) => tag);
|
|
15
|
-
|
|
16
|
-
export const extensionRecords = (iface) =>
|
|
17
|
-
Object.entries(iface?.elements ?? {})
|
|
18
|
-
.filter(([, el]) => typeof el.extends === 'string')
|
|
19
|
-
.map(([tag, record]) => ({ tag, record }));
|
|
20
|
-
|
|
21
|
-
// An extension's attribute the owner already declares adds its values alone.
|
|
22
|
-
export function addedNames(interfaces, ownerAttribute = () => null) {
|
|
23
|
-
const out = new Map();
|
|
24
|
-
const add = (n) => out.set(nameId(n), n);
|
|
25
|
-
for (const iface of interfaces) {
|
|
26
|
-
for (const c of iface.classes ?? []) add({ kind: 'class', element: null, name: c.name.slice(1) });
|
|
27
|
-
for (const [tag, el] of Object.entries(iface.elements ?? {})) {
|
|
28
|
-
const extended = typeof el.extends === 'string';
|
|
29
|
-
if (!extended) add({ kind: 'element', element: tag, name: tag });
|
|
30
|
-
for (const a of el.attributes ?? []) {
|
|
31
|
-
const declared = extended && ownerAttribute(el.extends, tag, a.name);
|
|
32
|
-
if (!declared) add({ kind: 'attribute', element: tag, name: a.name });
|
|
33
|
-
if (declared || !extended) {
|
|
34
|
-
for (const v of a.values) {
|
|
35
|
-
const value = literal(v);
|
|
36
|
-
if (value !== null) add({ kind: 'value', element: tag, name: a.name, value });
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
for (const child of el.children ?? []) {
|
|
41
|
-
if (typeof child.slot === 'string') add({ kind: 'slot', element: tag, name: child.slot });
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return [...out.values()];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function compounds(selector) {
|
|
49
|
-
const out = [];
|
|
50
|
-
let parts = [];
|
|
51
|
-
for (const c of selector) {
|
|
52
|
-
if (c.type === 'combinator') {
|
|
53
|
-
out.push({ parts, combinator: c.value });
|
|
54
|
-
parts = [];
|
|
55
|
-
} else {
|
|
56
|
-
parts.push(c);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
out.push({ parts, combinator: null });
|
|
60
|
-
return out;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function expandCompound(parts) {
|
|
64
|
-
const at = parts.findIndex((c) => c.type === 'pseudo-class' && UNWRAP.has(c.kind) && Array.isArray(c.selectors));
|
|
65
|
-
if (at < 0) return [[{ parts, combinator: null }]];
|
|
66
|
-
const rest = [...parts.slice(0, at), ...parts.slice(at + 1)];
|
|
67
|
-
const out = [];
|
|
68
|
-
for (const alternative of parts[at].selectors) {
|
|
69
|
-
for (const sequence of expandSelector(alternative)) {
|
|
70
|
-
const last = sequence.at(-1);
|
|
71
|
-
for (const tail of expandCompound([...rest, ...last.parts])) {
|
|
72
|
-
out.push([...sequence.slice(0, -1), ...tail]);
|
|
73
|
-
if (out.length >= MAX_ALTERNATIVES) return out;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return out;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// :not() and :has() stay wrapped, since neither names the element.
|
|
81
|
-
export function expandSelector(selector) {
|
|
82
|
-
let results = [[]];
|
|
83
|
-
for (const { parts, combinator } of compounds(selector)) {
|
|
84
|
-
const next = [];
|
|
85
|
-
for (const head of results) {
|
|
86
|
-
for (const option of expandCompound(parts)) {
|
|
87
|
-
next.push([...head, ...option.slice(0, -1), { ...option.at(-1), combinator }]);
|
|
88
|
-
if (next.length >= MAX_ALTERNATIVES) break;
|
|
89
|
-
}
|
|
90
|
-
if (next.length >= MAX_ALTERNATIVES) break;
|
|
91
|
-
}
|
|
92
|
-
results = next;
|
|
93
|
-
}
|
|
94
|
-
return results.map(positioned);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// An ancestor's sibling is neither sibling nor ancestor, so it reads as other.
|
|
98
|
-
function positioned(sequence) {
|
|
99
|
-
const out = sequence.map((c) => ({ ...c, position: null }));
|
|
100
|
-
for (let i = out.length - 1; i >= 0; i--) {
|
|
101
|
-
if (i === out.length - 1) out[i].position = 'subject';
|
|
102
|
-
else if (out[i].combinator === 'descendant' || out[i].combinator === 'child') out[i].position = 'ancestor';
|
|
103
|
-
else if (out[i].combinator === 'next-sibling' || out[i].combinator === 'later-sibling') out[i].position = out[i + 1].position === 'ancestor' ? 'other' : 'sibling';
|
|
104
|
-
else out[i].position = 'other';
|
|
105
|
-
}
|
|
106
|
-
return out;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export const subjects = (selector) => expandSelector(selector).map((alternative) => alternative.at(-1).parts);
|
|
110
|
-
|
|
111
|
-
export function compoundNames(parts, n) {
|
|
112
|
-
const typed = (tag) => parts.some((c) => c.type === 'type' && c.name === tag);
|
|
113
|
-
const attributes = parts.filter((c) => c.type === 'attribute');
|
|
114
|
-
const equals = (a, value) => ['equal', 'includes'].includes(a.operation?.operator) && a.operation.value === value;
|
|
115
|
-
switch (n.kind) {
|
|
116
|
-
case 'element':
|
|
117
|
-
return typed(n.name);
|
|
118
|
-
case 'attribute':
|
|
119
|
-
return typed(n.element) && attributes.some((a) => a.name === n.name);
|
|
120
|
-
case 'value':
|
|
121
|
-
return typed(n.element) && attributes.some((a) => a.name === n.name && equals(a, n.value));
|
|
122
|
-
case 'slot':
|
|
123
|
-
return attributes.some((a) => a.name === 'slot' && a.operation?.operator === 'equal' && a.operation.value === n.name);
|
|
124
|
-
case 'class':
|
|
125
|
-
return parts.some((c) => c.type === 'class' && c.name === n.name);
|
|
126
|
-
default:
|
|
127
|
-
return false;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// A sibling names no scope, since the rule it opens styles an element outside it.
|
|
132
|
-
export function selectorNames(selector, names) {
|
|
133
|
-
if (names.length === 0) return false;
|
|
134
|
-
return expandSelector(selector).every((alternative) =>
|
|
135
|
-
alternative.some((c) => (c.position === 'subject' || c.position === 'ancestor') && names.some((n) => compoundNames(c.parts, n))),
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// A compound with no key, such as * or a bare [hidden], is compared with nothing.
|
|
140
|
-
export function subjectKeys(parts) {
|
|
141
|
-
const pseudo = parts.filter((c) => c.type === 'pseudo-element').map((c) => `::${c.kind}`).join('');
|
|
142
|
-
const custom = parts.filter((c) => c.type === 'type' && c.name.startsWith('ca-')).map((c) => c.name);
|
|
143
|
-
if (custom.length) return custom.map((t) => `${t}${pseudo}`);
|
|
144
|
-
const named = [
|
|
145
|
-
...parts.filter((c) => c.type === 'attribute' && c.name === 'slot' && c.operation?.operator === 'equal').map((c) => `[slot="${c.operation.value}"]`),
|
|
146
|
-
...parts.filter((c) => c.type === 'class').map((c) => `.${c.name}`),
|
|
147
|
-
];
|
|
148
|
-
if (named.length) return named.map((k) => `${k}${pseudo}`);
|
|
149
|
-
return parts.filter((c) => c.type === 'type').map((c) => `${c.name}${pseudo}`);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const NAMED_KEY = /^(ca-|\[|\.)/;
|
|
153
|
-
|
|
154
|
-
// A native subject's key is led by its regions, as `ca-masthead > [slot="masthead-utility"] ul`.
|
|
155
|
-
export function scopedSubjectKeys(selector) {
|
|
156
|
-
const out = new Set();
|
|
157
|
-
for (const alternative of expandSelector(selector)) {
|
|
158
|
-
const keys = subjectKeys(alternative.at(-1).parts);
|
|
159
|
-
if (keys.length === 0 || keys.some((k) => NAMED_KEY.test(k))) {
|
|
160
|
-
for (const k of keys) out.add(k);
|
|
161
|
-
continue;
|
|
162
|
-
}
|
|
163
|
-
const regions = new Set();
|
|
164
|
-
for (let i = 0; i < alternative.length - 2; i++) {
|
|
165
|
-
if (alternative[i].position !== 'ancestor' || alternative[i].combinator !== 'child') continue;
|
|
166
|
-
const hosts = alternative[i].parts.filter((c) => c.type === 'type' && c.name.startsWith('ca-')).map((c) => c.name);
|
|
167
|
-
const child = alternative[i + 1].parts;
|
|
168
|
-
const children = [
|
|
169
|
-
...child.filter((c) => c.type === 'attribute' && c.name === 'slot' && c.operation?.operator === 'equal').map((c) => `[slot="${c.operation.value}"]`),
|
|
170
|
-
...child.filter((c) => c.type === 'type' && c.name.startsWith('ca-')).map((c) => c.name),
|
|
171
|
-
];
|
|
172
|
-
for (const h of hosts) for (const c of children) regions.add(`${h} > ${c}`);
|
|
173
|
-
}
|
|
174
|
-
const lead = [...regions].sort().join(' ');
|
|
175
|
-
for (const k of keys) out.add(lead ? `${lead} ${k}` : k);
|
|
176
|
-
}
|
|
177
|
-
return [...out];
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function regionsOf(key) {
|
|
181
|
-
const out = new Map();
|
|
182
|
-
for (const [, host, child] of key.matchAll(/(ca-[a-z0-9-]+) > (\S+)/g)) {
|
|
183
|
-
if (!out.has(host)) out.set(host, new Set());
|
|
184
|
-
out.get(host).add(child);
|
|
185
|
-
}
|
|
186
|
-
return out;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Native keys in two different children of one <ca-*> element never meet, since those are siblings.
|
|
190
|
-
export function keysMeet(a, b) {
|
|
191
|
-
if (a === b) return true;
|
|
192
|
-
const [na, nb] = [a.split(' ').at(-1), b.split(' ').at(-1)];
|
|
193
|
-
if (NAMED_KEY.test(na) || NAMED_KEY.test(nb) || na !== nb) return false;
|
|
194
|
-
const [ra, rb] = [regionsOf(a), regionsOf(b)];
|
|
195
|
-
for (const [host, children] of ra) {
|
|
196
|
-
const other = rb.get(host);
|
|
197
|
-
if (other && ![...children].some((c) => other.has(c))) return false;
|
|
198
|
-
}
|
|
199
|
-
return true;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// lookup(name) answers { manifest, interfaces } or null. An item inherits the keys of the remixes up its
|
|
203
|
-
// partOf chain and of a base that is itself a remix.
|
|
204
|
-
export function keyGraph(lookup) {
|
|
205
|
-
const chain = (field) => (name) => {
|
|
206
|
-
const out = [];
|
|
207
|
-
let at = lookup(name)?.manifest?.[field];
|
|
208
|
-
while (typeof at === 'string' && at !== name && !out.includes(at)) {
|
|
209
|
-
out.push(at);
|
|
210
|
-
at = lookup(at)?.manifest?.[field];
|
|
211
|
-
}
|
|
212
|
-
return out;
|
|
213
|
-
};
|
|
214
|
-
const partChain = chain('partOf');
|
|
215
|
-
const remixChain = chain('remixOf');
|
|
216
|
-
const isRemix = (name) => typeof lookup(name)?.manifest?.remixOf === 'string';
|
|
217
|
-
const ownerAttribute = (owner, tag, attribute) => {
|
|
218
|
-
for (const iface of lookup(owner)?.interfaces ?? []) {
|
|
219
|
-
const el = iface.elements?.[tag];
|
|
220
|
-
if (el && !el.extends) return (el.attributes ?? []).find((a) => a.name === attribute) ?? null;
|
|
221
|
-
}
|
|
222
|
-
return null;
|
|
223
|
-
};
|
|
224
|
-
const owns = new Map();
|
|
225
|
-
const own = (name) => {
|
|
226
|
-
if (!owns.has(name)) owns.set(name, addedNames(lookup(name)?.interfaces ?? [], ownerAttribute));
|
|
227
|
-
return owns.get(name);
|
|
228
|
-
};
|
|
229
|
-
const keys = new Map();
|
|
230
|
-
const inherits = new Map();
|
|
231
|
-
const inherited = (name) => {
|
|
232
|
-
if (inherits.has(name)) return inherits.get(name);
|
|
233
|
-
// A cycle answers what it has gathered so far.
|
|
234
|
-
inherits.set(name, new Map());
|
|
235
|
-
const out = new Map();
|
|
236
|
-
const from = [...partChain(name).filter(isRemix)];
|
|
237
|
-
const base = lookup(name)?.manifest?.remixOf;
|
|
238
|
-
if (typeof base === 'string' && base !== name && isRemix(base)) from.push(base);
|
|
239
|
-
for (const above of from) for (const n of key(above)) out.set(nameId(n), n);
|
|
240
|
-
inherits.set(name, out);
|
|
241
|
-
return out;
|
|
242
|
-
};
|
|
243
|
-
const key = (name) => {
|
|
244
|
-
if (!keys.has(name)) {
|
|
245
|
-
const out = new Map(own(name).map((n) => [nameId(n), n]));
|
|
246
|
-
for (const [id, n] of inherited(name)) out.set(id, n);
|
|
247
|
-
keys.set(name, [...out.values()]);
|
|
248
|
-
}
|
|
249
|
-
return keys.get(name);
|
|
250
|
-
};
|
|
251
|
-
const unresolved = (name) => {
|
|
252
|
-
const base = lookup(name)?.manifest?.remixOf;
|
|
253
|
-
return [...new Set([...partChain(name), ...(typeof base === 'string' ? [base] : [])])].filter((n) => n !== name && !lookup(n)).sort();
|
|
254
|
-
};
|
|
255
|
-
return { own, key, inherited, unresolved, partChain, remixChain, isRemix, ownerAttribute };
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// An element's identity takes no key, since a page defines a custom element once.
|
|
259
|
-
export function collisionId(graph, name, n) {
|
|
260
|
-
if (n.kind === 'element') return nameId(n);
|
|
261
|
-
return `${nameId(n)}\0\0${[...graph.inherited(name).keys()].sort().join('\0\0')}\0\0${graph.unresolved(name).join('\0')}`;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
export function describeName(n) {
|
|
265
|
-
if (n.kind === 'element') return `\`<${n.name}>\``;
|
|
266
|
-
if (n.kind === 'attribute') return `the attribute \`${n.name}\` of \`<${n.element}>\``;
|
|
267
|
-
if (n.kind === 'value') return `a value of \`${n.name}\` on \`<${n.element}>\``;
|
|
268
|
-
if (n.kind === 'slot') return `the slot \`${n.name}\` of \`<${n.element}>\``;
|
|
269
|
-
return `the class \`.${n.name}\``;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// Two items that add one element collide on it once, not again on each name they add to it.
|
|
273
|
-
export function setCollisions(graph, names) {
|
|
274
|
-
const index = new Map();
|
|
275
|
-
const out = [];
|
|
276
|
-
const elements = new Set();
|
|
277
|
-
for (const name of names) {
|
|
278
|
-
for (const n of graph.own(name)) {
|
|
279
|
-
const id = collisionId(graph, name, n);
|
|
280
|
-
const other = index.get(id);
|
|
281
|
-
if (other === undefined) {
|
|
282
|
-
index.set(id, name);
|
|
283
|
-
continue;
|
|
284
|
-
}
|
|
285
|
-
if (other === name) continue;
|
|
286
|
-
if (n.kind === 'element') elements.add(`${other}\0${name}\0${n.name}`);
|
|
287
|
-
else if (elements.has(`${other}\0${name}\0${n.element}`)) continue;
|
|
288
|
-
out.push({ items: [other, name], name: n });
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
return out;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
export const collisionMessage = ({ items: [a, b], name }) =>
|
|
295
|
-
`\`${a}\` and \`${b}\` both add ${describeName(name)}${name.kind === 'element' ? '' : ' under the same borrowed key'}, so the two conflict when both load.`;
|
|
296
|
-
|
|
297
|
-
export function elementCarries(el, n) {
|
|
298
|
-
const attribs = el?.attribs ?? {};
|
|
299
|
-
const has = (a) => Object.hasOwn(attribs, a);
|
|
300
|
-
switch (n.kind) {
|
|
301
|
-
case 'element':
|
|
302
|
-
return el.name === n.name;
|
|
303
|
-
case 'attribute':
|
|
304
|
-
return el.name === n.element && has(n.name);
|
|
305
|
-
case 'value':
|
|
306
|
-
return el.name === n.element && has(n.name) && (attribs[n.name] === n.value || attribs[n.name].split(/\s+/).includes(n.value));
|
|
307
|
-
case 'slot':
|
|
308
|
-
return attribs.slot === n.name;
|
|
309
|
-
case 'class':
|
|
310
|
-
return (attribs.class ?? '').split(/\s+/).includes(n.name);
|
|
311
|
-
default:
|
|
312
|
-
return false;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
export function withinKey(el, names) {
|
|
317
|
-
if (names.length === 0) return false;
|
|
318
|
-
for (let at = el; at; at = at.parent) {
|
|
319
|
-
if (typeof at.name === 'string' && at.attribs && names.some((n) => elementCarries(at, n))) return true;
|
|
320
|
-
}
|
|
321
|
-
return false;
|
|
322
|
-
}
|