@tiavision/sinter-mail 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +137 -2
- package/docs/mail.md +267 -0
- package/docs/proposals/progressive-rendering.md +180 -0
- package/docs/proposals/sinter-mail.md +520 -0
- package/llms.txt +20 -0
- package/package.json +65 -4
- package/sinter/autolink.ts +98 -0
- package/sinter/components/internal/mail-a.html +5 -0
- package/sinter/components/internal/mail-abbr.html +5 -0
- package/sinter/components/internal/mail-address.html +5 -0
- package/sinter/components/internal/mail-article.html +5 -0
- package/sinter/components/internal/mail-aside.html +5 -0
- package/sinter/components/internal/mail-b.html +5 -0
- package/sinter/components/internal/mail-bdi.html +5 -0
- package/sinter/components/internal/mail-bdo.html +5 -0
- package/sinter/components/internal/mail-blockquote.html +5 -0
- package/sinter/components/internal/mail-br.html +5 -0
- package/sinter/components/internal/mail-center.html +5 -0
- package/sinter/components/internal/mail-cite.html +5 -0
- package/sinter/components/internal/mail-code.html +5 -0
- package/sinter/components/internal/mail-dd.html +5 -0
- package/sinter/components/internal/mail-del.html +5 -0
- package/sinter/components/internal/mail-details.html +5 -0
- package/sinter/components/internal/mail-dfn.html +5 -0
- package/sinter/components/internal/mail-div.html +5 -0
- package/sinter/components/internal/mail-dl.html +5 -0
- package/sinter/components/internal/mail-dt.html +5 -0
- package/sinter/components/internal/mail-em.html +5 -0
- package/sinter/components/internal/mail-figcaption.html +5 -0
- package/sinter/components/internal/mail-figure.html +5 -0
- package/sinter/components/internal/mail-font.html +5 -0
- package/sinter/components/internal/mail-footer.html +5 -0
- package/sinter/components/internal/mail-h1.html +5 -0
- package/sinter/components/internal/mail-h2.html +5 -0
- package/sinter/components/internal/mail-h3.html +5 -0
- package/sinter/components/internal/mail-h4.html +5 -0
- package/sinter/components/internal/mail-h5.html +5 -0
- package/sinter/components/internal/mail-h6.html +5 -0
- package/sinter/components/internal/mail-header.html +5 -0
- package/sinter/components/internal/mail-hr.html +5 -0
- package/sinter/components/internal/mail-i.html +5 -0
- package/sinter/components/internal/mail-img.html +5 -0
- package/sinter/components/internal/mail-ins.html +5 -0
- package/sinter/components/internal/mail-kbd.html +5 -0
- package/sinter/components/internal/mail-li.html +5 -0
- package/sinter/components/internal/mail-main.html +5 -0
- package/sinter/components/internal/mail-mark.html +5 -0
- package/sinter/components/internal/mail-nav.html +5 -0
- package/sinter/components/internal/mail-node.html +5 -0
- package/sinter/components/internal/mail-ol.html +5 -0
- package/sinter/components/internal/mail-orphan.html +5 -0
- package/sinter/components/internal/mail-p.html +5 -0
- package/sinter/components/internal/mail-pre.html +5 -0
- package/sinter/components/internal/mail-q.html +5 -0
- package/sinter/components/internal/mail-s.html +5 -0
- package/sinter/components/internal/mail-samp.html +5 -0
- package/sinter/components/internal/mail-section.html +5 -0
- package/sinter/components/internal/mail-small.html +5 -0
- package/sinter/components/internal/mail-span.html +5 -0
- package/sinter/components/internal/mail-steps.html +5 -0
- package/sinter/components/internal/mail-steps.ts +46 -0
- package/sinter/components/internal/mail-strike.html +5 -0
- package/sinter/components/internal/mail-strong.html +5 -0
- package/sinter/components/internal/mail-sub.html +5 -0
- package/sinter/components/internal/mail-summary.html +5 -0
- package/sinter/components/internal/mail-sup.html +5 -0
- package/sinter/components/internal/mail-table.html +5 -0
- package/sinter/components/internal/mail-time.html +5 -0
- package/sinter/components/internal/mail-tt.html +5 -0
- package/sinter/components/internal/mail-u.html +5 -0
- package/sinter/components/internal/mail-ul.html +5 -0
- package/sinter/components/internal/mail-var.html +5 -0
- package/sinter/components/internal/mail-wbr.html +5 -0
- package/sinter/components/mail-body.html +21 -0
- package/sinter/css/hints.ts +259 -0
- package/sinter/css/index.ts +270 -0
- package/sinter/css/media.ts +251 -0
- package/sinter/css/parse.ts +124 -0
- package/sinter/css/selectors.ts +530 -0
- package/sinter/css/tokenize.ts +279 -0
- package/sinter/css/values.ts +647 -0
- package/sinter/engine.ts +6 -0
- package/sinter/index.ts +5 -0
- package/sinter/parse.ts +355 -0
- package/sinter/parser/entities.ts +22 -0
- package/sinter/parser/index.ts +54 -0
- package/sinter/parser/tokenizer.ts +275 -0
- package/sinter/parser/tree.ts +1265 -0
- package/sinter/plain.ts +125 -0
- package/sinter/raw.ts +28 -0
- package/sinter/sinter.helpers.ts +11 -0
- package/sinter/sinter.types.ts +135 -0
- package/sinter/tags.ts +169 -0
- package/sinter/tree.ts +168 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
// Media queries (Media Queries Level 4), evaluated once, at parse time, for one of two fixed screens:
|
|
2
|
+
// 'wide' is an 800 × 600 px landscape viewport, 'narrow' a 375 × 667 px portrait one, both at 1 dppx with
|
|
3
|
+
// a 16 px root font, light, colour, no hover information. There is no stylesheet to re-evaluate them
|
|
4
|
+
// later, so a query is a constant. Anything this does not know (a feature, a unit, a malformed query) is
|
|
5
|
+
// false, as a browser treats a query it cannot parse as `not all`.
|
|
6
|
+
import { parseComponents, type CV } from './tokenize.js';
|
|
7
|
+
|
|
8
|
+
export type Media = 'wide' | 'narrow';
|
|
9
|
+
|
|
10
|
+
interface Screen {
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
}
|
|
14
|
+
const SCREENS: Record<Media, Screen> = { wide: { width: 800, height: 600 }, narrow: { width: 375, height: 667 } };
|
|
15
|
+
|
|
16
|
+
/** a media query list (`@media`'s prelude, a `media` attribute): true when any query matches */
|
|
17
|
+
export function matchMedia(list: CV[] | string, media: Media): boolean {
|
|
18
|
+
const items = typeof list === 'string' ? parseComponents(list) : list;
|
|
19
|
+
const queries = split(items);
|
|
20
|
+
// an empty list matches everything
|
|
21
|
+
if (queries.length === 1 && queries[0].length === 0) return true;
|
|
22
|
+
const screen = SCREENS[media];
|
|
23
|
+
return queries.some((q) => query(q, screen) === true);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** split at top-level commas, whitespace trimmed */
|
|
27
|
+
function split(items: CV[]): CV[][] {
|
|
28
|
+
const out: CV[][] = [[]];
|
|
29
|
+
for (const v of items) {
|
|
30
|
+
if (v.t === ',') out.push([]);
|
|
31
|
+
else if (v.t !== 'ws') out[out.length - 1].push(v);
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// three-valued logic: true, false, or null (unknown), which a query as a whole reads as false
|
|
37
|
+
type K = boolean | null;
|
|
38
|
+
const not = (v: K): K => (v === null ? null : !v);
|
|
39
|
+
|
|
40
|
+
const TYPES_TRUE = new Set(['all', 'screen']);
|
|
41
|
+
const TYPES_FALSE = new Set(['print', 'tty', 'tv', 'projection', 'handheld', 'braille', 'embossed', 'aural', 'speech']);
|
|
42
|
+
const RESERVED = new Set(['only', 'not', 'and', 'or', 'layer']);
|
|
43
|
+
|
|
44
|
+
function query(q: CV[], screen: Screen): K {
|
|
45
|
+
if (q.length === 0) return false;
|
|
46
|
+
const first = q[0];
|
|
47
|
+
if ((first.t === 'block' && first.open === '(') || (first.t === 'ident' && first.v.toLowerCase() === 'not' && q[1]?.t === 'block')) {
|
|
48
|
+
return condition(q, 0, screen, true) ?? false;
|
|
49
|
+
}
|
|
50
|
+
// [not | only]? <media-type> [and <condition-without-or>]?
|
|
51
|
+
let i = 0;
|
|
52
|
+
let negate = false;
|
|
53
|
+
const kw = (v: CV | undefined) => (v?.t === 'ident' ? v.v.toLowerCase() : null);
|
|
54
|
+
if (kw(q[0]) === 'not') { negate = true; i++; } else if (kw(q[0]) === 'only') i++;
|
|
55
|
+
const type = kw(q[i]);
|
|
56
|
+
if (type === null || RESERVED.has(type)) return false;
|
|
57
|
+
i++;
|
|
58
|
+
let r: K = TYPES_TRUE.has(type) ? true : TYPES_FALSE.has(type) ? false : false;
|
|
59
|
+
if (i < q.length) {
|
|
60
|
+
if (kw(q[i]) !== 'and' || i + 1 >= q.length) return false;
|
|
61
|
+
const c = condition(q, i + 1, screen, false);
|
|
62
|
+
if (c === undefined) return false;
|
|
63
|
+
r = r === false ? false : c === false ? false : r === true && c === true ? true : null;
|
|
64
|
+
}
|
|
65
|
+
return negate ? not(r) : r;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A media condition from `q[i]` to the end: `not <in-parens>`, or in-parens joined by `and` (or `or`
|
|
70
|
+
* when `or` is allowed: not after a media type). undefined when malformed.
|
|
71
|
+
*/
|
|
72
|
+
function condition(q: CV[], i: number, screen: Screen, orAllowed: boolean): K | undefined {
|
|
73
|
+
const kw = (v: CV | undefined) => (v?.t === 'ident' ? v.v.toLowerCase() : null);
|
|
74
|
+
if (kw(q[i]) === 'not') {
|
|
75
|
+
if (i + 2 !== q.length) return undefined;
|
|
76
|
+
const v = inParens(q[i + 1], screen);
|
|
77
|
+
return v === undefined ? undefined : not(v);
|
|
78
|
+
}
|
|
79
|
+
let r = inParens(q[i], screen);
|
|
80
|
+
if (r === undefined) return undefined;
|
|
81
|
+
let op: string | null = null;
|
|
82
|
+
for (let j = i + 1; j < q.length; j += 2) {
|
|
83
|
+
const k = kw(q[j]);
|
|
84
|
+
if ((k !== 'and' && k !== 'or') || (op && k !== op) || (k === 'or' && !orAllowed)) return undefined;
|
|
85
|
+
op = k;
|
|
86
|
+
const v = inParens(q[j + 1], screen);
|
|
87
|
+
if (v === undefined) return undefined;
|
|
88
|
+
if (k === 'and') r = r === false || v === false ? false : r === true && v === true ? true : null;
|
|
89
|
+
else r = r === true || v === true ? true : r === false && v === false ? false : null;
|
|
90
|
+
}
|
|
91
|
+
return r;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** `( <condition> )` or `( <feature> )`; a parenthesized thing that is neither is unknown */
|
|
95
|
+
function inParens(v: CV | undefined, screen: Screen): K | undefined {
|
|
96
|
+
if (!v || v.t !== 'block' || v.open !== '(') {
|
|
97
|
+
// a function here is <general-enclosed>: unknown
|
|
98
|
+
return v?.t === 'func' ? null : undefined;
|
|
99
|
+
}
|
|
100
|
+
const items = v.items.filter((x) => x.t !== 'ws');
|
|
101
|
+
if (items.length === 0) return undefined;
|
|
102
|
+
if (items[0].t === 'block' || (items[0].t === 'ident' && items[0].v.toLowerCase() === 'not')) {
|
|
103
|
+
const c = condition(items, 0, screen, true);
|
|
104
|
+
return c === undefined ? null : c;
|
|
105
|
+
}
|
|
106
|
+
return feature(items, screen);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// the features this knows, as numbers in px (lengths), plain numbers, or keywords
|
|
110
|
+
type Value = { n: number; kind: 'length' | 'number' | 'ratio' | 'resolution' } | { kw: string };
|
|
111
|
+
|
|
112
|
+
function featureValue(name: string, screen: Screen): Value | null {
|
|
113
|
+
switch (name) {
|
|
114
|
+
case 'width': case 'device-width': return { n: screen.width, kind: 'length' };
|
|
115
|
+
case 'height': case 'device-height': return { n: screen.height, kind: 'length' };
|
|
116
|
+
case 'aspect-ratio': case 'device-aspect-ratio': return { n: screen.width / screen.height, kind: 'ratio' };
|
|
117
|
+
case 'orientation': return { kw: screen.width > screen.height ? 'landscape' : 'portrait' };
|
|
118
|
+
case 'prefers-color-scheme': return { kw: 'light' };
|
|
119
|
+
case 'prefers-reduced-motion': case 'prefers-reduced-transparency': return { kw: 'no-preference' };
|
|
120
|
+
case 'prefers-contrast': return { kw: 'no-preference' };
|
|
121
|
+
case 'forced-colors': return { kw: 'none' };
|
|
122
|
+
case 'color': return { n: 8, kind: 'number' };
|
|
123
|
+
case 'color-index': case 'monochrome': case 'grid': return { n: 0, kind: 'number' };
|
|
124
|
+
case 'resolution': return { n: 1, kind: 'resolution' };
|
|
125
|
+
case '-webkit-device-pixel-ratio': return { n: 1, kind: 'number' };
|
|
126
|
+
case 'scan': return { kw: 'progressive' };
|
|
127
|
+
default: return null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const RANGE = new Set(['width', 'height', 'device-width', 'device-height', 'aspect-ratio', 'device-aspect-ratio', 'color', 'color-index', 'monochrome', 'resolution', '-webkit-device-pixel-ratio']);
|
|
132
|
+
|
|
133
|
+
function feature(items: CV[], screen: Screen): K {
|
|
134
|
+
const id = (v: CV | undefined) => (v?.t === 'ident' ? v.v.toLowerCase() : null);
|
|
135
|
+
// (name)
|
|
136
|
+
if (items.length === 1) {
|
|
137
|
+
const name = id(items[0]);
|
|
138
|
+
if (name === null) return null;
|
|
139
|
+
const f = featureValue(name, screen);
|
|
140
|
+
if (!f) return null;
|
|
141
|
+
return 'n' in f ? f.n !== 0 : f.kw !== 'none' && f.kw !== 'no-preference';
|
|
142
|
+
}
|
|
143
|
+
// (name: value), with min-/max- prefixes
|
|
144
|
+
if (items[1].t === ':') {
|
|
145
|
+
let name = id(items[0]);
|
|
146
|
+
if (name === null) return null;
|
|
147
|
+
let cmp: '=' | '>=' | '<=' = '=';
|
|
148
|
+
const prefix = /^(-webkit-)?(min|max)-/.exec(name);
|
|
149
|
+
if (prefix) {
|
|
150
|
+
cmp = prefix[2] === 'min' ? '>=' : '<=';
|
|
151
|
+
name = (prefix[1] ?? '') + name.slice(prefix[0].length);
|
|
152
|
+
}
|
|
153
|
+
const f = featureValue(name, screen);
|
|
154
|
+
if (!f || (prefix && !RANGE.has(name))) return null;
|
|
155
|
+
const rest = items.slice(2);
|
|
156
|
+
if ('kw' in f) return rest.length === 1 && id(rest[0]) === f.kw ? true : rest.length === 1 && rest[0].t === 'ident' ? false : null;
|
|
157
|
+
const v = valueOf(rest, f.kind);
|
|
158
|
+
return v === null ? null : compare(f.n, cmp, v);
|
|
159
|
+
}
|
|
160
|
+
// range syntax: (name op value), (value op name), (value op name op value)
|
|
161
|
+
const ops: { op: string; at: number }[] = [];
|
|
162
|
+
for (let i = 0; i < items.length; i++) {
|
|
163
|
+
const v = items[i];
|
|
164
|
+
if (v.t === 'delim' && (v.v === '<' || v.v === '>' || v.v === '=')) {
|
|
165
|
+
let op = v.v;
|
|
166
|
+
const next = items[i + 1];
|
|
167
|
+
if (op !== '=' && next?.t === 'delim' && next.v === '=') { op += '='; i++; }
|
|
168
|
+
ops.push({ op, at: i });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (ops.length < 1 || ops.length > 2) return null;
|
|
172
|
+
const parts: CV[][] = [];
|
|
173
|
+
let from = 0;
|
|
174
|
+
for (const o of ops) {
|
|
175
|
+
parts.push(items.slice(from, o.at - (o.op.length - 1)));
|
|
176
|
+
from = o.at + 1;
|
|
177
|
+
}
|
|
178
|
+
parts.push(items.slice(from));
|
|
179
|
+
const nameAt = parts.findIndex((p) => p.length === 1 && id(p[0]) !== null && featureValue(id(p[0])!, screen) !== null);
|
|
180
|
+
if (nameAt < 0) return null;
|
|
181
|
+
const name = id(parts[nameAt][0])!;
|
|
182
|
+
const f = featureValue(name, screen)!;
|
|
183
|
+
if ('kw' in f || !RANGE.has(name)) return null;
|
|
184
|
+
if (ops.length === 2) {
|
|
185
|
+
// both ops point the same way, neither is `=`, and the name is in the middle
|
|
186
|
+
if (nameAt !== 1 || ops[0].op[0] !== ops[1].op[0] || ops[0].op === '=' || ops[1].op === '=') return null;
|
|
187
|
+
}
|
|
188
|
+
let r: K = true;
|
|
189
|
+
for (let k = 0; k < ops.length; k++) {
|
|
190
|
+
const left = parts[k];
|
|
191
|
+
const right = parts[k + 1];
|
|
192
|
+
const op = ops[k].op as '<' | '<=' | '>' | '>=' | '=';
|
|
193
|
+
// the comparison as "feature op value"
|
|
194
|
+
if (k + 1 === nameAt) {
|
|
195
|
+
const v = valueOf(left, f.kind);
|
|
196
|
+
if (v === null) return null;
|
|
197
|
+
const flipped = ({ '<': '>', '<=': '>=', '>': '<', '>=': '<=', '=': '=' } as const)[op];
|
|
198
|
+
r = r && compare(f.n, flipped, v);
|
|
199
|
+
} else if (k === nameAt) {
|
|
200
|
+
const v = valueOf(right, f.kind);
|
|
201
|
+
if (v === null) return null;
|
|
202
|
+
r = r && compare(f.n, op, v);
|
|
203
|
+
} else return null;
|
|
204
|
+
}
|
|
205
|
+
return r;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function compare(a: number, op: '<' | '<=' | '>' | '>=' | '=', b: number): boolean {
|
|
209
|
+
// a little tolerance, so a ratio computed in floating point compares equal to the one written
|
|
210
|
+
const eq = Math.abs(a - b) < 1e-9;
|
|
211
|
+
switch (op) {
|
|
212
|
+
case '=': return eq;
|
|
213
|
+
case '<': return a < b && !eq;
|
|
214
|
+
case '<=': return a < b || eq;
|
|
215
|
+
case '>': return a > b && !eq;
|
|
216
|
+
case '>=': return a > b || eq;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const PX: Record<string, number> = { px: 1, em: 16, rem: 16, ex: 8, ch: 8, pt: 96 / 72, pc: 16, in: 96, cm: 96 / 2.54, mm: 96 / 25.4, q: 96 / 101.6 };
|
|
221
|
+
const DPPX: Record<string, number> = { dppx: 1, x: 1, dpi: 1 / 96, dpcm: 2.54 / 96 };
|
|
222
|
+
|
|
223
|
+
/** a feature's value in the feature's unit (px, a plain number, a ratio, dppx), or null */
|
|
224
|
+
function valueOf(items: CV[], kind: 'length' | 'number' | 'ratio' | 'resolution'): number | null {
|
|
225
|
+
if (kind === 'ratio') {
|
|
226
|
+
if (items.length === 1 && items[0].t === 'number' && items[0].n >= 0) return items[0].n;
|
|
227
|
+
if (items.length === 3 && items[0].t === 'number' && items[1].t === 'delim' && items[1].v === '/' && items[2].t === 'number') {
|
|
228
|
+
const a = items[0].n;
|
|
229
|
+
const b = items[2].n;
|
|
230
|
+
if (a < 0 || b < 0) return null;
|
|
231
|
+
return b === 0 ? Infinity : a / b;
|
|
232
|
+
}
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
if (items.length !== 1) return null;
|
|
236
|
+
const v = items[0];
|
|
237
|
+
if (kind === 'length') {
|
|
238
|
+
if (v.t === 'number' && v.n === 0) return 0;
|
|
239
|
+
if (v.t === 'dimension') {
|
|
240
|
+
const k = PX[v.unit.toLowerCase()];
|
|
241
|
+
return k === undefined ? null : v.n * k;
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
if (kind === 'resolution') {
|
|
246
|
+
if (v.t !== 'dimension') return null;
|
|
247
|
+
const k = DPPX[v.unit.toLowerCase()];
|
|
248
|
+
return k === undefined ? null : v.n * k;
|
|
249
|
+
}
|
|
250
|
+
return v.t === 'number' ? v.n : null;
|
|
251
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// The stylesheet and declaration-list parsers (CSS Syntax Level 3, §5), over component values: what a
|
|
2
|
+
// browser keeps of a stylesheet's structure before it looks at selectors and values. Rules nested in a
|
|
3
|
+
// style rule (CSS Nesting) are skipped whole, as the current syntax says to consume them, not applied.
|
|
4
|
+
import { parseComponents, type Block, type CV } from './tokenize.js';
|
|
5
|
+
|
|
6
|
+
export interface Declaration {
|
|
7
|
+
/** lowercase (custom properties keep their case, and are dropped later anyway) */
|
|
8
|
+
name: string;
|
|
9
|
+
value: CV[];
|
|
10
|
+
important: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type Rule =
|
|
14
|
+
| { t: 'style'; prelude: CV[]; declarations: Declaration[] }
|
|
15
|
+
| { t: 'at'; name: string; prelude: CV[]; block: Block | null };
|
|
16
|
+
|
|
17
|
+
/** the rules of a stylesheet (§5.3.3) */
|
|
18
|
+
export function parseStylesheet(css: string): Rule[] {
|
|
19
|
+
return rules(parseComponents(css), true);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** the declarations of a `style` attribute (§5.3.7) */
|
|
23
|
+
export function parseDeclarations(css: string): Declaration[] {
|
|
24
|
+
return declarations(parseComponents(css));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const isWs = (v: CV | undefined) => v?.t === 'ws';
|
|
28
|
+
|
|
29
|
+
/** a list of rules from component values (§5.4.1); CDO and CDC are skipped at the top level only */
|
|
30
|
+
export function rules(items: CV[], top: boolean): Rule[] {
|
|
31
|
+
const out: Rule[] = [];
|
|
32
|
+
let i = 0;
|
|
33
|
+
while (i < items.length) {
|
|
34
|
+
const v = items[i];
|
|
35
|
+
if (v.t === 'ws') { i++; continue; }
|
|
36
|
+
if (v.t === 'cdo' || v.t === 'cdc') {
|
|
37
|
+
if (top) { i++; continue; }
|
|
38
|
+
}
|
|
39
|
+
if (v.t === 'at-keyword') {
|
|
40
|
+
// an at-rule ends at a `;` or with its `{}` block
|
|
41
|
+
const prelude: CV[] = [];
|
|
42
|
+
i++;
|
|
43
|
+
let block: Block | null = null;
|
|
44
|
+
while (i < items.length) {
|
|
45
|
+
const w = items[i++];
|
|
46
|
+
if (w.t === ';') break;
|
|
47
|
+
if (w.t === 'block' && w.open === '{') { block = w; break; }
|
|
48
|
+
prelude.push(w);
|
|
49
|
+
}
|
|
50
|
+
out.push({ t: 'at', name: v.v.toLowerCase(), prelude, block });
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
// a qualified rule: its prelude, up to its `{}` block; with no block it is dropped
|
|
54
|
+
const prelude: CV[] = [];
|
|
55
|
+
let block: Block | null = null;
|
|
56
|
+
while (i < items.length) {
|
|
57
|
+
const w = items[i++];
|
|
58
|
+
if (w.t === 'block' && w.open === '{') { block = w; break; }
|
|
59
|
+
prelude.push(w);
|
|
60
|
+
}
|
|
61
|
+
if (block) out.push({ t: 'style', prelude, declarations: declarations(block.items) });
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The declarations of a block's contents (§5.4.5, with nesting): `name: value [!important]` up to a `;`
|
|
68
|
+
* at this level; an at-rule or a nested rule is consumed and skipped; anything else is dropped up to
|
|
69
|
+
* the next `;`.
|
|
70
|
+
*/
|
|
71
|
+
export function declarations(items: CV[]): Declaration[] {
|
|
72
|
+
const out: Declaration[] = [];
|
|
73
|
+
let i = 0;
|
|
74
|
+
while (i < items.length) {
|
|
75
|
+
const v = items[i];
|
|
76
|
+
if (v.t === 'ws' || v.t === ';') { i++; continue; }
|
|
77
|
+
if (v.t === 'at-keyword') {
|
|
78
|
+
i++;
|
|
79
|
+
while (i < items.length) {
|
|
80
|
+
const w = items[i++];
|
|
81
|
+
if (w.t === ';' || (w.t === 'block' && w.open === '{')) break;
|
|
82
|
+
}
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
// the item: up to a `;`, or a nested rule's `{}` block
|
|
86
|
+
const from = i;
|
|
87
|
+
let nested = false;
|
|
88
|
+
while (i < items.length && items[i].t !== ';') {
|
|
89
|
+
const w = items[i++];
|
|
90
|
+
if (w.t === 'block' && w.open === '{') { nested = true; break; }
|
|
91
|
+
}
|
|
92
|
+
if (nested) continue;
|
|
93
|
+
const d = declaration(items.slice(from, i));
|
|
94
|
+
if (d) out.push(d);
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function declaration(items: CV[]): Declaration | null {
|
|
100
|
+
const head = items[0];
|
|
101
|
+
if (head?.t !== 'ident') return null;
|
|
102
|
+
let k = 1;
|
|
103
|
+
while (isWs(items[k])) k++;
|
|
104
|
+
if (items[k]?.t !== ':') return null;
|
|
105
|
+
const value = items.slice(k + 1);
|
|
106
|
+
// trailing whitespace, then `!important` (with whitespace allowed between `!` and the keyword)
|
|
107
|
+
const trim = () => { while (isWs(value[value.length - 1])) value.pop(); };
|
|
108
|
+
trim();
|
|
109
|
+
let important = false;
|
|
110
|
+
const last = value[value.length - 1];
|
|
111
|
+
if (last?.t === 'ident' && last.v.toLowerCase() === 'important') {
|
|
112
|
+
let j = value.length - 2;
|
|
113
|
+
while (isWs(value[j])) j--;
|
|
114
|
+
const bang = value[j];
|
|
115
|
+
if (bang?.t === 'delim' && bang.v === '!') {
|
|
116
|
+
important = true;
|
|
117
|
+
value.length = j;
|
|
118
|
+
trim();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
while (isWs(value[0])) value.shift();
|
|
122
|
+
const name = head.v.startsWith('--') ? head.v : head.v.toLowerCase();
|
|
123
|
+
return { name, value, important };
|
|
124
|
+
}
|