@tiavision/sinter-mail 0.0.0-stage → 0.2.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.
Files changed (95) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +113 -2
  3. package/docs/mail.md +298 -0
  4. package/docs/proposals/progressive-rendering.md +180 -0
  5. package/docs/proposals/sinter-mail.md +520 -0
  6. package/llms.txt +21 -0
  7. package/package.json +65 -4
  8. package/sinter/autolink.ts +98 -0
  9. package/sinter/components/internal/mail-a.html +5 -0
  10. package/sinter/components/internal/mail-abbr.html +5 -0
  11. package/sinter/components/internal/mail-address.html +5 -0
  12. package/sinter/components/internal/mail-article.html +5 -0
  13. package/sinter/components/internal/mail-aside.html +5 -0
  14. package/sinter/components/internal/mail-b.html +5 -0
  15. package/sinter/components/internal/mail-bdi.html +5 -0
  16. package/sinter/components/internal/mail-bdo.html +5 -0
  17. package/sinter/components/internal/mail-blockquote.html +5 -0
  18. package/sinter/components/internal/mail-br.html +5 -0
  19. package/sinter/components/internal/mail-center.html +5 -0
  20. package/sinter/components/internal/mail-cite.html +5 -0
  21. package/sinter/components/internal/mail-code.html +5 -0
  22. package/sinter/components/internal/mail-dd.html +5 -0
  23. package/sinter/components/internal/mail-del.html +5 -0
  24. package/sinter/components/internal/mail-details.html +5 -0
  25. package/sinter/components/internal/mail-dfn.html +5 -0
  26. package/sinter/components/internal/mail-div.html +5 -0
  27. package/sinter/components/internal/mail-dl.html +5 -0
  28. package/sinter/components/internal/mail-dt.html +5 -0
  29. package/sinter/components/internal/mail-em.html +5 -0
  30. package/sinter/components/internal/mail-figcaption.html +5 -0
  31. package/sinter/components/internal/mail-figure.html +5 -0
  32. package/sinter/components/internal/mail-font.html +5 -0
  33. package/sinter/components/internal/mail-footer.html +5 -0
  34. package/sinter/components/internal/mail-h1.html +5 -0
  35. package/sinter/components/internal/mail-h2.html +5 -0
  36. package/sinter/components/internal/mail-h3.html +5 -0
  37. package/sinter/components/internal/mail-h4.html +5 -0
  38. package/sinter/components/internal/mail-h5.html +5 -0
  39. package/sinter/components/internal/mail-h6.html +5 -0
  40. package/sinter/components/internal/mail-header.html +5 -0
  41. package/sinter/components/internal/mail-hr.html +5 -0
  42. package/sinter/components/internal/mail-i.html +5 -0
  43. package/sinter/components/internal/mail-img.html +5 -0
  44. package/sinter/components/internal/mail-ins.html +5 -0
  45. package/sinter/components/internal/mail-kbd.html +5 -0
  46. package/sinter/components/internal/mail-li.html +5 -0
  47. package/sinter/components/internal/mail-main.html +5 -0
  48. package/sinter/components/internal/mail-mark.html +5 -0
  49. package/sinter/components/internal/mail-nav.html +5 -0
  50. package/sinter/components/internal/mail-node.html +5 -0
  51. package/sinter/components/internal/mail-ol.html +5 -0
  52. package/sinter/components/internal/mail-orphan.html +5 -0
  53. package/sinter/components/internal/mail-p.html +5 -0
  54. package/sinter/components/internal/mail-pre.html +5 -0
  55. package/sinter/components/internal/mail-q.html +5 -0
  56. package/sinter/components/internal/mail-s.html +5 -0
  57. package/sinter/components/internal/mail-samp.html +5 -0
  58. package/sinter/components/internal/mail-section.html +5 -0
  59. package/sinter/components/internal/mail-small.html +5 -0
  60. package/sinter/components/internal/mail-span.html +5 -0
  61. package/sinter/components/internal/mail-steps.html +5 -0
  62. package/sinter/components/internal/mail-steps.ts +46 -0
  63. package/sinter/components/internal/mail-strike.html +5 -0
  64. package/sinter/components/internal/mail-strong.html +5 -0
  65. package/sinter/components/internal/mail-sub.html +5 -0
  66. package/sinter/components/internal/mail-summary.html +5 -0
  67. package/sinter/components/internal/mail-sup.html +5 -0
  68. package/sinter/components/internal/mail-table.html +5 -0
  69. package/sinter/components/internal/mail-time.html +5 -0
  70. package/sinter/components/internal/mail-tt.html +5 -0
  71. package/sinter/components/internal/mail-u.html +5 -0
  72. package/sinter/components/internal/mail-ul.html +5 -0
  73. package/sinter/components/internal/mail-var.html +5 -0
  74. package/sinter/components/internal/mail-wbr.html +5 -0
  75. package/sinter/components/mail-body.html +21 -0
  76. package/sinter/css/hints.ts +259 -0
  77. package/sinter/css/index.ts +348 -0
  78. package/sinter/css/media.ts +254 -0
  79. package/sinter/css/parse.ts +124 -0
  80. package/sinter/css/selectors.ts +530 -0
  81. package/sinter/css/tokenize.ts +279 -0
  82. package/sinter/css/values.ts +669 -0
  83. package/sinter/engine.ts +6 -0
  84. package/sinter/index.ts +5 -0
  85. package/sinter/parse.ts +359 -0
  86. package/sinter/parser/entities.ts +22 -0
  87. package/sinter/parser/index.ts +54 -0
  88. package/sinter/parser/tokenizer.ts +275 -0
  89. package/sinter/parser/tree.ts +1265 -0
  90. package/sinter/plain.ts +125 -0
  91. package/sinter/raw.ts +28 -0
  92. package/sinter/sinter.helpers.ts +11 -0
  93. package/sinter/sinter.types.ts +144 -0
  94. package/sinter/tags.ts +169 -0
  95. package/sinter/tree.ts +168 -0
