@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,279 @@
|
|
|
1
|
+
// The CSS tokenizer, after CSS Syntax Level 3 (§4), and the step after it: component values (§5.4.7),
|
|
2
|
+
// built with a stack, not recursion, so a hostile run of `((((` cannot exhaust the call stack.
|
|
3
|
+
//
|
|
4
|
+
// Escapes are decoded here, once: every name and string downstream holds the characters, never a
|
|
5
|
+
// backslash, and every value written out is re-serialized from these tokens (values.ts).
|
|
6
|
+
|
|
7
|
+
export type Token =
|
|
8
|
+
| { t: 'ident'; v: string }
|
|
9
|
+
| { t: 'function'; v: string }
|
|
10
|
+
| { t: 'at-keyword'; v: string }
|
|
11
|
+
| { t: 'hash'; v: string; id: boolean }
|
|
12
|
+
| { t: 'string'; v: string }
|
|
13
|
+
| { t: 'bad-string' }
|
|
14
|
+
| { t: 'url'; v: string }
|
|
15
|
+
| { t: 'bad-url' }
|
|
16
|
+
| { t: 'delim'; v: string }
|
|
17
|
+
| { t: 'number'; n: number; int: boolean; s: string }
|
|
18
|
+
| { t: 'percentage'; n: number; s: string }
|
|
19
|
+
| { t: 'dimension'; n: number; int: boolean; s: string; unit: string }
|
|
20
|
+
| { t: 'ws' }
|
|
21
|
+
| { t: 'cdo' }
|
|
22
|
+
| { t: 'cdc' }
|
|
23
|
+
| { t: ':' }
|
|
24
|
+
| { t: ';' }
|
|
25
|
+
| { t: ',' }
|
|
26
|
+
| { t: '[' }
|
|
27
|
+
| { t: ']' }
|
|
28
|
+
| { t: '(' }
|
|
29
|
+
| { t: ')' }
|
|
30
|
+
| { t: '{' }
|
|
31
|
+
| { t: '}' };
|
|
32
|
+
|
|
33
|
+
/** a function with its arguments, or a block (`{}`, `()`, `[]`) with its contents */
|
|
34
|
+
export type Func = { t: 'func'; name: string; args: CV[] };
|
|
35
|
+
export type Block = { t: 'block'; open: '{' | '(' | '['; items: CV[] };
|
|
36
|
+
/** a component value: a preserved token, a function or a block */
|
|
37
|
+
export type CV = Exclude<Token, { t: 'function' | '(' | '[' | '{' }> | Func | Block;
|
|
38
|
+
|
|
39
|
+
const WS: Token = { t: 'ws' };
|
|
40
|
+
|
|
41
|
+
const isDigit = (c: number) => c >= 48 && c <= 57;
|
|
42
|
+
const isHex = (c: number) => isDigit(c) || (c >= 65 && c <= 70) || (c >= 97 && c <= 102);
|
|
43
|
+
const isNameStart = (c: number) => (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c === 95 || c >= 0x80;
|
|
44
|
+
const isName = (c: number) => isNameStart(c) || isDigit(c) || c === 45;
|
|
45
|
+
const isWs = (c: number) => c === 10 || c === 9 || c === 32;
|
|
46
|
+
const isNonPrintable = (c: number) => (c >= 0 && c <= 8) || c === 11 || (c >= 14 && c <= 31) || c === 127;
|
|
47
|
+
|
|
48
|
+
/** the tokens of `input` (preprocessed as §3.3 says: newlines normalized, NUL and surrogates replaced) */
|
|
49
|
+
export function tokenize(input: string): Token[] {
|
|
50
|
+
const s = input.replace(/\r\n?|\f/g, '\n').replace(/\0|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, '�');
|
|
51
|
+
const n = s.length;
|
|
52
|
+
const out: Token[] = [];
|
|
53
|
+
let i = 0;
|
|
54
|
+
// code unit at i + k, or -1 past the end (EOF)
|
|
55
|
+
const at = (k = 0) => (i + k < n ? s.charCodeAt(i + k) : -1);
|
|
56
|
+
const validEscape = (k: number) => at(k) === 92 && at(k + 1) !== 10 && at(k + 1) !== -1;
|
|
57
|
+
const startsIdent = (k: number) => {
|
|
58
|
+
const c = at(k);
|
|
59
|
+
if (c === 45) return isNameStart(at(k + 1)) || at(k + 1) === 45 || validEscape(k + 1);
|
|
60
|
+
if (isNameStart(c)) return true;
|
|
61
|
+
return validEscape(k);
|
|
62
|
+
};
|
|
63
|
+
const startsNumber = (k: number) => {
|
|
64
|
+
const c = at(k);
|
|
65
|
+
if (c === 43 || c === 45) return isDigit(at(k + 1)) || (at(k + 1) === 46 && isDigit(at(k + 2)));
|
|
66
|
+
if (c === 46) return isDigit(at(k + 1));
|
|
67
|
+
return isDigit(c);
|
|
68
|
+
};
|
|
69
|
+
// §4.3.7, with i just past the backslash
|
|
70
|
+
const escape = (): string => {
|
|
71
|
+
const c = at();
|
|
72
|
+
if (c === -1) return '�';
|
|
73
|
+
if (isHex(c)) {
|
|
74
|
+
let j = 0;
|
|
75
|
+
while (j < 6 && isHex(at())) { j++; i++; }
|
|
76
|
+
const cp = parseInt(s.slice(i - j, i), 16);
|
|
77
|
+
if (isWs(at())) i++;
|
|
78
|
+
return cp === 0 || (cp >= 0xd800 && cp <= 0xdfff) || cp > 0x10ffff ? '�' : String.fromCodePoint(cp);
|
|
79
|
+
}
|
|
80
|
+
// a whole code point, a surrogate pair included
|
|
81
|
+
const cp = s.codePointAt(i)!;
|
|
82
|
+
i += cp > 0xffff ? 2 : 1;
|
|
83
|
+
return String.fromCodePoint(cp);
|
|
84
|
+
};
|
|
85
|
+
const name = (): string => {
|
|
86
|
+
let r = '';
|
|
87
|
+
let from = i;
|
|
88
|
+
for (;;) {
|
|
89
|
+
const c = at();
|
|
90
|
+
if (isName(c)) { i++; continue; }
|
|
91
|
+
if (validEscape(0)) {
|
|
92
|
+
r += s.slice(from, i);
|
|
93
|
+
i++;
|
|
94
|
+
r += escape();
|
|
95
|
+
from = i;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
return r + s.slice(from, i);
|
|
101
|
+
};
|
|
102
|
+
const number = (): { n: number; int: boolean; s: string } => {
|
|
103
|
+
const from = i;
|
|
104
|
+
let int = true;
|
|
105
|
+
if (at() === 43 || at() === 45) i++;
|
|
106
|
+
while (isDigit(at())) i++;
|
|
107
|
+
if (at() === 46 && isDigit(at(1))) { int = false; i += 2; while (isDigit(at())) i++; }
|
|
108
|
+
const e = at();
|
|
109
|
+
if ((e === 69 || e === 101) && (isDigit(at(1)) || ((at(1) === 43 || at(1) === 45) && isDigit(at(2))))) {
|
|
110
|
+
int = false;
|
|
111
|
+
i += 2;
|
|
112
|
+
while (isDigit(at())) i++;
|
|
113
|
+
}
|
|
114
|
+
const repr = s.slice(from, i);
|
|
115
|
+
return { n: Number(repr), int, s: repr };
|
|
116
|
+
};
|
|
117
|
+
const numeric = (): Token => {
|
|
118
|
+
const num = number();
|
|
119
|
+
if (startsIdent(0)) return { t: 'dimension', ...num, unit: name() };
|
|
120
|
+
if (at() === 37) { i++; return { t: 'percentage', n: num.n, s: num.s }; }
|
|
121
|
+
return { t: 'number', ...num };
|
|
122
|
+
};
|
|
123
|
+
// §4.3.6, with i just past `url(` and its whitespace
|
|
124
|
+
const url = (): Token => {
|
|
125
|
+
let v = '';
|
|
126
|
+
while (isWs(at())) i++;
|
|
127
|
+
for (;;) {
|
|
128
|
+
const c = at();
|
|
129
|
+
if (c === 41) { i++; return { t: 'url', v }; }
|
|
130
|
+
if (c === -1) return { t: 'url', v };
|
|
131
|
+
if (isWs(c)) {
|
|
132
|
+
while (isWs(at())) i++;
|
|
133
|
+
if (at() === 41) { i++; return { t: 'url', v }; }
|
|
134
|
+
if (at() === -1) return { t: 'url', v };
|
|
135
|
+
badUrl();
|
|
136
|
+
return { t: 'bad-url' };
|
|
137
|
+
}
|
|
138
|
+
if (c === 34 || c === 39 || c === 40 || isNonPrintable(c)) { badUrl(); return { t: 'bad-url' }; }
|
|
139
|
+
if (c === 92) {
|
|
140
|
+
if (validEscape(0)) { i++; v += escape(); continue; }
|
|
141
|
+
badUrl();
|
|
142
|
+
return { t: 'bad-url' };
|
|
143
|
+
}
|
|
144
|
+
v += s[i++];
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const badUrl = () => {
|
|
148
|
+
for (;;) {
|
|
149
|
+
const c = at();
|
|
150
|
+
if (c === -1) return;
|
|
151
|
+
if (c === 41) { i++; return; }
|
|
152
|
+
if (validEscape(0)) { i++; escape(); continue; }
|
|
153
|
+
i++;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
const identLike = (): Token => {
|
|
157
|
+
const v = name();
|
|
158
|
+
if (v.toLowerCase() === 'url' && at() === 40) {
|
|
159
|
+
i++;
|
|
160
|
+
// `url(` followed by a quote is an ordinary function (its argument a string token)
|
|
161
|
+
let k = 0;
|
|
162
|
+
while (isWs(at(k)) && isWs(at(k + 1))) k++;
|
|
163
|
+
const q = isWs(at(k)) ? at(k + 1) : at(k);
|
|
164
|
+
if (q === 34 || q === 39) { i += k; return { t: 'function', v }; }
|
|
165
|
+
return url();
|
|
166
|
+
}
|
|
167
|
+
if (at() === 40) { i++; return { t: 'function', v }; }
|
|
168
|
+
return { t: 'ident', v };
|
|
169
|
+
};
|
|
170
|
+
const string = (q: number): Token => {
|
|
171
|
+
let v = '';
|
|
172
|
+
let from = i;
|
|
173
|
+
for (;;) {
|
|
174
|
+
const c = at();
|
|
175
|
+
if (c === q) { v += s.slice(from, i); i++; return { t: 'string', v }; }
|
|
176
|
+
if (c === -1) return { t: 'string', v: v + s.slice(from, i) };
|
|
177
|
+
if (c === 10) return { t: 'bad-string' }; // the newline is left for the next token
|
|
178
|
+
if (c === 92) {
|
|
179
|
+
v += s.slice(from, i);
|
|
180
|
+
i++;
|
|
181
|
+
if (at() === -1) { from = i; continue; }
|
|
182
|
+
if (at() === 10) { i++; from = i; continue; }
|
|
183
|
+
v += escape();
|
|
184
|
+
from = i;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
i++;
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
while (i < n) {
|
|
192
|
+
const c = at();
|
|
193
|
+
// comments
|
|
194
|
+
if (c === 47 && at(1) === 42) {
|
|
195
|
+
const end = s.indexOf('*/', i + 2);
|
|
196
|
+
i = end < 0 ? n : end + 2;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (isWs(c)) {
|
|
200
|
+
while (isWs(at())) i++;
|
|
201
|
+
out.push(WS);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (c === 34 || c === 39) { i++; out.push(string(c)); continue; }
|
|
205
|
+
if (c === 35) {
|
|
206
|
+
if (isName(at(1)) || validEscape(1)) {
|
|
207
|
+
i++;
|
|
208
|
+
const id = startsIdent(0);
|
|
209
|
+
out.push({ t: 'hash', v: name(), id });
|
|
210
|
+
} else { i++; out.push({ t: 'delim', v: '#' }); }
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (startsNumber(0)) { out.push(numeric()); continue; }
|
|
214
|
+
if (c === 45 && at(1) === 45 && at(2) === 62) { i += 3; out.push({ t: 'cdc' }); continue; }
|
|
215
|
+
if (startsIdent(0)) { out.push(identLike()); continue; }
|
|
216
|
+
if (c === 60 && at(1) === 33 && at(2) === 45 && at(3) === 45) { i += 4; out.push({ t: 'cdo' }); continue; }
|
|
217
|
+
if (c === 64) {
|
|
218
|
+
i++;
|
|
219
|
+
if (startsIdent(0)) out.push({ t: 'at-keyword', v: name() });
|
|
220
|
+
else out.push({ t: 'delim', v: '@' });
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
i++;
|
|
224
|
+
switch (c) {
|
|
225
|
+
case 40: out.push({ t: '(' }); break;
|
|
226
|
+
case 41: out.push({ t: ')' }); break;
|
|
227
|
+
case 44: out.push({ t: ',' }); break;
|
|
228
|
+
case 58: out.push({ t: ':' }); break;
|
|
229
|
+
case 59: out.push({ t: ';' }); break;
|
|
230
|
+
case 91: out.push({ t: '[' }); break;
|
|
231
|
+
case 93: out.push({ t: ']' }); break;
|
|
232
|
+
case 123: out.push({ t: '{' }); break;
|
|
233
|
+
case 125: out.push({ t: '}' }); break;
|
|
234
|
+
default: {
|
|
235
|
+
// one code point (a lone backslash is a parse error and a delim)
|
|
236
|
+
const cp = s.codePointAt(i - 1)!;
|
|
237
|
+
if (cp > 0xffff) i++;
|
|
238
|
+
out.push({ t: 'delim', v: String.fromCodePoint(cp) });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return out;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const MIRROR = { '(': ')', '[': ']', '{': '}' } as const;
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The component values of `tokens` (§5.4.7): functions and blocks with their contents, closed by their
|
|
249
|
+
* mirror token or by the end. A stray closing token is kept as itself.
|
|
250
|
+
*/
|
|
251
|
+
export function componentValues(tokens: Token[]): CV[] {
|
|
252
|
+
const root: CV[] = [];
|
|
253
|
+
// the open containers, innermost last, each with the token that closes it
|
|
254
|
+
const stack: { items: CV[]; close: ')' | ']' | '}' }[] = [];
|
|
255
|
+
let items = root;
|
|
256
|
+
for (const tok of tokens) {
|
|
257
|
+
const top = stack[stack.length - 1];
|
|
258
|
+
if (top && tok.t === top.close) {
|
|
259
|
+
stack.pop();
|
|
260
|
+
items = stack.length ? stack[stack.length - 1].items : root;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (tok.t === 'function') {
|
|
264
|
+
const f: Func = { t: 'func', name: tok.v, args: [] };
|
|
265
|
+
items.push(f);
|
|
266
|
+
stack.push({ items: f.args, close: ')' });
|
|
267
|
+
items = f.args;
|
|
268
|
+
} else if (tok.t === '(' || tok.t === '[' || tok.t === '{') {
|
|
269
|
+
const b: Block = { t: 'block', open: tok.t, items: [] };
|
|
270
|
+
items.push(b);
|
|
271
|
+
stack.push({ items: b.items, close: MIRROR[tok.t] });
|
|
272
|
+
items = b.items;
|
|
273
|
+
} else items.push(tok);
|
|
274
|
+
}
|
|
275
|
+
return root;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** `input` as component values */
|
|
279
|
+
export const parseComponents = (input: string): CV[] => componentValues(tokenize(input));
|