@mdream/js 1.4.1 → 1.5.3

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];
@@ -657,6 +1026,7 @@ function buildTagOverrideHandlers(overrides) {
657
1026
  if (override.exit !== void 0) {
658
1027
  const exitStr = override.exit;
659
1028
  handler.exit = () => exitStr;
1029
+ handler.literalExit = true;
660
1030
  }
661
1031
  if (override.spacing !== void 0) handler.spacing = override.spacing;
662
1032
  if (override.isInline !== void 0) handler.isInline = override.isInline;
@@ -667,303 +1037,6 @@ function buildTagOverrideHandlers(overrides) {
667
1037
  }
668
1038
  return result;
669
1039
  }
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
1040
  const LT_CHAR = 60;
968
1041
  const GT_CHAR = 62;
969
1042
  const SLASH_CHAR = 47;
@@ -977,12 +1050,24 @@ const DASH_CHAR = 45;
977
1050
  const SPACE_CHAR = 32;
978
1051
  const TAB_CHAR = 9;
979
1052
  const NEWLINE_CHAR = 10;
1053
+ const FORM_FEED_CHAR = 12;
980
1054
  const CARRIAGE_RETURN_CHAR = 13;
981
1055
  const BACKTICK_CHAR = 96;
982
1056
  const PIPE_CHAR = 124;
983
1057
  const OPEN_BRACKET_CHAR = 91;
984
1058
  const CLOSE_BRACKET_CHAR = 93;
