@mdream/js 2.0.3 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,9 @@ function createParseState(processor, options) {
7
7
  resolvedPlugins: options.plugins,
8
8
  tagHandlers: options.tagHandlers,
9
9
  tagOverrideHandlers: options.tagOverrideHandlers,
10
- plainText: options.plainText
10
+ plainText: options.plainText,
11
+ keepLeadingBlanks: processor.state.outputFormat === "text",
12
+ urlBaseTarget: processor.state
11
13
  };
12
14
  }
13
15
  function createEventHandler(processor, plugins) {
@@ -1,44 +1,18 @@
1
1
  import { NO_SPACING, TagIdMap, isInsideRawHtmlBlock } from "./const.mjs";
2
- import { decodeHTMLEntities, traverseUpToFirstBlockNode } from "./utils.mjs";
3
- const selfClosing = { isSelfClosing: true };
4
- const rawText = { isNonNesting: true };
5
- const inertRawText = {
6
- isNonNesting: true,
7
- excludesTextNodes: true
8
- };
9
- const parserTagHandlers = {
10
- [4]: inertRawText,
11
- [52]: inertRawText,
12
- [53]: inertRawText,
13
- [5]: selfClosing,
14
- [6]: selfClosing,
15
- [13]: selfClosing,
16
- [27]: selfClosing,
17
- [29]: { excludesTextNodes: true },
18
- [30]: { excludesTextNodes: true },
19
- [38]: { excludesTextNodes: true },
20
- [39]: { excludesTextNodes: true },
21
- [54]: selfClosing,
22
- [55]: selfClosing,
23
- [56]: selfClosing,
24
- [57]: selfClosing,
25
- [58]: selfClosing,
26
- [59]: selfClosing,
27
- [60]: selfClosing,
28
- [61]: selfClosing,
29
- [62]: selfClosing,
30
- [63]: selfClosing,
31
- [64]: selfClosing,
32
- [66]: rawText,
33
- [73]: inertRawText,
34
- [78]: { excludesTextNodes: true },
35
- [84]: inertRawText,
36
- [112]: inertRawText,
37
- [108]: inertRawText,
38
- [85]: inertRawText,
39
- [86]: rawText,
40
- [87]: rawText
41
- };
2
+ import { decodeHTMLEntities, hasNonWhitespace, leadingWhitespaceLength, parserTagHandlers, resolveUrl, traverseUpToFirstBlockNode } from "./url.mjs";
3
+ const HTTP_BASE_RE = /^https?:\/\/(?:[^/?#]*@)?(?![^/?#]*@)[^:/?#]/i;
4
+ const MAX_BASE_HREF_LENGTH = 2048;
5
+ const MAX_ELEMENT_DEPTH = 512;
6
+ function captureBaseHref(state, href) {
7
+ const target = state.urlBaseTarget;
8
+ if (!target || href === void 0 || state.depthMap[26] || state.depthMap[78] || state.depth >= MAX_ELEMENT_DEPTH) return;
9
+ state.baseHrefSeen = true;
10
+ const base = resolveUrl(href, target.options.origin);
11
+ if (HTTP_BASE_RE.test(base) && base.length <= MAX_BASE_HREF_LENGTH && new TextEncoder().encode(base).length <= MAX_BASE_HREF_LENGTH) target.options = {
12
+ ...target.options,
13
+ origin: base
14
+ };
15
+ }
42
16
  const UNKNOWN_TAG_HANDLER = {
43
17
  isInline: true,
44
18
  spacing: NO_SPACING
@@ -52,6 +26,7 @@ const APOS_CHAR = 39;
52
26
  const EXCLAMATION_CHAR = 33;
53
27
  const QUESTION_CHAR = 63;
54
28
  const AMPERSAND_CHAR = 38;
29
+ const HASH_CHAR = 35;
55
30
  const DASH_CHAR = 45;
56
31
  const SPACE_CHAR = 32;
57
32
  const TAB_CHAR = 9;
@@ -72,6 +47,9 @@ function shouldProtectDecodedEntityReferences(state) {
72
47
  const depthMap = state.depthMap;
73
48
  return !state.plainText && !depthMap[4] && !depthMap[34] && !depthMap[23] && !isInsideRawHtmlBlock(depthMap);
74
49
  }
50
+ function keepsLeadingBlank(node) {
51
+ return node.tagHandler?.isInline ?? node.tagId === -1;
52
+ }
75
53
  const SCRIPT_DATA = 0;
76
54
  const SCRIPT_DATA_ESCAPED = 1;
77
55
  const SCRIPT_DATA_ESCAPED_DASH = 2;
@@ -82,7 +60,6 @@ const SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH = 6;
82
60
  const SCRIPT_SEQUENCE_NO_MATCH = -1;
83
61
  const SCRIPT_SEQUENCE_INCOMPLETE = -2;
84
62
  const SCRIPT_SCAN_COMPLETE = -1;
85
- const MAX_ELEMENT_DEPTH = 512;
86
63
  const HEAD_CONTENT_TAGS = /* @__PURE__ */ new Set([
87
64
  4,
88
65
  5,
@@ -354,6 +331,7 @@ function finalizeParse(leftover, state, handleEvent) {
354
331
  let text = trailing;
355
332
  if (state.currentNode?.tagHandler?.isNonNesting) {
356
333
  if (state.rawtextEndTagPending) text = "";
334
+ else if (!(state.discardExcludedText && state.currentNode.tagHandler.excludesTextNodes) && hasNonWhitespace(text)) state.textBufferContainsNonWhitespace = true;
357
335
  } else {
358
336
  const incompleteTagIndex = findIncompleteTagResidualIndex(trailing);
359
337
  if (incompleteTagIndex !== -1) text = trailing.slice(0, incompleteTagIndex);
@@ -417,6 +395,11 @@ function scriptSequenceEnd(html, nameStart) {
417
395
  const delimiter = html.charCodeAt(delimiterIndex);
418
396
  return delimiter === GT_CHAR || delimiter === SLASH_CHAR || isWhitespace(delimiter) ? delimiterIndex + 1 : SCRIPT_SEQUENCE_NO_MATCH;
419
397
  }
398
+ function inInertRawText(state) {
399
+ const node = state.currentNode;
400
+ const handler = node?.tagHandler;
401
+ return !!handler?.isNonNesting && !!handler.excludesTextNodes && node.tagId !== 4 && !isScriptElement(node);
402
+ }
420
403
  function isScriptElement(node) {
421
404
  return node.tagId === 52 && node.name === "script";
422
405
  }
@@ -482,7 +465,14 @@ function pushScriptTextChunk(state, text) {
482
465
  }
483
466
  function scanScriptChunk(html, start, state) {
484
467
  const result = findScriptEndTag(html, start, state);
485
- if (state.discardExcludedText && state.currentNode?.tagHandler?.excludesTextNodes) return result;
468
+ if (state.discardExcludedText && state.currentNode?.tagHandler?.excludesTextNodes) {
469
+ if ((result === SCRIPT_SCAN_COMPLETE ? html.length : result <= SCRIPT_SEQUENCE_INCOMPLETE ? -result - 2 : result) > start) {
470
+ state.lastCharWasWhitespace = false;
471
+ state.justClosedTag = false;
472
+ state.textBufferContainsWhitespace = false;
473
+ }
474
+ return result;
475
+ }
486
476
  if (result === SCRIPT_SCAN_COMPLETE) pushScriptTextChunk(state, html.substring(start));
487
477
  else if (result <= SCRIPT_SEQUENCE_INCOMPLETE) pushScriptTextChunk(state, html.substring(start, -result - 2));
488
478
  else pushScriptTextChunk(state, html.substring(start, result));
@@ -528,6 +518,23 @@ function parseHtml(html, options = {}) {
528
518
  remainingHtml
529
519
  };
530
520
  }
521
+ function commitPlainTextPrefix(remainingHtml, state, onEvent) {
522
+ const text = state.trailingText;
523
+ if (!state.textBufferContainsWhitespace || remainingHtml.length < 65536 || !text || text.length < 65536 || state.currentNode?.tagId !== 35 && state.currentNode?.tagId !== 25 || state.hasEncodedHtmlEntity || state.flattenedTagName || state.depthMap[34] || state.depthMap[23] || state.depthMap[28] || isInsideRawHtmlBlock(state.depthMap)) return remainingHtml;
524
+ const boundary = text.lastIndexOf(" ") + 1;
525
+ if (!boundary) return remainingHtml;
526
+ for (let offset = 0; offset < remainingHtml.length; offset++) {
527
+ const code = remainingHtml.charCodeAt(offset);
528
+ if (code !== SPACE_CHAR && !isAsciiAlpha(code) && code !== 44) return remainingHtml;
529
+ }
530
+ const suffix = text.substring(boundary);
531
+ processTextBuffer(text.substring(0, boundary), state, onEvent);
532
+ state.trailingText = suffix;
533
+ state.textBufferContainsWhitespace = false;
534
+ state.textBufferContainsNonWhitespace = suffix.length > 0;
535
+ state.lastCharWasWhitespace = true;
536
+ return suffix;
537
+ }
531
538
  function parseHtmlStream(html, state, onEvent) {
532
539
  return parseHtmlInternal(html, state, onEvent);
533
540
  }
@@ -541,7 +548,9 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
541
548
  state.justClosedTag ??= false;
542
549
  state.isFirstTextInElement ??= false;
543
550
  state.scriptDataState ??= SCRIPT_DATA;
551
+ state.listItemCounts ??= void 0;
544
552
  state.rawtextEndTagPending = false;
553
+ state.entitySeals = void 0;
545
554
  let i = 0;
546
555
  const chunkLength = htmlChunk.length;
547
556
  if (state.currentNode && isScriptElement(state.currentNode)) {
@@ -577,12 +586,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
577
586
  state.lastCharWasWhitespace = false;
578
587
  state.justClosedTag = false;
579
588
  const textStart = i++;
580
- while (i < chunkLength) {
581
- const code = htmlChunk.charCodeAt(i);
582
- if (code === LT_CHAR || isWhitespace(code)) break;
583
- if (code === AMPERSAND_CHAR) state.hasEncodedHtmlEntity = true;
584
- i++;
585
- }
589
+ i = scanTextRun(htmlChunk, i, chunkLength, state);
586
590
  textBuffer += htmlChunk.substring(textStart, i);
587
591
  continue;
588
592
  }
@@ -615,10 +619,17 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
615
619
  continue;
616
620
  }
617
621
  }
618
- if (nextCharCode === EXCLAMATION_CHAR) {
619
- const handler = state.currentNode?.tagHandler;
620
- const visibleRawText = state.flattenedRawTagName !== void 0 ? !state.flattenedRawExcludesText : handler?.isNonNesting && !handler.excludesTextNodes;
621
- const discardDeclaration = state.discardExcludedText && !visibleRawText;
622
+ if (state.currentNode?.tagHandler?.isNonNesting && nextCharCode !== SLASH_CHAR) {
623
+ state.textBufferContainsNonWhitespace = true;
624
+ state.lastCharWasWhitespace = false;
625
+ textBuffer += htmlChunk[i++];
626
+ continue;
627
+ }
628
+ if (nextCharCode === EXCLAMATION_CHAR && state.currentNode?.tagHandler?.isNonNesting) {
629
+ textBuffer += htmlChunk[i++];
630
+ continue;
631
+ } else if (nextCharCode === EXCLAMATION_CHAR) {
632
+ const discardDeclaration = state.discardExcludedText;
622
633
  if (htmlChunk.charCodeAt(i + 2) === OPEN_BRACKET_CHAR) {
623
634
  if (htmlChunk.startsWith("<![CDATA[", i)) {
624
635
  const end = htmlChunk.indexOf("]]>", i + 9);
@@ -649,26 +660,49 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
649
660
  break;
650
661
  }
651
662
  }
652
- if (textBuffer.length > 0) {
653
- processTextBuffer(textBuffer, state, handleEvent);
654
- textBuffer = "";
655
- runStart = i;
663
+ const declaration = (htmlChunk.charCodeAt(i + 2) | 32) === 100 || i + 2 === chunkLength ? htmlChunk.substring(i, i + 9).toLowerCase() : "";
664
+ if (declaration && declaration.length < 9 && "<!doctype".startsWith(declaration)) {
665
+ declarationPending = true;
666
+ textBuffer += htmlChunk.substring(i);
667
+ break;
656
668
  }
669
+ if (htmlChunk.startsWith("<!--", i)) {
670
+ if (textBuffer.length > 0) {
671
+ processTextBuffer(textBuffer, state, handleEvent);
672
+ textBuffer = "";
673
+ }
674
+ runStart = i;
675
+ } else sealTextRun(textBuffer, state);
657
676
  const result = processCommentOrDoctype(htmlChunk, i);
658
677
  if (result.complete) {
659
678
  i = result.newPosition;
660
- runStart = i;
679
+ if (textBuffer.length === 0) runStart = i;
661
680
  } else {
662
681
  declarationPending = true;
663
682
  if (discardDeclaration && htmlChunk.length - i >= 4) {
664
683
  const boundary = htmlChunk.charCodeAt(i + 2) === DASH_CHAR && htmlChunk.charCodeAt(i + 3) === DASH_CHAR ? result.remainingText.length <= 7 ? result.remainingText : `<!--x${result.remainingText.slice(-3)}` : "<!x";
665
- state.trailingText = boundary;
666
- return boundary;
684
+ if (textBuffer.length > 0 && isWhitespace(htmlChunk.charCodeAt(runStart))) state.lastCharWasWhitespace = false;
685
+ state.trailingText = textBuffer;
686
+ return htmlChunk.substring(runStart, i) + boundary;
667
687
  }
668
688
  textBuffer += result.remainingText;
669
689
  break;
670
690
  }
691
+ } else if ((nextCharCode === QUESTION_CHAR || nextCharCode === SLASH_CHAR && i + 2 < chunkLength && htmlChunk.charCodeAt(i + 2) !== GT_CHAR && !isAsciiAlpha(htmlChunk.charCodeAt(i + 2))) && !state.currentNode?.tagHandler?.isNonNesting) {
692
+ sealTextRun(textBuffer, state);
693
+ const end = htmlChunk.indexOf(">", i + 2);
694
+ if (end === -1) {
695
+ if (textBuffer.length > 0 && isWhitespace(htmlChunk.charCodeAt(runStart))) state.lastCharWasWhitespace = false;
696
+ state.trailingText = textBuffer;
697
+ return `${htmlChunk.substring(runStart, i)}<!x`;
698
+ }
699
+ i = end + 1;
700
+ if (textBuffer.length === 0) runStart = i;
671
701
  } else if (nextCharCode === SLASH_CHAR) {
702
+ if (i + 2 === chunkLength) {
703
+ textBuffer += htmlChunk.substring(i);
704
+ break;
705
+ }
672
706
  if (state.currentNode?.tagHandler?.isNonNesting) {
673
707
  let peekEnd = i + 2;
674
708
  while (peekEnd < chunkLength) {
@@ -677,8 +711,10 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
677
711
  peekEnd++;
678
712
  }
679
713
  if (peekEnd === chunkLength) {
680
- state.textBufferContainsNonWhitespace = true;
681
- state.lastCharWasWhitespace = false;
714
+ if (!state.currentNode.name.startsWith(normalizeTagName(htmlChunk.substring(i + 2, peekEnd)))) {
715
+ state.textBufferContainsNonWhitespace = true;
716
+ state.lastCharWasWhitespace = false;
717
+ }
682
718
  textBuffer += htmlChunk.substring(i);
683
719
  break;
684
720
  }
@@ -689,6 +725,10 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
689
725
  textBuffer += htmlChunk[i++];
690
726
  continue;
691
727
  }
728
+ if (inInertRawText(state)) {
729
+ if (state.inertBodyBlankPending || i > 0 && isWhitespace(htmlChunk.charCodeAt(i - 1))) state.lastCharWasWhitespace = false;
730
+ state.inertBodyBlankPending = false;
731
+ }
692
732
  }
693
733
  if (textBuffer.length > 0) {
694
734
  processTextBuffer(textBuffer, state, handleEvent);
@@ -732,10 +772,6 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
732
772
  const mappedTagId = TagIdMap[tagName];
733
773
  const tagId = effectiveTagId(tagName, typeof mappedTagId === "number" ? mappedTagId : -1, state);
734
774
  i2 = tagNameEnd;
735
- if (state.currentNode?.tagHandler?.isNonNesting) {
736
- textBuffer += htmlChunk[i++];
737
- continue;
738
- }
739
775
  if (!tagName) {
740
776
  state.textBufferContainsNonWhitespace = true;
741
777
  state.lastCharWasWhitespace = false;
@@ -802,12 +838,76 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
802
838
  state.textBufferContainsWhitespace = false;
803
839
  state.textBufferContainsNonWhitespace = false;
804
840
  state.hasEncodedHtmlEntity = false;
841
+ state.entitySeals = void 0;
805
842
  }
806
843
  if (remainingHtml.length > 0 && isWhitespace(remainingHtml.charCodeAt(0))) state.lastCharWasWhitespace = false;
844
+ if (inInertRawText(state)) {
845
+ const last = chunkLength - remainingHtml.length - 1;
846
+ if (last >= 0 && isWhitespace(htmlChunk.charCodeAt(last))) state.inertBodyBlankPending = true;
847
+ }
807
848
  state.trailingText = textBuffer;
808
849
  return remainingHtml;
809
850
  }
851
+ function scanTextRun(htmlChunk, i, chunkLength, state) {
852
+ while (i < chunkLength) {
853
+ const code = htmlChunk.charCodeAt(i);
854
+ if (code === LT_CHAR || isWhitespace(code)) break;
855
+ if (code === AMPERSAND_CHAR) state.hasEncodedHtmlEntity = true;
856
+ i++;
857
+ }
858
+ return i;
859
+ }
860
+ function sealTextRun(textBuffer, state) {
861
+ const at = textBuffer.length;
862
+ if (!state.hasEncodedHtmlEntity || at === 0) return;
863
+ const seals = state.entitySeals ??= [];
864
+ if (seals[seals.length - 1] !== at) seals.push(at);
865
+ }
866
+ function openReferenceStart(text, from, to) {
867
+ let amp = to - 1;
868
+ while (amp >= from && text.charCodeAt(amp) !== AMPERSAND_CHAR) amp--;
869
+ if (amp < from) return -1;
870
+ let k = amp + 1;
871
+ if (k < to && text.charCodeAt(k) === HASH_CHAR) {
872
+ k++;
873
+ const hex = k < to && (text.charCodeAt(k) | 32) === 120;
874
+ if (hex) k++;
875
+ for (; k < to; k++) {
876
+ const code = text.charCodeAt(k);
877
+ if (!(code >= 48 && code <= 57) && !(hex && (code | 32) >= 97 && (code | 32) <= 102)) return -1;
878
+ }
879
+ return amp;
880
+ }
881
+ for (; k < to; k++) {
882
+ const code = text.charCodeAt(k);
883
+ if (!(code >= 48 && code <= 57) && !isAsciiAlpha(code)) return -1;
884
+ }
885
+ return amp;
886
+ }
887
+ function endedReference(reference) {
888
+ if (reference.charCodeAt(1) === HASH_CHAR) {
889
+ const digitStart = (reference.charCodeAt(2) | 32) === 120 ? 3 : 2;
890
+ if (reference.length > digitStart) return `${reference};`;
891
+ }
892
+ const decoded = decodeHTMLEntities(reference, false, false);
893
+ return decoded === reference ? `&amp;${reference.slice(1)}` : decoded.replaceAll("&", "&amp;");
894
+ }
895
+ function endReferencesAtSeals(text, seals) {
896
+ let ended = "";
897
+ let from = 0;
898
+ for (let k = 0; k < seals.length; k++) {
899
+ const at = Math.min(seals[k], text.length);
900
+ const amp = openReferenceStart(text, from, at);
901
+ ended += amp === -1 ? text.slice(from, at) : text.slice(from, amp) + endedReference(text.slice(amp, at));
902
+ from = at;
903
+ }
904
+ return ended + text.slice(from);
905
+ }
810
906
  function processTextBuffer(textBuffer, state, handleEvent) {
907
+ if (state.entitySeals) {
908
+ textBuffer = endReferencesAtSeals(textBuffer, state.entitySeals);
909
+ state.entitySeals = void 0;
910
+ }
811
911
  const containsNonWhitespace = state.textBufferContainsNonWhitespace;
812
912
  const containsWhitespace = state.textBufferContainsWhitespace;
813
913
  state.textBufferContainsNonWhitespace = false;
@@ -815,6 +915,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
815
915
  if (!state.currentNode) {
816
916
  let rootText = textBuffer;
817
917
  if (rootText.length === 0) return;
918
+ const sourceLead = containsWhitespace ? leadingWhitespaceLength(rootText) : 0;
818
919
  if (state.hasEncodedHtmlEntity) {
819
920
  rootText = decodeHTMLEntities(String(rootText), false, shouldProtectDecodedEntityReferences(state));
820
921
  state.hasEncodedHtmlEntity = false;
@@ -829,7 +930,10 @@ function processTextBuffer(textBuffer, state, handleEvent) {
829
930
  excludedFromMarkdown: false
830
931
  };
831
932
  if (state.endTagClosedNothing) rootTextNode.joinsPrevious = true;
832
- if (containsWhitespace) rootTextNode.trimsAtLineStart = true;
933
+ if (sourceLead !== 0) {
934
+ rootTextNode.trimsAtLineStart = true;
935
+ rootTextNode.sourceLead = sourceLead;
936
+ }
833
937
  state.endTagClosedNothing = false;
834
938
  handleEvent({
835
939
  type: 0,
@@ -840,10 +944,18 @@ function processTextBuffer(textBuffer, state, handleEvent) {
840
944
  }
841
945
  const excludesTextNodes = (state.flattenedOpaqueDepth || 0) > 0 || state.flattenedRawExcludesText || state.currentNode?.tagHandler?.excludesTextNodes || state.currentNode?.excludedFromMarkdown;
842
946
  const inPreTag = (state.depthMap[34] || 0) > 0;
843
- if (!inPreTag && !containsNonWhitespace && !state.currentNode.childTextNodeIndex) return;
947
+ const isLeadingBlank = !inPreTag && !containsNonWhitespace && !state.currentNode.childTextNodeIndex;
948
+ if (isLeadingBlank && (state.plainText && !state.keepLeadingBlanks || !keepsLeadingBlank(state.currentNode))) return;
844
949
  let text = textBuffer;
845
950
  if (text.length === 0) return;
846
- const parentsToIncrement = traverseUpToFirstBlockNode(state.currentNode);
951
+ let parentsToIncrement;
952
+ if (isLeadingBlank && !state.resolvedPlugins?.length) {
953
+ if (state.leadingBlankParent !== state.currentNode) {
954
+ state.leadingBlankParent = state.currentNode;
955
+ state.leadingBlankAncestors = traverseUpToFirstBlockNode(state.currentNode);
956
+ }
957
+ parentsToIncrement = state.leadingBlankAncestors;
958
+ } else parentsToIncrement = traverseUpToFirstBlockNode(state.currentNode);
847
959
  const firstBlockParent = parentsToIncrement.at(-1);
848
960
  const firstInBlock = containsWhitespace && !firstBlockParent.childTextNodeIndex;
849
961
  const inlineAtRoot = firstInBlock && !firstBlockParent.parent && (firstBlockParent.tagHandler?.isInline ?? firstBlockParent.tagId === -1);
@@ -852,6 +964,30 @@ function processTextBuffer(textBuffer, state, handleEvent) {
852
964
  while (start < text.length && (inPreTag ? text.charCodeAt(start) === NEWLINE_CHAR || text.charCodeAt(start) === CARRIAGE_RETURN_CHAR : isWhitespace(text.charCodeAt(start)))) start++;
853
965
  if (start > 0) text = text.substring(start);
854
966
  }
967
+ if (isLeadingBlank) {
968
+ if (text) {
969
+ const blank = {
970
+ type: 2,
971
+ value: text,
972
+ parent: state.currentNode,
973
+ index: state.currentNode.currentWalkIndex,
974
+ depth: state.depth,
975
+ containsWhitespace,
976
+ excludedFromMarkdown: excludesTextNodes,
977
+ leadingBlank: true
978
+ };
979
+ handleEvent({
980
+ type: 0,
981
+ node: blank
982
+ });
983
+ if (!blank.excludedFromMarkdown && hasNonWhitespace(blank.value)) {
984
+ state.currentNode.currentWalkIndex++;
985
+ for (const parent of parentsToIncrement) parent.childTextNodeIndex = (parent.childTextNodeIndex || 0) + 1;
986
+ }
987
+ }
988
+ return;
989
+ }
990
+ const sourceLead = containsWhitespace && !inPreTag ? leadingWhitespaceLength(text) : 0;
855
991
  if (state.hasEncodedHtmlEntity) {
856
992
  text = decodeHTMLEntities(String(text), false, shouldProtectDecodedEntityReferences(state));
857
993
  state.hasEncodedHtmlEntity = false;
@@ -867,6 +1003,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
867
1003
  };
868
1004
  if (state.endTagClosedNothing) textNode.joinsPrevious = true;
869
1005
  if (inlineAtRoot) textNode.trimsAtLineStart = true;
1006
+ if (sourceLead !== 0) textNode.sourceLead = sourceLead;
870
1007
  state.endTagClosedNothing = false;
871
1008
  for (const parent of parentsToIncrement) parent.childTextNodeIndex = (parent.childTextNodeIndex || 0) + 1;
872
1009
  handleEvent({
@@ -1030,6 +1167,7 @@ function closeNode(node, state, handleEvent) {
1030
1167
  }
1031
1168
  const tagId = node.tagId;
1032
1169
  if (tagId !== void 0 && tagId >= 0 && tagId < 113) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
1170
+ if (tagId === 33 || tagId === 24) state.listItemCounts.pop();
1033
1171
  state.depth--;
1034
1172
  state.endTagClosedNothing = false;
1035
1173
  handleEvent({
@@ -1150,6 +1288,7 @@ function processCdataSection(content, state, handleEvent) {
1150
1288
  closeNode(state.currentNode ?? null, state, handleEvent);
1151
1289
  }
1152
1290
  function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1291
+ const isBase = tagId === 56;
1153
1292
  tagId = effectiveTagId(tagName, tagId, state);
1154
1293
  if (state.currentNode?.tagHandler?.isNonNesting) closeNode(state.currentNode, state, handleEvent);
1155
1294
  const tagHandler = state.tagOverrideHandlers?.get(tagName) ?? (tagId === -1 ? UNKNOWN_TAG_HANDLER : state.tagHandlers?.[tagId] ?? parserTagHandlers[tagId]);
@@ -1204,7 +1343,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1204
1343
  if (parserTagDepth(state, 30) > 0) closeTableContext(state, TR_CELLS, handleEvent);
1205
1344
  } else closeTableContext(state, SECTION_CELLS, handleEvent);
1206
1345
  }
1207
- } else {
1346
+ } else if (CLOSES_P[tagId] === 1) {
1208
1347
  if (parserTagDepth(state, 35) > 0) closeImpliedTo(state, SINGLE_P, P_SCOPE_BOUNDARY, handleEvent);
1209
1348
  if (HEADINGS.has(tagId)) {
1210
1349
  const topId = parserLastTagId(state);
@@ -1231,6 +1370,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1231
1370
  skip: true
1232
1371
  };
1233
1372
  }
1373
+ if (isBase && !state.baseHrefSeen) captureBaseHref(state, result.attributes.href);
1234
1374
  if (!result.selfClosing && state.depth >= MAX_ELEMENT_DEPTH) {
1235
1375
  state.flattenedTagName = tagName;
1236
1376
  state.flattenedTagId = tagId;
@@ -1252,8 +1392,10 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1252
1392
  state.depth++;
1253
1393
  i = result.newPosition;
1254
1394
  if (state.currentNode) state.currentNode.currentWalkIndex = state.currentNode.currentWalkIndex || 0;
1255
- const currentWalkIndex = state.currentNode ? state.currentNode.currentWalkIndex++ : 0;
1256
- const tag = new ParsedElementNode(tagName, result.attributes, state.currentNode, state.depth, currentWalkIndex, tagId, tagHandler);
1395
+ let index = state.currentNode ? state.currentNode.currentWalkIndex++ : 0;
1396
+ if (tagId === 33 || tagId === 24) (state.listItemCounts ||= []).push(0);
1397
+ else if (tagId === 25 && state.listItemCounts?.length && (state.currentNode?.tagId === 33 || state.currentNode?.tagId === 24)) index = state.listItemCounts[state.listItemCounts.length - 1]++;
1398
+ const tag = new ParsedElementNode(tagName, result.attributes, state.currentNode, state.depth, index, tagId, tagHandler);
1257
1399
  if (tagId === 78 || state.currentNode?.excludedFromMarkdown) tag.excludedFromMarkdown = true;
1258
1400
  state.lastTextNode = tag;
1259
1401
  state.endTagClosedNothing = false;
@@ -1297,13 +1439,21 @@ function scanAttributes(source, position, selfClosing, stopAtTagEnd) {
1297
1439
  let i = position;
1298
1440
  while (i < length) {
1299
1441
  const charCode = source.charCodeAt(i);
1300
- if (stopAtTagEnd && state !== ATTRIBUTE_QUOTED_VALUE) {
1301
- if (state !== ATTRIBUTE_UNQUOTED_VALUE && charCode === SLASH_CHAR && i + 1 < length && source.charCodeAt(i + 1) === GT_CHAR) {
1442
+ if (charCode === SLASH_CHAR && state <= ATTRIBUTE_AFTER_NAME) {
1443
+ if (stopAtTagEnd && i + 1 < length && source.charCodeAt(i + 1) === GT_CHAR) {
1302
1444
  selfClosing = true;
1303
1445
  valueEnd = i;
1304
1446
  newPosition = i + 2;
1305
1447
  break;
1306
1448
  }
1449
+ if (state !== ATTRIBUTE_GAP) {
1450
+ attributes = setAttribute(attributes, source.substring(nameStart, state === ATTRIBUTE_NAME ? i : nameEnd), "");
1451
+ state = ATTRIBUTE_GAP;
1452
+ }
1453
+ i++;
1454
+ continue;
1455
+ }
1456
+ if (stopAtTagEnd && state !== ATTRIBUTE_QUOTED_VALUE) {
1307
1457
  if (charCode === GT_CHAR) {
1308
1458
  valueEnd = i;
1309
1459
  newPosition = i + 1;
@@ -1381,9 +1531,9 @@ function scanTagAttributes(htmlChunk, position, tagHandler) {
1381
1531
  return scanAttributes(htmlChunk, position, tagHandler?.isSelfClosing || false, true);
1382
1532
  }
1383
1533
  function parseAttributes(attrStr) {
1384
- if (!attrStr) return EMPTY_ATTRIBUTES;
1534
+ if (!attrStr) return {};
1385
1535
  const result = scanAttributes(attrStr, 0, false, false);
1386
1536
  if (result._tag === "incomplete") throw new Error("Isolated attribute scans always complete at EOF");
1387
1537
  return result.attributes === EMPTY_ATTRIBUTES ? {} : result.attributes;
1388
1538
  }
1389
- export { finalizeParse, parseAttributes, parseHtml, parseHtmlStream, parserTagHandlers };
1539
+ export { commitPlainTextPrefix, finalizeParse, parseAttributes, parseHtml, parseHtmlStream };