@mdream/js 1.4.0 → 1.5.2

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.
@@ -1,4 +1,365 @@
1
1
  import { BLOCKQUOTE_SPACING, LIST_ITEM_SPACING, NO_SPACING, TABLE_ROW_SPACING, TagIdMap } from "./const.mjs";
2
+ const HTML_ENTITIES = {
3
+ AElig: "Æ",
4
+ AMP: "&",
5
+ Aacute: "Á",
6
+ Acirc: "Â",
7
+ Agrave: "À",
8
+ $Alpha: "Α",
9
+ Aring: "Å",
10
+ Atilde: "Ã",
11
+ Auml: "Ä",
12
+ $Beta: "Β",
13
+ COPY: "©",
14
+ Ccedil: "Ç",
15
+ $Chi: "Χ",
16
+ $Dagger: "‡",
17
+ $Delta: "Δ",
18
+ ETH: "Ð",
19
+ Eacute: "É",
20
+ Ecirc: "Ê",
21
+ Egrave: "È",
22
+ $Epsilon: "Ε",
23
+ $Eta: "Η",
24
+ Euml: "Ë",
25
+ GT: ">",
26
+ $Gamma: "Γ",
27
+ Iacute: "Í",
28
+ Icirc: "Î",
29
+ Igrave: "Ì",
30
+ $Iota: "Ι",
31
+ Iuml: "Ï",
32
+ $Kappa: "Κ",
33
+ LT: "<",
34
+ $Lambda: "Λ",
35
+ $Mu: "Μ",
36
+ Ntilde: "Ñ",
37
+ $Nu: "Ν",
38
+ $OElig: "Œ",
39
+ Oacute: "Ó",
40
+ Ocirc: "Ô",
41
+ Ograve: "Ò",
42
+ $Omega: "Ω",
43
+ $Omicron: "Ο",
44
+ Oslash: "Ø",
45
+ Otilde: "Õ",
46
+ Ouml: "Ö",
47
+ $Phi: "Φ",
48
+ $Pi: "Π",
49
+ $Prime: "″",
50
+ $Psi: "Ψ",
51
+ QUOT: "\"",
52
+ REG: "®",
53
+ $Rho: "Ρ",
54
+ $Scaron: "Š",
55
+ $Sigma: "Σ",
56
+ THORN: "Þ",
57
+ $Tau: "Τ",
58
+ $Theta: "Θ",
59
+ Uacute: "Ú",
60
+ Ucirc: "Û",
61
+ Ugrave: "Ù",
62
+ $Upsilon: "Υ",
63
+ Uuml: "Ü",
64
+ $Xi: "Ξ",
65
+ Yacute: "Ý",
66
+ $Yuml: "Ÿ",
67
+ $Zeta: "Ζ",
68
+ aacute: "á",
69
+ acirc: "â",
70
+ acute: "´",
71
+ aelig: "æ",
72
+ agrave: "à",
73
+ $alefsym: "ℵ",
74
+ $alpha: "α",
75
+ amp: "&",
76
+ $and: "∧",
77
+ $ang: "∠",
78
+ $apos: "'",
79
+ aring: "å",
80
+ $asymp: "≈",
81
+ atilde: "ã",
82
+ auml: "ä",
83
+ $bdquo: "„",
84
+ $beta: "β",
85
+ brvbar: "¦",
86
+ $bull: "•",
87
+ $cap: "∩",
88
+ ccedil: "ç",
89
+ cedil: "¸",
90
+ cent: "¢",
91
+ $chi: "χ",
92
+ $circ: "ˆ",
93
+ $clubs: "♣",
94
+ $cong: "≅",
95
+ copy: "©",
96
+ $crarr: "↵",
97
+ $cup: "∪",
98
+ curren: "¤",
99
+ $dArr: "⇓",
100
+ $dagger: "†",
101
+ $darr: "↓",
102
+ deg: "°",
103
+ $delta: "δ",
104
+ $diams: "♦",
105
+ divide: "÷",
106
+ eacute: "é",
107
+ ecirc: "ê",
108
+ egrave: "è",
109
+ $empty: "∅",
110
+ $epsilon: "ε",
111
+ $equiv: "≡",
112
+ $eta: "η",
113
+ eth: "ð",
114
+ euml: "ë",
115
+ $euro: "€",
116
+ $exist: "∃",
117
+ $fnof: "ƒ",
118
+ $forall: "∀",
119
+ frac12: "½",
120
+ frac14: "¼",
121
+ frac34: "¾",
122
+ $frasl: "⁄",
123
+ $gamma: "γ",
124
+ $ge: "≥",
125
+ gt: ">",
126
+ $hArr: "⇔",
127
+ $harr: "↔",
128
+ $hearts: "♥",
129
+ $hellip: "…",
130
+ iacute: "í",
131
+ icirc: "î",
132
+ iexcl: "¡",
133
+ igrave: "ì",
134
+ $image: "ℑ",
135
+ $infin: "∞",
136
+ $int: "∫",
137
+ $iota: "ι",
138
+ iquest: "¿",
139
+ $isin: "∈",
140
+ iuml: "ï",
141
+ $kappa: "κ",
142
+ $lArr: "⇐",
143
+ $lambda: "λ",
144
+ $lang: "⟨",
145
+ laquo: "«",
146
+ $larr: "←",
147
+ $lceil: "⌈",
148
+ $ldquo: "“",
149
+ $le: "≤",
150
+ $lfloor: "⌊",
151
+ $lowast: "∗",
152
+ $loz: "◊",
153
+ $lsaquo: "‹",
154
+ $lsquo: "‘",
155
+ lt: "<",
156
+ macr: "¯",
157
+ $mdash: "—",
158
+ micro: "µ",
159
+ middot: "·",
160
+ $minus: "−",
161
+ $mu: "μ",
162
+ $nabla: "∇",
163
+ nbsp: "\xA0",
164
+ $ndash: "–",
165
+ $ne: "≠",
166
+ $ni: "∋",
167
+ not: "¬",
168
+ $notin: "∉",
169
+ $nsub: "⊄",
170
+ ntilde: "ñ",
171
+ $nu: "ν",
172
+ oacute: "ó",
173
+ ocirc: "ô",
174
+ $oelig: "œ",
175
+ ograve: "ò",
176
+ $oline: "‾",
177
+ $omega: "ω",
178
+ $omicron: "ο",
179
+ $oplus: "⊕",
180
+ $or: "∨",
181
+ ordf: "ª",
182
+ ordm: "º",
183
+ oslash: "ø",
184
+ otilde: "õ",
185
+ $otimes: "⊗",
186
+ ouml: "ö",
187
+ para: "¶",
188
+ $part: "∂",
189
+ $permil: "‰",
190
+ $perp: "⊥",
191
+ $phi: "φ",
192
+ $pi: "π",
193
+ $piv: "ϖ",
194
+ plusmn: "±",
195
+ pound: "£",
196
+ $prime: "′",
197
+ $prod: "∏",
198
+ $prop: "∝",
199
+ $psi: "ψ",
200
+ quot: "\"",
201
+ $rArr: "⇒",
202
+ $radic: "√",
203
+ $rang: "⟩",
204
+ raquo: "»",
205
+ $rarr: "→",
206
+ $rceil: "⌉",
207
+ $rdquo: "”",
208
+ $real: "ℜ",
209
+ reg: "®",
210
+ $rfloor: "⌋",
211
+ $rho: "ρ",
212
+ $rsaquo: "›",
213
+ $rsquo: "’",
214
+ $sbquo: "‚",
215
+ $scaron: "š",
216
+ $sdot: "⋅",
217
+ sect: "§",
218
+ shy: "­",
219
+ $sigma: "σ",
220
+ $sigmaf: "ς",
221
+ $sim: "∼",
222
+ $spades: "♠",
223
+ $sub: "⊂",
224
+ $sube: "⊆",
225
+ $sum: "∑",
226
+ $sup: "⊃",
227
+ sup1: "¹",
228
+ sup2: "²",
229
+ sup3: "³",
230
+ $supe: "⊇",
231
+ szlig: "ß",
232
+ $tau: "τ",
233
+ $there4: "∴",
234
+ $theta: "θ",
235
+ $thetasym: "ϑ",
236
+ thorn: "þ",
237
+ $tilde: "˜",
238
+ times: "×",
239
+ $trade: "™",
240
+ $uArr: "⇑",
241
+ uacute: "ú",
242
+ $uarr: "↑",
243
+ ucirc: "û",
244
+ ugrave: "ù",
245
+ uml: "¨",
246
+ $upsih: "ϒ",
247
+ $upsilon: "υ",
248
+ uuml: "ü",
249
+ $weierp: "℘",
250
+ $xi: "ξ",
251
+ yacute: "ý",
252
+ yen: "¥",
253
+ yuml: "ÿ",
254
+ $zeta: "ζ"
255
+ };
256
+ function continuationPrefix(node, listIndentWidths) {
257
+ const chain = [];
258
+ let current = node.parent;
259
+ while (current) {
260
+ chain.push(current);
261
+ current = current.parent;
262
+ }
263
+ let prefix = "";
264
+ let listIndex = 0;
265
+ for (let i = chain.length - 1; i >= 0; i--) {
266
+ const tagId = chain[i].tagId;
267
+ if (tagId === 22) prefix += "> ";
268
+ else if (tagId === 25) {
269
+ prefix += " ".repeat(listIndentWidths[listIndex] ?? 2);
270
+ listIndex++;
271
+ }
272
+ }
273
+ return prefix;
274
+ }
275
+ const C1_REPLACEMENTS = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ";
276
+ function isAsciiAlphaNumeric(code) {
277
+ return code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122;
278
+ }
279
+ function numericReplacement(codePoint) {
280
+ if (codePoint === 0 || codePoint > 1114111 || codePoint >= 55296 && codePoint <= 57343) return "�";
281
+ if (codePoint >= 128 && codePoint <= 159) return C1_REPLACEMENTS[codePoint - 128];
282
+ return String.fromCodePoint(codePoint);
283
+ }
284
+ function decodeHTMLEntities(text, inAttribute = false) {
285
+ let result = "";
286
+ let i = 0;
287
+ const len = text.length;
288
+ while (i < len) {
289
+ if (text.charCodeAt(i) !== 38) {
290
+ const plainStart = i;
291
+ while (i < len && text.charCodeAt(i) !== 38) i++;
292
+ result += text.slice(plainStart, i);
293
+ continue;
294
+ }
295
+ if (i + 2 < len && text.charCodeAt(i + 1) === 35) {
296
+ let end = i + 2;
297
+ const isHex = text.charCodeAt(end) === 120 || text.charCodeAt(end) === 88;
298
+ if (isHex) end++;
299
+ const digitStart = end;
300
+ let codePoint = 0;
301
+ while (end < len) {
302
+ const code = text.charCodeAt(end);
303
+ let digit = -1;
304
+ if (code >= 48 && code <= 57) digit = code - 48;
305
+ else if (isHex && code >= 65 && code <= 70) digit = code - 55;
306
+ else if (isHex && code >= 97 && code <= 102) digit = code - 87;
307
+ if (digit < 0) break;
308
+ codePoint = codePoint * (isHex ? 16 : 10) + digit;
309
+ end++;
310
+ }
311
+ if (end > digitStart) {
312
+ result += numericReplacement(codePoint);
313
+ if (text.charCodeAt(end) === 59) end++;
314
+ i = end;
315
+ continue;
316
+ }
317
+ }
318
+ const nameStart = i + 1;
319
+ let nameEnd = nameStart;
320
+ const scanEnd = Math.min(len, nameStart + 8);
321
+ while (nameEnd < scanEnd && isAsciiAlphaNumeric(text.charCodeAt(nameEnd))) nameEnd++;
322
+ if (nameEnd > nameStart && text.charCodeAt(nameEnd) === 59) {
323
+ const name = text.slice(nameStart, nameEnd);
324
+ const direct = HTML_ENTITIES[name];
325
+ const replacement = typeof direct === "string" ? direct : HTML_ENTITIES[`$${name}`];
326
+ if (replacement !== void 0) {
327
+ result += replacement;
328
+ i = nameEnd + 1;
329
+ continue;
330
+ }
331
+ }
332
+ let legacyEnd = Math.min(nameEnd, nameStart + 6);
333
+ let decodedLegacy = false;
334
+ while (legacyEnd > nameStart) {
335
+ const replacement = HTML_ENTITIES[text.slice(nameStart, legacyEnd)];
336
+ if (typeof replacement === "string") {
337
+ const next = text.charCodeAt(legacyEnd);
338
+ if (replacement !== void 0 && !(inAttribute && (next === 61 || isAsciiAlphaNumeric(next)))) {
339
+ result += replacement;
340
+ i = legacyEnd;
341
+ decodedLegacy = true;
342
+ }
343
+ break;
344
+ }
345
+ legacyEnd--;
346
+ }
347
+ if (decodedLegacy) continue;
348
+ result += "&";
349
+ i++;
350
+ }
351
+ return result;
352
+ }
353
+ function traverseUpToFirstBlockNode(node) {
354
+ let firstBlockParent = node;
355
+ const parentsToIncrement = [firstBlockParent];
356
+ while (firstBlockParent.tagHandler?.isInline) {
357
+ if (!firstBlockParent.parent) break;
358
+ firstBlockParent = firstBlockParent.parent;
359
+ parentsToIncrement.push(firstBlockParent);
360
+ }
361
+ return parentsToIncrement;
362
+ }
2
363
  function resolveUrl(url, origin) {
3
364
  if (!url) return url;
4
365
  if (url.startsWith("//")) return `https:${url}`;
@@ -18,8 +379,13 @@ function isAutolinkUri(s) {
18
379
  }
19
380
  return true;
20
381
  }