985
- const HEAD_CONTENT_TAGS = new Set([
1059
+ const SCRIPT_DATA = 0;
1060
+ const SCRIPT_DATA_ESCAPED = 1;
1061
+ const SCRIPT_DATA_ESCAPED_DASH = 2;
1062
+ const SCRIPT_DATA_ESCAPED_DASH_DASH = 3;
1063
+ const SCRIPT_DATA_DOUBLE_ESCAPED = 4;
1064
+ const SCRIPT_DATA_DOUBLE_ESCAPED_DASH = 5;
1065
+ const SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH = 6;
1066
+ const SCRIPT_SEQUENCE_NO_MATCH = -1;
1067
+ const SCRIPT_SEQUENCE_INCOMPLETE = -2;
1068
+ const SCRIPT_SCAN_COMPLETE = -1;
1069
+ const MAX_ELEMENT_DEPTH = 512;
1070
+ const HEAD_CONTENT_TAGS = /* @__PURE__ */ new Set([
986
1071
  4,
987
1072
  5,
988
1073
  54,
@@ -993,7 +1078,7 @@ const HEAD_CONTENT_TAGS = new Set([
993
1078
  78
994
1079
  ]);
995
1080
  const CLOSES_P = (() => {
996
- const t = new Uint8Array(109);
1081
+ const t = /* @__PURE__ */ new Uint8Array(110);
997
1082
  for (const id of [
998
1083
  36,
999
1084
  35,
@@ -1041,11 +1126,14 @@ const NEEDS_IMPLIED_END_RECOVERY = (() => {
1041
1126
  30,
1042
1127
  29,
1043
1128
  38,
1044
- 39
1129
+ 39,
1130
+ 67,
1131
+ 109,
1132
+ 65
1045
1133
  ]) t[id] = 1;
1046
1134
  return t;
1047
1135
  })();
1048
- const P_SCOPE_BOUNDARY = new Set([
1136
+ const P_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1049
1137
  43,
1050
1138
  32,
1051
1139
  31,
@@ -1058,7 +1146,7 @@ const P_SCOPE_BOUNDARY = new Set([
1058
1146
  101,
1059
1147
  25
1060
1148
  ]);
1061
- const LI_SCOPE_BOUNDARY = new Set([
1149
+ const LI_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1062
1150
  24,
1063
1151
  33,
1064
1152
  28,
@@ -1068,7 +1156,7 @@ const LI_SCOPE_BOUNDARY = new Set([
1068
1156
  78,
1069
1157
  0
1070
1158
  ]);
1071
- const DL_SCOPE_BOUNDARY = new Set([
1159
+ const DL_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1072
1160
  101,
1073
1161
  24,
1074
1162
  33,
@@ -1080,7 +1168,7 @@ const DL_SCOPE_BOUNDARY = new Set([
1080
1168
  78,
1081
1169
  0
1082
1170
  ]);
1083
- const CELL_SCOPE_BOUNDARY = new Set([
1171
+ const CELL_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1084
1172
  30,
1085
1173
  29,
1086
1174
  38,
@@ -1090,7 +1178,7 @@ const CELL_SCOPE_BOUNDARY = new Set([
1090
1178
  78,
1091
1179
  0
1092
1180
  ]);
1093
- const A_SCOPE_BOUNDARY = new Set([
1181
+ const A_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1094
1182
  35,
1095
1183
  36,
1096
1184
  25,
@@ -1125,7 +1213,7 @@ const A_SCOPE_BOUNDARY = new Set([
1125
1213
  78,
1126
1214
  0
1127
1215
  ]);
1128
- const HEADINGS = new Set([
1216
+ const HEADINGS = /* @__PURE__ */ new Set([
1129
1217
  7,
1130
1218
  8,
1131
1219
  9,
@@ -1133,17 +1221,17 @@ const HEADINGS = new Set([
1133
1221
  11,
1134
1222
  12
1135
1223
  ]);
1136
- const SINGLE_P = new Set([35]);
1137
- const SINGLE_LI = new Set([25]);
1138
- const SINGLE_A = new Set([26]);
1139
- const DT_DD = new Set([99, 98]);
1140
- const TD_TH = new Set([32, 31]);
1141
- const TR_CELLS = new Set([
1224
+ const SINGLE_P = /* @__PURE__ */ new Set([35]);
1225
+ const SINGLE_LI = /* @__PURE__ */ new Set([25]);
1226
+ const SINGLE_A = /* @__PURE__ */ new Set([26]);
1227
+ const DT_DD = /* @__PURE__ */ new Set([99, 98]);
1228
+ const TD_TH = /* @__PURE__ */ new Set([32, 31]);
1229
+ const TR_CELLS = /* @__PURE__ */ new Set([
1142
1230
  32,
1143
1231
  31,
1144
1232
  30
1145
1233
  ]);
1146
- const SECTION_CELLS = new Set([
1234
+ const SECTION_CELLS = /* @__PURE__ */ new Set([
1147
1235
  32,
1148
1236
  31,
1149
1237
  30,
@@ -1177,44 +1265,212 @@ function closeTableContext(state, closeable, handleEvent) {
1177
1265
  closeNode(state.currentNode, state, handleEvent);
1178
1266
  }
1179
1267
  }
1268
+ function closeSelectTo(state, targetId, handleEvent) {
1269
+ let target;
1270
+ for (let node = state.currentNode; node; node = node.parent) {
1271
+ if (node.tagId === targetId) {
1272
+ target = node;
1273
+ break;
1274
+ }
1275
+ if (node.tagId === 65 || node.tagId === 78) break;
1276
+ }
1277
+ if (!target) return false;
1278
+ while (state.currentNode && state.currentNode !== target) closeNode(state.currentNode, state, handleEvent);
1279
+ closeNode(target, state, handleEvent);
1280
+ return true;
1281
+ }
1180
1282
  function finalizeParse(leftover, state, handleEvent) {
1181
- if (leftover.length > 0 && leftover.charCodeAt(0) !== LT_CHAR) processTextBuffer(leftover, state, handleEvent);
1283
+ if (state.currentNode && isScriptElement(state.currentNode)) {
1284
+ pushScriptTextChunk(state, leftover);
1285
+ const scriptText = takeScriptText(state);
1286
+ if (scriptText) processTextBuffer(scriptText, state, handleEvent);
1287
+ } else if (leftover.length > 0 && leftover.charCodeAt(0) !== LT_CHAR) processTextBuffer(leftover, state, handleEvent);
1182
1288
  while (state.currentNode) closeNode(state.currentNode, state, handleEvent);
1183
1289
  }
1184
1290
  const EMPTY_ATTRIBUTES = Object.freeze({});
1185
- function copyDepthMap(depthMap) {
1186
- return new Uint8Array(depthMap);
1187
- }
1291
+ var ParsedElementNode = class {
1292
+ type = 1;
1293
+ name;
1294
+ attributes;
1295
+ parent;
1296
+ depth;
1297
+ index;
1298
+ tagId;
1299
+ tagHandler;
1300
+ currentWalkIndex;
1301
+ childTextNodeIndex;
1302
+ containsWhitespace;
1303
+ pluginOutput;
1304
+ context;
1305
+ cachedDepthMap;
1306
+ constructor(name, attributes, parent, depth, index, tagId, tagHandler) {
1307
+ this.name = name;
1308
+ this.attributes = attributes;
1309
+ this.parent = parent;
1310
+ this.depth = depth;
1311
+ this.index = index;
1312
+ this.tagId = tagId;
1313
+ this.tagHandler = tagHandler;
1314
+ }
1315
+ get depthMap() {
1316
+ if (this.cachedDepthMap) return this.cachedDepthMap;
1317
+ const depthMap = /* @__PURE__ */ new Uint8Array(110);
1318
+ if (this.tagId >= 0 && this.tagId < 110) depthMap[this.tagId] = 1;
1319
+ let node = this.parent;
1320
+ while (node) {
1321
+ const tagId = node.tagId;
1322
+ if (tagId !== void 0 && tagId >= 0 && tagId < 110) depthMap[tagId] = (depthMap[tagId] || 0) + 1;
1323
+ node = node.parent;
1324
+ }
1325
+ this.cachedDepthMap = depthMap;
1326
+ return depthMap;
1327
+ }
1328
+ };
1188
1329
  function isWhitespace(charCode) {
1189
- return charCode === SPACE_CHAR || charCode === TAB_CHAR || charCode === NEWLINE_CHAR || charCode === CARRIAGE_RETURN_CHAR;
1330
+ return charCode === SPACE_CHAR || charCode === TAB_CHAR || charCode === NEWLINE_CHAR || charCode === FORM_FEED_CHAR || charCode === CARRIAGE_RETURN_CHAR;
1331
+ }
1332
+ function scriptSequenceEnd(html, nameStart) {
1333
+ const name = "script";
1334
+ for (let offset = 0; offset < 6; offset++) {
1335
+ const index = nameStart + offset;
1336
+ if (index >= html.length) return SCRIPT_SEQUENCE_INCOMPLETE;
1337
+ if ((html.charCodeAt(index) | 32) !== name.charCodeAt(offset)) return SCRIPT_SEQUENCE_NO_MATCH;
1338
+ }
1339
+ const delimiterIndex = nameStart + 6;
1340
+ if (delimiterIndex >= html.length) return SCRIPT_SEQUENCE_INCOMPLETE;
1341
+ const delimiter = html.charCodeAt(delimiterIndex);
1342
+ return delimiter === GT_CHAR || delimiter === SLASH_CHAR || isWhitespace(delimiter) ? delimiterIndex + 1 : SCRIPT_SEQUENCE_NO_MATCH;
1343
+ }
1344
+ function isScriptElement(node) {
1345
+ return node.tagId === 52 && node.name === "script";
1346
+ }
1347
+ function scriptScanResult(parseState, scriptState, result) {
1348
+ parseState.scriptDataState = scriptState;
1349
+ return result;
1350
+ }
1351
+ function findScriptEndTag(html, start, parseState) {
1352
+ let state = parseState.scriptDataState ?? SCRIPT_DATA;
1353
+ let i = start;
1354
+ while (i < html.length) {
1355
+ const charCode = html.charCodeAt(i);
1356
+ if (charCode === LT_CHAR) {
1357
+ if (i + 1 === html.length) return scriptScanResult(parseState, state, -i - 2);
1358
+ const next = html.charCodeAt(i + 1);
1359
+ if (state === SCRIPT_DATA) {
1360
+ if (next === SLASH_CHAR) {
1361
+ const sequenceEnd = scriptSequenceEnd(html, i + 2);
1362
+ if (sequenceEnd >= 0) return scriptScanResult(parseState, state, i);
1363
+ if (sequenceEnd === SCRIPT_SEQUENCE_INCOMPLETE) return scriptScanResult(parseState, state, -i - 2);
1364
+ } else if (next === EXCLAMATION_CHAR) {
1365
+ const marker = "<!--";
1366
+ const available = Math.min(html.length - i, 4);
1367
+ if (html.startsWith(marker.substring(0, available), i)) {
1368
+ if (available < 4) return scriptScanResult(parseState, state, -i - 2);
1369
+ state = SCRIPT_DATA_ESCAPED_DASH_DASH;
1370
+ i += 4;
1371
+ continue;
1372
+ }
1373
+ }
1374
+ } else {
1375
+ const escaped = state < SCRIPT_DATA_DOUBLE_ESCAPED;
1376
+ state = escaped ? SCRIPT_DATA_ESCAPED : SCRIPT_DATA_DOUBLE_ESCAPED;
1377
+ const isEndTag = next === SLASH_CHAR;
1378
+ if (isEndTag || escaped) {
1379
+ const sequenceEnd = scriptSequenceEnd(html, i + (isEndTag ? 2 : 1));
1380
+ if (sequenceEnd >= 0) {
1381
+ if (isEndTag) {
1382
+ if (escaped) return scriptScanResult(parseState, state, i);
1383
+ state = SCRIPT_DATA_ESCAPED;
1384
+ } else state = SCRIPT_DATA_DOUBLE_ESCAPED;
1385
+ i = sequenceEnd;
1386
+ continue;
1387
+ }
1388
+ if (sequenceEnd === SCRIPT_SEQUENCE_INCOMPLETE) return scriptScanResult(parseState, state, -i - 2);
1389
+ }
1390
+ }
1391
+ } else if (charCode === DASH_CHAR) {
1392
+ if (state === SCRIPT_DATA_ESCAPED || state === SCRIPT_DATA_ESCAPED_DASH) state++;
1393
+ else if (state === SCRIPT_DATA_DOUBLE_ESCAPED || state === SCRIPT_DATA_DOUBLE_ESCAPED_DASH) state++;
1394
+ } 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;
1395
+ 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;
1396
+ i++;
1397
+ }
1398
+ return scriptScanResult(parseState, state, SCRIPT_SCAN_COMPLETE);
1399
+ }
1400
+ function pushScriptTextChunk(state, text) {
1401
+ if (!text) return;
1402
+ (state.scriptTextChunks ??= []).push(text);
1403
+ state.textBufferContainsNonWhitespace = true;
1404
+ state.lastCharWasWhitespace = false;
1405
+ state.justClosedTag = false;
1406
+ }
1407
+ function scanScriptChunk(html, start, state) {
1408
+ const result = findScriptEndTag(html, start, state);
1409
+ if (result === SCRIPT_SCAN_COMPLETE) pushScriptTextChunk(state, html.substring(start));
1410
+ else if (result <= SCRIPT_SEQUENCE_INCOMPLETE) pushScriptTextChunk(state, html.substring(start, -result - 2));
1411
+ else pushScriptTextChunk(state, html.substring(start, result));
1412
+ return result;
1413
+ }
1414
+ function takeScriptText(state) {
1415
+ const chunks = state.scriptTextChunks;
1416
+ const text = chunks?.length ? chunks.length === 1 ? chunks[0] : chunks.join("") : "";
1417
+ if (chunks) chunks.length = 0;
1418
+ state.scriptDataState = SCRIPT_DATA;
1419
+ return text;
1420
+ }
1421
+ function normalizeTagName(tagName) {
1422
+ for (let i = 0; i < tagName.length; i++) {
1423
+ const code = tagName.charCodeAt(i);
1424
+ if (code >= 65 && code <= 90) return tagName.toLowerCase();
1425
+ }
1426
+ return tagName;
1427
+ }
1428
+ function effectiveTagId(tagName, fallbackTagId, state) {
1429
+ return state.tagOverrideHandlers?.get(tagName)?.aliasTagId ?? fallbackTagId;
1430
+ }
1431
+ function matchesClosingTag(node, tagName, tagId, closingIsAlias) {
1432
+ if (node.tagId !== tagId) return false;
1433
+ if (tagId === -1) return node.name === tagName;
1434
+ const nodeIsAlias = node.tagHandler?.aliasTagId !== void 0;
1435
+ return node.name === tagName || !nodeIsAlias && !closingIsAlias;
1190
1436
  }
1191
1437
  function parseHtml(html, options = {}) {
1192
1438
  const events = [];
1439
+ const state = {
1440
+ depthMap: /* @__PURE__ */ new Uint8Array(110),
1441
+ depth: 0,
1442
+ resolvedPlugins: options.resolvedPlugins || []
1443
+ };
1444
+ let remainingHtml = parseHtmlInternal(html, state, (event) => {
1445
+ events.push(event);
1446
+ });
1447
+ if (state.scriptTextChunks?.length) remainingHtml = `${takeScriptText(state)}${remainingHtml}`;
1193
1448
  return {
1194
1449
  events,
1195
- remainingHtml: parseHtmlInternal(html, {
1196
- depthMap: new Uint8Array(109),
1197
- depth: 0,
1198
- resolvedPlugins: options.resolvedPlugins || []
1199
- }, (event) => {
1200
- events.push(event);
1201
- })
1450
+ remainingHtml
1202
1451
  };
1203
1452
  }
1204
1453
  function parseHtmlStream(html, state, onEvent) {
1205
1454
  return parseHtmlInternal(html, state, onEvent);
1206
1455
  }
1207
1456
  function parseHtmlInternal(htmlChunk, state, handleEvent) {
1457
+ if (state.depthLimitReached) return "";
1208
1458
  let textBuffer = "";
1209
- state.depthMap ??= new Uint8Array(109);
1459
+ state.depthMap ??= /* @__PURE__ */ new Uint8Array(110);
1210
1460
  state.depth ??= 0;
1211
1461
  state.lastCharWasWhitespace ??= true;
1212
1462
  state.justClosedTag ??= false;
1213
1463
  state.isFirstTextInElement ??= false;
1214
- state.lastCharWasBackslash ??= false;
1464
+ state.scriptDataState ??= SCRIPT_DATA;
1215
1465
  let i = 0;
1216
1466
  const chunkLength = htmlChunk.length;
1217
- while (i < chunkLength) {
1467
+ if (state.currentNode && isScriptElement(state.currentNode)) {
1468
+ const scanResult = scanScriptChunk(htmlChunk, i, state);
1469
+ if (scanResult < 0) return scanResult <= SCRIPT_SEQUENCE_INCOMPLETE ? htmlChunk.substring(-scanResult - 2) : "";
1470
+ textBuffer = takeScriptText(state);
1471
+ i = scanResult;
1472
+ }
1473
+ while (i < chunkLength && !state.depthLimitReached) {
1218
1474
  const currentCharCode = htmlChunk.charCodeAt(i);
1219
1475
  if (currentCharCode !== LT_CHAR) {
1220
1476
  if (currentCharCode === AMPERSAND_CHAR) state.hasEncodedHtmlEntity = true;
@@ -1232,25 +1488,16 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1232
1488
  else if (currentCharCode === SPACE_CHAR || !state.lastCharWasWhitespace) textBuffer += " ";
1233
1489
  state.lastCharWasWhitespace = true;
1234
1490
  state.textBufferContainsWhitespace = true;
1235
- state.lastCharWasBackslash = false;
1236
1491
  } else {
1237
1492
  state.textBufferContainsNonWhitespace = true;
1238
1493
  state.lastCharWasWhitespace = false;
1239
1494
  state.justClosedTag = false;
1240
- if (currentCharCode === PIPE_CHAR && state.depthMap[28]) textBuffer += "\\|";
1241
- else if (currentCharCode === BACKTICK_CHAR && (state.depthMap[23] || state.depthMap[34])) textBuffer += "\\`";
1242
- else if (currentCharCode === OPEN_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\[";
1243
- else if (currentCharCode === CLOSE_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\]";
1244
- else if (currentCharCode === GT_CHAR && state.depthMap[22]) textBuffer += "\\>";
1495
+ if (!state.plainText && currentCharCode === PIPE_CHAR && state.depthMap[28]) textBuffer += "\\|";
1496
+ else if (!state.plainText && currentCharCode === BACKTICK_CHAR && (state.depthMap[23] || state.depthMap[34])) textBuffer += "\\`";
1497
+ else if (!state.plainText && currentCharCode === OPEN_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\[";
1498
+ else if (!state.plainText && currentCharCode === CLOSE_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\]";
1499
+ else if (!state.plainText && currentCharCode === GT_CHAR && state.depthMap[22]) textBuffer += "\\>";
1245
1500
  else textBuffer += htmlChunk[i];
1246
- if (state.inRawTextQuoteAware) {
1247
- if (!state.lastCharWasBackslash) {
1248
- if (currentCharCode === APOS_CHAR && !state.inDoubleQuote && !state.inBacktick) state.inSingleQuote = !state.inSingleQuote;
1249
- else if (currentCharCode === QUOTE_CHAR && !state.inSingleQuote && !state.inBacktick) state.inDoubleQuote = !state.inDoubleQuote;
1250
- else if (currentCharCode === BACKTICK_CHAR && !state.inSingleQuote && !state.inDoubleQuote) state.inBacktick = !state.inBacktick;
1251
- }
1252
- }
1253
- state.lastCharWasBackslash = currentCharCode === BACKSLASH_CHAR && !state.lastCharWasBackslash;
1254
1501
  }
1255
1502
  i++;
1256
1503
  continue;
@@ -1293,18 +1540,16 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1293
1540
  }
1294
1541
  } else if (nextCharCode === SLASH_CHAR) {
1295
1542
  if (state.currentNode?.tagHandler?.isNonNesting) {
1296
- if (state.inRawTextQuoteAware && (state.inSingleQuote || state.inDoubleQuote || state.inBacktick)) {
1297
- textBuffer += htmlChunk[i];
1298
- i++;
1299
- continue;
1300
- }
1301
1543
  let peekEnd = i + 2;
1302
1544
  while (peekEnd < chunkLength) {
1303
1545
  const c = htmlChunk.charCodeAt(peekEnd);
1304
- if (c === GT_CHAR || isWhitespace(c)) break;
1546
+ if (c === GT_CHAR || c === SLASH_CHAR || isWhitespace(c)) break;
1305
1547
  peekEnd++;
1306
1548
  }
1307
- if ((TagIdMap[htmlChunk.substring(i + 2, peekEnd).toLowerCase()] ?? -1) !== state.currentNode.tagId) {
1549
+ const peekTagName = normalizeTagName(htmlChunk.substring(i + 2, peekEnd));
1550
+ const peekHandler = state.tagOverrideHandlers?.get(peekTagName);
1551
+ const peekTagId = effectiveTagId(peekTagName, TagIdMap[peekTagName] ?? -1, state);
1552
+ if (!matchesClosingTag(state.currentNode, peekTagName, peekTagId, peekHandler?.aliasTagId !== void 0)) {
1308
1553
  textBuffer += htmlChunk[i++];
1309
1554
  continue;
1310
1555
  }
@@ -1335,8 +1580,11 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1335
1580
  textBuffer += htmlChunk.substring(i);
1336
1581
  break;
1337
1582
  }
1338
- const tagName = htmlChunk.substring(tagNameStart, tagNameEnd).toLowerCase();
1339
- const tagId = TagIdMap[tagName] ?? -1;
1583
+ const rawTagName = htmlChunk.substring(tagNameStart, tagNameEnd);
1584
+ let tagName = rawTagName;
1585
+ if (typeof TagIdMap[tagName] !== "number") tagName = normalizeTagName(rawTagName);
1586
+ const mappedTagId = TagIdMap[tagName];
1587
+ const tagId = effectiveTagId(tagName, typeof mappedTagId === "number" ? mappedTagId : -1, state);
1340
1588
  i2 = tagNameEnd;
1341
1589
  if (state.currentNode?.tagHandler?.isNonNesting) {
1342
1590
  textBuffer += htmlChunk[i++];
@@ -1351,20 +1599,28 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1351
1599
  textBuffer = "";
1352
1600
  }
1353
1601
  const result = processOpeningTag(tagName, tagId, htmlChunk, i2, state, handleEvent);
1354
- if (result.skip) textBuffer += htmlChunk[i++];
1602
+ if (result.skip) i = result.newPosition;
1355
1603
  else if (result.complete) {
1356
1604
  i = result.newPosition;
1357
- if (!result.selfClosing) state.isFirstTextInElement = true;
1605
+ if (!result.selfClosing) {
1606
+ state.isFirstTextInElement = true;
1607
+ if (tagId === 52 && tagName === "script") {
1608
+ const scanResult = scanScriptChunk(htmlChunk, i, state);
1609
+ if (scanResult < 0) {
1610
+ textBuffer = scanResult <= SCRIPT_SEQUENCE_INCOMPLETE ? htmlChunk.substring(-scanResult - 2) : "";
1611
+ break;
1612
+ }
1613
+ textBuffer = takeScriptText(state);
1614
+ i = scanResult;
1615
+ }
1616
+ }
1358
1617
  } else {
1359
1618
  textBuffer += result.remainingText;
1360
1619
  break;
1361
1620
  }
1362
1621
  }
1363
1622
  }
1364
- state.inSingleQuote = false;
1365
- state.inDoubleQuote = false;
1366
- state.inBacktick = false;
1367
- state.lastCharWasBackslash = false;
1623
+ if (textBuffer.length > 0 && isWhitespace(textBuffer.charCodeAt(0))) state.lastCharWasWhitespace = false;
1368
1624
  return textBuffer;
1369
1625
  }
1370
1626
  function processTextBuffer(textBuffer, state, handleEvent) {
@@ -1373,7 +1629,6 @@ function processTextBuffer(textBuffer, state, handleEvent) {
1373
1629
  state.textBufferContainsNonWhitespace = false;
1374
1630
  state.textBufferContainsWhitespace = false;
1375
1631
  if (!state.currentNode) {
1376
- if (!containsNonWhitespace) return;
1377
1632
  let rootText = textBuffer;
1378
1633
  if (rootText.length === 0) return;
1379
1634
  if (state.hasEncodedHtmlEntity) {
@@ -1396,7 +1651,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
1396
1651
  state.lastTextNode = rootTextNode;
1397
1652
  return;
1398
1653
  }
1399
- const excludesTextNodes = state.currentNode?.tagHandler?.excludesTextNodes;
1654
+ const excludesTextNodes = state.currentNode?.tagHandler?.excludesTextNodes || state.currentNode?.excludedFromMarkdown;
1400
1655
  const inPreTag = (state.depthMap[34] || 0) > 0;
1401
1656
  if (!inPreTag && !containsNonWhitespace && !state.currentNode.childTextNodeIndex) return;
1402
1657
  let text = textBuffer;
@@ -1432,12 +1687,28 @@ function processClosingTag(htmlChunk, position, state, handleEvent) {
1432
1687
  let i = position + 2;
1433
1688
  const tagNameStart = i;
1434
1689
  const chunkLength = htmlChunk.length;
1690
+ let tagNameEnd = -1;
1691
+ let quoteChar = 0;
1435
1692
  let foundClose = false;
1436
1693
  while (i < chunkLength) {
1437
- if (htmlChunk.charCodeAt(i) === GT_CHAR) {
1694
+ const charCode = htmlChunk.charCodeAt(i);
1695
+ if (tagNameEnd !== -1) {
1696
+ if (quoteChar) {
1697
+ if (charCode === quoteChar) quoteChar = 0;
1698
+ i++;
1699
+ continue;
1700
+ }
1701
+ if (charCode === QUOTE_CHAR || charCode === APOS_CHAR) {
1702
+ quoteChar = charCode;
1703
+ i++;
1704
+ continue;
1705
+ }
1706
+ }
1707
+ if (charCode === GT_CHAR) {
1438
1708
  foundClose = true;
1439
1709
  break;
1440
1710
  }
1711
+ if (tagNameEnd === -1 && (isWhitespace(charCode) || charCode === SLASH_CHAR)) tagNameEnd = i;
1441
1712
  i++;
1442
1713
  }
1443
1714
  if (!foundClose) return {
@@ -1445,22 +1716,31 @@ function processClosingTag(htmlChunk, position, state, handleEvent) {
1445
1716
  newPosition: position,
1446
1717
  remainingText: htmlChunk.substring(position)
1447
1718
  };
1448
- const tagId = TagIdMap[htmlChunk.substring(tagNameStart, i).toLowerCase()] ?? -1;
1449
- if (state.currentNode?.tagHandler?.isNonNesting && tagId !== state.currentNode.tagId) return {
1719
+ const rawTagName = htmlChunk.substring(tagNameStart, tagNameEnd === -1 ? i : tagNameEnd);
1720
+ let tagName = rawTagName;
1721
+ if (typeof TagIdMap[tagName] !== "number") tagName = normalizeTagName(rawTagName);
1722
+ const tagHandler = state.tagOverrideHandlers?.get(tagName);
1723
+ const mappedTagId = TagIdMap[tagName];
1724
+ const tagId = effectiveTagId(tagName, typeof mappedTagId === "number" ? mappedTagId : -1, state);
1725
+ const closingIsAlias = tagHandler?.aliasTagId !== void 0;
1726
+ if (state.currentNode?.tagHandler?.isNonNesting && !matchesClosingTag(state.currentNode, tagName, tagId, closingIsAlias)) return {
1450
1727
  complete: false,
1451
1728
  newPosition: position,
1452
1729
  remainingText: htmlChunk.substring(position)
1453
1730
  };
1731
+ const stopAtTemplate = tagId !== 78 && (state.depthMap[78] || 0) > 0;
1454
1732
  let curr = state.currentNode;
1455
- if (curr) {
1456
- let match = curr.tagId !== tagId;
1457
- while (curr && match) {
1458
- closeNode(curr, state, handleEvent);
1459
- curr = curr.parent;
1460
- match = curr?.tagId !== tagId;
1733
+ while (curr && !matchesClosingTag(curr, tagName, tagId, closingIsAlias)) {
1734
+ if (stopAtTemplate && curr.tagId === 78) {
1735
+ curr = null;
1736
+ break;
1461
1737
  }
1738
+ curr = curr.parent;
1739
+ }
1740
+ if (curr) {
1741
+ while (state.currentNode && state.currentNode !== curr) closeNode(state.currentNode, state, handleEvent);
1742
+ closeNode(curr, state, handleEvent);
1462
1743
  }
1463
- if (curr) closeNode(curr, state, handleEvent);
1464
1744
  state.justClosedTag = true;
1465
1745
  return {
1466
1746
  complete: true,
@@ -1487,14 +1767,8 @@ function closeNode(node, state, handleEvent) {
1487
1767
  for (const parent of traverseUpToFirstBlockNode(node)) parent.childTextNodeIndex = (parent.childTextNodeIndex || 0) + 1;
1488
1768
  }
1489
1769
  }
1490
- if (node.tagId) state.depthMap[node.tagId] = Math.max(0, (state.depthMap[node.tagId] || 0) - 1);
1491
- if (node.tagHandler?.isNonNesting) {
1492
- state.inSingleQuote = false;
1493
- state.inDoubleQuote = false;
1494
- state.inBacktick = false;
1495
- state.lastCharWasBackslash = false;
1496
- state.inRawTextQuoteAware = false;
1497
- }
1770
+ const tagId = node.tagId;
1771
+ if (tagId !== void 0 && tagId >= 0 && tagId < 110) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
1498
1772
  state.depth--;
1499
1773
  handleEvent({
1500
1774
  type: 1,
@@ -1548,7 +1822,7 @@ function processCommentOrDoctype(htmlChunk, position) {
1548
1822
  function processCdataSection(content, state, handleEvent) {
1549
1823
  if (!state.tagOverrideHandlers?.has("#cdata-section")) return;
1550
1824
  const open = processOpeningTag("#cdata-section", -1, ">", 0, state, handleEvent);
1551
- if (!open.complete || open.selfClosing) return;
1825
+ if (!open.complete || open.selfClosing || open.skip) return;
1552
1826
  const node = state.currentNode;
1553
1827
  if (content.length > 0 && !node.tagHandler?.excludesTextNodes) {
1554
1828
  const textNode = {
@@ -1569,6 +1843,7 @@ function processCdataSection(content, state, handleEvent) {
1569
1843
  closeNode(state.currentNode ?? null, state, handleEvent);
1570
1844
  }
1571
1845
  function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1846
+ tagId = effectiveTagId(tagName, tagId, state);
1572
1847
  if (state.currentNode?.tagHandler?.isNonNesting) closeNode(state.currentNode, state, handleEvent);
1573
1848
  const tagHandler = state.tagOverrideHandlers?.get(tagName) ?? tagHandlers[tagId];
1574
1849
  const result = processTagAttributes(htmlChunk, i, tagHandler);
@@ -1578,12 +1853,26 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1578
1853
  remainingText: `<${tagName}${result.attrBuffer}`,
1579
1854
  selfClosing: false
1580
1855
  };
1581
- if ((state.depthMap[1] || 0) > 0 && !HEAD_CONTENT_TAGS.has(tagId)) {
1856
+ if ((state.depthMap[1] || 0) > 0 && (state.depthMap[78] || 0) === 0 && !HEAD_CONTENT_TAGS.has(tagId)) {
1582
1857
  while (state.currentNode && state.currentNode.tagId !== 1) closeNode(state.currentNode, state, handleEvent);
1583
1858
  const headNode = state.currentNode;
1584
1859
  if (headNode && headNode.tagId === 1) closeNode(headNode, state, handleEvent);
1585
1860
  }
1586
- if (tagId >= 0 && tagId < 109 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 26) {
1861
+ if (tagId >= 0 && tagId < 110 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && (state.depthMap[65] || 0) > 0) {
1862
+ if (closeSelectTo(state, 65, handleEvent)) return {
1863
+ complete: true,
1864
+ newPosition: result.newPosition,
1865
+ remainingText: "",
1866
+ selfClosing: false,
1867
+ skip: true
1868
+ };
1869
+ } else if (tagId === 67) {
1870
+ if ((state.depthMap[65] || 0) > 0) closeSelectTo(state, 67, handleEvent);
1871
+ else if (state.currentNode?.tagId === 67) closeNode(state.currentNode, state, handleEvent);
1872
+ } else if (tagId === 109 && (state.depthMap[65] || 0) > 0) {
1873
+ closeSelectTo(state, 67, handleEvent);
1874
+ closeSelectTo(state, 109, handleEvent);
1875
+ } else if (tagId === 26) {
1587
1876
  if ((state.depthMap[26] || 0) > 0) closeImpliedTo(state, SINGLE_A, A_SCOPE_BOUNDARY, handleEvent);
1588
1877
  } else if (tagId === 32 || tagId === 31 || tagId === 30 || tagId === 29 || tagId === 38 || tagId === 39) {
1589
1878
  if ((state.depthMap[28] || 0) > 0) if (tagId === 32 || tagId === 31) {
@@ -1602,23 +1891,23 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1602
1891
  if ((state.depthMap[99] || 0) > 0 || (state.depthMap[98] || 0) > 0) closeImpliedTo(state, DT_DD, DL_SCOPE_BOUNDARY, handleEvent);
1603
1892
  }
1604
1893
  }
1605
- const currentTagCount = result.attributes && result.attributes.id ? void 0 : state.depthMap[tagId] || 0;
1606
- state.depthMap[tagId] = (currentTagCount || 0) + 1;
1894
+ if (!result.selfClosing && state.depth >= MAX_ELEMENT_DEPTH) {
1895
+ state.depthLimitReached = true;
1896
+ return {
1897
+ complete: true,
1898
+ newPosition: result.newPosition,
1899
+ remainingText: "",
1900
+ selfClosing: false,
1901
+ skip: true
1902
+ };
1903
+ }
1904
+ if (tagId >= 0 && tagId < 110) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
1607
1905
  state.depth++;
1608
1906
  i = result.newPosition;
1609
1907
  if (state.currentNode) state.currentNode.currentWalkIndex = state.currentNode.currentWalkIndex || 0;
1610
1908
  const currentWalkIndex = state.currentNode ? state.currentNode.currentWalkIndex++ : 0;
1611
- const tag = {
1612
- type: 1,
1613
- name: tagName,
1614
- attributes: result.attributes,
1615
- parent: state.currentNode,
1616
- depthMap: copyDepthMap(state.depthMap),
1617
- depth: state.depth,
1618
- index: currentWalkIndex,
1619
- tagId,
1620
- tagHandler
1621
- };
1909
+ const tag = new ParsedElementNode(tagName, result.attributes, state.currentNode, state.depth, currentWalkIndex, tagId, tagHandler);
1910
+ if (tagId === 78 || state.currentNode?.excludedFromMarkdown) tag.excludedFromMarkdown = true;
1622
1911
  state.lastTextNode = tag;
1623
1912
  handleEvent({
1624
1913
  type: 0,
@@ -1628,13 +1917,6 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1628
1917
  parentNode.currentWalkIndex = 0;
1629
1918
  state.currentNode = parentNode;
1630
1919
  state.hasEncodedHtmlEntity = false;
1631
- if (tagHandler?.isNonNesting && !result.selfClosing) {
1632
- state.inSingleQuote = false;
1633
- state.inDoubleQuote = false;
1634
- state.inBacktick = false;
1635
- state.lastCharWasBackslash = false;
1636
- state.inRawTextQuoteAware = tag.tagId === 52 || tag.tagId === 53;
1637
- }
1638
1920
  if (result.selfClosing) {
1639
1921
  closeNode(tag, state, handleEvent);
1640
1922
  state.justClosedTag = true;
@@ -1653,6 +1935,7 @@ function processTagAttributes(htmlChunk, position, tagHandler) {
1653
1935
  const attrStartPos = i;
1654
1936
  let insideQuote = false;
1655
1937
  let quoteChar = 0;
1938
+ let hasAttributeContent = false;
1656
1939
  let prevChar = 0;
1657
1940
  while (i < chunkLength) {
1658
1941
  const c = htmlChunk.charCodeAt(i);
@@ -1663,25 +1946,26 @@ function processTagAttributes(htmlChunk, position, tagHandler) {
1663
1946
  } else if (c === QUOTE_CHAR || c === APOS_CHAR) {
1664
1947
  insideQuote = true;
1665
1948
  quoteChar = c;
1949
+ hasAttributeContent = true;
1666
1950
  } else if (c === SLASH_CHAR && i + 1 < chunkLength && htmlChunk.charCodeAt(i + 1) === GT_CHAR) {
1667
- const attrStr = htmlChunk.substring(attrStartPos, i).trim();
1951
+ const attrStr = hasAttributeContent ? htmlChunk.substring(attrStartPos, i).trim() : "";
1668
1952
  return {
1669
1953
  complete: true,
1670
1954
  newPosition: i + 2,
1671
- attributes: parseAttributes(attrStr),
1955
+ attributes: hasAttributeContent ? parseAttributes(attrStr) : EMPTY_ATTRIBUTES,
1672
1956
  selfClosing: true,
1673
1957
  attrBuffer: attrStr
1674
1958
  };
1675
1959
  } else if (c === GT_CHAR) {
1676
- const attrStr = htmlChunk.substring(attrStartPos, i).trim();
1960
+ const attrStr = hasAttributeContent ? htmlChunk.substring(attrStartPos, i).trim() : "";
1677
1961
  return {
1678
1962
  complete: true,
1679
1963
  newPosition: i + 1,
1680
- attributes: parseAttributes(attrStr),
1964
+ attributes: hasAttributeContent ? parseAttributes(attrStr) : EMPTY_ATTRIBUTES,
1681
1965
  selfClosing,
1682
1966
  attrBuffer: attrStr
1683
1967
  };
1684
- }
1968
+ } else if (!isWhitespace(c)) hasAttributeContent = true;
1685
1969
  i++;
1686
1970
  prevChar = c;
1687
1971
  }
@@ -1751,14 +2035,14 @@ function parseAttributes(attrStr) {
1751
2035
  if (charCode === BACKSLASH_CHAR && i + 1 < len) i++;
1752
2036
  else if (charCode === quoteChar) {
1753
2037
  const raw = attrStr.substring(valueStart, i);
1754
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw) : raw;
2038
+ result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
1755
2039
  state = WHITESPACE;
1756
2040
  }
1757
2041
  break;
1758
2042
  case UNQUOTED_VALUE:
1759
2043
  if (isSpace || charCode === GT_CHAR) {
1760
2044
  const raw = attrStr.substring(valueStart, i);
1761
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw) : raw;
2045
+ result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
1762
2046
  state = WHITESPACE;
1763
2047
  }
1764
2048
  break;
@@ -1768,7 +2052,7 @@ function parseAttributes(attrStr) {
1768
2052
  if (state === QUOTED_VALUE || state === UNQUOTED_VALUE) {
1769
2053
  if (name) {
1770
2054
  const raw = attrStr.substring(valueStart, i);
1771
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw) : raw;
2055
+ result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
1772
2056
  }
1773
2057
  } else if (state === NAME || state === AFTER_NAME || state === BEFORE_VALUE) {
1774
2058
  nameEnd = nameEnd || i;
@@ -1777,4 +2061,4 @@ function parseAttributes(attrStr) {
1777
2061
  }
1778
2062
  return result;
1779
2063
  }
1780
- export { buildTagOverrideHandlers, finalizeParse, parseAttributes, parseHtml, parseHtmlStream };
2064
+ export { buildTagOverrideHandlers, continuationPrefix, finalizeParse, parseAttributes, parseHtml, parseHtmlStream };