@@ -0,0 +1,275 @@
1
+ // The HTML standard's tokenizer, for a whole string at once. It hands the tree builder (tree.ts) its tokens
2
+ // as calls: runs of text with their character references decoded, start and end tags, comments, doctypes
3
+ // and the end of the input. The tree builder answers each start tag with the state the text after it is
4
+ // read in: RCDATA (`<title>`, `<textarea>`), RAWTEXT (`<style>`, `<xmp>`, `<iframe>`, `<noembed>`,
5
+ // `<noframes>`, `<noscript>`), script data or PLAINTEXT.
6
+ //
7
+ // The standard's states are a character-at-a-time machine; most of them only decide where a token ends, so
8
+ // here they are scans: a comment ends at the first `-->` or `--!>`, a doctype at the first `>`, raw text at
9
+ // its end tag. Every scan moves forward, so the time is linear in the input.
10
+ import { ENTITIES, LEGACY } from './entities.js';
11
+
12
+ export type Attrs = [name: string, value: string][];
13
+
14
+ export interface Sink {
15
+ /** text, never empty: NUL kept in data (the tree builder drops or replaces it), U+FFFD in raw text */
16
+ text(s: string): void;
17
+ /** a start tag; returns the state its content is read in (DATA, RCDATA, RAWTEXT, SCRIPT, PLAINTEXT) */
18
+ start(name: string, attrs: Attrs, selfClosing: boolean): number;
19
+ end(name: string): void;
20
+ comment(): void;
21
+ doctype(name: string | null, publicId: string | null, systemId: string | null, forceQuirks: boolean): void;
22
+ /** whether `<![CDATA[` opens a CDATA section here (the current node is foreign) or a bogus comment */
23
+ cdata(): boolean;
24
+ eof(): void;
25
+ }
26
+
27
+ export const DATA = 0, RCDATA = 1, RAWTEXT = 2, SCRIPT = 3, PLAINTEXT = 4;
28
+
29
+ // the characters names and unquoted values end at (a tag name and an attribute name at `/` too, an attribute
30
+ // name at `=` too), scanned by code: a tag is read with no regular expression and no match array
31
+ const isWs = (c: number): boolean => c === 9 || c === 10 || c === 12 || c === 32;
32
+ const TAG_END = 1, NAME_END = 2, VALUE_END = 3;
33
+ const ends = (c: number, kind: number): boolean =>
34
+ isWs(c) || c === 62 || (kind !== VALUE_END && c === 47) || (kind === NAME_END && c === 61);
35
+ const COMMENT_END = /--!?>/g;
36
+ const REFERENCE = /&(?:#[xX]([\dA-Fa-f]+);?|#(\d+);?|([A-Za-z\d]{1,32})(;?))/g;
37
+ // numeric references to 0x80\u20130x9F mean what windows-1252 has there; a code with no character there stays itself
38
+ const C1 = '\u20AC\x81\u201A\u0192\u201E\u2026\u2020\u2021\u02C6\u2030\u0160\u2039\u0152\x8D\u017D\x8F\x90\u2018\u2019\u201C\u201D\u2022\u2013\u2014\u02DC\u2122\u0161\u203A\u0153\x9D\u017E\u0178';
39
+
40
+ /** ASCII lowercase, as the standard lowercases names (`toLowerCase` would also fold other letters) */
41
+ const lower = (s: string): string => (/[A-Z]/.test(s) ? s.replace(/[A-Z]/g, (c) => String.fromCharCode(c.charCodeAt(0) + 32)) : s);
42
+ const nul = (s: string): string => (s.includes('\0') ? s.replace(/\0/g, '\uFFFD') : s);
43
+ const isAlpha = (c: number): boolean => ((c | 32) >= 97 && (c | 32) <= 122);
44
+
45
+ /** `s` with its character references decoded; in an attribute value, a legacy one before `=` or a letter stays */
46
+ function decode(s: string, inAttr: boolean): string {
47
+ if (!s.includes('&')) return s;
48
+ return s.replace(REFERENCE, (m, hex: string | undefined, dec: string | undefined, name: string | undefined, semi: string, at: number) => {
49
+ if (name === undefined) {
50
+ const digits = (hex ?? dec!).replace(/^0+/, '');
51
+ const n = digits.length > 8 ? 0x110000 : parseInt(digits || '0', hex === undefined ? 10 : 16);
52
+ return n === 0 || n > 0x10FFFF || (n >= 0xD800 && n <= 0xDFFF) ? '\uFFFD' : n >= 0x80 && n <= 0x9F ? C1[n - 0x80] : String.fromCodePoint(n);
53
+ }
54
+ if (semi && ENTITIES.has(name)) return ENTITIES.get(name)!;
55
+ // the longest name that is a reference without `;` (at most six letters long)
56
+ for (let k = Math.min(name.length, 6); k > 1; k--) {
57
+ const prefix = name.slice(0, k);
58
+ if (!LEGACY.has(prefix)) continue;
59
+ const next = k < name.length ? name[k] : semi || s[at + m.length];
60
+ if (inAttr && next !== undefined && /[=\dA-Za-z]/.test(next)) return m;
61
+ return ENTITIES.get(prefix)! + m.slice(k + 1);
62
+ }
63
+ return m;
64
+ });
65
+ }
66
+
67
+ /** tokenizes `input`, calling `sink` for each token in order */
68
+ export function tokenize(input: string, sink: Sink): void {
69
+ const s = input.includes('\r') ? input.replace(/\r\n?/g, '\n') : input; // newlines normalized first, as the standard does
70
+ const n = s.length;
71
+ let pos = 0, text = '', state = DATA, last = '';
72
+
73
+ const flush = (): void => {
74
+ if (text) sink.text(text);
75
+ text = '';
76
+ };
77
+ const skipWs = (): void => {
78
+ while (pos < n && isWs(s.charCodeAt(pos))) pos++;
79
+ };
80
+ /** the characters from `pos` up to one that ends `kind` (or the end of the input) */
81
+ const scan = (kind: number): string => {
82
+ const from = pos;
83
+ while (pos < n && !ends(s.charCodeAt(pos), kind)) pos++;
84
+ return s.slice(from, pos);
85
+ };
86
+ /** `</name` followed by whitespace, `/` or `>` at `i`: the end tag raw text ends at */
87
+ const endsAt = (i: number, name: string): boolean =>
88
+ s.charCodeAt(i) === 60 && s.charCodeAt(i + 1) === 47 && lower(s.substr(i + 2, name.length)) === name && /[\t\n\f />]/.test(s.charAt(i + 2 + name.length));
89
+
90
+ /** reads a tag from `pos` (its name's first letter); returns false at the end of the input, which drops it */
91
+ const tag = (isEnd: boolean): boolean => {
92
+ const name = lower(nul(scan(TAG_END)));
93
+ const attrs: Attrs = [];
94
+ let names: Set<string> | null = null, selfClosing = false;
95
+ for (;;) {
96
+ skipWs();
97
+ if (pos >= n) return false;
98
+ let c = s[pos];
99
+ if (c === '>') break;
100
+ if (c === '/') {
101
+ pos++;
102
+ if (s[pos] === '>') { selfClosing = true; break; }
103
+ continue;
104
+ }
105
+ pos++; // the name's first character, which may be `=`
106
+ const an = lower(nul(c + scan(NAME_END)));
107
+ skipWs();
108
+ let value = '';
109
+ if (s[pos] === '=') {
110
+ pos++;
111
+ skipWs();
112
+ c = s[pos];
113
+ if (c === '"' || c === "'") {
114
+ const close = s.indexOf(c, pos + 1);
115
+ if (close < 0) return false;
116
+ value = s.slice(pos + 1, close);
117
+ pos = close + 1;
118
+ } else value = scan(VALUE_END);
119
+ value = nul(decode(value, true));
120
+ }
121
+ // the first of duplicate names is kept; a set only for tags with many attributes
122
+ if (attrs.length > 16 && !names) names = new Set(attrs.map((a) => a[0]));
123
+ if (names ? names.has(an) : attrs.some((a) => a[0] === an)) continue;
124
+ names?.add(an);
125
+ attrs.push([an, value]);
126
+ }
127
+ pos++;
128
+ if (isEnd) sink.end(name);
129
+ else {
130
+ state = sink.start(name, attrs, selfClosing);
131
+ last = name;
132
+ }
133
+ return true;
134
+ };
135
+
136
+ while (pos < n) {
137
+ if (state !== DATA) {
138
+ // raw text: up to its end tag (script data with its escapes), or to the end of the input
139
+ let end = n;
140
+ if (state === SCRIPT) end = scriptEnd(s, pos);
141
+ else if (state !== PLAINTEXT) for (let i = s.indexOf('</', pos); i >= 0; i = s.indexOf('</', i + 2)) if (endsAt(i, last)) { end = i; break; }
142
+ const raw = nul(s.slice(pos, end));
143
+ text += state === RCDATA ? decode(raw, false) : raw;
144
+ pos = end;
145
+ state = DATA;
146
+ continue;
147
+ }
148
+ let i = s.indexOf('<', pos);
149
+ if (i < 0) i = n;
150
+ if (i > pos) text += decode(s.slice(pos, i), false);
151
+ pos = i;
152
+ if (pos >= n) break;
153
+ const c = s.charCodeAt(pos + 1);
154
+ if (isAlpha(c)) {
155
+ flush();
156
+ pos++;
157
+ if (!tag(false)) break;
158
+ } else if (c === 47) { // `</`
159
+ const d = s.charCodeAt(pos + 2);
160
+ if (isAlpha(d)) {
161
+ flush();
162
+ pos += 2;
163
+ if (!tag(true)) break;
164
+ } else if (d === 62) pos += 3; // `</>` is nothing
165
+ else if (pos + 2 >= n) { text += '</'; pos = n; }
166
+ else bogus();
167
+ } else if (c === 33) { // `<!`
168
+ if (s.startsWith('<!--', pos)) {
169
+ flush();
170
+ pos += 4;
171
+ if (s[pos] === '>') pos++;
172
+ else if (s.startsWith('->', pos)) pos += 2;
173
+ else {
174
+ COMMENT_END.lastIndex = pos;
175
+ const m = COMMENT_END.exec(s);
176
+ pos = m ? m.index + m[0].length : n;
177
+ }
178
+ sink.comment();
179
+ } else if (lower(s.substr(pos + 2, 7)) === 'doctype') {
180
+ flush();
181
+ let close = s.indexOf('>', pos + 9);
182
+ if (close < 0) close = n;
183
+ doctype(s.slice(pos + 9, close), close === n, sink);
184
+ pos = close + 1;
185
+ } else if (s.startsWith('<![CDATA[', pos) && sink.cdata()) {
186
+ let close = s.indexOf(']]>', pos + 9);
187
+ if (close < 0) close = n;
188
+ text += s.slice(pos + 9, close);
189
+ pos = close + 3;
190
+ } else bogus();
191
+ } else if (c === 63) bogus(); // `<?`
192
+ else {
193
+ text += '<';
194
+ pos++;
195
+ }
196
+ }
197
+ flush();
198
+ sink.eof();
199
+
200
+ /** a bogus comment: up to the next `>` */
201
+ function bogus(): void {
202
+ flush();
203
+ const close = s.indexOf('>', pos + 2);
204
+ pos = close < 0 ? n : close + 1;
205
+ sink.comment();
206
+ }
207
+ }
208
+
209
+ /** where script data from `pos` ends: the standard's escaped and double-escaped states, which `<!--` opens */
210
+ function scriptEnd(s: string, pos: number): number {
211
+ const n = s.length;
212
+ // 0 data, 1 escaped, 2 double escaped; dashes counts the `-` just read in an escaped state
213
+ let st = 0, dashes = 0;
214
+ const word = (i: number): string => /^[A-Za-z]*/.exec(s.slice(i, i + 7))![0];
215
+ const isScript = (i: number): boolean => lower(word(i)) === 'script' && /[\t\n\f />]/.test(s.charAt(i + 6));
216
+ for (let i = pos; i < n; i++) {
217
+ const c = s[i];
218
+ if (c === '-') {
219
+ if (st) dashes++;
220
+ continue;
221
+ }
222
+ if (c === '>' && st && dashes >= 2) { st = 0; dashes = 0; continue; }
223
+ dashes = 0;
224
+ if (c !== '<') continue;
225
+ if (st === 0) {
226
+ if (s.startsWith('</', i) && isScript(i + 2)) return i;
227
+ if (s.startsWith('<!--', i)) { st = 1; dashes = 2; i += 3; }
228
+ } else if (st === 1) {
229
+ if (s[i + 1] === '/') { if (isScript(i + 2)) return i; }
230
+ else if (isScript(i + 1)) { st = 2; i += 7; }
231
+ } else if (s[i + 1] === '/' && isScript(i + 2)) { st = 1; i += 8; }
232
+ }
233
+ return n;
234
+ }
235
+
236
+ /** a doctype's text after `<!DOCTYPE`, up to its `>` (or the end of the input: `eof`), read as the standard's doctype states read it */
237
+ function doctype(d: string, eof: boolean, sink: Sink): void {
238
+ const n = d.length;
239
+ // `trailing`: text after the system identifier, the one bogus doctype that does not force quirks
240
+ let i = 0, quirks = false, trailing = false;
241
+ const ws = (): void => { while (i < n && /[\t\n\f ]/.test(d[i])) i++; };
242
+ /** a quoted identifier, or null where there is none */
243
+ const quoted = (): string | null => {
244
+ ws();
245
+ const q = d[i];
246
+ if (q !== '"' && q !== "'") { quirks = true; i = n; return null; }
247
+ let close = d.indexOf(q, i + 1);
248
+ if (close < 0) { quirks = true; close = n; } // ended by `>`
249
+ const id = nul(d.slice(i + 1, close));
250
+ i = close + 1;
251
+ return id;
252
+ };
253
+ ws();
254
+ let j = i;
255
+ while (j < n && !/[\t\n\f ]/.test(d[j])) j++;
256
+ const name = i < n ? lower(nul(d.slice(i, j))) : null;
257
+ if (name === null) quirks = true;
258
+ i = j;
259
+ ws();
260
+ let publicId: string | null = null, systemId: string | null = null;
261
+ if (i < n) {
262
+ const keyword = lower(d.substr(i, 6));
263
+ i += 6;
264
+ if (keyword === 'public') {
265
+ publicId = quoted();
266
+ ws();
267
+ if (i < n) systemId = quoted();
268
+ } else if (keyword === 'system') systemId = quoted();
269
+ else { quirks = true; i = n; }
270
+ ws();
271
+ if (i < n) trailing = true;
272
+ }
273
+ // the end of the input forces quirks in every doctype state but the bogus one
274
+ sink.doctype(name, publicId, systemId, quirks || (eof && !trailing));
275
+ }