21
- function isInsideTableCell(node) {
22
- return (node.depthMap[32] || 0) > 0 || (node.depthMap[31] || 0) > 0;
382
+ function isInsideTableCell(state) {
383
+ const depthMap = state.depthMap;
384
+ return (depthMap[32] || 0) > 0 || (depthMap[31] || 0) > 0;
385
+ }
386
+ function isInsideRawHtmlBlock(state) {
387
+ const depthMap = state.depthMap;
388
+ return Boolean(depthMap[2] || depthMap[3] || depthMap[100] || depthMap[101] || depthMap[99] || depthMap[98]);
23
389
  }
24
390
  function getLanguageFromClass(className) {
25
391
  if (!className) return "";
@@ -28,23 +394,23 @@ function getLanguageFromClass(className) {
28
394
  }
29
395
  function handleHeading(depth) {
30
396
  return {
31
- enter: ({ node }) => {
32
- if ((node.depthMap[26] || 0) > 0) return `<h${depth}>`;
397
+ enter: ({ state }) => {
398
+ if ((state.depthMap?.[26] || 0) > 0) return `<h${depth}>`;
33
399
  return `${"#".repeat(depth)} `;
34
400
  },
35
- exit: ({ node }) => {
36
- if ((node.depthMap[26] || 0) > 0) return `</h${depth}>`;
401
+ exit: ({ state }) => {
402
+ if ((state.depthMap?.[26] || 0) > 0) return `</h${depth}>`;
37
403
  },
38
404
  collapsesInnerWhiteSpace: true
39
405
  };
40
406
  }
41
407
  const Strong = {
42
- enter: ({ node }) => {
43
- if ((node?.depthMap?.[15] || 0) > 1) return "";
408
+ enter: ({ state }) => {
409
+ if ((state.depthMap?.[15] || 0) > 1) return "";
44
410
  return "**";
45
411
  },
46
- exit: ({ node }) => {
47
- if ((node?.depthMap?.[15] || 0) > 1) return "";
412
+ exit: ({ node, state }) => {
413
+ if ((state.depthMap?.[15] || 0) + (node.tagId === 15 ? 1 : 0) > 1) return "";
48
414
  return "**";
49
415
  },
50
416
  collapsesInnerWhiteSpace: true,
@@ -52,12 +418,12 @@ const Strong = {
52
418
  isInline: true
53
419
  };
54
420
  const Emphasis = {
55
- enter: ({ node }) => {
56
- if ((node?.depthMap?.[17] || 0) > 1) return "";
421
+ enter: ({ state }) => {
422
+ if ((state.depthMap?.[17] || 0) > 1) return "";
57
423
  return "_";
58
424
  },
59
- exit: ({ node }) => {
60
- if ((node?.depthMap?.[17] || 0) > 1) return "";
425
+ exit: ({ node, state }) => {
426
+ if ((state.depthMap?.[17] || 0) + (node.tagId === 17 ? 1 : 0) > 1) return "";
61
427
  return "_";
62
428
  },
63
429
  collapsesInnerWhiteSpace: true,
@@ -96,8 +462,10 @@ const tagHandlers = {
96
462
  spacing: NO_SPACING
97
463
  },
98
464
  [6]: {
99
- enter: ({ node }) => {
100
- return isInsideTableCell(node) ? "<br>" : void 0;
465
+ enter: ({ node, state }) => {
466
+ const depthMap = state.depthMap;
467
+ if (isInsideTableCell(state) || isInsideRawHtmlBlock(state) || depthMap[7] || depthMap[8] || depthMap[9] || depthMap[10] || depthMap[11] || depthMap[12]) return "<br>";
468
+ return `\n${continuationPrefix(node, state.listIndentWidths || [])}`;
101
469
  },
102
470
  isSelfClosing: true,
103
471
  spacing: NO_SPACING,
@@ -147,10 +515,10 @@ const tagHandlers = {
147
515
  isInline: true
148
516
  },
149
517
  [22]: {
150
- enter: ({ node, state }) => {
151
- const depth = node.depthMap[22] || 1;
518
+ enter: ({ state }) => {
519
+ const depth = state.depthMap?.[22] || 1;
152
520
  let prefix = "> ".repeat(depth);
153
- if ((node.depthMap[25] || 0) > 0) prefix = `\n${state.listIndent}${prefix}`;
521
+ if ((state.depthMap?.[25] || 0) > 0) prefix = `\n${state.listIndent}${prefix}`;
154
522
  return prefix;
155
523
  },
156
524
  spacing: BLOCKQUOTE_SPACING
@@ -161,12 +529,12 @@ const tagHandlers = {
161
529
  state.preOwnFence = false;
162
530
  state.preFenceLang = getLanguageFromClass(node.attributes?.class);
163
531
  },
164
- exit: ({ node, state }) => {
532
+ exit: ({ state }) => {
165
533
  const ownFence = state.preOwnFence;
166
534
  state.preFencePending = false;
167
535
  state.preOwnFence = false;
168
536
  if (!ownFence) return;
169
- if ((node.depthMap[25] || 0) > 0) {
537
+ if ((state.depthMap?.[25] || 0) > 0) {
170
538
  const indent = state.listIndent;
171
539
  return `\n${indent}\`\`\`\n\n${indent}`;
172
540
  }
@@ -175,24 +543,24 @@ const tagHandlers = {
175
543
  },
176
544
  [23]: {
177
545
  enter: ({ node, state }) => {
178
- if ((node.depthMap[34] || 0) > 0) {
546
+ if ((state.depthMap?.[34] || 0) > 0) {
179
547
  if (state.preOwnFence) return;
180
548
  state.preFencePending = false;
181
549
  const language = getLanguageFromClass(node.attributes?.class);
182
- if ((node.depthMap[25] || 0) > 0) return `\n\n${state.listIndent}\`\`\`${language}\n`;
550
+ if ((state.depthMap?.[25] || 0) > 0) return `\n\n${state.listIndent}\`\`\`${language}\n`;
183
551
  return `\`\`\`${language}\n`;
184
552
  }
185
- if ((node.depthMap[25] || 0) > 0) {
553
+ if ((state.depthMap?.[25] || 0) > 0) {
186
554
  const lastEntry = state.buffer.at(-1);
187
555
  const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
188
556
  if (lastChar && lastChar !== " " && lastChar !== "\n" && lastChar !== " " && lastChar !== "*" && lastChar !== "_" && lastChar !== "~" && lastChar !== "[" && lastChar !== ">") return ` \``;
189
557
  }
190
558
  return "`";
191
559
  },
192
- exit: ({ node, state }) => {
193
- if ((node.depthMap[34] || 0) > 0) {
560
+ exit: ({ state }) => {
561
+ if ((state.depthMap?.[34] || 0) > 0) {
194
562
  if (state.preOwnFence) return;
195
- if ((node.depthMap[25] || 0) > 0) {
563
+ if ((state.depthMap?.[25] || 0) > 0) {
196
564
  const indent = state.listIndent;
197
565
  return `\n${indent}\`\`\`\n\n${indent}`;
198
566
  }
@@ -205,20 +573,20 @@ const tagHandlers = {
205
573
  isInline: true
206
574
  },
207
575
  [24]: {
208
- enter: ({ node }) => isInsideTableCell(node) ? "<ul>" : void 0,
209
- exit: ({ node }) => isInsideTableCell(node) ? "</ul>" : void 0
576
+ enter: ({ state }) => isInsideTableCell(state) ? "<ul>" : void 0,
577
+ exit: ({ state }) => isInsideTableCell(state) ? "</ul>" : void 0
210
578
  },
211
579
  [33]: {
212
- enter: ({ node }) => isInsideTableCell(node) ? "<ol>" : void 0,
213
- exit: ({ node }) => isInsideTableCell(node) ? "</ol>" : void 0
580
+ enter: ({ state }) => isInsideTableCell(state) ? "<ol>" : void 0,
581
+ exit: ({ state }) => isInsideTableCell(state) ? "</ol>" : void 0
214
582
  },
215
583
  [25]: {
216
584
  enter: ({ node, state }) => {
217
- if (isInsideTableCell(node)) return "<li>";
585
+ if (isInsideTableCell(state)) return "<li>";
218
586
  const marker = node.parent?.tagId === 33 ? `${node.index + 1}. ` : "- ";
219
587
  return `${state.listIndent}${marker}`;
220
588
  },
221
- exit: ({ node }) => isInsideTableCell(node) ? "</li>" : void 0,
589
+ exit: ({ state }) => isInsideTableCell(state) ? "</li>" : void 0,
222
590
  spacing: LIST_ITEM_SPACING
223
591
  },
224
592
  [26]: {
@@ -264,29 +632,29 @@ const tagHandlers = {
264
632
  isInline: true
265
633
  },
266
634
  [28]: {
267
- enter: ({ node, state }) => {
268
- if (isInsideTableCell(node)) return "<table>";
269
- if ((node.depthMap[28] || 0) <= 1) state.tableRenderedTable = false;
635
+ enter: ({ state }) => {
636
+ if (isInsideTableCell(state)) return "<table>";
637
+ if ((state.depthMap?.[28] || 0) <= 1) state.tableRenderedTable = false;
270
638
  state.tableColumnAlignments = [];
271
639
  },
272
- exit: ({ node }) => isInsideTableCell(node) ? "</table>" : void 0
640
+ exit: ({ state }) => isInsideTableCell(state) ? "</table>" : void 0
273
641
  },
274
642
  [29]: {
275
- enter: ({ node }) => {
276
- if (isInsideTableCell(node)) return "<thead>";
643
+ enter: ({ state }) => {
644
+ if (isInsideTableCell(state)) return "<thead>";
277
645
  },
278
- exit: ({ node }) => isInsideTableCell(node) ? "</thead>" : void 0,
646
+ exit: ({ state }) => isInsideTableCell(state) ? "</thead>" : void 0,
279
647
  spacing: TABLE_ROW_SPACING,
280
648
  excludesTextNodes: true
281
649
  },
282
650
  [30]: {
283
- enter: ({ node, state }) => {
284
- if (isInsideTableCell(node)) return "<tr>";
651
+ enter: ({ state }) => {
652
+ if (isInsideTableCell(state)) return "<tr>";
285
653
  state.tableCurrentRowCells = 0;
286
654
  return "| ";
287
655
  },
288
- exit: ({ node, state }) => {
289
- if (isInsideTableCell(node) || (node.depthMap[28] || 0) > 1) return "</tr>";
656
+ exit: ({ state }) => {
657
+ if (isInsideTableCell(state) || (state.depthMap?.[28] || 0) > 1) return "</tr>";
290
658
  if (!state.tableRenderedTable) {
291
659
  state.tableRenderedTable = true;
292
660
  const alignments = state.tableColumnAlignments;
@@ -307,33 +675,33 @@ const tagHandlers = {
307
675
  },
308
676
  [31]: {
309
677
  enter: ({ node, state }) => {
310
- if ((node.depthMap[28] || 0) > 1) return "<th>";
678
+ if ((state.depthMap?.[28] || 0) > 1) return "<th>";
311
679
  const align = node.attributes?.align?.toLowerCase();
312
680
  if (align) state.tableColumnAlignments.push(align);
313
681
  else if (state.tableColumnAlignments.length <= state.tableCurrentRowCells) state.tableColumnAlignments.push("");
314
682
  return node.index === 0 ? "" : " | ";
315
683
  },
316
- exit: ({ node, state }) => {
317
- if ((node.depthMap[28] || 0) > 1) return "</th>";
684
+ exit: ({ state }) => {
685
+ if ((state.depthMap?.[28] || 0) > 1) return "</th>";
318
686
  state.tableCurrentRowCells++;
319
687
  },
320
688
  collapsesInnerWhiteSpace: true,
321
689
  spacing: NO_SPACING
322
690
  },
323
691
  [32]: {
324
- enter: ({ node }) => {
325
- if ((node.depthMap[28] || 0) > 1) return "<td>";
692
+ enter: ({ node, state }) => {
693
+ if ((state.depthMap?.[28] || 0) > 1) return "<td>";
326
694
  return node.index === 0 ? "" : " | ";
327
695
  },
328
- exit: ({ node, state }) => {
329
- if ((node.depthMap[28] || 0) > 1) return "</td>";
696
+ exit: ({ state }) => {
697
+ if ((state.depthMap?.[28] || 0) > 1) return "</td>";
330
698
  state.tableCurrentRowCells++;
331
699
  },
332
700
  collapsesInnerWhiteSpace: true,
333
701
  spacing: NO_SPACING
334
702
  },
335
- [35]: { enter: ({ node, state }) => {
336
- const bqDepth = node.depthMap[22] || 0;
703
+ [35]: { enter: ({ state }) => {
704
+ const bqDepth = state.depthMap?.[22] || 0;
337
705
  if (bqDepth > 0) {
338
706
  const lastEntry = state.buffer.at(-1);
339
707
  const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
@@ -342,7 +710,7 @@ const tagHandlers = {
342
710
  return `\n${prefix.trimEnd()}\n${prefix}`;
343
711
  }
344
712
  }
345
- if ((node.depthMap[25] || 0) > 0 && !isInsideTableCell(node)) {
713
+ if ((state.depthMap?.[25] || 0) > 0 && !isInsideTableCell(state)) {
346
714
  const lastEntry = state.buffer.at(-1);
347
715
  const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
348
716
  if (lastChar && lastChar !== " " && lastChar !== "\n") return `\n\n${state.listIndent}`;
@@ -362,15 +730,16 @@ const tagHandlers = {
362
730
  },
363
731
  [43]: {
364
732
  collapsesInnerWhiteSpace: true,
733
+ spacing: NO_SPACING,
365
734
  isInline: true
366
735
  },
367
736
  [44]: { spacing: NO_SPACING },
368
737
  [45]: {
369
- enter: ({ node }) => {
370
- if ((node.depthMap[28] || 0) > 1) return "<center>";
738
+ enter: ({ state }) => {
739
+ if ((state.depthMap?.[28] || 0) > 1) return "<center>";
371
740
  },
372
- exit: ({ node }) => {
373
- if ((node.depthMap[28] || 0) > 1) return "</center>";
741
+ exit: ({ state }) => {
742
+ if ((state.depthMap?.[28] || 0) > 1) return "</center>";
374
743
  },
375
744
  spacing: NO_SPACING
376
745
  },
@@ -453,10 +822,8 @@ const tagHandlers = {
453
822
  isNonNesting: true,
454
823
  spacing: NO_SPACING
455
824
  },
456
- [67]: {
457
- isNonNesting: true,
458
- spacing: NO_SPACING
459
- },
825
+ [67]: { spacing: NO_SPACING },
826
+ [109]: { spacing: NO_SPACING },
460
827
  [68]: { spacing: NO_SPACING },
461
828
  [69]: { spacing: NO_SPACING },
462
829
  [70]: { spacing: NO_SPACING },
@@ -471,7 +838,6 @@ const tagHandlers = {
471
838
  [76]: { spacing: NO_SPACING },
472
839
  [77]: { spacing: NO_SPACING },
473
840
  [78]: {
474
- isNonNesting: true,
475
841
  excludesTextNodes: true,
476
842
  spacing: NO_SPACING
477
843
  },
@@ -643,7 +1009,10 @@ function buildTagOverrideHandlers(overrides) {
643
1009
  const targetId = TagIdMap[override];
644
1010
  if (targetId !== void 0) {
645
1011
  const baseHandler = tagHandlers[targetId];
646
- if (baseHandler) result.set(tagName, { ...baseHandler });
1012
+ if (baseHandler) result.set(tagName, {
1013
+ ...baseHandler,
1014
+ aliasTagId: targetId
1015
+ });
647
1016
  }
648
1017
  } else {
649
1018
  const baseId = TagIdMap[tagName];
@@ -667,303 +1036,6 @@ function buildTagOverrideHandlers(overrides) {
667
1036
  }
668
1037
  return result;
669
1038
  }
670
- const HTML_ENTITIES = {
671
- "&aacute;": "á",
672
- "&Aacute;": "Á",
673
- "&acirc;": "â",
674
- "&Acirc;": "Â",
675
- "&acute;": "´",
676
- "&aelig;": "æ",
677
- "&AElig;": "Æ",
678
- "&agrave;": "à",
679
- "&Agrave;": "À",
680
- "&alefsym;": "ℵ",
681
- "&alpha;": "α",
682
- "&Alpha;": "Α",
683
- "&amp;": "&",
684
- "&and;": "∧",
685
- "&ang;": "∠",
686
- "&apos;": "'",
687
- "&aring;": "å",
688
- "&Aring;": "Å",
689
- "&asymp;": "≈",
690
- "&atilde;": "ã",
691
- "&Atilde;": "Ã",
692
- "&auml;": "ä",
693
- "&Auml;": "Ä",
694
- "&bdquo;": "„",
695
- "&beta;": "β",
696
- "&Beta;": "Β",
697
- "&brvbar;": "¦",
698
- "&bull;": "•",
699
- "&cap;": "∩",
700
- "&ccedil;": "ç",
701
- "&Ccedil;": "Ç",
702
- "&cedil;": "¸",
703
- "&cent;": "¢",
704
- "&chi;": "χ",
705
- "&Chi;": "Χ",
706
- "&circ;": "ˆ",
707
- "&clubs;": "♣",
708
- "&cong;": "≅",
709
- "&copy;": "©",
710
- "&crarr;": "↵",
711
- "&cup;": "∪",
712
- "&curren;": "¤",
713
- "&dagger;": "†",
714
- "&Dagger;": "‡",
715
- "&darr;": "↓",
716
- "&dArr;": "⇓",
717
- "&deg;": "°",
718
- "&delta;": "δ",
719
- "&Delta;": "Δ",
720
- "&diams;": "♦",
721
- "&divide;": "÷",
722
- "&eacute;": "é",
723
- "&Eacute;": "É",
724
- "&ecirc;": "ê",
725
- "&Ecirc;": "Ê",
726
- "&egrave;": "è",
727
- "&Egrave;": "È",
728
- "&empty;": "∅",
729
- "&epsilon;": "ε",
730
- "&Epsilon;": "Ε",
731
- "&equiv;": "≡",
732
- "&eta;": "η",
733
- "&Eta;": "Η",
734
- "&eth;": "ð",
735
- "&ETH;": "Ð",
736
- "&euml;": "ë",
737
- "&Euml;": "Ë",
738
- "&euro;": "€",
739
- "&exist;": "∃",
740
- "&fnof;": "ƒ",
741
- "&forall;": "∀",
742
- "&frac12;": "½",
743
- "&frac14;": "¼",
744
- "&frac34;": "¾",
745
- "&frasl;": "⁄",
746
- "&gamma;": "γ",
747
- "&Gamma;": "Γ",
748
- "&ge;": "≥",
749
- "&gt;": ">",
750
- "&harr;": "↔",
751
- "&hArr;": "⇔",
752
- "&hearts;": "♥",
753
- "&hellip;": "…",
754
- "&iacute;": "í",
755
- "&Iacute;": "Í",
756
- "&icirc;": "î",
757
- "&Icirc;": "Î",
758
- "&iexcl;": "¡",
759
- "&igrave;": "ì",
760
- "&Igrave;": "Ì",
761
- "&image;": "ℑ",
762
- "&infin;": "∞",
763
- "&int;": "∫",
764
- "&iota;": "ι",
765
- "&Iota;": "Ι",
766
- "&iquest;": "¿",
767
- "&isin;": "∈",
768
- "&iuml;": "ï",
769
- "&Iuml;": "Ï",
770
- "&kappa;": "κ",
771
- "&Kappa;": "Κ",
772
- "&lambda;": "λ",
773
- "&Lambda;": "Λ",
774
- "&lang;": "⟨",
775
- "&laquo;": "«",
776
- "&larr;": "←",
777
- "&lArr;": "⇐",
778
- "&lceil;": "⌈",
779
- "&ldquo;": "“",
780
- "&le;": "≤",
781
- "&lfloor;": "⌊",
782
- "&lowast;": "∗",
783
- "&loz;": "◊",
784
- "&lsaquo;": "‹",
785
- "&lsquo;": "‘",
786
- "&lt;": "<",
787
- "&macr;": "¯",
788
- "&mdash;": "—",
789
- "&micro;": "µ",
790
- "&middot;": "·",
791
- "&minus;": "−",
792
- "&mu;": "μ",
793
- "&Mu;": "Μ",
794
- "&nabla;": "∇",
795
- "&nbsp;": " ",
796
- "&ndash;": "–",
797
- "&ne;": "≠",
798
- "&ni;": "∋",
799
- "&not;": "¬",
800
- "&notin;": "∉",
801
- "&nsub;": "⊄",
802
- "&ntilde;": "ñ",
803
- "&Ntilde;": "Ñ",
804
- "&nu;": "ν",
805
- "&Nu;": "Ν",
806
- "&oacute;": "ó",
807
- "&Oacute;": "Ó",
808
- "&ocirc;": "ô",
809
- "&Ocirc;": "Ô",
810
- "&oelig;": "œ",
811
- "&OElig;": "Œ",
812
- "&ograve;": "ò",
813
- "&Ograve;": "Ò",
814
- "&oline;": "‾",
815
- "&omega;": "ω",
816
- "&Omega;": "Ω",
817
- "&omicron;": "ο",
818
- "&Omicron;": "Ο",
819
- "&oplus;": "⊕",
820
- "&or;": "∨",
821
- "&ordf;": "ª",
822
- "&ordm;": "º",
823
- "&oslash;": "ø",
824
- "&Oslash;": "Ø",
825
- "&otilde;": "õ",
826
- "&Otilde;": "Õ",
827
- "&otimes;": "⊗",
828
- "&ouml;": "ö",
829
- "&Ouml;": "Ö",
830
- "&para;": "¶",
831
- "&part;": "∂",
832
- "&permil;": "‰",
833
- "&perp;": "⊥",
834
- "&phi;": "φ",
835
- "&Phi;": "Φ",
836
- "&pi;": "π",
837
- "&Pi;": "Π",
838
- "&piv;": "ϖ",
839
- "&plusmn;": "±",
840
- "&pound;": "£",
841
- "&prime;": "′",
842
- "&Prime;": "″",
843
- "&prod;": "∏",
844
- "&prop;": "∝",
845
- "&psi;": "ψ",
846
- "&Psi;": "Ψ",
847
- "&quot;": "\"",
848
- "&radic;": "√",
849
- "&rang;": "⟩",
850
- "&raquo;": "»",
851
- "&rarr;": "→",
852
- "&rArr;": "⇒",
853
- "&rceil;": "⌉",
854
- "&rdquo;": "”",
855
- "&real;": "ℜ",
856
- "&reg;": "®",
857
- "&rfloor;": "⌋",
858
- "&rho;": "ρ",
859
- "&Rho;": "Ρ",
860
- "&rsaquo;": "›",
861
- "&rsquo;": "’",
862
- "&sbquo;": "‚",
863
- "&scaron;": "š",
864
- "&Scaron;": "Š",
865
- "&sdot;": "⋅",
866
- "&sect;": "§",
867
- "&sigma;": "σ",
868
- "&Sigma;": "Σ",
869
- "&sigmaf;": "ς",
870
- "&sim;": "∼",
871
- "&spades;": "♠",
872
- "&sub;": "⊂",
873
- "&sube;": "⊆",
874
- "&sum;": "∑",
875
- "&sup;": "⊃",
876
- "&sup1;": "¹",
877
- "&sup2;": "²",
878
- "&sup3;": "³",
879
- "&supe;": "⊇",
880
- "&szlig;": "ß",
881
- "&tau;": "τ",
882
- "&Tau;": "Τ",
883
- "&there4;": "∴",
884
- "&theta;": "θ",
885
- "&Theta;": "Θ",
886
- "&thetasym;": "ϑ",
887
- "&thorn;": "þ",
888
- "&THORN;": "Þ",
889
- "&tilde;": "˜",
890
- "&times;": "×",
891
- "&trade;": "™",
892
- "&uacute;": "ú",
893
- "&Uacute;": "Ú",
894
- "&uarr;": "↑",
895
- "&uArr;": "⇑",
896
- "&ucirc;": "û",
897
- "&Ucirc;": "Û",
898
- "&ugrave;": "ù",
899
- "&Ugrave;": "Ù",
900
- "&uml;": "¨",
901
- "&upsih;": "ϒ",
902
- "&upsilon;": "υ",
903
- "&Upsilon;": "Υ",
904
- "&uuml;": "ü",
905
- "&Uuml;": "Ü",
906
- "&weierp;": "℘",
907
- "&xi;": "ξ",
908
- "&Xi;": "Ξ",
909
- "&yacute;": "ý",
910
- "&Yacute;": "Ý",
911
- "&yen;": "¥",
912
- "&yuml;": "ÿ",
913
- "&Yuml;": "Ÿ",
914
- "&zeta;": "ζ",
915
- "&Zeta;": "Ζ"
916
- };
917
- function decodeHTMLEntities(text) {
918
- let result = "";
919
- let i = 0;
920
- const len = text.length;
921
- while (i < len) {
922
- if (text.charCodeAt(i) === 38) if (i + 2 < len && text.charCodeAt(i + 1) === 35) {
923
- const start = i;
924
- i += 2;
925
- const isHex = text.charCodeAt(i) === 120 || text.charCodeAt(i) === 88;
926
- if (isHex) i++;
927
- const numStart = i;
928
- const maxDigits = i + (isHex ? 7 : 8);
929
- while (i < len && i < maxDigits && text.charCodeAt(i) !== 59) i++;
930
- if (i < len && text.charCodeAt(i) === 59) {
931
- const codePoint = Number.parseInt(text.substring(numStart, i), isHex ? 16 : 10);
932
- if (codePoint >= 0 && codePoint <= 1114111 && !Number.isNaN(codePoint)) {
933
- result += String.fromCodePoint(codePoint);
934
- i++;
935
- continue;
936
- }
937
- }
938
- i = start;
939
- } else {
940
- const maxEnd = i + 33 < len ? i + 33 : len;
941
- let j = i + 1;
942
- while (j < maxEnd && text.charCodeAt(j) !== 59) j++;
943
- if (j < maxEnd && text.charCodeAt(j) === 59) {
944
- const replacement = HTML_ENTITIES[text.substring(i, j + 1)];
945
- if (replacement !== void 0) {
946
- result += replacement;
947
- i = j + 1;
948
- continue;
949
- }
950
- }
951
- }
952
- result += text[i];
953
- i++;
954
- }
955
- return result;
956
- }
957
- function traverseUpToFirstBlockNode(node) {
958
- let firstBlockParent = node;
959
- const parentsToIncrement = [firstBlockParent];
960
- while (firstBlockParent.tagHandler?.isInline) {
961
- if (!firstBlockParent.parent) break;
962
- firstBlockParent = firstBlockParent.parent;
963
- parentsToIncrement.push(firstBlockParent);
964
- }
965
- return parentsToIncrement;
966
- }
967
1039
  const LT_CHAR = 60;
968
1040
  const GT_CHAR = 62;
969
1041
  const SLASH_CHAR = 47;
@@ -977,29 +1049,403 @@ const DASH_CHAR = 45;
977
1049
  const SPACE_CHAR = 32;
978
1050
  const TAB_CHAR = 9;
979
1051
  const NEWLINE_CHAR = 10;
1052
+ const FORM_FEED_CHAR = 12;
980
1053
  const CARRIAGE_RETURN_CHAR = 13;
981
1054
  const BACKTICK_CHAR = 96;
982
1055
  const PIPE_CHAR = 124;
983
1056
  const OPEN_BRACKET_CHAR = 91;
984
1057
  const CLOSE_BRACKET_CHAR = 93;
985
- const EMPTY_ATTRIBUTES = Object.freeze({});
986
- function copyDepthMap(depthMap) {
987
- return new Uint8Array(depthMap);
1058
+ const SCRIPT_DATA = 0;
1059
+ const SCRIPT_DATA_ESCAPED = 1;
1060
+ const SCRIPT_DATA_ESCAPED_DASH = 2;
1061
+ const SCRIPT_DATA_ESCAPED_DASH_DASH = 3;
1062
+ const SCRIPT_DATA_DOUBLE_ESCAPED = 4;
1063
+ const SCRIPT_DATA_DOUBLE_ESCAPED_DASH = 5;
1064
+ const SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH = 6;
1065
+ const SCRIPT_SEQUENCE_NO_MATCH = -1;
1066
+ const SCRIPT_SEQUENCE_INCOMPLETE = -2;
1067
+ const SCRIPT_SCAN_COMPLETE = -1;
1068
+ const HEAD_CONTENT_TAGS = /* @__PURE__ */ new Set([
1069
+ 4,
1070
+ 5,
1071
+ 54,
1072
+ 56,
1073
+ 53,
1074
+ 52,
1075
+ 84,
1076
+ 78
1077
+ ]);
1078
+ const CLOSES_P = (() => {
1079
+ const t = /* @__PURE__ */ new Uint8Array(110);
1080
+ for (const id of [
1081
+ 36,
1082
+ 35,
1083
+ 24,
1084
+ 33,
1085
+ 101,
1086
+ 25,
1087
+ 98,
1088
+ 99,
1089
+ 28,
1090
+ 7,
1091
+ 8,
1092
+ 9,
1093
+ 10,
1094
+ 11,
1095
+ 12,
1096
+ 22,
1097
+ 51,
1098
+ 50,
1099
+ 105,
1100
+ 47,
1101
+ 41,
1102
+ 88,
1103
+ 34,
1104
+ 13,
1105
+ 40,
1106
+ 68,
1107
+ 102,
1108
+ 106,
1109
+ 100,
1110
+ 104,
1111
+ 45,
1112
+ 2,
1113
+ 3,
1114
+ 75
1115
+ ]) t[id] = 1;
1116
+ return t;
1117
+ })();
1118
+ const NEEDS_IMPLIED_END_RECOVERY = (() => {
1119
+ const t = CLOSES_P.slice();
1120
+ for (const id of [
1121
+ 26,
1122
+ 32,
1123
+ 31,
1124
+ 30,
1125
+ 29,
1126
+ 38,
1127
+ 39,
1128
+ 67,
1129
+ 109,
1130
+ 65
1131
+ ]) t[id] = 1;
1132
+ return t;
1133
+ })();
1134
+ const P_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1135
+ 43,
1136
+ 32,
1137
+ 31,
1138
+ 107,
1139
+ 28,
1140
+ 78,
1141
+ 0,
1142
+ 24,
1143
+ 33,
1144
+ 101,
1145
+ 25
1146
+ ]);
1147
+ const LI_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1148
+ 24,
1149
+ 33,
1150
+ 28,
1151
+ 32,
1152
+ 31,
1153
+ 107,
1154
+ 78,
1155
+ 0
1156
+ ]);
1157
+ const DL_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1158
+ 101,
1159
+ 24,
1160
+ 33,
1161
+ 25,
1162
+ 28,
1163
+ 32,
1164
+ 31,
1165
+ 107,
1166
+ 78,
1167
+ 0
1168
+ ]);
1169
+ const CELL_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1170
+ 30,
1171
+ 29,
1172
+ 38,
1173
+ 39,
1174
+ 28,
1175
+ 107,
1176
+ 78,
1177
+ 0
1178
+ ]);
1179
+ const A_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1180
+ 35,
1181
+ 36,
1182
+ 25,
1183
+ 24,
1184
+ 33,
1185
+ 101,
1186
+ 98,
1187
+ 99,
1188
+ 28,
1189
+ 32,
1190
+ 31,
1191
+ 30,
1192
+ 107,
1193
+ 22,
1194
+ 51,
1195
+ 50,
1196
+ 105,
1197
+ 47,
1198
+ 41,
1199
+ 88,
1200
+ 104,
1201
+ 40,
1202
+ 68,
1203
+ 102,
1204
+ 43,
1205
+ 7,
1206
+ 8,
1207
+ 9,
1208
+ 10,
1209
+ 11,
1210
+ 12,
1211
+ 78,
1212
+ 0
1213
+ ]);
1214
+ const HEADINGS = /* @__PURE__ */ new Set([
1215
+ 7,
1216
+ 8,
1217
+ 9,
1218
+ 10,
1219
+ 11,
1220
+ 12
1221
+ ]);
1222
+ const SINGLE_P = /* @__PURE__ */ new Set([35]);
1223
+ const SINGLE_LI = /* @__PURE__ */ new Set([25]);
1224
+ const SINGLE_A = /* @__PURE__ */ new Set([26]);
1225
+ const DT_DD = /* @__PURE__ */ new Set([99, 98]);
1226
+ const TD_TH = /* @__PURE__ */ new Set([32, 31]);
1227
+ const TR_CELLS = /* @__PURE__ */ new Set([
1228
+ 32,
1229
+ 31,
1230
+ 30
1231
+ ]);
1232
+ const SECTION_CELLS = /* @__PURE__ */ new Set([
1233
+ 32,
1234
+ 31,
1235
+ 30,
1236
+ 29,
1237
+ 38,
1238
+ 39,
1239
+ 107
1240
+ ]);
1241
+ function closeImpliedTo(state, target, boundary, handleEvent) {
1242
+ let found = false;
1243
+ for (let node = state.currentNode; node; node = node.parent) {
1244
+ const id = node.tagId;
1245
+ if (id !== void 0 && target.has(id)) {
1246
+ found = true;
1247
+ break;
1248
+ }
1249
+ if (id !== void 0 && boundary.has(id)) break;
1250
+ }
1251
+ if (!found) return;
1252
+ while (state.currentNode) {
1253
+ const id = state.currentNode.tagId;
1254
+ const isTarget = id !== void 0 && target.has(id);
1255
+ closeNode(state.currentNode, state, handleEvent);
1256
+ if (isTarget) break;
1257
+ }
1258
+ }
1259
+ function closeTableContext(state, closeable, handleEvent) {
1260
+ while (state.currentNode) {
1261
+ const id = state.currentNode.tagId;
1262
+ if (id === void 0 || !closeable.has(id)) break;
1263
+ closeNode(state.currentNode, state, handleEvent);
1264
+ }
1265
+ }
1266
+ function closeSelectTo(state, targetId, handleEvent) {
1267
+ let target;
1268
+ for (let node = state.currentNode; node; node = node.parent) {
1269
+ if (node.tagId === targetId) {
1270
+ target = node;
1271
+ break;
1272
+ }
1273
+ if (node.tagId === 65 || node.tagId === 78) break;
1274
+ }
1275
+ if (!target) return false;
1276
+ while (state.currentNode && state.currentNode !== target) closeNode(state.currentNode, state, handleEvent);
1277
+ closeNode(target, state, handleEvent);
1278
+ return true;
1279
+ }
1280
+ function finalizeParse(leftover, state, handleEvent) {
1281
+ if (state.currentNode && isScriptElement(state.currentNode)) {
1282
+ pushScriptTextChunk(state, leftover);
1283
+ const scriptText = takeScriptText(state);
1284
+ if (scriptText) processTextBuffer(scriptText, state, handleEvent);
1285
+ } else if (leftover.length > 0 && leftover.charCodeAt(0) !== LT_CHAR) processTextBuffer(leftover, state, handleEvent);
1286
+ while (state.currentNode) closeNode(state.currentNode, state, handleEvent);
988
1287
  }
1288
+ const EMPTY_ATTRIBUTES = Object.freeze({});
1289
+ var ParsedElementNode = class {
1290
+ type = 1;
1291
+ name;
1292
+ attributes;
1293
+ parent;
1294
+ depth;
1295
+ index;
1296
+ tagId;
1297
+ tagHandler;
1298
+ currentWalkIndex;
1299
+ childTextNodeIndex;
1300
+ containsWhitespace;
1301
+ pluginOutput;
1302
+ context;
1303
+ cachedDepthMap;
1304
+ constructor(name, attributes, parent, depth, index, tagId, tagHandler) {
1305
+ this.name = name;
1306
+ this.attributes = attributes;
1307
+ this.parent = parent;
1308
+ this.depth = depth;
1309
+ this.index = index;
1310
+ this.tagId = tagId;
1311
+ this.tagHandler = tagHandler;
1312
+ }
1313
+ get depthMap() {
1314
+ if (this.cachedDepthMap) return this.cachedDepthMap;
1315
+ const depthMap = /* @__PURE__ */ new Uint8Array(110);
1316
+ if (this.tagId >= 0 && this.tagId < 110) depthMap[this.tagId] = 1;
1317
+ let node = this.parent;
1318
+ while (node) {
1319
+ const tagId = node.tagId;
1320
+ if (tagId !== void 0 && tagId >= 0 && tagId < 110) depthMap[tagId] = (depthMap[tagId] || 0) + 1;
1321
+ node = node.parent;
1322
+ }
1323
+ this.cachedDepthMap = depthMap;
1324
+ return depthMap;
1325
+ }
1326
+ };
989
1327
  function isWhitespace(charCode) {
990
- return charCode === SPACE_CHAR || charCode === TAB_CHAR || charCode === NEWLINE_CHAR || charCode === CARRIAGE_RETURN_CHAR;
1328
+ return charCode === SPACE_CHAR || charCode === TAB_CHAR || charCode === NEWLINE_CHAR || charCode === FORM_FEED_CHAR || charCode === CARRIAGE_RETURN_CHAR;
1329
+ }
1330
+ function scriptSequenceEnd(html, nameStart) {
1331
+ const name = "script";
1332
+ for (let offset = 0; offset < 6; offset++) {
1333
+ const index = nameStart + offset;
1334
+ if (index >= html.length) return SCRIPT_SEQUENCE_INCOMPLETE;
1335
+ if ((html.charCodeAt(index) | 32) !== name.charCodeAt(offset)) return SCRIPT_SEQUENCE_NO_MATCH;
1336
+ }
1337
+ const delimiterIndex = nameStart + 6;
1338
+ if (delimiterIndex >= html.length) return SCRIPT_SEQUENCE_INCOMPLETE;
1339
+ const delimiter = html.charCodeAt(delimiterIndex);
1340
+ return delimiter === GT_CHAR || delimiter === SLASH_CHAR || isWhitespace(delimiter) ? delimiterIndex + 1 : SCRIPT_SEQUENCE_NO_MATCH;
1341
+ }
1342
+ function isScriptElement(node) {
1343
+ return node.tagId === 52 && node.name === "script";
1344
+ }
1345
+ function scriptScanResult(parseState, scriptState, result) {
1346
+ parseState.scriptDataState = scriptState;
1347
+ return result;
1348
+ }
1349
+ function findScriptEndTag(html, start, parseState) {
1350
+ let state = parseState.scriptDataState ?? SCRIPT_DATA;
1351
+ let i = start;
1352
+ while (i < html.length) {
1353
+ const charCode = html.charCodeAt(i);
1354
+ if (charCode === LT_CHAR) {
1355
+ if (i + 1 === html.length) return scriptScanResult(parseState, state, -i - 2);
1356
+ const next = html.charCodeAt(i + 1);
1357
+ if (state === SCRIPT_DATA) {
1358
+ if (next === SLASH_CHAR) {
1359
+ const sequenceEnd = scriptSequenceEnd(html, i + 2);
1360
+ if (sequenceEnd >= 0) return scriptScanResult(parseState, state, i);
1361
+ if (sequenceEnd === SCRIPT_SEQUENCE_INCOMPLETE) return scriptScanResult(parseState, state, -i - 2);
1362
+ } else if (next === EXCLAMATION_CHAR) {
1363
+ const marker = "<!--";
1364
+ const available = Math.min(html.length - i, 4);
1365
+ if (html.startsWith(marker.substring(0, available), i)) {
1366
+ if (available < 4) return scriptScanResult(parseState, state, -i - 2);
1367
+ state = SCRIPT_DATA_ESCAPED_DASH_DASH;
1368
+ i += 4;
1369
+ continue;
1370
+ }
1371
+ }
1372
+ } else {
1373
+ const escaped = state < SCRIPT_DATA_DOUBLE_ESCAPED;
1374
+ state = escaped ? SCRIPT_DATA_ESCAPED : SCRIPT_DATA_DOUBLE_ESCAPED;
1375
+ const isEndTag = next === SLASH_CHAR;
1376
+ if (isEndTag || escaped) {
1377
+ const sequenceEnd = scriptSequenceEnd(html, i + (isEndTag ? 2 : 1));
1378
+ if (sequenceEnd >= 0) {
1379
+ if (isEndTag) {
1380
+ if (escaped) return scriptScanResult(parseState, state, i);
1381
+ state = SCRIPT_DATA_ESCAPED;
1382
+ } else state = SCRIPT_DATA_DOUBLE_ESCAPED;
1383
+ i = sequenceEnd;
1384
+ continue;
1385
+ }
1386
+ if (sequenceEnd === SCRIPT_SEQUENCE_INCOMPLETE) return scriptScanResult(parseState, state, -i - 2);
1387
+ }
1388
+ }
1389
+ } else if (charCode === DASH_CHAR) {
1390
+ if (state === SCRIPT_DATA_ESCAPED || state === SCRIPT_DATA_ESCAPED_DASH) state++;
1391
+ else if (state === SCRIPT_DATA_DOUBLE_ESCAPED || state === SCRIPT_DATA_DOUBLE_ESCAPED_DASH) state++;
1392
+ } else if (state === SCRIPT_DATA_ESCAPED_DASH || state === SCRIPT_DATA_ESCAPED_DASH_DASH) state = charCode === GT_CHAR && state === SCRIPT_DATA_ESCAPED_DASH_DASH ? SCRIPT_DATA : SCRIPT_DATA_ESCAPED;
1393
+ else if (state === SCRIPT_DATA_DOUBLE_ESCAPED_DASH || state === SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH) state = charCode === GT_CHAR && state === SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH ? SCRIPT_DATA : SCRIPT_DATA_DOUBLE_ESCAPED;
1394
+ i++;
1395
+ }
1396
+ return scriptScanResult(parseState, state, SCRIPT_SCAN_COMPLETE);
1397
+ }
1398
+ function pushScriptTextChunk(state, text) {
1399
+ if (!text) return;
1400
+ (state.scriptTextChunks ??= []).push(text);
1401
+ state.textBufferContainsNonWhitespace = true;
1402
+ state.lastCharWasWhitespace = false;
1403
+ state.justClosedTag = false;
1404
+ }
1405
+ function scanScriptChunk(html, start, state) {
1406
+ const result = findScriptEndTag(html, start, state);
1407
+ if (result === SCRIPT_SCAN_COMPLETE) pushScriptTextChunk(state, html.substring(start));
1408
+ else if (result <= SCRIPT_SEQUENCE_INCOMPLETE) pushScriptTextChunk(state, html.substring(start, -result - 2));
1409
+ else pushScriptTextChunk(state, html.substring(start, result));
1410
+ return result;
1411
+ }
1412
+ function takeScriptText(state) {
1413
+ const chunks = state.scriptTextChunks;
1414
+ const text = chunks?.length ? chunks.length === 1 ? chunks[0] : chunks.join("") : "";
1415
+ if (chunks) chunks.length = 0;
1416
+ state.scriptDataState = SCRIPT_DATA;
1417
+ return text;
1418
+ }
1419
+ function normalizeTagName(tagName) {
1420
+ for (let i = 0; i < tagName.length; i++) {
1421
+ const code = tagName.charCodeAt(i);
1422
+ if (code >= 65 && code <= 90) return tagName.toLowerCase();
1423
+ }
1424
+ return tagName;
1425
+ }
1426
+ function effectiveTagId(tagName, fallbackTagId, state) {
1427
+ return state.tagOverrideHandlers?.get(tagName)?.aliasTagId ?? fallbackTagId;
1428
+ }
1429
+ function matchesClosingTag(node, tagName, tagId, closingIsAlias) {
1430
+ if (node.tagId !== tagId) return false;
1431
+ if (tagId === -1) return node.name === tagName;
1432
+ const nodeIsAlias = node.tagHandler?.aliasTagId !== void 0;
1433
+ return node.name === tagName || !nodeIsAlias && !closingIsAlias;
991
1434
  }
992
1435
  function parseHtml(html, options = {}) {
993
1436
  const events = [];
1437
+ const state = {
1438
+ depthMap: /* @__PURE__ */ new Uint8Array(110),
1439
+ depth: 0,
1440
+ resolvedPlugins: options.resolvedPlugins || []
1441
+ };
1442
+ let remainingHtml = parseHtmlInternal(html, state, (event) => {
1443
+ events.push(event);
1444
+ });
1445
+ if (state.scriptTextChunks?.length) remainingHtml = `${takeScriptText(state)}${remainingHtml}`;
994
1446
  return {
995
1447
  events,
996
- remainingHtml: parseHtmlInternal(html, {
997
- depthMap: new Uint8Array(109),
998
- depth: 0,
999
- resolvedPlugins: options.resolvedPlugins || []
1000
- }, (event) => {
1001
- events.push(event);
1002
- })
1448
+ remainingHtml
1003
1449
  };
1004
1450
  }
1005
1451
  function parseHtmlStream(html, state, onEvent) {
@@ -1007,14 +1453,20 @@ function parseHtmlStream(html, state, onEvent) {
1007
1453
  }
1008
1454
  function parseHtmlInternal(htmlChunk, state, handleEvent) {
1009
1455
  let textBuffer = "";
1010
- state.depthMap ??= new Uint8Array(109);
1456
+ state.depthMap ??= /* @__PURE__ */ new Uint8Array(110);
1011
1457
  state.depth ??= 0;
1012
1458
  state.lastCharWasWhitespace ??= true;
1013
1459
  state.justClosedTag ??= false;
1014
1460
  state.isFirstTextInElement ??= false;
1015
- state.lastCharWasBackslash ??= false;
1461
+ state.scriptDataState ??= SCRIPT_DATA;
1016
1462
  let i = 0;
1017
1463
  const chunkLength = htmlChunk.length;
1464
+ if (state.currentNode && isScriptElement(state.currentNode)) {
1465
+ const scanResult = scanScriptChunk(htmlChunk, i, state);
1466
+ if (scanResult < 0) return scanResult <= SCRIPT_SEQUENCE_INCOMPLETE ? htmlChunk.substring(-scanResult - 2) : "";
1467
+ textBuffer = takeScriptText(state);
1468
+ i = scanResult;
1469
+ }
1018
1470
  while (i < chunkLength) {
1019
1471
  const currentCharCode = htmlChunk.charCodeAt(i);
1020
1472
  if (currentCharCode !== LT_CHAR) {
@@ -1033,25 +1485,16 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1033
1485
  else if (currentCharCode === SPACE_CHAR || !state.lastCharWasWhitespace) textBuffer += " ";
1034
1486
  state.lastCharWasWhitespace = true;
1035
1487
  state.textBufferContainsWhitespace = true;
1036
- state.lastCharWasBackslash = false;
1037
1488
  } else {
1038
1489
  state.textBufferContainsNonWhitespace = true;
1039
1490
  state.lastCharWasWhitespace = false;
1040
1491
  state.justClosedTag = false;
1041
- if (currentCharCode === PIPE_CHAR && state.depthMap[28]) textBuffer += "\\|";
1042
- else if (currentCharCode === BACKTICK_CHAR && (state.depthMap[23] || state.depthMap[34])) textBuffer += "\\`";
1043
- else if (currentCharCode === OPEN_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\[";
1044
- else if (currentCharCode === CLOSE_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\]";
1045
- else if (currentCharCode === GT_CHAR && state.depthMap[22]) textBuffer += "\\>";
1492
+ if (!state.plainText && currentCharCode === PIPE_CHAR && state.depthMap[28]) textBuffer += "\\|";
1493
+ else if (!state.plainText && currentCharCode === BACKTICK_CHAR && (state.depthMap[23] || state.depthMap[34])) textBuffer += "\\`";
1494
+ else if (!state.plainText && currentCharCode === OPEN_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\[";
1495
+ else if (!state.plainText && currentCharCode === CLOSE_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\]";
1496
+ else if (!state.plainText && currentCharCode === GT_CHAR && state.depthMap[22]) textBuffer += "\\>";
1046
1497
  else textBuffer += htmlChunk[i];
1047
- if (state.inRawTextQuoteAware) {
1048
- if (!state.lastCharWasBackslash) {
1049
- if (currentCharCode === APOS_CHAR && !state.inDoubleQuote && !state.inBacktick) state.inSingleQuote = !state.inSingleQuote;
1050
- else if (currentCharCode === QUOTE_CHAR && !state.inSingleQuote && !state.inBacktick) state.inDoubleQuote = !state.inDoubleQuote;
1051
- else if (currentCharCode === BACKTICK_CHAR && !state.inSingleQuote && !state.inDoubleQuote) state.inBacktick = !state.inBacktick;
1052
- }
1053
- }
1054
- state.lastCharWasBackslash = currentCharCode === BACKSLASH_CHAR && !state.lastCharWasBackslash;
1055
1498
  }
1056
1499
  i++;
1057
1500
  continue;
@@ -1094,18 +1537,16 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1094
1537
  }
1095
1538
  } else if (nextCharCode === SLASH_CHAR) {
1096
1539
  if (state.currentNode?.tagHandler?.isNonNesting) {
1097
- if (state.inRawTextQuoteAware && (state.inSingleQuote || state.inDoubleQuote || state.inBacktick)) {
1098
- textBuffer += htmlChunk[i];
1099
- i++;
1100
- continue;
1101
- }
1102
1540
  let peekEnd = i + 2;
1103
1541
  while (peekEnd < chunkLength) {
1104
1542
  const c = htmlChunk.charCodeAt(peekEnd);
1105
- if (c === GT_CHAR || isWhitespace(c)) break;
1543
+ if (c === GT_CHAR || c === SLASH_CHAR || isWhitespace(c)) break;
1106
1544
  peekEnd++;
1107
1545
  }
1108
- if ((TagIdMap[htmlChunk.substring(i + 2, peekEnd).toLowerCase()] ?? -1) !== state.currentNode.tagId) {
1546
+ const peekTagName = normalizeTagName(htmlChunk.substring(i + 2, peekEnd));
1547
+ const peekHandler = state.tagOverrideHandlers?.get(peekTagName);
1548
+ const peekTagId = effectiveTagId(peekTagName, TagIdMap[peekTagName] ?? -1, state);
1549
+ if (!matchesClosingTag(state.currentNode, peekTagName, peekTagId, peekHandler?.aliasTagId !== void 0)) {
1109
1550
  textBuffer += htmlChunk[i++];
1110
1551
  continue;
1111
1552
  }
@@ -1136,8 +1577,11 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1136
1577
  textBuffer += htmlChunk.substring(i);
1137
1578
  break;
1138
1579
  }
1139
- const tagName = htmlChunk.substring(tagNameStart, tagNameEnd).toLowerCase();
1140
- const tagId = TagIdMap[tagName] ?? -1;
1580
+ const rawTagName = htmlChunk.substring(tagNameStart, tagNameEnd);
1581
+ let tagName = rawTagName;
1582
+ if (typeof TagIdMap[tagName] !== "number") tagName = normalizeTagName(rawTagName);
1583
+ const mappedTagId = TagIdMap[tagName];
1584
+ const tagId = effectiveTagId(tagName, typeof mappedTagId === "number" ? mappedTagId : -1, state);
1141
1585
  i2 = tagNameEnd;
1142
1586
  if (state.currentNode?.tagHandler?.isNonNesting) {
1143
1587
  textBuffer += htmlChunk[i++];
@@ -1152,20 +1596,28 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1152
1596
  textBuffer = "";
1153
1597
  }
1154
1598
  const result = processOpeningTag(tagName, tagId, htmlChunk, i2, state, handleEvent);
1155
- if (result.skip) textBuffer += htmlChunk[i++];
1599
+ if (result.skip) i = result.newPosition;
1156
1600
  else if (result.complete) {
1157
1601
  i = result.newPosition;
1158
- if (!result.selfClosing) state.isFirstTextInElement = true;
1602
+ if (!result.selfClosing) {
1603
+ state.isFirstTextInElement = true;
1604
+ if (tagId === 52 && tagName === "script") {
1605
+ const scanResult = scanScriptChunk(htmlChunk, i, state);
1606
+ if (scanResult < 0) {
1607
+ textBuffer = scanResult <= SCRIPT_SEQUENCE_INCOMPLETE ? htmlChunk.substring(-scanResult - 2) : "";
1608
+ break;
1609
+ }
1610
+ textBuffer = takeScriptText(state);
1611
+ i = scanResult;
1612
+ }
1613
+ }
1159
1614
  } else {
1160
1615
  textBuffer += result.remainingText;
1161
1616
  break;
1162
1617
  }
1163
1618
  }
1164
1619
  }
1165
- state.inSingleQuote = false;
1166
- state.inDoubleQuote = false;
1167
- state.inBacktick = false;
1168
- state.lastCharWasBackslash = false;
1620
+ if (textBuffer.length > 0 && isWhitespace(textBuffer.charCodeAt(0))) state.lastCharWasWhitespace = false;
1169
1621
  return textBuffer;
1170
1622
  }
1171
1623
  function processTextBuffer(textBuffer, state, handleEvent) {
@@ -1174,7 +1626,6 @@ function processTextBuffer(textBuffer, state, handleEvent) {
1174
1626
  state.textBufferContainsNonWhitespace = false;
1175
1627
  state.textBufferContainsWhitespace = false;
1176
1628
  if (!state.currentNode) {
1177
- if (!containsNonWhitespace) return;
1178
1629
  let rootText = textBuffer;
1179
1630
  if (rootText.length === 0) return;
1180
1631
  if (state.hasEncodedHtmlEntity) {
@@ -1197,7 +1648,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
1197
1648
  state.lastTextNode = rootTextNode;
1198
1649
  return;
1199
1650
  }
1200
- const excludesTextNodes = state.currentNode?.tagHandler?.excludesTextNodes;
1651
+ const excludesTextNodes = state.currentNode?.tagHandler?.excludesTextNodes || state.currentNode?.excludedFromMarkdown;
1201
1652
  const inPreTag = (state.depthMap[34] || 0) > 0;
1202
1653
  if (!inPreTag && !containsNonWhitespace && !state.currentNode.childTextNodeIndex) return;
1203
1654
  let text = textBuffer;
@@ -1233,12 +1684,28 @@ function processClosingTag(htmlChunk, position, state, handleEvent) {
1233
1684
  let i = position + 2;
1234
1685
  const tagNameStart = i;
1235
1686
  const chunkLength = htmlChunk.length;
1687
+ let tagNameEnd = -1;
1688
+ let quoteChar = 0;
1236
1689
  let foundClose = false;
1237
1690
  while (i < chunkLength) {
1238
- if (htmlChunk.charCodeAt(i) === GT_CHAR) {
1691
+ const charCode = htmlChunk.charCodeAt(i);
1692
+ if (tagNameEnd !== -1) {
1693
+ if (quoteChar) {
1694
+ if (charCode === quoteChar) quoteChar = 0;
1695
+ i++;
1696
+ continue;
1697
+ }
1698
+ if (charCode === QUOTE_CHAR || charCode === APOS_CHAR) {
1699
+ quoteChar = charCode;
1700
+ i++;
1701
+ continue;
1702
+ }
1703
+ }
1704
+ if (charCode === GT_CHAR) {
1239
1705
  foundClose = true;
1240
1706
  break;
1241
1707
  }
1708
+ if (tagNameEnd === -1 && (isWhitespace(charCode) || charCode === SLASH_CHAR)) tagNameEnd = i;
1242
1709
  i++;
1243
1710
  }
1244
1711
  if (!foundClose) return {
@@ -1246,22 +1713,31 @@ function processClosingTag(htmlChunk, position, state, handleEvent) {
1246
1713
  newPosition: position,
1247
1714
  remainingText: htmlChunk.substring(position)
1248
1715
  };
1249
- const tagId = TagIdMap[htmlChunk.substring(tagNameStart, i).toLowerCase()] ?? -1;
1250
- if (state.currentNode?.tagHandler?.isNonNesting && tagId !== state.currentNode.tagId) return {
1716
+ const rawTagName = htmlChunk.substring(tagNameStart, tagNameEnd === -1 ? i : tagNameEnd);
1717
+ let tagName = rawTagName;
1718
+ if (typeof TagIdMap[tagName] !== "number") tagName = normalizeTagName(rawTagName);
1719
+ const tagHandler = state.tagOverrideHandlers?.get(tagName);
1720
+ const mappedTagId = TagIdMap[tagName];
1721
+ const tagId = effectiveTagId(tagName, typeof mappedTagId === "number" ? mappedTagId : -1, state);
1722
+ const closingIsAlias = tagHandler?.aliasTagId !== void 0;
1723
+ if (state.currentNode?.tagHandler?.isNonNesting && !matchesClosingTag(state.currentNode, tagName, tagId, closingIsAlias)) return {
1251
1724
  complete: false,
1252
1725
  newPosition: position,
1253
1726
  remainingText: htmlChunk.substring(position)
1254
1727
  };
1728
+ const stopAtTemplate = tagId !== 78 && (state.depthMap[78] || 0) > 0;
1255
1729
  let curr = state.currentNode;
1256
- if (curr) {
1257
- let match = curr.tagId !== tagId;
1258
- while (curr && match) {
1259
- closeNode(curr, state, handleEvent);
1260
- curr = curr.parent;
1261
- match = curr?.tagId !== tagId;
1730
+ while (curr && !matchesClosingTag(curr, tagName, tagId, closingIsAlias)) {
1731
+ if (stopAtTemplate && curr.tagId === 78) {
1732
+ curr = null;
1733
+ break;
1262
1734
  }
1735
+ curr = curr.parent;
1736
+ }
1737
+ if (curr) {
1738
+ while (state.currentNode && state.currentNode !== curr) closeNode(state.currentNode, state, handleEvent);
1739
+ closeNode(curr, state, handleEvent);
1263
1740
  }
1264
- if (curr) closeNode(curr, state, handleEvent);
1265
1741
  state.justClosedTag = true;
1266
1742
  return {
1267
1743
  complete: true,
@@ -1288,14 +1764,8 @@ function closeNode(node, state, handleEvent) {
1288
1764
  for (const parent of traverseUpToFirstBlockNode(node)) parent.childTextNodeIndex = (parent.childTextNodeIndex || 0) + 1;
1289
1765
  }
1290
1766
  }
1291
- if (node.tagId) state.depthMap[node.tagId] = Math.max(0, (state.depthMap[node.tagId] || 0) - 1);
1292
- if (node.tagHandler?.isNonNesting) {
1293
- state.inSingleQuote = false;
1294
- state.inDoubleQuote = false;
1295
- state.inBacktick = false;
1296
- state.lastCharWasBackslash = false;
1297
- state.inRawTextQuoteAware = false;
1298
- }
1767
+ const tagId = node.tagId;
1768
+ if (tagId !== void 0 && tagId >= 0 && tagId < 110) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
1299
1769
  state.depth--;
1300
1770
  handleEvent({
1301
1771
  type: 1,
@@ -1370,6 +1840,7 @@ function processCdataSection(content, state, handleEvent) {
1370
1840
  closeNode(state.currentNode ?? null, state, handleEvent);
1371
1841
  }
1372
1842
  function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1843
+ tagId = effectiveTagId(tagName, tagId, state);
1373
1844
  if (state.currentNode?.tagHandler?.isNonNesting) closeNode(state.currentNode, state, handleEvent);
1374
1845
  const tagHandler = state.tagOverrideHandlers?.get(tagName) ?? tagHandlers[tagId];
1375
1846
  const result = processTagAttributes(htmlChunk, i, tagHandler);
@@ -1379,23 +1850,51 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1379
1850
  remainingText: `<${tagName}${result.attrBuffer}`,
1380
1851
  selfClosing: false
1381
1852
  };
1382
- const currentTagCount = result.attributes && result.attributes.id ? void 0 : state.depthMap[tagId] || 0;
1383
- state.depthMap[tagId] = (currentTagCount || 0) + 1;
1853
+ if ((state.depthMap[1] || 0) > 0 && (state.depthMap[78] || 0) === 0 && !HEAD_CONTENT_TAGS.has(tagId)) {
1854
+ while (state.currentNode && state.currentNode.tagId !== 1) closeNode(state.currentNode, state, handleEvent);
1855
+ const headNode = state.currentNode;
1856
+ if (headNode && headNode.tagId === 1) closeNode(headNode, state, handleEvent);
1857
+ }
1858
+ if (tagId >= 0 && tagId < 110 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && (state.depthMap[65] || 0) > 0) {
1859
+ if (closeSelectTo(state, 65, handleEvent)) return {
1860
+ complete: true,
1861
+ newPosition: result.newPosition,
1862
+ remainingText: "",
1863
+ selfClosing: false,
1864
+ skip: true
1865
+ };
1866
+ } else if (tagId === 67) {
1867
+ if ((state.depthMap[65] || 0) > 0) closeSelectTo(state, 67, handleEvent);
1868
+ else if (state.currentNode?.tagId === 67) closeNode(state.currentNode, state, handleEvent);
1869
+ } else if (tagId === 109 && (state.depthMap[65] || 0) > 0) {
1870
+ closeSelectTo(state, 67, handleEvent);
1871
+ closeSelectTo(state, 109, handleEvent);
1872
+ } else if (tagId === 26) {
1873
+ if ((state.depthMap[26] || 0) > 0) closeImpliedTo(state, SINGLE_A, A_SCOPE_BOUNDARY, handleEvent);
1874
+ } else if (tagId === 32 || tagId === 31 || tagId === 30 || tagId === 29 || tagId === 38 || tagId === 39) {
1875
+ if ((state.depthMap[28] || 0) > 0) if (tagId === 32 || tagId === 31) {
1876
+ if ((state.depthMap[32] || 0) > 0 || (state.depthMap[31] || 0) > 0) closeImpliedTo(state, TD_TH, CELL_SCOPE_BOUNDARY, handleEvent);
1877
+ } else if (tagId === 30) {
1878
+ if ((state.depthMap[30] || 0) > 0) closeTableContext(state, TR_CELLS, handleEvent);
1879
+ } else closeTableContext(state, SECTION_CELLS, handleEvent);
1880
+ } else {
1881
+ if ((state.depthMap[35] || 0) > 0) closeImpliedTo(state, SINGLE_P, P_SCOPE_BOUNDARY, handleEvent);
1882
+ if (HEADINGS.has(tagId)) {
1883
+ const top = state.currentNode;
1884
+ if (top && top.tagId !== void 0 && HEADINGS.has(top.tagId)) closeNode(top, state, handleEvent);
1885
+ } else if (tagId === 25) {
1886
+ if ((state.depthMap[25] || 0) > 0) closeImpliedTo(state, SINGLE_LI, LI_SCOPE_BOUNDARY, handleEvent);
1887
+ } else if (tagId === 99 || tagId === 98) {
1888
+ if ((state.depthMap[99] || 0) > 0 || (state.depthMap[98] || 0) > 0) closeImpliedTo(state, DT_DD, DL_SCOPE_BOUNDARY, handleEvent);
1889
+ }
1890
+ }
1891
+ if (tagId >= 0 && tagId < 110) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
1384
1892
  state.depth++;
1385
1893
  i = result.newPosition;
1386
1894
  if (state.currentNode) state.currentNode.currentWalkIndex = state.currentNode.currentWalkIndex || 0;
1387
1895
  const currentWalkIndex = state.currentNode ? state.currentNode.currentWalkIndex++ : 0;
1388
- const tag = {
1389
- type: 1,
1390
- name: tagName,
1391
- attributes: result.attributes,
1392
- parent: state.currentNode,
1393
- depthMap: copyDepthMap(state.depthMap),
1394
- depth: state.depth,
1395
- index: currentWalkIndex,
1396
- tagId,
1397
- tagHandler
1398
- };
1896
+ const tag = new ParsedElementNode(tagName, result.attributes, state.currentNode, state.depth, currentWalkIndex, tagId, tagHandler);
1897
+ if (tagId === 78 || state.currentNode?.excludedFromMarkdown) tag.excludedFromMarkdown = true;
1399
1898
  state.lastTextNode = tag;
1400
1899
  handleEvent({
1401
1900
  type: 0,
@@ -1405,13 +1904,6 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1405
1904
  parentNode.currentWalkIndex = 0;
1406
1905
  state.currentNode = parentNode;
1407
1906
  state.hasEncodedHtmlEntity = false;
1408
- if (tagHandler?.isNonNesting && !result.selfClosing) {
1409
- state.inSingleQuote = false;
1410
- state.inDoubleQuote = false;
1411
- state.inBacktick = false;
1412
- state.lastCharWasBackslash = false;
1413
- state.inRawTextQuoteAware = tag.tagId === 52 || tag.tagId === 53;
1414
- }
1415
1907
  if (result.selfClosing) {
1416
1908
  closeNode(tag, state, handleEvent);
1417
1909
  state.justClosedTag = true;
@@ -1430,6 +1922,7 @@ function processTagAttributes(htmlChunk, position, tagHandler) {
1430
1922
  const attrStartPos = i;
1431
1923
  let insideQuote = false;
1432
1924
  let quoteChar = 0;
1925
+ let hasAttributeContent = false;
1433
1926
  let prevChar = 0;
1434
1927
  while (i < chunkLength) {
1435
1928
  const c = htmlChunk.charCodeAt(i);
@@ -1440,25 +1933,26 @@ function processTagAttributes(htmlChunk, position, tagHandler) {
1440
1933
  } else if (c === QUOTE_CHAR || c === APOS_CHAR) {
1441
1934
  insideQuote = true;
1442
1935
  quoteChar = c;
1936
+ hasAttributeContent = true;
1443
1937
  } else if (c === SLASH_CHAR && i + 1 < chunkLength && htmlChunk.charCodeAt(i + 1) === GT_CHAR) {
1444
- const attrStr = htmlChunk.substring(attrStartPos, i).trim();
1938
+ const attrStr = hasAttributeContent ? htmlChunk.substring(attrStartPos, i).trim() : "";
1445
1939
  return {
1446
1940
  complete: true,
1447
1941
  newPosition: i + 2,
1448
- attributes: parseAttributes(attrStr),
1942
+ attributes: hasAttributeContent ? parseAttributes(attrStr) : EMPTY_ATTRIBUTES,
1449
1943
  selfClosing: true,
1450
1944
  attrBuffer: attrStr
1451
1945
  };
1452
1946
  } else if (c === GT_CHAR) {
1453
- const attrStr = htmlChunk.substring(attrStartPos, i).trim();
1947
+ const attrStr = hasAttributeContent ? htmlChunk.substring(attrStartPos, i).trim() : "";
1454
1948
  return {
1455
1949
  complete: true,
1456
1950
  newPosition: i + 1,
1457
- attributes: parseAttributes(attrStr),
1951
+ attributes: hasAttributeContent ? parseAttributes(attrStr) : EMPTY_ATTRIBUTES,
1458
1952
  selfClosing,
1459
1953
  attrBuffer: attrStr
1460
1954
  };
1461
- }
1955
+ } else if (!isWhitespace(c)) hasAttributeContent = true;
1462
1956
  i++;
1463
1957
  prevChar = c;
1464
1958
  }
@@ -1528,14 +2022,14 @@ function parseAttributes(attrStr) {
1528
2022
  if (charCode === BACKSLASH_CHAR && i + 1 < len) i++;
1529
2023
  else if (charCode === quoteChar) {
1530
2024
  const raw = attrStr.substring(valueStart, i);
1531
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw) : raw;
2025
+ result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
1532
2026
  state = WHITESPACE;
1533
2027
  }
1534
2028
  break;
1535
2029
  case UNQUOTED_VALUE:
1536
2030
  if (isSpace || charCode === GT_CHAR) {
1537
2031
  const raw = attrStr.substring(valueStart, i);
1538
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw) : raw;
2032
+ result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
1539
2033
  state = WHITESPACE;
1540
2034
  }
1541
2035
  break;
@@ -1545,7 +2039,7 @@ function parseAttributes(attrStr) {
1545
2039
  if (state === QUOTED_VALUE || state === UNQUOTED_VALUE) {
1546
2040
  if (name) {
1547
2041
  const raw = attrStr.substring(valueStart, i);
1548
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw) : raw;
2042
+ result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
1549
2043
  }
1550
2044
  } else if (state === NAME || state === AFTER_NAME || state === BEFORE_VALUE) {
1551
2045
  nameEnd = nameEnd || i;
@@ -1554,4 +2048,4 @@ function parseAttributes(attrStr) {
1554
2048
  }
1555
2049
  return result;
1556
2050
  }
1557
- export { buildTagOverrideHandlers, parseAttributes, parseHtml, parseHtmlStream };
2051
+ export { buildTagOverrideHandlers, continuationPrefix, finalizeParse, parseAttributes, parseHtml, parseHtmlStream };