@mdream/js 1.4.1 → 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,12 +1049,23 @@ 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 HEAD_CONTENT_TAGS = new Set([
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([
986
1069
  4,
987
1070
  5,
988
1071
  54,
@@ -993,7 +1076,7 @@ const HEAD_CONTENT_TAGS = new Set([
993
1076
  78
994
1077
  ]);
995
1078
  const CLOSES_P = (() => {
996
- const t = new Uint8Array(109);
1079
+ const t = /* @__PURE__ */ new Uint8Array(110);
997
1080
  for (const id of [
998
1081
  36,
999
1082
  35,
@@ -1041,11 +1124,14 @@ const NEEDS_IMPLIED_END_RECOVERY = (() => {
1041
1124
  30,
1042
1125
  29,
1043
1126
  38,
1044
- 39
1127
+ 39,
1128
+ 67,
1129
+ 109,
1130
+ 65
1045
1131
  ]) t[id] = 1;
1046
1132
  return t;
1047
1133
  })();
1048
- const P_SCOPE_BOUNDARY = new Set([
1134
+ const P_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1049
1135
  43,
1050
1136
  32,
1051
1137
  31,
@@ -1058,7 +1144,7 @@ const P_SCOPE_BOUNDARY = new Set([
1058
1144
  101,
1059
1145
  25
1060
1146
  ]);
1061
- const LI_SCOPE_BOUNDARY = new Set([
1147
+ const LI_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1062
1148
  24,
1063
1149
  33,
1064
1150
  28,
@@ -1068,7 +1154,7 @@ const LI_SCOPE_BOUNDARY = new Set([
1068
1154
  78,
1069
1155
  0
1070
1156
  ]);
1071
- const DL_SCOPE_BOUNDARY = new Set([
1157
+ const DL_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1072
1158
  101,
1073
1159
  24,
1074
1160
  33,
@@ -1080,7 +1166,7 @@ const DL_SCOPE_BOUNDARY = new Set([
1080
1166
  78,
1081
1167
  0
1082
1168
  ]);
1083
- const CELL_SCOPE_BOUNDARY = new Set([
1169
+ const CELL_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1084
1170
  30,
1085
1171
  29,
1086
1172
  38,
@@ -1090,7 +1176,7 @@ const CELL_SCOPE_BOUNDARY = new Set([
1090
1176
  78,
1091
1177
  0
1092
1178
  ]);
1093
- const A_SCOPE_BOUNDARY = new Set([
1179
+ const A_SCOPE_BOUNDARY = /* @__PURE__ */ new Set([
1094
1180
  35,
1095
1181
  36,
1096
1182
  25,
@@ -1125,7 +1211,7 @@ const A_SCOPE_BOUNDARY = new Set([
1125
1211
  78,
1126
1212
  0
1127
1213
  ]);
1128
- const HEADINGS = new Set([
1214
+ const HEADINGS = /* @__PURE__ */ new Set([
1129
1215
  7,
1130
1216
  8,
1131
1217
  9,
@@ -1133,17 +1219,17 @@ const HEADINGS = new Set([
1133
1219
  11,
1134
1220
  12
1135
1221
  ]);
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([
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([
1142
1228
  32,
1143
1229
  31,
1144
1230
  30
1145
1231
  ]);
1146
- const SECTION_CELLS = new Set([
1232
+ const SECTION_CELLS = /* @__PURE__ */ new Set([
1147
1233
  32,
1148
1234
  31,
1149
1235
  30,
@@ -1177,28 +1263,189 @@ function closeTableContext(state, closeable, handleEvent) {
1177
1263
  closeNode(state.currentNode, state, handleEvent);
1178
1264
  }
1179
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
+ }
1180
1280
  function finalizeParse(leftover, state, handleEvent) {
1181
- if (leftover.length > 0 && leftover.charCodeAt(0) !== LT_CHAR) processTextBuffer(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);
1182
1286
  while (state.currentNode) closeNode(state.currentNode, state, handleEvent);
1183
1287
  }
1184
1288
  const EMPTY_ATTRIBUTES = Object.freeze({});
1185
- function copyDepthMap(depthMap) {
1186
- return new Uint8Array(depthMap);
1187
- }
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
+ };
1188
1327
  function isWhitespace(charCode) {
1189
- 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;
1190
1434
  }
1191
1435
  function parseHtml(html, options = {}) {
1192
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}`;
1193
1446
  return {
1194
1447
  events,
1195
- remainingHtml: parseHtmlInternal(html, {
1196
- depthMap: new Uint8Array(109),
1197
- depth: 0,
1198
- resolvedPlugins: options.resolvedPlugins || []
1199
- }, (event) => {
1200
- events.push(event);
1201
- })
1448
+ remainingHtml
1202
1449
  };
1203
1450
  }
1204
1451
  function parseHtmlStream(html, state, onEvent) {
@@ -1206,14 +1453,20 @@ function parseHtmlStream(html, state, onEvent) {
1206
1453
  }
1207
1454
  function parseHtmlInternal(htmlChunk, state, handleEvent) {
1208
1455
  let textBuffer = "";
1209
- state.depthMap ??= new Uint8Array(109);
1456
+ state.depthMap ??= /* @__PURE__ */ new Uint8Array(110);
1210
1457
  state.depth ??= 0;
1211
1458
  state.lastCharWasWhitespace ??= true;
1212
1459
  state.justClosedTag ??= false;
1213
1460
  state.isFirstTextInElement ??= false;
1214
- state.lastCharWasBackslash ??= false;
1461
+ state.scriptDataState ??= SCRIPT_DATA;
1215
1462
  let i = 0;
1216
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
+ }
1217
1470
  while (i < chunkLength) {
1218
1471
  const currentCharCode = htmlChunk.charCodeAt(i);
1219
1472
  if (currentCharCode !== LT_CHAR) {
@@ -1232,25 +1485,16 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1232
1485
  else if (currentCharCode === SPACE_CHAR || !state.lastCharWasWhitespace) textBuffer += " ";
1233
1486
  state.lastCharWasWhitespace = true;
1234
1487
  state.textBufferContainsWhitespace = true;
1235
- state.lastCharWasBackslash = false;
1236
1488
  } else {
1237
1489
  state.textBufferContainsNonWhitespace = true;
1238
1490
  state.lastCharWasWhitespace = false;
1239
1491
  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 += "\\>";
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 += "\\>";
1245
1497
  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
1498
  }
1255
1499
  i++;
1256
1500
  continue;
@@ -1293,18 +1537,16 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1293
1537
  }
1294
1538
  } else if (nextCharCode === SLASH_CHAR) {
1295
1539
  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
1540
  let peekEnd = i + 2;
1302
1541
  while (peekEnd < chunkLength) {
1303
1542
  const c = htmlChunk.charCodeAt(peekEnd);
1304
- if (c === GT_CHAR || isWhitespace(c)) break;
1543
+ if (c === GT_CHAR || c === SLASH_CHAR || isWhitespace(c)) break;
1305
1544
  peekEnd++;
1306
1545
  }
1307
- 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)) {
1308
1550
  textBuffer += htmlChunk[i++];
1309
1551
  continue;
1310
1552
  }
@@ -1335,8 +1577,11 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1335
1577
  textBuffer += htmlChunk.substring(i);
1336
1578
  break;
1337
1579
  }
1338
- const tagName = htmlChunk.substring(tagNameStart, tagNameEnd).toLowerCase();
1339
- 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);
1340
1585
  i2 = tagNameEnd;
1341
1586
  if (state.currentNode?.tagHandler?.isNonNesting) {
1342
1587
  textBuffer += htmlChunk[i++];
@@ -1351,20 +1596,28 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1351
1596
  textBuffer = "";
1352
1597
  }
1353
1598
  const result = processOpeningTag(tagName, tagId, htmlChunk, i2, state, handleEvent);
1354
- if (result.skip) textBuffer += htmlChunk[i++];
1599
+ if (result.skip) i = result.newPosition;
1355
1600
  else if (result.complete) {
1356
1601
  i = result.newPosition;
1357
- 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
+ }
1358
1614
  } else {
1359
1615
  textBuffer += result.remainingText;
1360
1616
  break;
1361
1617
  }
1362
1618
  }
1363
1619
  }
1364
- state.inSingleQuote = false;
1365
- state.inDoubleQuote = false;
1366
- state.inBacktick = false;
1367
- state.lastCharWasBackslash = false;
1620
+ if (textBuffer.length > 0 && isWhitespace(textBuffer.charCodeAt(0))) state.lastCharWasWhitespace = false;
1368
1621
  return textBuffer;
1369
1622
  }
1370
1623
  function processTextBuffer(textBuffer, state, handleEvent) {
@@ -1373,7 +1626,6 @@ function processTextBuffer(textBuffer, state, handleEvent) {
1373
1626
  state.textBufferContainsNonWhitespace = false;
1374
1627
  state.textBufferContainsWhitespace = false;
1375
1628
  if (!state.currentNode) {
1376
- if (!containsNonWhitespace) return;
1377
1629
  let rootText = textBuffer;
1378
1630
  if (rootText.length === 0) return;
1379
1631
  if (state.hasEncodedHtmlEntity) {
@@ -1396,7 +1648,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
1396
1648
  state.lastTextNode = rootTextNode;
1397
1649
  return;
1398
1650
  }
1399
- const excludesTextNodes = state.currentNode?.tagHandler?.excludesTextNodes;
1651
+ const excludesTextNodes = state.currentNode?.tagHandler?.excludesTextNodes || state.currentNode?.excludedFromMarkdown;
1400
1652
  const inPreTag = (state.depthMap[34] || 0) > 0;
1401
1653
  if (!inPreTag && !containsNonWhitespace && !state.currentNode.childTextNodeIndex) return;
1402
1654
  let text = textBuffer;
@@ -1432,12 +1684,28 @@ function processClosingTag(htmlChunk, position, state, handleEvent) {
1432
1684
  let i = position + 2;
1433
1685
  const tagNameStart = i;
1434
1686
  const chunkLength = htmlChunk.length;
1687
+ let tagNameEnd = -1;
1688
+ let quoteChar = 0;
1435
1689
  let foundClose = false;
1436
1690
  while (i < chunkLength) {
1437
- 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) {
1438
1705
  foundClose = true;
1439
1706
  break;
1440
1707
  }
1708
+ if (tagNameEnd === -1 && (isWhitespace(charCode) || charCode === SLASH_CHAR)) tagNameEnd = i;
1441
1709
  i++;
1442
1710
  }
1443
1711
  if (!foundClose) return {
@@ -1445,22 +1713,31 @@ function processClosingTag(htmlChunk, position, state, handleEvent) {
1445
1713
  newPosition: position,
1446
1714
  remainingText: htmlChunk.substring(position)
1447
1715
  };
1448
- const tagId = TagIdMap[htmlChunk.substring(tagNameStart, i).toLowerCase()] ?? -1;
1449
- 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 {
1450
1724
  complete: false,
1451
1725
  newPosition: position,
1452
1726
  remainingText: htmlChunk.substring(position)
1453
1727
  };
1728
+ const stopAtTemplate = tagId !== 78 && (state.depthMap[78] || 0) > 0;
1454
1729
  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;
1730
+ while (curr && !matchesClosingTag(curr, tagName, tagId, closingIsAlias)) {
1731
+ if (stopAtTemplate && curr.tagId === 78) {
1732
+ curr = null;
1733
+ break;
1461
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);
1462
1740
  }
1463
- if (curr) closeNode(curr, state, handleEvent);
1464
1741
  state.justClosedTag = true;
1465
1742
  return {
1466
1743
  complete: true,
@@ -1487,14 +1764,8 @@ function closeNode(node, state, handleEvent) {
1487
1764
  for (const parent of traverseUpToFirstBlockNode(node)) parent.childTextNodeIndex = (parent.childTextNodeIndex || 0) + 1;
1488
1765
  }
1489
1766
  }
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
- }
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);
1498
1769
  state.depth--;
1499
1770
  handleEvent({
1500
1771
  type: 1,
@@ -1569,6 +1840,7 @@ function processCdataSection(content, state, handleEvent) {
1569
1840
  closeNode(state.currentNode ?? null, state, handleEvent);
1570
1841
  }
1571
1842
  function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1843
+ tagId = effectiveTagId(tagName, tagId, state);
1572
1844
  if (state.currentNode?.tagHandler?.isNonNesting) closeNode(state.currentNode, state, handleEvent);
1573
1845
  const tagHandler = state.tagOverrideHandlers?.get(tagName) ?? tagHandlers[tagId];
1574
1846
  const result = processTagAttributes(htmlChunk, i, tagHandler);
@@ -1578,12 +1850,26 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1578
1850
  remainingText: `<${tagName}${result.attrBuffer}`,
1579
1851
  selfClosing: false
1580
1852
  };
1581
- if ((state.depthMap[1] || 0) > 0 && !HEAD_CONTENT_TAGS.has(tagId)) {
1853
+ if ((state.depthMap[1] || 0) > 0 && (state.depthMap[78] || 0) === 0 && !HEAD_CONTENT_TAGS.has(tagId)) {
1582
1854
  while (state.currentNode && state.currentNode.tagId !== 1) closeNode(state.currentNode, state, handleEvent);
1583
1855
  const headNode = state.currentNode;
1584
1856
  if (headNode && headNode.tagId === 1) closeNode(headNode, state, handleEvent);
1585
1857
  }
1586
- if (tagId >= 0 && tagId < 109 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 26) {
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) {
1587
1873
  if ((state.depthMap[26] || 0) > 0) closeImpliedTo(state, SINGLE_A, A_SCOPE_BOUNDARY, handleEvent);
1588
1874
  } else if (tagId === 32 || tagId === 31 || tagId === 30 || tagId === 29 || tagId === 38 || tagId === 39) {
1589
1875
  if ((state.depthMap[28] || 0) > 0) if (tagId === 32 || tagId === 31) {
@@ -1602,23 +1888,13 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1602
1888
  if ((state.depthMap[99] || 0) > 0 || (state.depthMap[98] || 0) > 0) closeImpliedTo(state, DT_DD, DL_SCOPE_BOUNDARY, handleEvent);
1603
1889
  }
1604
1890
  }
1605
- const currentTagCount = result.attributes && result.attributes.id ? void 0 : state.depthMap[tagId] || 0;
1606
- state.depthMap[tagId] = (currentTagCount || 0) + 1;
1891
+ if (tagId >= 0 && tagId < 110) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
1607
1892
  state.depth++;
1608
1893
  i = result.newPosition;
1609
1894
  if (state.currentNode) state.currentNode.currentWalkIndex = state.currentNode.currentWalkIndex || 0;
1610
1895
  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
- };
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;
1622
1898
  state.lastTextNode = tag;
1623
1899
  handleEvent({
1624
1900
  type: 0,
@@ -1628,13 +1904,6 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1628
1904
  parentNode.currentWalkIndex = 0;
1629
1905
  state.currentNode = parentNode;
1630
1906
  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
1907
  if (result.selfClosing) {
1639
1908
  closeNode(tag, state, handleEvent);
1640
1909
  state.justClosedTag = true;
@@ -1653,6 +1922,7 @@ function processTagAttributes(htmlChunk, position, tagHandler) {
1653
1922
  const attrStartPos = i;
1654
1923
  let insideQuote = false;
1655
1924
  let quoteChar = 0;
1925
+ let hasAttributeContent = false;
1656
1926
  let prevChar = 0;
1657
1927
  while (i < chunkLength) {
1658
1928
  const c = htmlChunk.charCodeAt(i);
@@ -1663,25 +1933,26 @@ function processTagAttributes(htmlChunk, position, tagHandler) {
1663
1933
  } else if (c === QUOTE_CHAR || c === APOS_CHAR) {
1664
1934
  insideQuote = true;
1665
1935
  quoteChar = c;
1936
+ hasAttributeContent = true;
1666
1937
  } else if (c === SLASH_CHAR && i + 1 < chunkLength && htmlChunk.charCodeAt(i + 1) === GT_CHAR) {
1667
- const attrStr = htmlChunk.substring(attrStartPos, i).trim();
1938
+ const attrStr = hasAttributeContent ? htmlChunk.substring(attrStartPos, i).trim() : "";
1668
1939
  return {
1669
1940
  complete: true,
1670
1941
  newPosition: i + 2,
1671
- attributes: parseAttributes(attrStr),
1942
+ attributes: hasAttributeContent ? parseAttributes(attrStr) : EMPTY_ATTRIBUTES,
1672
1943
  selfClosing: true,
1673
1944
  attrBuffer: attrStr
1674
1945
  };
1675
1946
  } else if (c === GT_CHAR) {
1676
- const attrStr = htmlChunk.substring(attrStartPos, i).trim();
1947
+ const attrStr = hasAttributeContent ? htmlChunk.substring(attrStartPos, i).trim() : "";
1677
1948
  return {
1678
1949
  complete: true,
1679
1950
  newPosition: i + 1,
1680
- attributes: parseAttributes(attrStr),
1951
+ attributes: hasAttributeContent ? parseAttributes(attrStr) : EMPTY_ATTRIBUTES,
1681
1952
  selfClosing,
1682
1953
  attrBuffer: attrStr
1683
1954
  };
1684
- }
1955
+ } else if (!isWhitespace(c)) hasAttributeContent = true;
1685
1956
  i++;
1686
1957
  prevChar = c;
1687
1958
  }
@@ -1751,14 +2022,14 @@ function parseAttributes(attrStr) {
1751
2022
  if (charCode === BACKSLASH_CHAR && i + 1 < len) i++;
1752
2023
  else if (charCode === quoteChar) {
1753
2024
  const raw = attrStr.substring(valueStart, i);
1754
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw) : raw;
2025
+ result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
1755
2026
  state = WHITESPACE;
1756
2027
  }
1757
2028
  break;
1758
2029
  case UNQUOTED_VALUE:
1759
2030
  if (isSpace || charCode === GT_CHAR) {
1760
2031
  const raw = attrStr.substring(valueStart, i);
1761
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw) : raw;
2032
+ result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
1762
2033
  state = WHITESPACE;
1763
2034
  }
1764
2035
  break;
@@ -1768,7 +2039,7 @@ function parseAttributes(attrStr) {
1768
2039
  if (state === QUOTED_VALUE || state === UNQUOTED_VALUE) {
1769
2040
  if (name) {
1770
2041
  const raw = attrStr.substring(valueStart, i);
1771
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw) : raw;
2042
+ result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
1772
2043
  }
1773
2044
  } else if (state === NAME || state === AFTER_NAME || state === BEFORE_VALUE) {
1774
2045
  nameEnd = nameEnd || i;
@@ -1777,4 +2048,4 @@ function parseAttributes(attrStr) {
1777
2048
  }
1778
2049
  return result;
1779
2050
  }
1780
- export { buildTagOverrideHandlers, finalizeParse, parseAttributes, parseHtml, parseHtmlStream };
2051
+ export { buildTagOverrideHandlers, continuationPrefix, finalizeParse, parseAttributes, parseHtml, parseHtmlStream };