@mdream/js 1.5.7 → 1.5.8
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 +1 -0
- package/dist/_chunks/markdown-processor.mjs +2 -2
- package/dist/_chunks/parse.mjs +153 -128
- package/dist/parse.d.mts +6 -6
- package/package.json +1 -1
package/dist/_chunks/const.mjs
CHANGED
|
@@ -35,7 +35,7 @@ function processPluginsForEvent(event, plugins, state, processEvent) {
|
|
|
35
35
|
processEvent(event);
|
|
36
36
|
return false;
|
|
37
37
|
}
|
|
38
|
-
const INLINE_MARKER_TYPE = /* @__PURE__ */ new Uint8Array(
|
|
38
|
+
const INLINE_MARKER_TYPE = /* @__PURE__ */ new Uint8Array(113);
|
|
39
39
|
INLINE_MARKER_TYPE[14] = 1;
|
|
40
40
|
INLINE_MARKER_TYPE[15] = 1;
|
|
41
41
|
INLINE_MARKER_TYPE[91] = 1;
|
|
@@ -581,7 +581,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
581
581
|
const state = {
|
|
582
582
|
options,
|
|
583
583
|
buffer: [],
|
|
584
|
-
depthMap: /* @__PURE__ */ new Uint16Array(
|
|
584
|
+
depthMap: /* @__PURE__ */ new Uint16Array(113),
|
|
585
585
|
listIndent: "",
|
|
586
586
|
listIndentWidths: [],
|
|
587
587
|
blockquotes: [],
|
package/dist/_chunks/parse.mjs
CHANGED
|
@@ -551,6 +551,11 @@ const Strikethrough = {
|
|
|
551
551
|
spacing: NO_SPACING,
|
|
552
552
|
isInline: true
|
|
553
553
|
};
|
|
554
|
+
const INERT_RAWTEXT = {
|
|
555
|
+
isNonNesting: true,
|
|
556
|
+
excludesTextNodes: true,
|
|
557
|
+
spacing: NO_SPACING
|
|
558
|
+
};
|
|
554
559
|
const tagHandlers = {
|
|
555
560
|
[1]: {
|
|
556
561
|
spacing: NO_SPACING,
|
|
@@ -952,10 +957,7 @@ const tagHandlers = {
|
|
|
952
957
|
[70]: { spacing: NO_SPACING },
|
|
953
958
|
[71]: { spacing: NO_SPACING },
|
|
954
959
|
[72]: { spacing: NO_SPACING },
|
|
955
|
-
[73]: {
|
|
956
|
-
isNonNesting: true,
|
|
957
|
-
spacing: NO_SPACING
|
|
958
|
-
},
|
|
960
|
+
[73]: { ...INERT_RAWTEXT },
|
|
959
961
|
[74]: { spacing: NO_SPACING },
|
|
960
962
|
[75]: { spacing: NO_SPACING },
|
|
961
963
|
[76]: { spacing: NO_SPACING },
|
|
@@ -999,19 +1001,14 @@ const tagHandlers = {
|
|
|
999
1001
|
spacing: NO_SPACING,
|
|
1000
1002
|
isInline: true
|
|
1001
1003
|
},
|
|
1002
|
-
[84]: {
|
|
1003
|
-
|
|
1004
|
-
spacing: NO_SPACING
|
|
1005
|
-
},
|
|
1004
|
+
[84]: { ...INERT_RAWTEXT },
|
|
1005
|
+
[112]: { ...INERT_RAWTEXT },
|
|
1006
1006
|
[108]: {
|
|
1007
1007
|
isNonNesting: true,
|
|
1008
1008
|
excludesTextNodes: true,
|
|
1009
1009
|
spacing: NO_SPACING
|
|
1010
1010
|
},
|
|
1011
|
-
[85]: {
|
|
1012
|
-
isNonNesting: true,
|
|
1013
|
-
spacing: NO_SPACING
|
|
1014
|
-
},
|
|
1011
|
+
[85]: { ...INERT_RAWTEXT },
|
|
1015
1012
|
[86]: {
|
|
1016
1013
|
isNonNesting: true,
|
|
1017
1014
|
spacing: NO_SPACING
|
|
@@ -1167,8 +1164,8 @@ const EQUALS_CHAR = 61;
|
|
|
1167
1164
|
const QUOTE_CHAR = 34;
|
|
1168
1165
|
const APOS_CHAR = 39;
|
|
1169
1166
|
const EXCLAMATION_CHAR = 33;
|
|
1167
|
+
const QUESTION_CHAR = 63;
|
|
1170
1168
|
const AMPERSAND_CHAR = 38;
|
|
1171
|
-
const BACKSLASH_CHAR = 92;
|
|
1172
1169
|
const DASH_CHAR = 45;
|
|
1173
1170
|
const SPACE_CHAR = 32;
|
|
1174
1171
|
const TAB_CHAR = 9;
|
|
@@ -1176,6 +1173,15 @@ const NEWLINE_CHAR = 10;
|
|
|
1176
1173
|
const FORM_FEED_CHAR = 12;
|
|
1177
1174
|
const CARRIAGE_RETURN_CHAR = 13;
|
|
1178
1175
|
const OPEN_BRACKET_CHAR = 91;
|
|
1176
|
+
const ATTRIBUTE_GAP = 0;
|
|
1177
|
+
const ATTRIBUTE_NAME = 1;
|
|
1178
|
+
const ATTRIBUTE_AFTER_NAME = 2;
|
|
1179
|
+
const ATTRIBUTE_BEFORE_VALUE = 3;
|
|
1180
|
+
const ATTRIBUTE_QUOTED_VALUE = 4;
|
|
1181
|
+
const ATTRIBUTE_UNQUOTED_VALUE = 5;
|
|
1182
|
+
function isAsciiAlpha(code) {
|
|
1183
|
+
return code >= 65 && code <= 90 || code >= 97 && code <= 122;
|
|
1184
|
+
}
|
|
1179
1185
|
function shouldProtectDecodedEntityReferences(state) {
|
|
1180
1186
|
const depthMap = state.depthMap;
|
|
1181
1187
|
return !state.plainText && !depthMap[34] && !depthMap[23] && !isInsideRawHtmlBlock(depthMap);
|
|
@@ -1202,7 +1208,7 @@ const HEAD_CONTENT_TAGS = /* @__PURE__ */ new Set([
|
|
|
1202
1208
|
78
|
|
1203
1209
|
]);
|
|
1204
1210
|
const CLOSES_P = (() => {
|
|
1205
|
-
const t = /* @__PURE__ */ new Uint8Array(
|
|
1211
|
+
const t = /* @__PURE__ */ new Uint8Array(113);
|
|
1206
1212
|
for (const id of [
|
|
1207
1213
|
36,
|
|
1208
1214
|
35,
|
|
@@ -1436,12 +1442,26 @@ function popParserTop(state, handleEvent) {
|
|
|
1436
1442
|
if (state.flattenedTagName) clearFlattenedElements(state);
|
|
1437
1443
|
else closeNode(state.currentNode, state, handleEvent);
|
|
1438
1444
|
}
|
|
1445
|
+
function findIncompleteTagResidualIndex(leftover) {
|
|
1446
|
+
let index = leftover.indexOf("<");
|
|
1447
|
+
while (index !== -1) {
|
|
1448
|
+
if (index + 1 === leftover.length) return index;
|
|
1449
|
+
const c = leftover.charCodeAt(index + 1);
|
|
1450
|
+
if (isAsciiAlpha(c) || c === SLASH_CHAR || c === EXCLAMATION_CHAR || c === QUESTION_CHAR) return index;
|
|
1451
|
+
index = leftover.indexOf("<", index + 1);
|
|
1452
|
+
}
|
|
1453
|
+
return -1;
|
|
1454
|
+
}
|
|
1439
1455
|
function finalizeParse(leftover, state, handleEvent) {
|
|
1440
1456
|
if (state.currentNode && isScriptElement(state.currentNode)) {
|
|
1441
1457
|
pushScriptTextChunk(state, leftover);
|
|
1442
1458
|
const scriptText = takeScriptText(state);
|
|
1443
1459
|
if (scriptText) processTextBuffer(scriptText, state, handleEvent);
|
|
1444
|
-
} else if (leftover.length > 0
|
|
1460
|
+
} else if (leftover.length > 0) {
|
|
1461
|
+
const incompleteTagIndex = findIncompleteTagResidualIndex(leftover);
|
|
1462
|
+
const text = incompleteTagIndex === -1 ? leftover : leftover.slice(0, incompleteTagIndex);
|
|
1463
|
+
if (text) processTextBuffer(text, state, handleEvent);
|
|
1464
|
+
}
|
|
1445
1465
|
while (state.currentNode) closeNode(state.currentNode, state, handleEvent);
|
|
1446
1466
|
}
|
|
1447
1467
|
const EMPTY_ATTRIBUTES = Object.freeze({});
|
|
@@ -1471,12 +1491,12 @@ var ParsedElementNode = class {
|
|
|
1471
1491
|
}
|
|
1472
1492
|
get depthMap() {
|
|
1473
1493
|
if (this.cachedDepthMap) return this.cachedDepthMap;
|
|
1474
|
-
const depthMap = /* @__PURE__ */ new Uint16Array(
|
|
1475
|
-
if (this.tagId >= 0 && this.tagId <
|
|
1494
|
+
const depthMap = /* @__PURE__ */ new Uint16Array(113);
|
|
1495
|
+
if (this.tagId >= 0 && this.tagId < 113) depthMap[this.tagId] = 1;
|
|
1476
1496
|
let node = this.parent;
|
|
1477
1497
|
while (node) {
|
|
1478
1498
|
const tagId = node.tagId;
|
|
1479
|
-
if (tagId !== void 0 && tagId >= 0 && tagId <
|
|
1499
|
+
if (tagId !== void 0 && tagId >= 0 && tagId < 113) depthMap[tagId] = (depthMap[tagId] || 0) + 1;
|
|
1480
1500
|
node = node.parent;
|
|
1481
1501
|
}
|
|
1482
1502
|
this.cachedDepthMap = depthMap;
|
|
@@ -1594,7 +1614,7 @@ function matchesClosingTag(node, tagName, tagId, closingIsAlias) {
|
|
|
1594
1614
|
function parseHtml(html, options = {}) {
|
|
1595
1615
|
const events = [];
|
|
1596
1616
|
const state = {
|
|
1597
|
-
depthMap: /* @__PURE__ */ new Uint16Array(
|
|
1617
|
+
depthMap: /* @__PURE__ */ new Uint16Array(113),
|
|
1598
1618
|
depth: 0,
|
|
1599
1619
|
resolvedPlugins: options.resolvedPlugins || []
|
|
1600
1620
|
};
|
|
@@ -1613,7 +1633,7 @@ function parseHtmlStream(html, state, onEvent) {
|
|
|
1613
1633
|
function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
1614
1634
|
let textBuffer = "";
|
|
1615
1635
|
let runStart = 0;
|
|
1616
|
-
state.depthMap ??= /* @__PURE__ */ new Uint16Array(
|
|
1636
|
+
state.depthMap ??= /* @__PURE__ */ new Uint16Array(113);
|
|
1617
1637
|
state.depth ??= 0;
|
|
1618
1638
|
state.lastCharWasWhitespace ??= true;
|
|
1619
1639
|
state.justClosedTag ??= false;
|
|
@@ -1744,6 +1764,12 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
|
1744
1764
|
textBuffer += result.remainingText;
|
|
1745
1765
|
break;
|
|
1746
1766
|
}
|
|
1767
|
+
} else if (!isAsciiAlpha(nextCharCode) && nextCharCode !== QUESTION_CHAR) {
|
|
1768
|
+
state.textBufferContainsNonWhitespace = true;
|
|
1769
|
+
state.lastCharWasWhitespace = false;
|
|
1770
|
+
state.justClosedTag = false;
|
|
1771
|
+
textBuffer += htmlChunk[i++];
|
|
1772
|
+
continue;
|
|
1747
1773
|
} else {
|
|
1748
1774
|
let i2 = i + 1;
|
|
1749
1775
|
const tagNameStart = i2;
|
|
@@ -2028,7 +2054,7 @@ function closeNode(node, state, handleEvent) {
|
|
|
2028
2054
|
}
|
|
2029
2055
|
}
|
|
2030
2056
|
const tagId = node.tagId;
|
|
2031
|
-
if (tagId !== void 0 && tagId >= 0 && tagId <
|
|
2057
|
+
if (tagId !== void 0 && tagId >= 0 && tagId < 113) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
|
|
2032
2058
|
state.depth--;
|
|
2033
2059
|
handleEvent({
|
|
2034
2060
|
type: 1,
|
|
@@ -2070,12 +2096,30 @@ function processCommentOrDoctype(htmlChunk, position) {
|
|
|
2070
2096
|
const chunkLength = htmlChunk.length;
|
|
2071
2097
|
if (i + 3 < chunkLength && htmlChunk.charCodeAt(i + 2) === DASH_CHAR && htmlChunk.charCodeAt(i + 3) === DASH_CHAR) {
|
|
2072
2098
|
i += 4;
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2099
|
+
if (i < chunkLength && htmlChunk.charCodeAt(i) === GT_CHAR) return {
|
|
2100
|
+
complete: true,
|
|
2101
|
+
newPosition: i + 1,
|
|
2102
|
+
remainingText: ""
|
|
2103
|
+
};
|
|
2104
|
+
if (i + 1 < chunkLength && htmlChunk.charCodeAt(i) === DASH_CHAR && htmlChunk.charCodeAt(i + 1) === GT_CHAR) return {
|
|
2105
|
+
complete: true,
|
|
2106
|
+
newPosition: i + 2,
|
|
2107
|
+
remainingText: ""
|
|
2108
|
+
};
|
|
2109
|
+
while (i + 2 < chunkLength) {
|
|
2110
|
+
const dash = htmlChunk.indexOf("-", i);
|
|
2111
|
+
if (dash < 0 || dash + 2 >= chunkLength) break;
|
|
2112
|
+
i = dash;
|
|
2113
|
+
if (htmlChunk.charCodeAt(i + 1) === DASH_CHAR) {
|
|
2114
|
+
const afterDashes = htmlChunk.charCodeAt(i + 2);
|
|
2115
|
+
if (afterDashes === GT_CHAR) return {
|
|
2077
2116
|
complete: true,
|
|
2078
|
-
newPosition: i,
|
|
2117
|
+
newPosition: i + 3,
|
|
2118
|
+
remainingText: ""
|
|
2119
|
+
};
|
|
2120
|
+
if (afterDashes === EXCLAMATION_CHAR && i + 3 < chunkLength && htmlChunk.charCodeAt(i + 3) === GT_CHAR) return {
|
|
2121
|
+
complete: true,
|
|
2122
|
+
newPosition: i + 4,
|
|
2079
2123
|
remainingText: ""
|
|
2080
2124
|
};
|
|
2081
2125
|
}
|
|
@@ -2133,8 +2177,8 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
|
|
|
2133
2177
|
tagId = effectiveTagId(tagName, tagId, state);
|
|
2134
2178
|
if (state.currentNode?.tagHandler?.isNonNesting) closeNode(state.currentNode, state, handleEvent);
|
|
2135
2179
|
const tagHandler = state.tagOverrideHandlers?.get(tagName) ?? tagHandlers[tagId];
|
|
2136
|
-
const result =
|
|
2137
|
-
if (
|
|
2180
|
+
const result = scanTagAttributes(htmlChunk, i, tagHandler);
|
|
2181
|
+
if (result._tag === "incomplete") return {
|
|
2138
2182
|
complete: false,
|
|
2139
2183
|
newPosition: i,
|
|
2140
2184
|
remainingText: `<${tagName}${result.attrBuffer}`,
|
|
@@ -2159,7 +2203,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
|
|
|
2159
2203
|
const headNode = state.currentNode;
|
|
2160
2204
|
if (headNode && headNode.tagId === 1) closeNode(headNode, state, handleEvent);
|
|
2161
2205
|
}
|
|
2162
|
-
if (tagId >= 0 && tagId <
|
|
2206
|
+
if (tagId >= 0 && tagId < 113 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && parserTagDepth(state, 65) > 0) {
|
|
2163
2207
|
if (closeSelectTo(state, 65, handleEvent)) return {
|
|
2164
2208
|
complete: true,
|
|
2165
2209
|
newPosition: result.newPosition,
|
|
@@ -2224,7 +2268,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
|
|
|
2224
2268
|
skip: true
|
|
2225
2269
|
};
|
|
2226
2270
|
}
|
|
2227
|
-
if (tagId >= 0 && tagId <
|
|
2271
|
+
if (tagId >= 0 && tagId < 113) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
|
|
2228
2272
|
state.depth++;
|
|
2229
2273
|
i = result.newPosition;
|
|
2230
2274
|
if (state.currentNode) state.currentNode.currentWalkIndex = state.currentNode.currentWalkIndex || 0;
|
|
@@ -2251,137 +2295,118 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
|
|
|
2251
2295
|
selfClosing: result.selfClosing
|
|
2252
2296
|
};
|
|
2253
2297
|
}
|
|
2254
|
-
function
|
|
2255
|
-
|
|
2256
|
-
const
|
|
2257
|
-
|
|
2258
|
-
const
|
|
2259
|
-
|
|
2260
|
-
|
|
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
|
-
};
|
|
2298
|
+
function setAttribute(attributes, rawName, rawValue) {
|
|
2299
|
+
if (!rawName) return attributes;
|
|
2300
|
+
const name = rawName.toLowerCase();
|
|
2301
|
+
if (Object.hasOwn(attributes, name)) return attributes;
|
|
2302
|
+
const result = attributes === EMPTY_ATTRIBUTES ? {} : attributes;
|
|
2303
|
+
result[name] = rawValue.includes("&") ? decodeHTMLEntities(rawValue, true) : rawValue;
|
|
2304
|
+
return result;
|
|
2302
2305
|
}
|
|
2303
|
-
function
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
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;
|
|
2306
|
+
function scanAttributes(source, position, selfClosing, stopAtTagEnd) {
|
|
2307
|
+
const length = source.length;
|
|
2308
|
+
let attributes = EMPTY_ATTRIBUTES;
|
|
2309
|
+
let state = ATTRIBUTE_GAP;
|
|
2315
2310
|
let nameStart = 0;
|
|
2316
2311
|
let nameEnd = 0;
|
|
2317
2312
|
let valueStart = 0;
|
|
2318
2313
|
let quoteChar = 0;
|
|
2319
|
-
let
|
|
2320
|
-
|
|
2321
|
-
|
|
2314
|
+
let newPosition = -1;
|
|
2315
|
+
let valueEnd = length;
|
|
2316
|
+
let i = position;
|
|
2317
|
+
while (i < length) {
|
|
2318
|
+
const charCode = source.charCodeAt(i);
|
|
2319
|
+
if (stopAtTagEnd && state !== ATTRIBUTE_QUOTED_VALUE) {
|
|
2320
|
+
if (state !== ATTRIBUTE_UNQUOTED_VALUE && charCode === SLASH_CHAR && i + 1 < length && source.charCodeAt(i + 1) === GT_CHAR) {
|
|
2321
|
+
selfClosing = true;
|
|
2322
|
+
valueEnd = i;
|
|
2323
|
+
newPosition = i + 2;
|
|
2324
|
+
break;
|
|
2325
|
+
}
|
|
2326
|
+
if (charCode === GT_CHAR) {
|
|
2327
|
+
valueEnd = i;
|
|
2328
|
+
newPosition = i + 1;
|
|
2329
|
+
break;
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2322
2332
|
const isSpace = isWhitespace(charCode);
|
|
2323
2333
|
switch (state) {
|
|
2324
|
-
case
|
|
2334
|
+
case ATTRIBUTE_GAP:
|
|
2325
2335
|
if (!isSpace) {
|
|
2326
|
-
state =
|
|
2336
|
+
state = ATTRIBUTE_NAME;
|
|
2327
2337
|
nameStart = i;
|
|
2328
|
-
nameEnd = 0;
|
|
2329
2338
|
}
|
|
2330
2339
|
break;
|
|
2331
|
-
case
|
|
2340
|
+
case ATTRIBUTE_NAME:
|
|
2332
2341
|
if (charCode === EQUALS_CHAR || isSpace) {
|
|
2333
2342
|
nameEnd = i;
|
|
2334
|
-
|
|
2335
|
-
state = charCode === EQUALS_CHAR ? BEFORE_VALUE : AFTER_NAME;
|
|
2343
|
+
state = charCode === EQUALS_CHAR ? ATTRIBUTE_BEFORE_VALUE : ATTRIBUTE_AFTER_NAME;
|
|
2336
2344
|
}
|
|
2337
2345
|
break;
|
|
2338
|
-
case
|
|
2339
|
-
if (charCode === EQUALS_CHAR) state =
|
|
2346
|
+
case ATTRIBUTE_AFTER_NAME:
|
|
2347
|
+
if (charCode === EQUALS_CHAR) state = ATTRIBUTE_BEFORE_VALUE;
|
|
2340
2348
|
else if (!isSpace) {
|
|
2341
|
-
|
|
2342
|
-
state =
|
|
2349
|
+
attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), "");
|
|
2350
|
+
state = ATTRIBUTE_NAME;
|
|
2343
2351
|
nameStart = i;
|
|
2344
|
-
nameEnd = 0;
|
|
2345
2352
|
}
|
|
2346
2353
|
break;
|
|
2347
|
-
case
|
|
2354
|
+
case ATTRIBUTE_BEFORE_VALUE:
|
|
2348
2355
|
if (charCode === QUOTE_CHAR || charCode === APOS_CHAR) {
|
|
2349
2356
|
quoteChar = charCode;
|
|
2350
|
-
state =
|
|
2357
|
+
state = ATTRIBUTE_QUOTED_VALUE;
|
|
2351
2358
|
valueStart = i + 1;
|
|
2352
2359
|
} else if (!isSpace) {
|
|
2353
|
-
state =
|
|
2360
|
+
state = ATTRIBUTE_UNQUOTED_VALUE;
|
|
2354
2361
|
valueStart = i;
|
|
2355
2362
|
}
|
|
2356
2363
|
break;
|
|
2357
|
-
case
|
|
2358
|
-
if (charCode ===
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
|
|
2362
|
-
state = WHITESPACE;
|
|
2364
|
+
case ATTRIBUTE_QUOTED_VALUE:
|
|
2365
|
+
if (charCode === quoteChar) {
|
|
2366
|
+
attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, i));
|
|
2367
|
+
state = ATTRIBUTE_GAP;
|
|
2363
2368
|
}
|
|
2364
2369
|
break;
|
|
2365
|
-
case
|
|
2366
|
-
if (isSpace || charCode === GT_CHAR) {
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
state = WHITESPACE;
|
|
2370
|
+
case ATTRIBUTE_UNQUOTED_VALUE:
|
|
2371
|
+
if (isSpace || !stopAtTagEnd && charCode === GT_CHAR) {
|
|
2372
|
+
attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, i));
|
|
2373
|
+
state = ATTRIBUTE_GAP;
|
|
2370
2374
|
}
|
|
2371
2375
|
break;
|
|
2372
2376
|
}
|
|
2373
2377
|
i++;
|
|
2374
2378
|
}
|
|
2375
|
-
if (
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2379
|
+
if (stopAtTagEnd && newPosition === -1) return {
|
|
2380
|
+
_tag: "incomplete",
|
|
2381
|
+
attrBuffer: source.substring(position)
|
|
2382
|
+
};
|
|
2383
|
+
switch (state) {
|
|
2384
|
+
case ATTRIBUTE_NAME:
|
|
2385
|
+
attributes = setAttribute(attributes, source.substring(nameStart, valueEnd), "");
|
|
2386
|
+
break;
|
|
2387
|
+
case ATTRIBUTE_AFTER_NAME:
|
|
2388
|
+
case ATTRIBUTE_BEFORE_VALUE:
|
|
2389
|
+
attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), "");
|
|
2390
|
+
break;
|
|
2391
|
+
case ATTRIBUTE_QUOTED_VALUE:
|
|
2392
|
+
case ATTRIBUTE_UNQUOTED_VALUE:
|
|
2393
|
+
attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, valueEnd));
|
|
2394
|
+
break;
|
|
2384
2395
|
}
|
|
2385
|
-
return
|
|
2396
|
+
return {
|
|
2397
|
+
_tag: "complete",
|
|
2398
|
+
newPosition: stopAtTagEnd ? newPosition : length,
|
|
2399
|
+
attributes,
|
|
2400
|
+
selfClosing
|
|
2401
|
+
};
|
|
2402
|
+
}
|
|
2403
|
+
function scanTagAttributes(htmlChunk, position, tagHandler) {
|
|
2404
|
+
return scanAttributes(htmlChunk, position, tagHandler?.isSelfClosing || false, true);
|
|
2405
|
+
}
|
|
2406
|
+
function parseAttributes(attrStr) {
|
|
2407
|
+
if (!attrStr) return EMPTY_ATTRIBUTES;
|
|
2408
|
+
const result = scanAttributes(attrStr, 0, false, false);
|
|
2409
|
+
if (result._tag === "incomplete") throw new Error("Isolated attribute scans always complete at EOF");
|
|
2410
|
+
return result.attributes === EMPTY_ATTRIBUTES ? {} : result.attributes;
|
|
2386
2411
|
}
|
|
2387
2412
|
export { breakHandler, buildTagOverrideHandlers, continuationPrefix, finalizeParse, isEmptyLinkHref, parseAttributes, parseHtml, parseHtmlStream, renderBreak, resolveUrl };
|
package/dist/parse.d.mts
CHANGED
|
@@ -5,12 +5,12 @@ import { ElementNode, Node, NodeEvent, TagHandler, TransformPlugin } from "./_ch
|
|
|
5
5
|
* pending (a later chunk might continue them); at true EOF they must be
|
|
6
6
|
* committed so trailing content is not dropped (e.g. `<p>a<p>b`).
|
|
7
7
|
*
|
|
8
|
-
* `leftover` is the residual returned by the final `parseHtmlStream`.
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* scanned persist on `state`, so `processTextBuffer` commits
|
|
13
|
-
* tag had triggered the flush.
|
|
8
|
+
* `leftover` is the residual returned by the final `parseHtmlStream`. Trailing
|
|
9
|
+
* text is emitted; a residual that is an incomplete start tag is dropped,
|
|
10
|
+
* matching the browser tokenizer's EOF-in-tag behaviour. A leading `<` alone
|
|
11
|
+
* does not make it a tag: `<3` is text. The text-buffer flags set while the
|
|
12
|
+
* trailing text was scanned persist on `state`, so `processTextBuffer` commits
|
|
13
|
+
* it as if the next tag had triggered the flush.
|
|
14
14
|
*/
|
|
15
15
|
declare function finalizeParse(leftover: string, state: ParseState, handleEvent: (event: NodeEvent) => void): void;
|
|
16
16
|
interface ParseOptions {
|
package/package.json
CHANGED