@mdream/js 1.5.5 → 1.5.7
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.
- package/dist/_chunks/const.mjs +7 -2
- package/dist/_chunks/markdown-processor.mjs +439 -67
- package/dist/_chunks/parse.mjs +430 -129
- package/dist/_chunks/plugins.mjs +35 -41
- package/dist/_chunks/src.mjs +2 -2
- package/dist/_chunks/types.d.mts +42 -5
- package/dist/index.mjs +1 -1
- package/dist/parse.d.mts +14 -3
- package/dist/splitter.mjs +5 -15
- package/package.json +1 -1
package/dist/_chunks/parse.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BLOCKQUOTE_SPACING, LIST_ITEM_SPACING, NO_SPACING, TABLE_ROW_SPACING, TagIdMap } from "./const.mjs";
|
|
1
|
+
import { BLOCKQUOTE_SPACING, LIST_ITEM_SPACING, NO_SPACING, TABLE_ROW_SPACING, TagIdMap, isInsideRawHtmlBlock } from "./const.mjs";
|
|
2
2
|
const HTML_ENTITIES = {
|
|
3
3
|
AElig: "Æ",
|
|
4
4
|
AMP: "&",
|
|
@@ -253,7 +253,20 @@ const HTML_ENTITIES = {
|
|
|
253
253
|
yuml: "ÿ",
|
|
254
254
|
$zeta: "ζ"
|
|
255
255
|
};
|
|
256
|
-
function
|
|
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;
|
|
259
|
+
}
|
|
260
|
+
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");
|
|
266
|
+
default: return false;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function continuationPrefix(node, listIndentWidths, includeBlockquotes = true) {
|
|
257
270
|
const chain = [];
|
|
258
271
|
let current = node.parent;
|
|
259
272
|
while (current) {
|
|
@@ -264,7 +277,7 @@ function continuationPrefix(node, listIndentWidths) {
|
|
|
264
277
|
let listIndex = 0;
|
|
265
278
|
for (let i = chain.length - 1; i >= 0; i--) {
|
|
266
279
|
const tagId = chain[i].tagId;
|
|
267
|
-
if (tagId === 22) prefix += "> ";
|
|
280
|
+
if (tagId === 22 && includeBlockquotes) prefix += "> ";
|
|
268
281
|
else if (tagId === 25) {
|
|
269
282
|
prefix += " ".repeat(listIndentWidths[listIndex] ?? 2);
|
|
270
283
|
listIndex++;
|
|
@@ -276,12 +289,80 @@ const C1_REPLACEMENTS = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”
|
|
|
276
289
|
function isAsciiAlphaNumeric(code) {
|
|
277
290
|
return code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122;
|
|
278
291
|
}
|
|
292
|
+
function isHtmlAsciiWhitespace(code) {
|
|
293
|
+
return code === 32 || code === 9 || code === 10 || code === 12 || code === 13;
|
|
294
|
+
}
|
|
295
|
+
const LANGUAGE_PREFIX = [
|
|
296
|
+
108,
|
|
297
|
+
97,
|
|
298
|
+
110,
|
|
299
|
+
103,
|
|
300
|
+
117,
|
|
301
|
+
97,
|
|
302
|
+
103,
|
|
303
|
+
101,
|
|
304
|
+
45
|
|
305
|
+
];
|
|
306
|
+
function hasLanguagePrefix(value, start) {
|
|
307
|
+
for (let offset = 0; offset < 9; offset++) if (value.charCodeAt(start + offset) !== LANGUAGE_PREFIX[offset]) return false;
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
function isUnsafeFenceInfoCode(code) {
|
|
311
|
+
if (code < 32 || code === 127) return true;
|
|
312
|
+
return code === 34 || code === 39 || code === 38 || code === 60 || code === 62 || code === 96 || code === 126;
|
|
313
|
+
}
|
|
314
|
+
function getLanguageFromClass(className) {
|
|
315
|
+
if (!className) return "";
|
|
316
|
+
const length = className.length;
|
|
317
|
+
let index = 0;
|
|
318
|
+
while (index < length) {
|
|
319
|
+
if (isHtmlAsciiWhitespace(className.charCodeAt(index))) {
|
|
320
|
+
index++;
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
const tokenStart = index;
|
|
324
|
+
do
|
|
325
|
+
index++;
|
|
326
|
+
while (index < length && !isHtmlAsciiWhitespace(className.charCodeAt(index)));
|
|
327
|
+
const tokenEnd = index;
|
|
328
|
+
const languageStart = tokenStart + 9;
|
|
329
|
+
if (languageStart >= tokenEnd || !hasLanguagePrefix(className, tokenStart)) continue;
|
|
330
|
+
let valid = true;
|
|
331
|
+
for (let cursor = languageStart; cursor < tokenEnd; cursor++) if (isUnsafeFenceInfoCode(className.charCodeAt(cursor))) {
|
|
332
|
+
valid = false;
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
if (valid) return className.slice(languageStart, tokenEnd);
|
|
336
|
+
}
|
|
337
|
+
return "";
|
|
338
|
+
}
|
|
279
339
|
function numericReplacement(codePoint) {
|
|
280
340
|
if (codePoint === 0 || codePoint > 1114111 || codePoint >= 55296 && codePoint <= 57343) return "�";
|
|
281
341
|
if (codePoint >= 128 && codePoint <= 159) return C1_REPLACEMENTS[codePoint - 128];
|
|
282
342
|
return String.fromCodePoint(codePoint);
|
|
283
343
|
}
|
|
284
|
-
function
|
|
344
|
+
function isCharacterReferenceTail(text, start) {
|
|
345
|
+
let index = start;
|
|
346
|
+
if (text.charCodeAt(index) === 35) {
|
|
347
|
+
index++;
|
|
348
|
+
const hex = text.charCodeAt(index) === 120 || text.charCodeAt(index) === 88;
|
|
349
|
+
if (hex) index++;
|
|
350
|
+
const digitStart = index;
|
|
351
|
+
while (index < text.length) {
|
|
352
|
+
const code = text.charCodeAt(index);
|
|
353
|
+
if (!(code >= 48 && code <= 57) && !(hex && (code >= 65 && code <= 70 || code >= 97 && code <= 102))) break;
|
|
354
|
+
index++;
|
|
355
|
+
}
|
|
356
|
+
return index > digitStart && text.charCodeAt(index) === 59;
|
|
357
|
+
}
|
|
358
|
+
const nameStart = index;
|
|
359
|
+
while (index < text.length && isAsciiAlphaNumeric(text.charCodeAt(index))) index++;
|
|
360
|
+
return index > nameStart && text.charCodeAt(index) === 59;
|
|
361
|
+
}
|
|
362
|
+
function decodedReference(replacement, text, next, protectDecodedEntityReferences) {
|
|
363
|
+
return replacement === "&" && protectDecodedEntityReferences && isCharacterReferenceTail(text, next) ? "\\&" : replacement;
|
|
364
|
+
}
|
|
365
|
+
function decodeHTMLEntities(text, inAttribute = false, protectDecodedEntityReferences = false) {
|
|
285
366
|
let result = "";
|
|
286
367
|
let i = 0;
|
|
287
368
|
const len = text.length;
|
|
@@ -309,7 +390,7 @@ function decodeHTMLEntities(text, inAttribute = false) {
|
|
|
309
390
|
end++;
|
|
310
391
|
}
|
|
311
392
|
if (end > digitStart) {
|
|
312
|
-
result += numericReplacement(codePoint);
|
|
393
|
+
result += decodedReference(numericReplacement(codePoint), text, text.charCodeAt(end) === 59 ? end + 1 : end, protectDecodedEntityReferences);
|
|
313
394
|
if (text.charCodeAt(end) === 59) end++;
|
|
314
395
|
i = end;
|
|
315
396
|
continue;
|
|
@@ -324,7 +405,7 @@ function decodeHTMLEntities(text, inAttribute = false) {
|
|
|
324
405
|
const direct = HTML_ENTITIES[name];
|
|
325
406
|
const replacement = typeof direct === "string" ? direct : HTML_ENTITIES[`$${name}`];
|
|
326
407
|
if (replacement !== void 0) {
|
|
327
|
-
result += replacement;
|
|
408
|
+
result += decodedReference(replacement, text, nameEnd + 1, protectDecodedEntityReferences);
|
|
328
409
|
i = nameEnd + 1;
|
|
329
410
|
continue;
|
|
330
411
|
}
|
|
@@ -336,7 +417,7 @@ function decodeHTMLEntities(text, inAttribute = false) {
|
|
|
336
417
|
if (typeof replacement === "string") {
|
|
337
418
|
const next = text.charCodeAt(legacyEnd);
|
|
338
419
|
if (replacement !== void 0 && !(inAttribute && (next === 61 || isAsciiAlphaNumeric(next)))) {
|
|
339
|
-
result += replacement;
|
|
420
|
+
result += decodedReference(replacement, text, legacyEnd, protectDecodedEntityReferences);
|
|
340
421
|
i = legacyEnd;
|
|
341
422
|
decodedLegacy = true;
|
|
342
423
|
}
|
|
@@ -360,16 +441,45 @@ function traverseUpToFirstBlockNode(node) {
|
|
|
360
441
|
}
|
|
361
442
|
return parentsToIncrement;
|
|
362
443
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
444
|
+
const TRACKING_PARAM_RE = /^(?:utm_|fbclid|gclid|mc_eid|msclkid|oly_)/;
|
|
445
|
+
const URL_SCHEME_RE = /^[\dA-Z+.-]+:/i;
|
|
446
|
+
function stripTrackingParams(url) {
|
|
447
|
+
const queryStart = url.indexOf("?");
|
|
448
|
+
const fragmentStart = url.indexOf("#");
|
|
449
|
+
if (queryStart === -1 || fragmentStart !== -1 && fragmentStart < queryStart) return url;
|
|
450
|
+
const queryEnd = fragmentStart === -1 ? url.length : fragmentStart;
|
|
451
|
+
const query = url.slice(queryStart + 1, queryEnd).split("&").filter((parameter) => !TRACKING_PARAM_RE.test(parameter)).join("&");
|
|
452
|
+
return `${url.slice(0, queryStart)}${query ? `?${query}` : ""}${url.slice(queryEnd)}`;
|
|
453
|
+
}
|
|
454
|
+
function resolveUrl(url, origin, clean) {
|
|
455
|
+
if (!url || url[0] === "#") return url;
|
|
456
|
+
let resolved = url;
|
|
457
|
+
if (url.startsWith("//")) resolved = `https:${url}`;
|
|
458
|
+
else if (origin && !URL_SCHEME_RE.test(url)) {
|
|
459
|
+
const path = url.startsWith("./") ? url.slice(2) : url;
|
|
460
|
+
while (origin.endsWith("/")) origin = origin.slice(0, -1);
|
|
461
|
+
resolved = `${origin}${path[0] === "/" ? "" : "/"}${path}`;
|
|
371
462
|
}
|
|
372
|
-
return
|
|
463
|
+
return (clean === true || !!clean && clean.urls === true) && resolved.includes("?") ? stripTrackingParams(resolved) : resolved;
|
|
464
|
+
}
|
|
465
|
+
function serializeMarkdownDestination(destination) {
|
|
466
|
+
if (!/[\t\n\f\r ()\\<>]/.test(destination)) return destination;
|
|
467
|
+
return `<${/[\\<>]/.test(destination) ? destination.replace(/[\\<>]/g, "\\$&") : destination}>`;
|
|
468
|
+
}
|
|
469
|
+
function stripsEmptyLink(state, href) {
|
|
470
|
+
const clean = state.options?.clean;
|
|
471
|
+
if (!(clean === true || typeof clean === "object" && clean.emptyLinks)) return false;
|
|
472
|
+
return isEmptyLinkHref(href);
|
|
473
|
+
}
|
|
474
|
+
function serializeMarkdownTitle(title) {
|
|
475
|
+
return /[\\"]/.test(title) ? title.replace(/[\\"]/g, "\\$&") : title;
|
|
476
|
+
}
|
|
477
|
+
function serializeMarkdownResource(destination, title) {
|
|
478
|
+
const serializedTitle = title ? ` "${serializeMarkdownTitle(title)}"` : "";
|
|
479
|
+
return `(${serializeMarkdownDestination(destination)}${serializedTitle})`;
|
|
480
|
+
}
|
|
481
|
+
function serializeImageDescription(alt) {
|
|
482
|
+
return /[\\[\]*_`~<&]/.test(alt) ? alt.replace(/[\\[\]*_`~<&]/g, "\\$&") : alt;
|
|
373
483
|
}
|
|
374
484
|
function isAutolinkUri(s) {
|
|
375
485
|
if (!(s.startsWith("http://") || s.startsWith("https://") || s.startsWith("ftp://") || s.startsWith("mailto:"))) return false;
|
|
@@ -383,15 +493,19 @@ function isInsideTableCell(state) {
|
|
|
383
493
|
const depthMap = state.depthMap;
|
|
384
494
|
return depthMap[32] > 0 || depthMap[31] > 0;
|
|
385
495
|
}
|
|
386
|
-
function
|
|
496
|
+
function renderBreak(node, state) {
|
|
387
497
|
const depthMap = state.depthMap;
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
if (!className) return "";
|
|
392
|
-
const langParts = className.split(" ").map((c) => c.split("language-")[1]).filter(Boolean);
|
|
393
|
-
return langParts && langParts.length > 0 ? langParts[0].trim() : "";
|
|
498
|
+
if (isInsideTableCell(state) || isInsideRawHtmlBlock(depthMap) || depthMap[7] || depthMap[8] || depthMap[9] || depthMap[10] || depthMap[11] || depthMap[12]) return "<br>";
|
|
499
|
+
if (depthMap[34] || depthMap[23]) return "\n";
|
|
500
|
+
return ` \n${continuationPrefix(node, state.listIndentWidths || [], !state.bufferedBlockquoteDepth)}`;
|
|
394
501
|
}
|
|
502
|
+
const breakHandler = {
|
|
503
|
+
enter: ({ node, state }) => renderBreak(node, state),
|
|
504
|
+
isSelfClosing: true,
|
|
505
|
+
spacing: NO_SPACING,
|
|
506
|
+
collapsesInnerWhiteSpace: true,
|
|
507
|
+
isInline: true
|
|
508
|
+
};
|
|
395
509
|
function handleHeading(depth) {
|
|
396
510
|
return {
|
|
397
511
|
enter: ({ state }) => {
|
|
@@ -420,16 +534,23 @@ const Strong = {
|
|
|
420
534
|
const Emphasis = {
|
|
421
535
|
enter: ({ state }) => {
|
|
422
536
|
if ((state.depthMap?.[17] || 0) > 1) return "";
|
|
423
|
-
return "
|
|
537
|
+
return "*";
|
|
424
538
|
},
|
|
425
539
|
exit: ({ node, state }) => {
|
|
426
540
|
if ((state.depthMap?.[17] || 0) + (node.tagId === 17 ? 1 : 0) > 1) return "";
|
|
427
|
-
return "
|
|
541
|
+
return "*";
|
|
428
542
|
},
|
|
429
543
|
collapsesInnerWhiteSpace: true,
|
|
430
544
|
spacing: NO_SPACING,
|
|
431
545
|
isInline: true
|
|
432
546
|
};
|
|
547
|
+
const Strikethrough = {
|
|
548
|
+
enter: () => "~~",
|
|
549
|
+
exit: () => "~~",
|
|
550
|
+
collapsesInnerWhiteSpace: true,
|
|
551
|
+
spacing: NO_SPACING,
|
|
552
|
+
isInline: true
|
|
553
|
+
};
|
|
433
554
|
const tagHandlers = {
|
|
434
555
|
[1]: {
|
|
435
556
|
spacing: NO_SPACING,
|
|
@@ -461,17 +582,7 @@ const tagHandlers = {
|
|
|
461
582
|
isSelfClosing: true,
|
|
462
583
|
spacing: NO_SPACING
|
|
463
584
|
},
|
|
464
|
-
[6]:
|
|
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 || [])}`;
|
|
469
|
-
},
|
|
470
|
-
isSelfClosing: true,
|
|
471
|
-
spacing: NO_SPACING,
|
|
472
|
-
collapsesInnerWhiteSpace: true,
|
|
473
|
-
isInline: true
|
|
474
|
-
},
|
|
585
|
+
[6]: breakHandler,
|
|
475
586
|
[7]: handleHeading(1),
|
|
476
587
|
[8]: handleHeading(2),
|
|
477
588
|
[9]: handleHeading(3),
|
|
@@ -486,13 +597,9 @@ const tagHandlers = {
|
|
|
486
597
|
[15]: Strong,
|
|
487
598
|
[16]: Emphasis,
|
|
488
599
|
[17]: Emphasis,
|
|
489
|
-
[18]:
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
collapsesInnerWhiteSpace: true,
|
|
493
|
-
spacing: NO_SPACING,
|
|
494
|
-
isInline: true
|
|
495
|
-
},
|
|
600
|
+
[18]: Strikethrough,
|
|
601
|
+
[110]: Strikethrough,
|
|
602
|
+
[111]: Strikethrough,
|
|
496
603
|
[19]: {
|
|
497
604
|
enter: () => "<sub>",
|
|
498
605
|
exit: () => "</sub>",
|
|
@@ -516,31 +623,25 @@ const tagHandlers = {
|
|
|
516
623
|
},
|
|
517
624
|
[22]: {
|
|
518
625
|
enter: ({ state }) => {
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
626
|
+
return {
|
|
627
|
+
_tag: "BlockquoteEnter",
|
|
628
|
+
output: (state.depthMap?.[25] || 0) > 0 ? "\n" : void 0
|
|
629
|
+
};
|
|
523
630
|
},
|
|
631
|
+
exit: () => ({ _tag: "BlockquoteExit" }),
|
|
524
632
|
spacing: BLOCKQUOTE_SPACING
|
|
525
633
|
},
|
|
526
634
|
[34]: {
|
|
527
635
|
enter: ({ node, state }) => {
|
|
528
636
|
if (isInsideTableCell(state)) return "<pre>";
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
637
|
+
return {
|
|
638
|
+
_tag: "PreEnter",
|
|
639
|
+
language: getLanguageFromClass(node.attributes?.class)
|
|
640
|
+
};
|
|
532
641
|
},
|
|
533
642
|
exit: ({ state }) => {
|
|
534
643
|
if (isInsideTableCell(state)) return "</pre>";
|
|
535
|
-
|
|
536
|
-
state.preFencePending = false;
|
|
537
|
-
state.preOwnFence = false;
|
|
538
|
-
if (!ownFence) return;
|
|
539
|
-
if ((state.depthMap?.[25] || 0) > 0) {
|
|
540
|
-
const indent = state.listIndent;
|
|
541
|
-
return `\n${indent}\`\`\`\n\n${indent}`;
|
|
542
|
-
}
|
|
543
|
-
return `\n\`\`\``;
|
|
644
|
+
return { _tag: "PreExit" };
|
|
544
645
|
}
|
|
545
646
|
},
|
|
546
647
|
[23]: {
|
|
@@ -548,17 +649,31 @@ const tagHandlers = {
|
|
|
548
649
|
if ((state.depthMap?.[34] || 0) > 0) {
|
|
549
650
|
if (isInsideTableCell(state)) return "<code>";
|
|
550
651
|
if (state.preOwnFence) return;
|
|
551
|
-
state.preFencePending = false;
|
|
552
652
|
const language = getLanguageFromClass(node.attributes?.class);
|
|
553
|
-
if ((state.depthMap?.[25] || 0) > 0) return
|
|
554
|
-
|
|
653
|
+
if ((state.depthMap?.[25] || 0) > 0) return {
|
|
654
|
+
_tag: "CodeFenceEnter",
|
|
655
|
+
language,
|
|
656
|
+
output: `\n\n${state.listIndent}\`\`\`${language}\n`
|
|
657
|
+
};
|
|
658
|
+
return {
|
|
659
|
+
_tag: "CodeFenceEnter",
|
|
660
|
+
language,
|
|
661
|
+
output: `\`\`\`${language}\n`
|
|
662
|
+
};
|
|
555
663
|
}
|
|
664
|
+
if (isInsideRawHtmlBlock(state.depthMap)) return "<code>";
|
|
556
665
|
if ((state.depthMap?.[25] || 0) > 0) {
|
|
557
666
|
const lastEntry = state.buffer.at(-1);
|
|
558
667
|
const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
|
|
559
|
-
if (lastChar && lastChar !== " " && lastChar !== "\n" && lastChar !== " " && lastChar !== "*" && lastChar !== "_" && lastChar !== "~" && lastChar !== "[" && lastChar !== ">") return
|
|
668
|
+
if (lastChar && lastChar !== " " && lastChar !== "\n" && lastChar !== " " && lastChar !== "*" && lastChar !== "_" && lastChar !== "~" && lastChar !== "[" && lastChar !== ">") return {
|
|
669
|
+
_tag: "CodeSpanEnter",
|
|
670
|
+
output: ` \``
|
|
671
|
+
};
|
|
560
672
|
}
|
|
561
|
-
return
|
|
673
|
+
return {
|
|
674
|
+
_tag: "CodeSpanEnter",
|
|
675
|
+
output: "`"
|
|
676
|
+
};
|
|
562
677
|
},
|
|
563
678
|
exit: ({ state }) => {
|
|
564
679
|
if ((state.depthMap?.[34] || 0) > 0) {
|
|
@@ -566,11 +681,18 @@ const tagHandlers = {
|
|
|
566
681
|
if (state.preOwnFence) return;
|
|
567
682
|
if ((state.depthMap?.[25] || 0) > 0) {
|
|
568
683
|
const indent = state.listIndent;
|
|
569
|
-
return
|
|
684
|
+
return {
|
|
685
|
+
_tag: "CodeFenceExit",
|
|
686
|
+
output: `\n${indent}\`\`\`\n\n${indent}`
|
|
687
|
+
};
|
|
570
688
|
}
|
|
571
|
-
return
|
|
689
|
+
return {
|
|
690
|
+
_tag: "CodeFenceExit",
|
|
691
|
+
output: `\n\`\`\``
|
|
692
|
+
};
|
|
572
693
|
}
|
|
573
|
-
return "
|
|
694
|
+
if (isInsideRawHtmlBlock(state.depthMap)) return "</code>";
|
|
695
|
+
return { _tag: "CodeSpanExit" };
|
|
574
696
|
},
|
|
575
697
|
collapsesInnerWhiteSpace: true,
|
|
576
698
|
spacing: NO_SPACING,
|
|
@@ -594,12 +716,16 @@ const tagHandlers = {
|
|
|
594
716
|
spacing: LIST_ITEM_SPACING
|
|
595
717
|
},
|
|
596
718
|
[26]: {
|
|
597
|
-
enter: ({ node }) => {
|
|
598
|
-
if (node.attributes?.href)
|
|
719
|
+
enter: ({ node, state }) => {
|
|
720
|
+
if (node.attributes?.href !== void 0) {
|
|
721
|
+
if (stripsEmptyLink(state, node.attributes.href)) return;
|
|
722
|
+
return "[";
|
|
723
|
+
}
|
|
599
724
|
},
|
|
600
725
|
exit: ({ node, state }) => {
|
|
601
|
-
if (
|
|
602
|
-
|
|
726
|
+
if (node.attributes?.href === void 0) return "";
|
|
727
|
+
if (stripsEmptyLink(state, node.attributes.href)) return "";
|
|
728
|
+
const href = resolveUrl(node.attributes.href, state.options?.origin, state.options?.clean);
|
|
603
729
|
let title = node.attributes?.title;
|
|
604
730
|
if (state.lastContentCache === title) title = "";
|
|
605
731
|
if (!title && isAutolinkUri(href)) {
|
|
@@ -620,7 +746,7 @@ const tagHandlers = {
|
|
|
620
746
|
return "";
|
|
621
747
|
}
|
|
622
748
|
}
|
|
623
|
-
return
|
|
749
|
+
return `]${serializeMarkdownResource(href, title)}`;
|
|
624
750
|
},
|
|
625
751
|
collapsesInnerWhiteSpace: true,
|
|
626
752
|
spacing: NO_SPACING,
|
|
@@ -628,7 +754,9 @@ const tagHandlers = {
|
|
|
628
754
|
},
|
|
629
755
|
[27]: {
|
|
630
756
|
enter: ({ node, state }) => {
|
|
631
|
-
|
|
757
|
+
const alt = node.attributes?.alt || "";
|
|
758
|
+
const src = resolveUrl(node.attributes?.src || "", state.options?.origin, state.options?.clean);
|
|
759
|
+
return `![${serializeImageDescription(alt)}]${serializeMarkdownResource(src, node.attributes?.title)}`;
|
|
632
760
|
},
|
|
633
761
|
collapsesInnerWhiteSpace: true,
|
|
634
762
|
isSelfClosing: true,
|
|
@@ -705,15 +833,6 @@ const tagHandlers = {
|
|
|
705
833
|
spacing: NO_SPACING
|
|
706
834
|
},
|
|
707
835
|
[35]: { enter: ({ state }) => {
|
|
708
|
-
const bqDepth = state.depthMap?.[22] || 0;
|
|
709
|
-
if (bqDepth > 0) {
|
|
710
|
-
const lastEntry = state.buffer.at(-1);
|
|
711
|
-
const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
|
|
712
|
-
if (lastChar && lastChar !== "\n" && lastChar !== " " && lastChar !== ">") {
|
|
713
|
-
const prefix = "> ".repeat(bqDepth);
|
|
714
|
-
return `\n${prefix.trimEnd()}\n${prefix}`;
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
836
|
if ((state.depthMap?.[25] || 0) > 0 && !isInsideTableCell(state)) {
|
|
718
837
|
const lastEntry = state.buffer.at(-1);
|
|
719
838
|
const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
|
|
@@ -997,8 +1116,8 @@ const tagHandlers = {
|
|
|
997
1116
|
[104]: {},
|
|
998
1117
|
[102]: {},
|
|
999
1118
|
[106]: {
|
|
1000
|
-
enter: () => "
|
|
1001
|
-
exit: () => "
|
|
1119
|
+
enter: () => "*",
|
|
1120
|
+
exit: () => "*",
|
|
1002
1121
|
collapsesInnerWhiteSpace: true,
|
|
1003
1122
|
spacing: NO_SPACING,
|
|
1004
1123
|
isInline: true
|
|
@@ -1056,10 +1175,11 @@ const TAB_CHAR = 9;
|
|
|
1056
1175
|
const NEWLINE_CHAR = 10;
|
|
1057
1176
|
const FORM_FEED_CHAR = 12;
|
|
1058
1177
|
const CARRIAGE_RETURN_CHAR = 13;
|
|
1059
|
-
const BACKTICK_CHAR = 96;
|
|
1060
|
-
const PIPE_CHAR = 124;
|
|
1061
1178
|
const OPEN_BRACKET_CHAR = 91;
|
|
1062
|
-
|
|
1179
|
+
function shouldProtectDecodedEntityReferences(state) {
|
|
1180
|
+
const depthMap = state.depthMap;
|
|
1181
|
+
return !state.plainText && !depthMap[34] && !depthMap[23] && !isInsideRawHtmlBlock(depthMap);
|
|
1182
|
+
}
|
|
1063
1183
|
const SCRIPT_DATA = 0;
|
|
1064
1184
|
const SCRIPT_DATA_ESCAPED = 1;
|
|
1065
1185
|
const SCRIPT_DATA_ESCAPED_DASH = 2;
|
|
@@ -1082,7 +1202,7 @@ const HEAD_CONTENT_TAGS = /* @__PURE__ */ new Set([
|
|
|
1082
1202
|
78
|
|
1083
1203
|
]);
|
|
1084
1204
|
const CLOSES_P = (() => {
|
|
1085
|
-
const t = /* @__PURE__ */ new Uint8Array(
|
|
1205
|
+
const t = /* @__PURE__ */ new Uint8Array(112);
|
|
1086
1206
|
for (const id of [
|
|
1087
1207
|
36,
|
|
1088
1208
|
35,
|
|
@@ -1245,6 +1365,14 @@ const SECTION_CELLS = /* @__PURE__ */ new Set([
|
|
|
1245
1365
|
107
|
|
1246
1366
|
]);
|
|
1247
1367
|
function closeImpliedTo(state, target, boundary, handleEvent) {
|
|
1368
|
+
if (state.flattenedTagName) {
|
|
1369
|
+
const id = state.flattenedTagId;
|
|
1370
|
+
if (id !== void 0 && target.has(id)) {
|
|
1371
|
+
clearFlattenedElements(state);
|
|
1372
|
+
return;
|
|
1373
|
+
}
|
|
1374
|
+
if (id !== void 0 && boundary.has(id)) return;
|
|
1375
|
+
}
|
|
1248
1376
|
let found = false;
|
|
1249
1377
|
for (let node = state.currentNode; node; node = node.parent) {
|
|
1250
1378
|
const id = node.tagId;
|
|
@@ -1255,6 +1383,7 @@ function closeImpliedTo(state, target, boundary, handleEvent) {
|
|
|
1255
1383
|
if (id !== void 0 && boundary.has(id)) break;
|
|
1256
1384
|
}
|
|
1257
1385
|
if (!found) return;
|
|
1386
|
+
clearFlattenedElements(state);
|
|
1258
1387
|
while (state.currentNode) {
|
|
1259
1388
|
const id = state.currentNode.tagId;
|
|
1260
1389
|
const isTarget = id !== void 0 && target.has(id);
|
|
@@ -1263,6 +1392,11 @@ function closeImpliedTo(state, target, boundary, handleEvent) {
|
|
|
1263
1392
|
}
|
|
1264
1393
|
}
|
|
1265
1394
|
function closeTableContext(state, closeable, handleEvent) {
|
|
1395
|
+
if (state.flattenedTagName) {
|
|
1396
|
+
const id = state.flattenedTagId;
|
|
1397
|
+
if (id === void 0 || !closeable.has(id)) return;
|
|
1398
|
+
clearFlattenedElements(state);
|
|
1399
|
+
}
|
|
1266
1400
|
while (state.currentNode) {
|
|
1267
1401
|
const id = state.currentNode.tagId;
|
|
1268
1402
|
if (id === void 0 || !closeable.has(id)) break;
|
|
@@ -1270,6 +1404,14 @@ function closeTableContext(state, closeable, handleEvent) {
|
|
|
1270
1404
|
}
|
|
1271
1405
|
}
|
|
1272
1406
|
function closeSelectTo(state, targetId, handleEvent) {
|
|
1407
|
+
if (state.flattenedTagName) {
|
|
1408
|
+
const id = state.flattenedTagId;
|
|
1409
|
+
if (id === targetId) {
|
|
1410
|
+
clearFlattenedElements(state);
|
|
1411
|
+
return true;
|
|
1412
|
+
}
|
|
1413
|
+
if (id === 65 && targetId !== 65 || id === 78) return false;
|
|
1414
|
+
}
|
|
1273
1415
|
let target;
|
|
1274
1416
|
for (let node = state.currentNode; node; node = node.parent) {
|
|
1275
1417
|
if (node.tagId === targetId) {
|
|
@@ -1279,10 +1421,21 @@ function closeSelectTo(state, targetId, handleEvent) {
|
|
|
1279
1421
|
if (node.tagId === 65 || node.tagId === 78) break;
|
|
1280
1422
|
}
|
|
1281
1423
|
if (!target) return false;
|
|
1424
|
+
clearFlattenedElements(state);
|
|
1282
1425
|
while (state.currentNode && state.currentNode !== target) closeNode(state.currentNode, state, handleEvent);
|
|
1283
1426
|
closeNode(target, state, handleEvent);
|
|
1284
1427
|
return true;
|
|
1285
1428
|
}
|
|
1429
|
+
function parserTagDepth(state, tagId) {
|
|
1430
|
+
return (state.depthMap[tagId] || 0) + (state.flattenedTagId === tagId ? state.flattenedTagDepth || 0 : 0);
|
|
1431
|
+
}
|
|
1432
|
+
function parserLastTagId(state) {
|
|
1433
|
+
return state.flattenedTagName ? state.flattenedTagId : state.currentNode?.tagId;
|
|
1434
|
+
}
|
|
1435
|
+
function popParserTop(state, handleEvent) {
|
|
1436
|
+
if (state.flattenedTagName) clearFlattenedElements(state);
|
|
1437
|
+
else closeNode(state.currentNode, state, handleEvent);
|
|
1438
|
+
}
|
|
1286
1439
|
function finalizeParse(leftover, state, handleEvent) {
|
|
1287
1440
|
if (state.currentNode && isScriptElement(state.currentNode)) {
|
|
1288
1441
|
pushScriptTextChunk(state, leftover);
|
|
@@ -1318,12 +1471,12 @@ var ParsedElementNode = class {
|
|
|
1318
1471
|
}
|
|
1319
1472
|
get depthMap() {
|
|
1320
1473
|
if (this.cachedDepthMap) return this.cachedDepthMap;
|
|
1321
|
-
const depthMap = /* @__PURE__ */ new
|
|
1322
|
-
if (this.tagId >= 0 && this.tagId <
|
|
1474
|
+
const depthMap = /* @__PURE__ */ new Uint16Array(112);
|
|
1475
|
+
if (this.tagId >= 0 && this.tagId < 112) depthMap[this.tagId] = 1;
|
|
1323
1476
|
let node = this.parent;
|
|
1324
1477
|
while (node) {
|
|
1325
1478
|
const tagId = node.tagId;
|
|
1326
|
-
if (tagId !== void 0 && tagId >= 0 && tagId <
|
|
1479
|
+
if (tagId !== void 0 && tagId >= 0 && tagId < 112) depthMap[tagId] = (depthMap[tagId] || 0) + 1;
|
|
1327
1480
|
node = node.parent;
|
|
1328
1481
|
}
|
|
1329
1482
|
this.cachedDepthMap = depthMap;
|
|
@@ -1441,7 +1594,7 @@ function matchesClosingTag(node, tagName, tagId, closingIsAlias) {
|
|
|
1441
1594
|
function parseHtml(html, options = {}) {
|
|
1442
1595
|
const events = [];
|
|
1443
1596
|
const state = {
|
|
1444
|
-
depthMap: /* @__PURE__ */ new
|
|
1597
|
+
depthMap: /* @__PURE__ */ new Uint16Array(112),
|
|
1445
1598
|
depth: 0,
|
|
1446
1599
|
resolvedPlugins: options.resolvedPlugins || []
|
|
1447
1600
|
};
|
|
@@ -1458,10 +1611,9 @@ function parseHtmlStream(html, state, onEvent) {
|
|
|
1458
1611
|
return parseHtmlInternal(html, state, onEvent);
|
|
1459
1612
|
}
|
|
1460
1613
|
function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
1461
|
-
if (state.depthLimitReached) return "";
|
|
1462
1614
|
let textBuffer = "";
|
|
1463
1615
|
let runStart = 0;
|
|
1464
|
-
state.depthMap ??= /* @__PURE__ */ new
|
|
1616
|
+
state.depthMap ??= /* @__PURE__ */ new Uint16Array(112);
|
|
1465
1617
|
state.depth ??= 0;
|
|
1466
1618
|
state.lastCharWasWhitespace ??= true;
|
|
1467
1619
|
state.justClosedTag ??= false;
|
|
@@ -1475,7 +1627,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
|
1475
1627
|
textBuffer = takeScriptText(state);
|
|
1476
1628
|
i = scanResult;
|
|
1477
1629
|
}
|
|
1478
|
-
while (i < chunkLength
|
|
1630
|
+
while (i < chunkLength) {
|
|
1479
1631
|
const currentCharCode = htmlChunk.charCodeAt(i);
|
|
1480
1632
|
if (currentCharCode !== LT_CHAR) {
|
|
1481
1633
|
if (currentCharCode === AMPERSAND_CHAR) state.hasEncodedHtmlEntity = true;
|
|
@@ -1498,12 +1650,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
|
1498
1650
|
state.textBufferContainsNonWhitespace = true;
|
|
1499
1651
|
state.lastCharWasWhitespace = false;
|
|
1500
1652
|
state.justClosedTag = false;
|
|
1501
|
-
|
|
1502
|
-
else if (!state.plainText && currentCharCode === BACKTICK_CHAR && (state.depthMap[23] || state.depthMap[34])) textBuffer += "\\`";
|
|
1503
|
-
else if (!state.plainText && currentCharCode === OPEN_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\[";
|
|
1504
|
-
else if (!state.plainText && currentCharCode === CLOSE_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\]";
|
|
1505
|
-
else if (!state.plainText && currentCharCode === GT_CHAR && state.depthMap[22]) textBuffer += "\\>";
|
|
1506
|
-
else textBuffer += htmlChunk[i];
|
|
1653
|
+
textBuffer += htmlChunk[i];
|
|
1507
1654
|
}
|
|
1508
1655
|
i++;
|
|
1509
1656
|
continue;
|
|
@@ -1513,6 +1660,25 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
|
1513
1660
|
break;
|
|
1514
1661
|
}
|
|
1515
1662
|
const nextCharCode = htmlChunk.charCodeAt(i + 1);
|
|
1663
|
+
if (state.flattenedRawTagName) if (nextCharCode === SLASH_CHAR) {
|
|
1664
|
+
let peekEnd = i + 2;
|
|
1665
|
+
while (peekEnd < chunkLength) {
|
|
1666
|
+
const c = htmlChunk.charCodeAt(peekEnd);
|
|
1667
|
+
if (c === GT_CHAR || c === SLASH_CHAR || isWhitespace(c)) break;
|
|
1668
|
+
peekEnd++;
|
|
1669
|
+
}
|
|
1670
|
+
if (peekEnd === chunkLength) {
|
|
1671
|
+
textBuffer += htmlChunk.substring(i);
|
|
1672
|
+
break;
|
|
1673
|
+
}
|
|
1674
|
+
if (normalizeTagName(htmlChunk.substring(i + 2, peekEnd)) !== state.flattenedRawTagName) {
|
|
1675
|
+
textBuffer += htmlChunk[i++];
|
|
1676
|
+
continue;
|
|
1677
|
+
}
|
|
1678
|
+
} else {
|
|
1679
|
+
textBuffer += htmlChunk[i++];
|
|
1680
|
+
continue;
|
|
1681
|
+
}
|
|
1516
1682
|
if (nextCharCode === EXCLAMATION_CHAR) {
|
|
1517
1683
|
if (htmlChunk.charCodeAt(i + 2) === OPEN_BRACKET_CHAR) {
|
|
1518
1684
|
if (htmlChunk.startsWith("<![CDATA[", i)) {
|
|
@@ -1605,6 +1771,9 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
|
1605
1771
|
continue;
|
|
1606
1772
|
}
|
|
1607
1773
|
if (!tagName) {
|
|
1774
|
+
state.textBufferContainsNonWhitespace = true;
|
|
1775
|
+
state.lastCharWasWhitespace = false;
|
|
1776
|
+
state.justClosedTag = false;
|
|
1608
1777
|
textBuffer += htmlChunk[i++];
|
|
1609
1778
|
continue;
|
|
1610
1779
|
}
|
|
@@ -1654,7 +1823,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
|
|
|
1654
1823
|
let rootText = textBuffer;
|
|
1655
1824
|
if (rootText.length === 0) return;
|
|
1656
1825
|
if (state.hasEncodedHtmlEntity) {
|
|
1657
|
-
rootText = decodeHTMLEntities(String(rootText));
|
|
1826
|
+
rootText = decodeHTMLEntities(String(rootText), false, shouldProtectDecodedEntityReferences(state));
|
|
1658
1827
|
state.hasEncodedHtmlEntity = false;
|
|
1659
1828
|
}
|
|
1660
1829
|
const rootTextNode = {
|
|
@@ -1673,7 +1842,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
|
|
|
1673
1842
|
state.lastTextNode = rootTextNode;
|
|
1674
1843
|
return;
|
|
1675
1844
|
}
|
|
1676
|
-
const excludesTextNodes = state.currentNode?.tagHandler?.excludesTextNodes || state.currentNode?.excludedFromMarkdown;
|
|
1845
|
+
const excludesTextNodes = (state.flattenedOpaqueDepth || 0) > 0 || state.flattenedRawExcludesText || state.currentNode?.tagHandler?.excludesTextNodes || state.currentNode?.excludedFromMarkdown;
|
|
1677
1846
|
const inPreTag = (state.depthMap[34] || 0) > 0;
|
|
1678
1847
|
if (!inPreTag && !containsNonWhitespace && !state.currentNode.childTextNodeIndex) return;
|
|
1679
1848
|
let text = textBuffer;
|
|
@@ -1686,7 +1855,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
|
|
|
1686
1855
|
if (start > 0) text = text.substring(start);
|
|
1687
1856
|
}
|
|
1688
1857
|
if (state.hasEncodedHtmlEntity) {
|
|
1689
|
-
text = decodeHTMLEntities(String(text));
|
|
1858
|
+
text = decodeHTMLEntities(String(text), false, shouldProtectDecodedEntityReferences(state));
|
|
1690
1859
|
state.hasEncodedHtmlEntity = false;
|
|
1691
1860
|
}
|
|
1692
1861
|
const textNode = {
|
|
@@ -1745,19 +1914,88 @@ function processClosingTag(htmlChunk, position, state, handleEvent) {
|
|
|
1745
1914
|
const mappedTagId = TagIdMap[tagName];
|
|
1746
1915
|
const tagId = effectiveTagId(tagName, typeof mappedTagId === "number" ? mappedTagId : -1, state);
|
|
1747
1916
|
const closingIsAlias = tagHandler?.aliasTagId !== void 0;
|
|
1917
|
+
if ((state.flattenedOpaqueDepth || 0) > 0) {
|
|
1918
|
+
if (state.flattenedRawTagName !== void 0) {
|
|
1919
|
+
state.flattenedRawTagName = void 0;
|
|
1920
|
+
state.flattenedRawExcludesText = void 0;
|
|
1921
|
+
}
|
|
1922
|
+
if (tagName === state.flattenedOpaqueTagName) {
|
|
1923
|
+
state.flattenedOpaqueDepth--;
|
|
1924
|
+
if (!state.flattenedOpaqueDepth) {
|
|
1925
|
+
state.flattenedOpaqueTagName = void 0;
|
|
1926
|
+
if (tagName === state.flattenedTagName) {
|
|
1927
|
+
state.flattenedTagDepth--;
|
|
1928
|
+
if (!state.flattenedTagDepth) clearFlattenedElements(state);
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
}
|
|
1932
|
+
state.justClosedTag = true;
|
|
1933
|
+
return {
|
|
1934
|
+
complete: true,
|
|
1935
|
+
newPosition: i + 1,
|
|
1936
|
+
remainingText: ""
|
|
1937
|
+
};
|
|
1938
|
+
}
|
|
1939
|
+
const stopAtTemplate = tagId !== 78 && (state.depthMap[78] || 0) > 0;
|
|
1940
|
+
let curr;
|
|
1941
|
+
if (state.flattenedTagName) {
|
|
1942
|
+
const closedRaw = state.flattenedRawTagName !== void 0;
|
|
1943
|
+
if (closedRaw) {
|
|
1944
|
+
state.flattenedRawTagName = void 0;
|
|
1945
|
+
state.flattenedRawExcludesText = void 0;
|
|
1946
|
+
}
|
|
1947
|
+
if (tagName === state.flattenedTagName) {
|
|
1948
|
+
state.flattenedTagDepth--;
|
|
1949
|
+
if (!state.flattenedTagDepth) clearFlattenedElements(state);
|
|
1950
|
+
state.justClosedTag = true;
|
|
1951
|
+
return {
|
|
1952
|
+
complete: true,
|
|
1953
|
+
newPosition: i + 1,
|
|
1954
|
+
remainingText: ""
|
|
1955
|
+
};
|
|
1956
|
+
}
|
|
1957
|
+
if (!closedRaw) {
|
|
1958
|
+
curr = state.currentNode;
|
|
1959
|
+
while (curr && !matchesClosingTag(curr, tagName, tagId, closingIsAlias)) {
|
|
1960
|
+
if (stopAtTemplate && curr.tagId === 78) {
|
|
1961
|
+
curr = null;
|
|
1962
|
+
break;
|
|
1963
|
+
}
|
|
1964
|
+
curr = curr.parent;
|
|
1965
|
+
}
|
|
1966
|
+
if (curr) clearFlattenedElements(state);
|
|
1967
|
+
else {
|
|
1968
|
+
state.justClosedTag = true;
|
|
1969
|
+
return {
|
|
1970
|
+
complete: true,
|
|
1971
|
+
newPosition: i + 1,
|
|
1972
|
+
remainingText: ""
|
|
1973
|
+
};
|
|
1974
|
+
}
|
|
1975
|
+
}
|
|
1976
|
+
if (closedRaw) {
|
|
1977
|
+
state.justClosedTag = true;
|
|
1978
|
+
return {
|
|
1979
|
+
complete: true,
|
|
1980
|
+
newPosition: i + 1,
|
|
1981
|
+
remainingText: ""
|
|
1982
|
+
};
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1748
1985
|
if (state.currentNode?.tagHandler?.isNonNesting && !matchesClosingTag(state.currentNode, tagName, tagId, closingIsAlias)) return {
|
|
1749
1986
|
complete: false,
|
|
1750
1987
|
newPosition: position,
|
|
1751
1988
|
remainingText: htmlChunk.substring(position)
|
|
1752
1989
|
};
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1990
|
+
if (!curr) {
|
|
1991
|
+
curr = state.currentNode;
|
|
1992
|
+
while (curr && !matchesClosingTag(curr, tagName, tagId, closingIsAlias)) {
|
|
1993
|
+
if (stopAtTemplate && curr.tagId === 78) {
|
|
1994
|
+
curr = null;
|
|
1995
|
+
break;
|
|
1996
|
+
}
|
|
1997
|
+
curr = curr.parent;
|
|
1759
1998
|
}
|
|
1760
|
-
curr = curr.parent;
|
|
1761
1999
|
}
|
|
1762
2000
|
if (curr) {
|
|
1763
2001
|
while (state.currentNode && state.currentNode !== curr) closeNode(state.currentNode, state, handleEvent);
|
|
@@ -1790,7 +2028,7 @@ function closeNode(node, state, handleEvent) {
|
|
|
1790
2028
|
}
|
|
1791
2029
|
}
|
|
1792
2030
|
const tagId = node.tagId;
|
|
1793
|
-
if (tagId !== void 0 && tagId >= 0 && tagId <
|
|
2031
|
+
if (tagId !== void 0 && tagId >= 0 && tagId < 112) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
|
|
1794
2032
|
state.depth--;
|
|
1795
2033
|
handleEvent({
|
|
1796
2034
|
type: 1,
|
|
@@ -1800,6 +2038,33 @@ function closeNode(node, state, handleEvent) {
|
|
|
1800
2038
|
state.hasEncodedHtmlEntity = false;
|
|
1801
2039
|
state.justClosedTag = true;
|
|
1802
2040
|
}
|
|
2041
|
+
function clearFlattenedElements(state) {
|
|
2042
|
+
state.flattenedTagName = void 0;
|
|
2043
|
+
state.flattenedTagId = void 0;
|
|
2044
|
+
state.flattenedTagDepth = void 0;
|
|
2045
|
+
state.flattenedRawTagName = void 0;
|
|
2046
|
+
state.flattenedRawExcludesText = void 0;
|
|
2047
|
+
}
|
|
2048
|
+
function startsOpaqueFlattening(state, tagName, tagId, attributes, tagHandler) {
|
|
2049
|
+
if (tagId === 78) return true;
|
|
2050
|
+
const plugins = state.resolvedPlugins;
|
|
2051
|
+
if (!plugins) return false;
|
|
2052
|
+
let node;
|
|
2053
|
+
for (const plugin of plugins) {
|
|
2054
|
+
if (!plugin.excludesOverflowSubtree) continue;
|
|
2055
|
+
node ||= new ParsedElementNode(tagName, attributes, state.currentNode, state.depth + 1, 0, tagId, tagHandler);
|
|
2056
|
+
if (plugin.excludesOverflowSubtree(node)) return true;
|
|
2057
|
+
}
|
|
2058
|
+
return false;
|
|
2059
|
+
}
|
|
2060
|
+
function enterOpaqueFlattening(state, tagName, tagHandler) {
|
|
2061
|
+
state.flattenedOpaqueTagName = tagName;
|
|
2062
|
+
state.flattenedOpaqueDepth = 1;
|
|
2063
|
+
if (tagHandler?.isNonNesting) {
|
|
2064
|
+
state.flattenedRawTagName = tagName;
|
|
2065
|
+
state.flattenedRawExcludesText = tagHandler.excludesTextNodes;
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
1803
2068
|
function processCommentOrDoctype(htmlChunk, position) {
|
|
1804
2069
|
let i = position;
|
|
1805
2070
|
const chunkLength = htmlChunk.length;
|
|
@@ -1875,12 +2140,26 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
|
|
|
1875
2140
|
remainingText: `<${tagName}${result.attrBuffer}`,
|
|
1876
2141
|
selfClosing: false
|
|
1877
2142
|
};
|
|
1878
|
-
if ((state.
|
|
2143
|
+
if ((state.flattenedOpaqueDepth || 0) > 0) {
|
|
2144
|
+
if (!result.selfClosing && tagName === state.flattenedOpaqueTagName) state.flattenedOpaqueDepth++;
|
|
2145
|
+
if (!result.selfClosing && tagHandler?.isNonNesting) {
|
|
2146
|
+
state.flattenedRawTagName = tagName;
|
|
2147
|
+
state.flattenedRawExcludesText = tagHandler.excludesTextNodes;
|
|
2148
|
+
}
|
|
2149
|
+
return {
|
|
2150
|
+
complete: true,
|
|
2151
|
+
newPosition: result.newPosition,
|
|
2152
|
+
remainingText: "",
|
|
2153
|
+
selfClosing: result.selfClosing,
|
|
2154
|
+
skip: true
|
|
2155
|
+
};
|
|
2156
|
+
}
|
|
2157
|
+
if (!state.flattenedTagName && (state.depthMap[1] || 0) > 0 && (state.depthMap[78] || 0) === 0 && !HEAD_CONTENT_TAGS.has(tagId)) {
|
|
1879
2158
|
while (state.currentNode && state.currentNode.tagId !== 1) closeNode(state.currentNode, state, handleEvent);
|
|
1880
2159
|
const headNode = state.currentNode;
|
|
1881
2160
|
if (headNode && headNode.tagId === 1) closeNode(headNode, state, handleEvent);
|
|
1882
2161
|
}
|
|
1883
|
-
if (tagId >= 0 && tagId <
|
|
2162
|
+
if (tagId >= 0 && tagId < 112 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && parserTagDepth(state, 65) > 0) {
|
|
1884
2163
|
if (closeSelectTo(state, 65, handleEvent)) return {
|
|
1885
2164
|
complete: true,
|
|
1886
2165
|
newPosition: result.newPosition,
|
|
@@ -1889,32 +2168,54 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
|
|
|
1889
2168
|
skip: true
|
|
1890
2169
|
};
|
|
1891
2170
|
} else if (tagId === 67) {
|
|
1892
|
-
if ((state
|
|
1893
|
-
else if (state
|
|
1894
|
-
} else if (tagId === 109 && (state
|
|
2171
|
+
if (parserTagDepth(state, 65) > 0) closeSelectTo(state, 67, handleEvent);
|
|
2172
|
+
else if (parserLastTagId(state) === 67) popParserTop(state, handleEvent);
|
|
2173
|
+
} else if (tagId === 109 && parserTagDepth(state, 65) > 0) {
|
|
1895
2174
|
closeSelectTo(state, 67, handleEvent);
|
|
1896
2175
|
closeSelectTo(state, 109, handleEvent);
|
|
1897
2176
|
} else if (tagId === 26) {
|
|
1898
|
-
if ((state
|
|
2177
|
+
if (parserTagDepth(state, 26) > 0) closeImpliedTo(state, SINGLE_A, A_SCOPE_BOUNDARY, handleEvent);
|
|
1899
2178
|
} else if (tagId === 32 || tagId === 31 || tagId === 30 || tagId === 29 || tagId === 38 || tagId === 39) {
|
|
1900
|
-
if ((state
|
|
1901
|
-
if ((state
|
|
2179
|
+
if (parserTagDepth(state, 28) > 0) if (tagId === 32 || tagId === 31) {
|
|
2180
|
+
if (parserTagDepth(state, 32) > 0 || parserTagDepth(state, 31) > 0) closeImpliedTo(state, TD_TH, CELL_SCOPE_BOUNDARY, handleEvent);
|
|
1902
2181
|
} else if (tagId === 30) {
|
|
1903
|
-
if ((state
|
|
2182
|
+
if (parserTagDepth(state, 30) > 0) closeTableContext(state, TR_CELLS, handleEvent);
|
|
1904
2183
|
} else closeTableContext(state, SECTION_CELLS, handleEvent);
|
|
1905
2184
|
} else {
|
|
1906
|
-
if ((state
|
|
2185
|
+
if (parserTagDepth(state, 35) > 0) closeImpliedTo(state, SINGLE_P, P_SCOPE_BOUNDARY, handleEvent);
|
|
1907
2186
|
if (HEADINGS.has(tagId)) {
|
|
1908
|
-
const
|
|
1909
|
-
if (
|
|
2187
|
+
const topId = parserLastTagId(state);
|
|
2188
|
+
if (topId !== void 0 && HEADINGS.has(topId)) popParserTop(state, handleEvent);
|
|
1910
2189
|
} else if (tagId === 25) {
|
|
1911
|
-
if ((state
|
|
2190
|
+
if (parserTagDepth(state, 25) > 0) closeImpliedTo(state, SINGLE_LI, LI_SCOPE_BOUNDARY, handleEvent);
|
|
1912
2191
|
} else if (tagId === 99 || tagId === 98) {
|
|
1913
|
-
if ((state
|
|
2192
|
+
if (parserTagDepth(state, 99) > 0 || parserTagDepth(state, 98) > 0) closeImpliedTo(state, DT_DD, DL_SCOPE_BOUNDARY, handleEvent);
|
|
1914
2193
|
}
|
|
1915
2194
|
}
|
|
2195
|
+
if (state.flattenedTagName && !result.selfClosing) {
|
|
2196
|
+
if (tagName === state.flattenedTagName) state.flattenedTagDepth++;
|
|
2197
|
+
if (startsOpaqueFlattening(state, tagName, tagId, result.attributes, tagHandler)) enterOpaqueFlattening(state, tagName, tagHandler);
|
|
2198
|
+
else if (tagHandler?.isNonNesting) {
|
|
2199
|
+
state.flattenedRawTagName = tagName;
|
|
2200
|
+
state.flattenedRawExcludesText = tagHandler.excludesTextNodes;
|
|
2201
|
+
}
|
|
2202
|
+
return {
|
|
2203
|
+
complete: true,
|
|
2204
|
+
newPosition: result.newPosition,
|
|
2205
|
+
remainingText: "",
|
|
2206
|
+
selfClosing: false,
|
|
2207
|
+
skip: true
|
|
2208
|
+
};
|
|
2209
|
+
}
|
|
1916
2210
|
if (!result.selfClosing && state.depth >= MAX_ELEMENT_DEPTH) {
|
|
1917
|
-
state.
|
|
2211
|
+
state.flattenedTagName = tagName;
|
|
2212
|
+
state.flattenedTagId = tagId;
|
|
2213
|
+
state.flattenedTagDepth = 1;
|
|
2214
|
+
if (startsOpaqueFlattening(state, tagName, tagId, result.attributes, tagHandler)) enterOpaqueFlattening(state, tagName, tagHandler);
|
|
2215
|
+
else if (tagHandler?.isNonNesting) {
|
|
2216
|
+
state.flattenedRawTagName = tagName;
|
|
2217
|
+
state.flattenedRawExcludesText = tagHandler.excludesTextNodes;
|
|
2218
|
+
}
|
|
1918
2219
|
return {
|
|
1919
2220
|
complete: true,
|
|
1920
2221
|
newPosition: result.newPosition,
|
|
@@ -1923,7 +2224,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
|
|
|
1923
2224
|
skip: true
|
|
1924
2225
|
};
|
|
1925
2226
|
}
|
|
1926
|
-
if (tagId >= 0 && tagId <
|
|
2227
|
+
if (tagId >= 0 && tagId < 112) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
|
|
1927
2228
|
state.depth++;
|
|
1928
2229
|
i = result.newPosition;
|
|
1929
2230
|
if (state.currentNode) state.currentNode.currentWalkIndex = state.currentNode.currentWalkIndex || 0;
|
|
@@ -2083,4 +2384,4 @@ function parseAttributes(attrStr) {
|
|
|
2083
2384
|
}
|
|
2084
2385
|
return result;
|
|
2085
2386
|
}
|
|
2086
|
-
export { buildTagOverrideHandlers, continuationPrefix, finalizeParse, parseAttributes, parseHtml, parseHtmlStream, resolveUrl };
|
|
2387
|
+
export { breakHandler, buildTagOverrideHandlers, continuationPrefix, finalizeParse, isEmptyLinkHref, parseAttributes, parseHtml, parseHtmlStream, renderBreak, resolveUrl };
|