@mdream/js 1.5.7 → 1.5.10

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.
@@ -253,16 +253,37 @@ const HTML_ENTITIES = {
253
253
  yuml: "ÿ",
254
254
  $zeta: "ζ"
255
255
  };
256
- function equalsAsciiCaseInsensitive(value, expected) {
257
- for (let index = 0; index < expected.length; index++) if ((value.charCodeAt(index) | 32) !== expected.charCodeAt(index)) return false;
258
- return true;
256
+ function lowerAscii(code) {
257
+ return code >= 65 && code <= 90 ? code + 32 : code;
258
+ }
259
+ function isInsideHeading(depthMap) {
260
+ for (let tagId = 7; tagId <= 12; tagId++) if (depthMap[tagId]) return true;
261
+ return false;
262
+ }
263
+ function isInsideTableCell(state) {
264
+ const depthMap = state.depthMap;
265
+ return depthMap[32] > 0 || depthMap[31] > 0;
266
+ }
267
+ function schemeMatches(href, start, end, scheme) {
268
+ let matched = 0;
269
+ for (let index = start; index < end; index++) {
270
+ const code = href.charCodeAt(index);
271
+ if (code === 9 || code === 10 || code === 13) continue;
272
+ if (lowerAscii(code) !== scheme.charCodeAt(matched)) return false;
273
+ if (++matched === scheme.length) return true;
274
+ }
275
+ return false;
259
276
  }
260
277
  function isEmptyLinkHref(href) {
261
- if (href === "#") return true;
262
- switch (href.indexOf(":")) {
263
- case 4: return equalsAsciiCaseInsensitive(href, "data");
264
- case 8: return equalsAsciiCaseInsensitive(href, "vbscript");
265
- case 10: return equalsAsciiCaseInsensitive(href, "javascript");
278
+ let start = 0;
279
+ while (start < href.length && href.charCodeAt(start) <= 32) start++;
280
+ let end = href.length;
281
+ while (end > start && href.charCodeAt(end - 1) <= 32) end--;
282
+ switch (lowerAscii(href.charCodeAt(start))) {
283
+ case 35: return end - start === 1;
284
+ case 106: return schemeMatches(href, start, end, "javascript:");
285
+ case 100: return schemeMatches(href, start, end, "data:");
286
+ case 118: return schemeMatches(href, start, end, "vbscript:");
266
287
  default: return false;
267
288
  }
268
289
  }
@@ -336,6 +357,71 @@ function getLanguageFromClass(className) {
336
357
  }
337
358
  return "";
338
359
  }
360
+ function parseUnsignedInteger(raw) {
361
+ return raw !== void 0 && /^[\t\n\f\r ]*\+?\d+[\t\n\f\r ]*$/.test(raw) ? Number(raw) : void 0;
362
+ }
363
+ function lastOutputChar(buffer) {
364
+ for (let index = buffer.length - 1; index >= 0; index--) {
365
+ const fragment = buffer[index];
366
+ if (fragment.length > 0) return fragment.charCodeAt(fragment.length - 1);
367
+ }
368
+ return -1;
369
+ }
370
+ function blockOpenPrefix(buffer, prefix) {
371
+ switch (lastOutputChar(buffer)) {
372
+ case -1: return;
373
+ case 32: {
374
+ let fragment = buffer.length - 1;
375
+ let index = buffer[fragment].length;
376
+ for (;;) {
377
+ if (index === 0) {
378
+ if (--fragment < 0) return void 0;
379
+ index = buffer[fragment].length;
380
+ continue;
381
+ }
382
+ const code = buffer[fragment].charCodeAt(--index);
383
+ if (code !== 32) return code === 10 || listMarkerLineStart(buffer, fragment, index) ? void 0 : `\n\n${prefix ?? ""}`;
384
+ }
385
+ }
386
+ case 10: return prefix || void 0;
387
+ default: return `\n\n${prefix ?? ""}`;
388
+ }
389
+ }
390
+ function listMarkerLineStart(buffer, fragmentIndex, markerIndex) {
391
+ const marker = buffer[fragmentIndex].charCodeAt(markerIndex);
392
+ const ordered = marker === 46 || marker === 41;
393
+ if (!ordered && marker !== 45 && marker !== 42 && marker !== 43) return false;
394
+ let digits = 0;
395
+ let spaces = 0;
396
+ let fragment = fragmentIndex;
397
+ let cursor = markerIndex;
398
+ for (;;) {
399
+ if (cursor === 0) {
400
+ if (--fragment < 0) return !ordered || digits > 0;
401
+ cursor = buffer[fragment].length;
402
+ continue;
403
+ }
404
+ const code = buffer[fragment].charCodeAt(--cursor);
405
+ if (ordered && spaces === 0 && code >= 48 && code <= 57) {
406
+ if (++digits > 9) return false;
407
+ continue;
408
+ }
409
+ if (ordered && digits === 0) return false;
410
+ if (code === 32) {
411
+ if (++spaces > 3) return false;
412
+ continue;
413
+ }
414
+ return code === 10;
415
+ }
416
+ }
417
+ const MAX_ORDERED_START = 999999999;
418
+ function orderedItemNumber(list, index) {
419
+ const raw = list?.attributes?.start;
420
+ if (raw === void 0) return index + 1;
421
+ const start = parseUnsignedInteger(raw);
422
+ if (start === void 0 || start > MAX_ORDERED_START) return Math.min(1 + index, MAX_ORDERED_START);
423
+ return Math.min(start + index, MAX_ORDERED_START);
424
+ }
339
425
  function numericReplacement(codePoint) {
340
426
  if (codePoint === 0 || codePoint > 1114111 || codePoint >= 55296 && codePoint <= 57343) return "�";
341
427
  if (codePoint >= 128 && codePoint <= 159) return C1_REPLACEMENTS[codePoint - 128];
@@ -489,13 +575,9 @@ function isAutolinkUri(s) {
489
575
  }
490
576
  return true;
491
577
  }
492
- function isInsideTableCell(state) {
493
- const depthMap = state.depthMap;
494
- return depthMap[32] > 0 || depthMap[31] > 0;
495
- }
496
578
  function renderBreak(node, state) {
497
579
  const depthMap = state.depthMap;
498
- if (isInsideTableCell(state) || isInsideRawHtmlBlock(depthMap) || depthMap[7] || depthMap[8] || depthMap[9] || depthMap[10] || depthMap[11] || depthMap[12]) return "<br>";
580
+ if (isInsideTableCell(state) || isInsideRawHtmlBlock(depthMap) || isInsideHeading(depthMap)) return "<br>";
499
581
  if (depthMap[34] || depthMap[23]) return "\n";
500
582
  return ` \n${continuationPrefix(node, state.listIndentWidths || [], !state.bufferedBlockquoteDepth)}`;
501
583
  }
@@ -506,14 +588,87 @@ const breakHandler = {
506
588
  collapsesInnerWhiteSpace: true,
507
589
  isInline: true
508
590
  };
591
+ function escapeTrailingHeadingHashes(buffer) {
592
+ let index = buffer.length - 1;
593
+ while (index >= 0 && buffer[index] === "") index--;
594
+ if (index < 0) return;
595
+ const entry = buffer[index];
596
+ let end = entry.length;
597
+ while (end > 0 && (entry[end - 1] === " " || entry[end - 1] === " ")) end--;
598
+ let run = end;
599
+ while (run > 0 && entry[run - 1] === "#") run--;
600
+ if (run === end) return;
601
+ const previous = buffer[index - 1];
602
+ const before = run > 0 ? entry[run - 1] : previous?.charAt(previous.length - 1);
603
+ if (before !== " " && before !== " ") return;
604
+ buffer[index] = `${entry.slice(0, run)}\\${entry.slice(run)}`;
605
+ }
606
+ const LINE_FRESH = -1;
607
+ const LINE_OPEN = 0;
608
+ const LINE_ROW = 1;
609
+ const LINE_CONTENT = 2;
610
+ function lineStateBeforeRow(buffer) {
611
+ let fragment = buffer.length - 1;
612
+ while (fragment >= 0 && buffer[fragment].length === 0) fragment--;
613
+ if (fragment < 0) return LINE_OPEN;
614
+ const tail = buffer[fragment];
615
+ if (tail.charCodeAt(tail.length - 1) === 10) return LINE_FRESH;
616
+ let firstNonSpace = -1;
617
+ let markerFragment = -1;
618
+ let markerIndex = -1;
619
+ let cursor = tail.length;
620
+ for (;;) {
621
+ if (cursor === 0) {
622
+ if (--fragment < 0) break;
623
+ cursor = buffer[fragment].length;
624
+ continue;
625
+ }
626
+ const code = buffer[fragment].charCodeAt(--cursor);
627
+ if (code === 10) break;
628
+ if (code !== 32 && code !== 9) {
629
+ firstNonSpace = code;
630
+ if (markerFragment < 0) {
631
+ markerFragment = fragment;
632
+ markerIndex = cursor;
633
+ }
634
+ }
635
+ }
636
+ if (firstNonSpace === -1) return LINE_OPEN;
637
+ if (firstNonSpace === 124) return LINE_ROW;
638
+ return listMarkerLineStart(buffer, markerFragment, markerIndex) ? LINE_OPEN : LINE_CONTENT;
639
+ }
640
+ function rowMarker(state) {
641
+ const indent = state.listIndent;
642
+ const lineState = lineStateBeforeRow(state.buffer);
643
+ if (!indent) return lineState === LINE_ROW ? "\n| " : lineState === LINE_CONTENT ? "\n\n| " : "| ";
644
+ switch (lineState) {
645
+ case LINE_ROW: return `\n${indent}| `;
646
+ case LINE_CONTENT: return `\n\n${indent}| `;
647
+ case LINE_FRESH: return `${indent}| `;
648
+ default: return "| ";
649
+ }
650
+ }
651
+ const MAX_CELL_SPAN = 64;
652
+ function cellEnter(node, state) {
653
+ if (node.index === 0) return "";
654
+ const header = state.tableHeaderCells || 0;
655
+ return header > 0 && (state.tableCurrentRowCells || 0) >= header ? " " : " | ";
656
+ }
657
+ function cellExit(node, state) {
658
+ const parsedSpan = parseUnsignedInteger(node.attributes?.colspan);
659
+ const span = parsedSpan && parsedSpan < 256 ? Math.min(parsedSpan, MAX_CELL_SPAN) : 1;
660
+ state.tableCurrentRowCells += span;
661
+ return span > 1 ? " |".repeat(span - 1) : void 0;
662
+ }
509
663
  function handleHeading(depth) {
510
664
  return {
511
665
  enter: ({ state }) => {
512
- if ((state.depthMap?.[26] || 0) > 0) return `<h${depth}>`;
666
+ if ((state.depthMap?.[26] || 0) > 0 || isInsideTableCell(state)) return `<h${depth}>`;
513
667
  return `${"#".repeat(depth)} `;
514
668
  },
515
669
  exit: ({ state }) => {
516
- if ((state.depthMap?.[26] || 0) > 0) return `</h${depth}>`;
670
+ if ((state.depthMap?.[26] || 0) > 0 || isInsideTableCell(state)) return `</h${depth}>`;
671
+ escapeTrailingHeadingHashes(state.buffer);
517
672
  },
518
673
  collapsesInnerWhiteSpace: true
519
674
  };
@@ -551,6 +706,11 @@ const Strikethrough = {
551
706
  spacing: NO_SPACING,
552
707
  isInline: true
553
708
  };
709
+ const INERT_RAWTEXT = {
710
+ isNonNesting: true,
711
+ excludesTextNodes: true,
712
+ spacing: NO_SPACING
713
+ };
554
714
  const tagHandlers = {
555
715
  [1]: {
556
716
  spacing: NO_SPACING,
@@ -590,7 +750,15 @@ const tagHandlers = {
590
750
  [11]: handleHeading(5),
591
751
  [12]: handleHeading(6),
592
752
  [13]: {
593
- enter: () => "---",
753
+ enter: ({ node, state }) => {
754
+ if (isInsideTableCell(state)) return "<hr>";
755
+ if (!(state.depthMap?.[25] || state.bufferedBlockquoteDepth)) return "---";
756
+ const prefix = continuationPrefix(node, state.listIndentWidths || [], !state.bufferedBlockquoteDepth);
757
+ if (!prefix) return "---";
758
+ if (state.depthMap?.[25]) state.listRulePending = prefix;
759
+ const open = blockOpenPrefix(state.buffer, prefix);
760
+ return open === void 0 ? "***" : `${open}---`;
761
+ },
594
762
  isSelfClosing: true
595
763
  },
596
764
  [14]: Strong,
@@ -648,13 +816,16 @@ const tagHandlers = {
648
816
  enter: ({ node, state }) => {
649
817
  if ((state.depthMap?.[34] || 0) > 0) {
650
818
  if (isInsideTableCell(state)) return "<code>";
651
- if (state.preOwnFence) return;
819
+ if (state.preFenceOpen) return;
652
820
  const language = getLanguageFromClass(node.attributes?.class);
653
- if ((state.depthMap?.[25] || 0) > 0) return {
654
- _tag: "CodeFenceEnter",
655
- language,
656
- output: `\n\n${state.listIndent}\`\`\`${language}\n`
657
- };
821
+ if ((state.depthMap?.[25] || 0) > 0) {
822
+ const indent = state.listIndent;
823
+ return {
824
+ _tag: "CodeFenceEnter",
825
+ language,
826
+ output: `${blockOpenPrefix(state.buffer, indent) ?? ""}\`\`\`${language}\n`
827
+ };
828
+ }
658
829
  return {
659
830
  _tag: "CodeFenceEnter",
660
831
  language,
@@ -678,18 +849,7 @@ const tagHandlers = {
678
849
  exit: ({ state }) => {
679
850
  if ((state.depthMap?.[34] || 0) > 0) {
680
851
  if (isInsideTableCell(state)) return "</code>";
681
- if (state.preOwnFence) return;
682
- if ((state.depthMap?.[25] || 0) > 0) {
683
- const indent = state.listIndent;
684
- return {
685
- _tag: "CodeFenceExit",
686
- output: `\n${indent}\`\`\`\n\n${indent}`
687
- };
688
- }
689
- return {
690
- _tag: "CodeFenceExit",
691
- output: `\n\`\`\``
692
- };
852
+ return;
693
853
  }
694
854
  if (isInsideRawHtmlBlock(state.depthMap)) return "</code>";
695
855
  return { _tag: "CodeSpanExit" };
@@ -709,7 +869,7 @@ const tagHandlers = {
709
869
  [25]: {
710
870
  enter: ({ node, state }) => {
711
871
  if (isInsideTableCell(state)) return "<li>";
712
- const marker = node.parent?.tagId === 33 ? `${node.index + 1}. ` : "- ";
872
+ const marker = node.parent?.tagId === 33 ? `${orderedItemNumber(node.parent, node.index)}. ` : "- ";
713
873
  return `${state.listIndent}${marker}`;
714
874
  },
715
875
  exit: ({ state }) => isInsideTableCell(state) ? "</li>" : void 0,
@@ -768,9 +928,14 @@ const tagHandlers = {
768
928
  if (isInsideTableCell(state)) return "<table>";
769
929
  if ((state.depthMap?.[28] || 0) <= 1) state.tableRenderedTable = false;
770
930
  state.tableColumnAlignments = [];
931
+ state.tableHeaderCells = 0;
771
932
  },
772
933
  exit: ({ state }) => isInsideTableCell(state) ? "</table>" : void 0
773
934
  },
935
+ [107]: { enter: ({ state }) => {
936
+ if ((state.depthMap?.[25] || 0) === 0 || isInsideTableCell(state)) return void 0;
937
+ return blockOpenPrefix(state.buffer, state.listIndent);
938
+ } },
774
939
  [29]: {
775
940
  enter: ({ state }) => {
776
941
  if (isInsideTableCell(state)) return "<thead>";
@@ -783,7 +948,7 @@ const tagHandlers = {
783
948
  enter: ({ state }) => {
784
949
  if (isInsideTableCell(state)) return "<tr>";
785
950
  state.tableCurrentRowCells = 0;
786
- return "| ";
951
+ return rowMarker(state);
787
952
  },
788
953
  exit: ({ state }) => {
789
954
  if (isInsideTableCell(state) || (state.depthMap?.[28] || 0) > 1) return "</tr>";
@@ -791,14 +956,16 @@ const tagHandlers = {
791
956
  state.tableRenderedTable = true;
792
957
  const alignments = state.tableColumnAlignments;
793
958
  while (alignments.length < state.tableCurrentRowCells) alignments.push("");
794
- return ` |\n| ${alignments.map((align) => {
959
+ const alignmentMarkers = alignments.map((align) => {
795
960
  switch (align) {
796
961
  case "left": return ":---";
797
962
  case "center": return ":---:";
798
963
  case "right": return "---:";
799
964
  default: return "---";
800
965
  }
801
- }).join(" | ")} |`;
966
+ });
967
+ state.tableHeaderCells = alignments.length;
968
+ return ` |\n${(state.depthMap?.[25] || 0) > 0 ? state.listIndent : ""}| ${alignmentMarkers.join(" | ")} |`;
802
969
  }
803
970
  return " |";
804
971
  },
@@ -811,11 +978,11 @@ const tagHandlers = {
811
978
  const align = node.attributes?.align?.toLowerCase();
812
979
  if (align) state.tableColumnAlignments.push(align);
813
980
  else if (state.tableColumnAlignments.length <= state.tableCurrentRowCells) state.tableColumnAlignments.push("");
814
- return node.index === 0 ? "" : " | ";
981
+ return cellEnter(node, state);
815
982
  },
816
- exit: ({ state }) => {
983
+ exit: ({ node, state }) => {
817
984
  if ((state.depthMap?.[28] || 0) > 1) return "</th>";
818
- state.tableCurrentRowCells++;
985
+ return cellExit(node, state);
819
986
  },
820
987
  collapsesInnerWhiteSpace: true,
821
988
  spacing: NO_SPACING
@@ -823,11 +990,11 @@ const tagHandlers = {
823
990
  [32]: {
824
991
  enter: ({ node, state }) => {
825
992
  if ((state.depthMap?.[28] || 0) > 1) return "<td>";
826
- return node.index === 0 ? "" : " | ";
993
+ return cellEnter(node, state);
827
994
  },
828
- exit: ({ state }) => {
995
+ exit: ({ node, state }) => {
829
996
  if ((state.depthMap?.[28] || 0) > 1) return "</td>";
830
- state.tableCurrentRowCells++;
997
+ return cellExit(node, state);
831
998
  },
832
999
  collapsesInnerWhiteSpace: true,
833
1000
  spacing: NO_SPACING
@@ -952,10 +1119,7 @@ const tagHandlers = {
952
1119
  [70]: { spacing: NO_SPACING },
953
1120
  [71]: { spacing: NO_SPACING },
954
1121
  [72]: { spacing: NO_SPACING },
955
- [73]: {
956
- isNonNesting: true,
957
- spacing: NO_SPACING
958
- },
1122
+ [73]: { ...INERT_RAWTEXT },
959
1123
  [74]: { spacing: NO_SPACING },
960
1124
  [75]: { spacing: NO_SPACING },
961
1125
  [76]: { spacing: NO_SPACING },
@@ -999,19 +1163,14 @@ const tagHandlers = {
999
1163
  spacing: NO_SPACING,
1000
1164
  isInline: true
1001
1165
  },
1002
- [84]: {
1003
- excludesTextNodes: true,
1004
- spacing: NO_SPACING
1005
- },
1166
+ [84]: { ...INERT_RAWTEXT },
1167
+ [112]: { ...INERT_RAWTEXT },
1006
1168
  [108]: {
1007
1169
  isNonNesting: true,
1008
1170
  excludesTextNodes: true,
1009
1171
  spacing: NO_SPACING
1010
1172
  },
1011
- [85]: {
1012
- isNonNesting: true,
1013
- spacing: NO_SPACING
1014
- },
1173
+ [85]: { ...INERT_RAWTEXT },
1015
1174
  [86]: {
1016
1175
  isNonNesting: true,
1017
1176
  spacing: NO_SPACING
@@ -1167,8 +1326,8 @@ const EQUALS_CHAR = 61;
1167
1326
  const QUOTE_CHAR = 34;
1168
1327
  const APOS_CHAR = 39;
1169
1328
  const EXCLAMATION_CHAR = 33;
1329
+ const QUESTION_CHAR = 63;
1170
1330
  const AMPERSAND_CHAR = 38;
1171
- const BACKSLASH_CHAR = 92;
1172
1331
  const DASH_CHAR = 45;
1173
1332
  const SPACE_CHAR = 32;
1174
1333
  const TAB_CHAR = 9;
@@ -1176,6 +1335,15 @@ const NEWLINE_CHAR = 10;
1176
1335
  const FORM_FEED_CHAR = 12;
1177
1336
  const CARRIAGE_RETURN_CHAR = 13;
1178
1337
  const OPEN_BRACKET_CHAR = 91;
1338
+ const ATTRIBUTE_GAP = 0;
1339
+ const ATTRIBUTE_NAME = 1;
1340
+ const ATTRIBUTE_AFTER_NAME = 2;
1341
+ const ATTRIBUTE_BEFORE_VALUE = 3;
1342
+ const ATTRIBUTE_QUOTED_VALUE = 4;
1343
+ const ATTRIBUTE_UNQUOTED_VALUE = 5;
1344
+ function isAsciiAlpha(code) {
1345
+ return code >= 65 && code <= 90 || code >= 97 && code <= 122;
1346
+ }
1179
1347
  function shouldProtectDecodedEntityReferences(state) {
1180
1348
  const depthMap = state.depthMap;
1181
1349
  return !state.plainText && !depthMap[34] && !depthMap[23] && !isInsideRawHtmlBlock(depthMap);
@@ -1202,7 +1370,7 @@ const HEAD_CONTENT_TAGS = /* @__PURE__ */ new Set([
1202
1370
  78
1203
1371
  ]);
1204
1372
  const CLOSES_P = (() => {
1205
- const t = /* @__PURE__ */ new Uint8Array(112);
1373
+ const t = /* @__PURE__ */ new Uint8Array(113);
1206
1374
  for (const id of [
1207
1375
  36,
1208
1376
  35,
@@ -1397,11 +1565,17 @@ function closeTableContext(state, closeable, handleEvent) {
1397
1565
  if (id === void 0 || !closeable.has(id)) return;
1398
1566
  clearFlattenedElements(state);
1399
1567
  }
1400
- while (state.currentNode) {
1401
- const id = state.currentNode.tagId;
1402
- if (id === void 0 || !closeable.has(id)) break;
1403
- closeNode(state.currentNode, state, handleEvent);
1568
+ let closeCount = 0;
1569
+ let walked = 0;
1570
+ for (let node = state.currentNode; node; node = node.parent) {
1571
+ const id = node.tagId;
1572
+ if (id !== void 0 && closeable.has(id)) {
1573
+ walked++;
1574
+ closeCount = walked;
1575
+ } else if (id === 28 || id === 78 || id === 107) break;
1576
+ else walked++;
1404
1577
  }
1578
+ for (let i = 0; i < closeCount && state.currentNode; i++) closeNode(state.currentNode, state, handleEvent);
1405
1579
  }
1406
1580
  function closeSelectTo(state, targetId, handleEvent) {
1407
1581
  if (state.flattenedTagName) {
@@ -1436,12 +1610,26 @@ function popParserTop(state, handleEvent) {
1436
1610
  if (state.flattenedTagName) clearFlattenedElements(state);
1437
1611
  else closeNode(state.currentNode, state, handleEvent);
1438
1612
  }
1613
+ function findIncompleteTagResidualIndex(leftover) {
1614
+ let index = leftover.indexOf("<");
1615
+ while (index !== -1) {
1616
+ if (index + 1 === leftover.length) return index;
1617
+ const c = leftover.charCodeAt(index + 1);
1618
+ if (isAsciiAlpha(c) || c === SLASH_CHAR || c === EXCLAMATION_CHAR || c === QUESTION_CHAR) return index;
1619
+ index = leftover.indexOf("<", index + 1);
1620
+ }
1621
+ return -1;
1622
+ }
1439
1623
  function finalizeParse(leftover, state, handleEvent) {
1440
1624
  if (state.currentNode && isScriptElement(state.currentNode)) {
1441
1625
  pushScriptTextChunk(state, leftover);
1442
1626
  const scriptText = takeScriptText(state);
1443
1627
  if (scriptText) processTextBuffer(scriptText, state, handleEvent);
1444
- } else if (leftover.length > 0 && leftover.charCodeAt(0) !== LT_CHAR) processTextBuffer(leftover, state, handleEvent);
1628
+ } else if (leftover.length > 0) {
1629
+ const incompleteTagIndex = findIncompleteTagResidualIndex(leftover);
1630
+ const text = incompleteTagIndex === -1 ? leftover : leftover.slice(0, incompleteTagIndex);
1631
+ if (text) processTextBuffer(text, state, handleEvent);
1632
+ }
1445
1633
  while (state.currentNode) closeNode(state.currentNode, state, handleEvent);
1446
1634
  }
1447
1635
  const EMPTY_ATTRIBUTES = Object.freeze({});
@@ -1471,12 +1659,12 @@ var ParsedElementNode = class {
1471
1659
  }
1472
1660
  get depthMap() {
1473
1661
  if (this.cachedDepthMap) return this.cachedDepthMap;
1474
- const depthMap = /* @__PURE__ */ new Uint16Array(112);
1475
- if (this.tagId >= 0 && this.tagId < 112) depthMap[this.tagId] = 1;
1662
+ const depthMap = /* @__PURE__ */ new Uint16Array(113);
1663
+ if (this.tagId >= 0 && this.tagId < 113) depthMap[this.tagId] = 1;
1476
1664
  let node = this.parent;
1477
1665
  while (node) {
1478
1666
  const tagId = node.tagId;
1479
- if (tagId !== void 0 && tagId >= 0 && tagId < 112) depthMap[tagId] = (depthMap[tagId] || 0) + 1;
1667
+ if (tagId !== void 0 && tagId >= 0 && tagId < 113) depthMap[tagId] = (depthMap[tagId] || 0) + 1;
1480
1668
  node = node.parent;
1481
1669
  }
1482
1670
  this.cachedDepthMap = depthMap;
@@ -1594,7 +1782,7 @@ function matchesClosingTag(node, tagName, tagId, closingIsAlias) {
1594
1782
  function parseHtml(html, options = {}) {
1595
1783
  const events = [];
1596
1784
  const state = {
1597
- depthMap: /* @__PURE__ */ new Uint16Array(112),
1785
+ depthMap: /* @__PURE__ */ new Uint16Array(113),
1598
1786
  depth: 0,
1599
1787
  resolvedPlugins: options.resolvedPlugins || []
1600
1788
  };
@@ -1613,7 +1801,7 @@ function parseHtmlStream(html, state, onEvent) {
1613
1801
  function parseHtmlInternal(htmlChunk, state, handleEvent) {
1614
1802
  let textBuffer = "";
1615
1803
  let runStart = 0;
1616
- state.depthMap ??= /* @__PURE__ */ new Uint16Array(112);
1804
+ state.depthMap ??= /* @__PURE__ */ new Uint16Array(113);
1617
1805
  state.depth ??= 0;
1618
1806
  state.lastCharWasWhitespace ??= true;
1619
1807
  state.justClosedTag ??= false;
@@ -1744,6 +1932,12 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1744
1932
  textBuffer += result.remainingText;
1745
1933
  break;
1746
1934
  }
1935
+ } else if (!isAsciiAlpha(nextCharCode) && nextCharCode !== QUESTION_CHAR) {
1936
+ state.textBufferContainsNonWhitespace = true;
1937
+ state.lastCharWasWhitespace = false;
1938
+ state.justClosedTag = false;
1939
+ textBuffer += htmlChunk[i++];
1940
+ continue;
1747
1941
  } else {
1748
1942
  let i2 = i + 1;
1749
1943
  const tagNameStart = i2;
@@ -2028,7 +2222,7 @@ function closeNode(node, state, handleEvent) {
2028
2222
  }
2029
2223
  }
2030
2224
  const tagId = node.tagId;
2031
- if (tagId !== void 0 && tagId >= 0 && tagId < 112) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
2225
+ if (tagId !== void 0 && tagId >= 0 && tagId < 113) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
2032
2226
  state.depth--;
2033
2227
  handleEvent({
2034
2228
  type: 1,
@@ -2070,12 +2264,30 @@ function processCommentOrDoctype(htmlChunk, position) {
2070
2264
  const chunkLength = htmlChunk.length;
2071
2265
  if (i + 3 < chunkLength && htmlChunk.charCodeAt(i + 2) === DASH_CHAR && htmlChunk.charCodeAt(i + 3) === DASH_CHAR) {
2072
2266
  i += 4;
2073
- while (i < chunkLength - 2) {
2074
- if (htmlChunk.charCodeAt(i) === DASH_CHAR && htmlChunk.charCodeAt(i + 1) === DASH_CHAR && htmlChunk.charCodeAt(i + 2) === GT_CHAR) {
2075
- i += 3;
2076
- return {
2267
+ if (i < chunkLength && htmlChunk.charCodeAt(i) === GT_CHAR) return {
2268
+ complete: true,
2269
+ newPosition: i + 1,
2270
+ remainingText: ""
2271
+ };
2272
+ if (i + 1 < chunkLength && htmlChunk.charCodeAt(i) === DASH_CHAR && htmlChunk.charCodeAt(i + 1) === GT_CHAR) return {
2273
+ complete: true,
2274
+ newPosition: i + 2,
2275
+ remainingText: ""
2276
+ };
2277
+ while (i + 2 < chunkLength) {
2278
+ const dash = htmlChunk.indexOf("-", i);
2279
+ if (dash < 0 || dash + 2 >= chunkLength) break;
2280
+ i = dash;
2281
+ if (htmlChunk.charCodeAt(i + 1) === DASH_CHAR) {
2282
+ const afterDashes = htmlChunk.charCodeAt(i + 2);
2283
+ if (afterDashes === GT_CHAR) return {
2077
2284
  complete: true,
2078
- newPosition: i,
2285
+ newPosition: i + 3,
2286
+ remainingText: ""
2287
+ };
2288
+ if (afterDashes === EXCLAMATION_CHAR && i + 3 < chunkLength && htmlChunk.charCodeAt(i + 3) === GT_CHAR) return {
2289
+ complete: true,
2290
+ newPosition: i + 4,
2079
2291
  remainingText: ""
2080
2292
  };
2081
2293
  }
@@ -2133,8 +2345,8 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
2133
2345
  tagId = effectiveTagId(tagName, tagId, state);
2134
2346
  if (state.currentNode?.tagHandler?.isNonNesting) closeNode(state.currentNode, state, handleEvent);
2135
2347
  const tagHandler = state.tagOverrideHandlers?.get(tagName) ?? tagHandlers[tagId];
2136
- const result = processTagAttributes(htmlChunk, i, tagHandler);
2137
- if (!result.complete) return {
2348
+ const result = scanTagAttributes(htmlChunk, i, tagHandler);
2349
+ if (result._tag === "incomplete") return {
2138
2350
  complete: false,
2139
2351
  newPosition: i,
2140
2352
  remainingText: `<${tagName}${result.attrBuffer}`,
@@ -2159,7 +2371,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
2159
2371
  const headNode = state.currentNode;
2160
2372
  if (headNode && headNode.tagId === 1) closeNode(headNode, state, handleEvent);
2161
2373
  }
2162
- if (tagId >= 0 && tagId < 112 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && parserTagDepth(state, 65) > 0) {
2374
+ if (tagId >= 0 && tagId < 113 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && parserTagDepth(state, 65) > 0) {
2163
2375
  if (closeSelectTo(state, 65, handleEvent)) return {
2164
2376
  complete: true,
2165
2377
  newPosition: result.newPosition,
@@ -2224,7 +2436,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
2224
2436
  skip: true
2225
2437
  };
2226
2438
  }
2227
- if (tagId >= 0 && tagId < 112) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
2439
+ if (tagId >= 0 && tagId < 113) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
2228
2440
  state.depth++;
2229
2441
  i = result.newPosition;
2230
2442
  if (state.currentNode) state.currentNode.currentWalkIndex = state.currentNode.currentWalkIndex || 0;
@@ -2251,137 +2463,118 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
2251
2463
  selfClosing: result.selfClosing
2252
2464
  };
2253
2465
  }
2254
- function processTagAttributes(htmlChunk, position, tagHandler) {
2255
- let i = position;
2256
- const chunkLength = htmlChunk.length;
2257
- const selfClosing = tagHandler?.isSelfClosing || false;
2258
- const attrStartPos = i;
2259
- let insideQuote = false;
2260
- let quoteChar = 0;
2261
- let hasAttributeContent = false;
2262
- let prevChar = 0;
2263
- while (i < chunkLength) {
2264
- const c = htmlChunk.charCodeAt(i);
2265
- if (insideQuote) {
2266
- if (c === quoteChar && prevChar !== BACKSLASH_CHAR) insideQuote = false;
2267
- i++;
2268
- continue;
2269
- } else if (c === QUOTE_CHAR || c === APOS_CHAR) {
2270
- insideQuote = true;
2271
- quoteChar = c;
2272
- hasAttributeContent = true;
2273
- } else if (c === SLASH_CHAR && i + 1 < chunkLength && htmlChunk.charCodeAt(i + 1) === GT_CHAR) {
2274
- const attrStr = hasAttributeContent ? htmlChunk.substring(attrStartPos, i).trim() : "";
2275
- return {
2276
- complete: true,
2277
- newPosition: i + 2,
2278
- attributes: hasAttributeContent ? parseAttributes(attrStr) : EMPTY_ATTRIBUTES,
2279
- selfClosing: true,
2280
- attrBuffer: attrStr
2281
- };
2282
- } else if (c === GT_CHAR) {
2283
- const attrStr = hasAttributeContent ? htmlChunk.substring(attrStartPos, i).trim() : "";
2284
- return {
2285
- complete: true,
2286
- newPosition: i + 1,
2287
- attributes: hasAttributeContent ? parseAttributes(attrStr) : EMPTY_ATTRIBUTES,
2288
- selfClosing,
2289
- attrBuffer: attrStr
2290
- };
2291
- } else if (!isWhitespace(c)) hasAttributeContent = true;
2292
- i++;
2293
- prevChar = c;
2294
- }
2295
- return {
2296
- complete: false,
2297
- newPosition: i,
2298
- attributes: EMPTY_ATTRIBUTES,
2299
- selfClosing: false,
2300
- attrBuffer: htmlChunk.substring(attrStartPos, i)
2301
- };
2466
+ function setAttribute(attributes, rawName, rawValue) {
2467
+ if (!rawName) return attributes;
2468
+ const name = rawName.toLowerCase();
2469
+ if (Object.hasOwn(attributes, name)) return attributes;
2470
+ const result = attributes === EMPTY_ATTRIBUTES ? {} : attributes;
2471
+ result[name] = rawValue.includes("&") ? decodeHTMLEntities(rawValue, true) : rawValue;
2472
+ return result;
2302
2473
  }
2303
- function parseAttributes(attrStr) {
2304
- if (!attrStr) return EMPTY_ATTRIBUTES;
2305
- const result = {};
2306
- const len = attrStr.length;
2307
- let i = 0;
2308
- const WHITESPACE = 0;
2309
- const NAME = 1;
2310
- const AFTER_NAME = 2;
2311
- const BEFORE_VALUE = 3;
2312
- const QUOTED_VALUE = 4;
2313
- const UNQUOTED_VALUE = 5;
2314
- let state = WHITESPACE;
2474
+ function scanAttributes(source, position, selfClosing, stopAtTagEnd) {
2475
+ const length = source.length;
2476
+ let attributes = EMPTY_ATTRIBUTES;
2477
+ let state = ATTRIBUTE_GAP;
2315
2478
  let nameStart = 0;
2316
2479
  let nameEnd = 0;
2317
2480
  let valueStart = 0;
2318
2481
  let quoteChar = 0;
2319
- let name = "";
2320
- while (i < len) {
2321
- const charCode = attrStr.charCodeAt(i);
2482
+ let newPosition = -1;
2483
+ let valueEnd = length;
2484
+ let i = position;
2485
+ while (i < length) {
2486
+ const charCode = source.charCodeAt(i);
2487
+ if (stopAtTagEnd && state !== ATTRIBUTE_QUOTED_VALUE) {
2488
+ if (state !== ATTRIBUTE_UNQUOTED_VALUE && charCode === SLASH_CHAR && i + 1 < length && source.charCodeAt(i + 1) === GT_CHAR) {
2489
+ selfClosing = true;
2490
+ valueEnd = i;
2491
+ newPosition = i + 2;
2492
+ break;
2493
+ }
2494
+ if (charCode === GT_CHAR) {
2495
+ valueEnd = i;
2496
+ newPosition = i + 1;
2497
+ break;
2498
+ }
2499
+ }
2322
2500
  const isSpace = isWhitespace(charCode);
2323
2501
  switch (state) {
2324
- case WHITESPACE:
2502
+ case ATTRIBUTE_GAP:
2325
2503
  if (!isSpace) {
2326
- state = NAME;
2504
+ state = ATTRIBUTE_NAME;
2327
2505
  nameStart = i;
2328
- nameEnd = 0;
2329
2506
  }
2330
2507
  break;
2331
- case NAME:
2508
+ case ATTRIBUTE_NAME:
2332
2509
  if (charCode === EQUALS_CHAR || isSpace) {
2333
2510
  nameEnd = i;
2334
- name = attrStr.substring(nameStart, nameEnd).toLowerCase();
2335
- state = charCode === EQUALS_CHAR ? BEFORE_VALUE : AFTER_NAME;
2511
+ state = charCode === EQUALS_CHAR ? ATTRIBUTE_BEFORE_VALUE : ATTRIBUTE_AFTER_NAME;
2336
2512
  }
2337
2513
  break;
2338
- case AFTER_NAME:
2339
- if (charCode === EQUALS_CHAR) state = BEFORE_VALUE;
2514
+ case ATTRIBUTE_AFTER_NAME:
2515
+ if (charCode === EQUALS_CHAR) state = ATTRIBUTE_BEFORE_VALUE;
2340
2516
  else if (!isSpace) {
2341
- result[name] = "";
2342
- state = NAME;
2517
+ attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), "");
2518
+ state = ATTRIBUTE_NAME;
2343
2519
  nameStart = i;
2344
- nameEnd = 0;
2345
2520
  }
2346
2521
  break;
2347
- case BEFORE_VALUE:
2522
+ case ATTRIBUTE_BEFORE_VALUE:
2348
2523
  if (charCode === QUOTE_CHAR || charCode === APOS_CHAR) {
2349
2524
  quoteChar = charCode;
2350
- state = QUOTED_VALUE;
2525
+ state = ATTRIBUTE_QUOTED_VALUE;
2351
2526
  valueStart = i + 1;
2352
2527
  } else if (!isSpace) {
2353
- state = UNQUOTED_VALUE;
2528
+ state = ATTRIBUTE_UNQUOTED_VALUE;
2354
2529
  valueStart = i;
2355
2530
  }
2356
2531
  break;
2357
- case QUOTED_VALUE:
2358
- if (charCode === BACKSLASH_CHAR && i + 1 < len) i++;
2359
- else if (charCode === quoteChar) {
2360
- const raw = attrStr.substring(valueStart, i);
2361
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
2362
- state = WHITESPACE;
2532
+ case ATTRIBUTE_QUOTED_VALUE:
2533
+ if (charCode === quoteChar) {
2534
+ attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, i));
2535
+ state = ATTRIBUTE_GAP;
2363
2536
  }
2364
2537
  break;
2365
- case UNQUOTED_VALUE:
2366
- if (isSpace || charCode === GT_CHAR) {
2367
- const raw = attrStr.substring(valueStart, i);
2368
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
2369
- state = WHITESPACE;
2538
+ case ATTRIBUTE_UNQUOTED_VALUE:
2539
+ if (isSpace || !stopAtTagEnd && charCode === GT_CHAR) {
2540
+ attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, i));
2541
+ state = ATTRIBUTE_GAP;
2370
2542
  }
2371
2543
  break;
2372
2544
  }
2373
2545
  i++;
2374
2546
  }
2375
- if (state === QUOTED_VALUE || state === UNQUOTED_VALUE) {
2376
- if (name) {
2377
- const raw = attrStr.substring(valueStart, i);
2378
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
2379
- }
2380
- } else if (state === NAME || state === AFTER_NAME || state === BEFORE_VALUE) {
2381
- nameEnd = nameEnd || i;
2382
- const currentName = attrStr.substring(nameStart, nameEnd).toLowerCase();
2383
- if (currentName) result[currentName] = "";
2547
+ if (stopAtTagEnd && newPosition === -1) return {
2548
+ _tag: "incomplete",
2549
+ attrBuffer: source.substring(position)
2550
+ };
2551
+ switch (state) {
2552
+ case ATTRIBUTE_NAME:
2553
+ attributes = setAttribute(attributes, source.substring(nameStart, valueEnd), "");
2554
+ break;
2555
+ case ATTRIBUTE_AFTER_NAME:
2556
+ case ATTRIBUTE_BEFORE_VALUE:
2557
+ attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), "");
2558
+ break;
2559
+ case ATTRIBUTE_QUOTED_VALUE:
2560
+ case ATTRIBUTE_UNQUOTED_VALUE:
2561
+ attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, valueEnd));
2562
+ break;
2384
2563
  }
2385
- return result;
2564
+ return {
2565
+ _tag: "complete",
2566
+ newPosition: stopAtTagEnd ? newPosition : length,
2567
+ attributes,
2568
+ selfClosing
2569
+ };
2570
+ }
2571
+ function scanTagAttributes(htmlChunk, position, tagHandler) {
2572
+ return scanAttributes(htmlChunk, position, tagHandler?.isSelfClosing || false, true);
2573
+ }
2574
+ function parseAttributes(attrStr) {
2575
+ if (!attrStr) return EMPTY_ATTRIBUTES;
2576
+ const result = scanAttributes(attrStr, 0, false, false);
2577
+ if (result._tag === "incomplete") throw new Error("Isolated attribute scans always complete at EOF");
2578
+ return result.attributes === EMPTY_ATTRIBUTES ? {} : result.attributes;
2386
2579
  }
2387
- export { breakHandler, buildTagOverrideHandlers, continuationPrefix, finalizeParse, isEmptyLinkHref, parseAttributes, parseHtml, parseHtmlStream, renderBreak, resolveUrl };
2580
+ export { blockOpenPrefix, breakHandler, buildTagOverrideHandlers, continuationPrefix, finalizeParse, isCharacterReferenceTail, isEmptyLinkHref, isInsideHeading, isInsideTableCell, listMarkerLineStart, orderedItemNumber, parseAttributes, parseHtml, parseHtmlStream, renderBreak, resolveUrl };