@markuplint/parser-utils 4.0.0-dev.0 → 4.0.0-dev.12

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.
Files changed (47) hide show
  1. package/LICENSE +1 -1
  2. package/lib/attr-tokenizer.d.ts +16 -4
  3. package/lib/attr-tokenizer.js +164 -70
  4. package/lib/const.d.ts +2 -1
  5. package/lib/const.js +2 -1
  6. package/lib/debugger.d.ts +3 -2
  7. package/lib/debugger.js +35 -19
  8. package/lib/enums.d.ts +16 -0
  9. package/lib/enums.js +18 -0
  10. package/lib/get-location.d.ts +4 -13
  11. package/lib/get-location.js +10 -21
  12. package/lib/ignore-block.d.ts +3 -2
  13. package/lib/ignore-block.js +62 -124
  14. package/lib/ignore-front-matter.d.ts +4 -1
  15. package/lib/ignore-front-matter.js +12 -3
  16. package/lib/index.d.ts +3 -16
  17. package/lib/index.js +3 -16
  18. package/lib/parser-error.d.ts +1 -0
  19. package/lib/parser-error.js +1 -0
  20. package/lib/parser.d.ts +108 -0
  21. package/lib/parser.js +1076 -0
  22. package/lib/script-parser.d.ts +1 -1
  23. package/lib/script-parser.js +1 -1
  24. package/lib/sort-nodes.d.ts +2 -0
  25. package/lib/sort-nodes.js +18 -0
  26. package/lib/types.d.ts +34 -0
  27. package/package.json +9 -5
  28. package/lib/attr-parser.d.ts +0 -25
  29. package/lib/attr-parser.js +0 -188
  30. package/lib/create-token.d.ts +0 -4
  31. package/lib/create-token.js +0 -29
  32. package/lib/flatten-nodes.d.ts +0 -2
  33. package/lib/flatten-nodes.js +0 -247
  34. package/lib/get-space-before.d.ts +0 -1
  35. package/lib/get-space-before.js +0 -8
  36. package/lib/parse-attr.d.ts +0 -24
  37. package/lib/parse-attr.js +0 -144
  38. package/lib/remove-deprecated-node.d.ts +0 -7
  39. package/lib/remove-deprecated-node.js +0 -39
  40. package/lib/siblings-correction.d.ts +0 -9
  41. package/lib/siblings-correction.js +0 -21
  42. package/lib/tag-parser.d.ts +0 -10
  43. package/lib/tag-parser.js +0 -152
  44. package/lib/tag-splitter.d.ts +0 -7
  45. package/lib/tag-splitter.js +0 -96
  46. package/lib/walker.d.ts +0 -2
  47. package/lib/walker.js +0 -18
package/lib/parser.js ADDED
@@ -0,0 +1,1076 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
+ if (kind === "m") throw new TypeError("Private method is not writable");
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
+ };
12
+ var _Parser_instances, _Parser_booleanish, _Parser_defaultState, _Parser_endTagType, _Parser_ignoreTags, _Parser_maskChar, _Parser_tagNameCaseSensitive, _Parser_selfCloseType, _Parser_spaceChars, _Parser_rawTextElements, _Parser_authoredElementName, _Parser_originalRawCode, _Parser_rawCode, _Parser_defaultDepth, _Parser_walkMethodSequentailPrevNode, _Parser_arrayize, _Parser_concatText, _Parser_concatTextNodes, _Parser_convertIntoInvalidNode, _Parser_createOffsetSpaces, _Parser_createRemnantNode, _Parser_exposeRemnantNodes, _Parser_getEndLocation, _Parser_orphanEndTagToBogusMark, _Parser_pairing, _Parser_parseEndTag, _Parser_parseStartTag, _Parser_parseTag, _Parser_removeChild, _Parser_removeDeprecatedNode, _Parser_removeOffsetSpaces, _Parser_reset, _Parser_setRawCode, _Parser_siblingsCorrection;
13
+ import { isVoidElement as detectVoidElement } from '@markuplint/ml-spec';
14
+ import { v4 as uuid } from 'uuid';
15
+ import { attrTokenizer } from './attr-tokenizer.js';
16
+ import { defaultSpaces } from './const.js';
17
+ import { detectElementType } from './detect-element-type.js';
18
+ import { AttrState, TagState } from './enums.js';
19
+ import { getCol, getEndCol, getEndLine, getLine } from './get-location.js';
20
+ import { ignoreBlock, restoreNode } from './ignore-block.js';
21
+ import { ignoreFrontMatter } from './ignore-front-matter.js';
22
+ import { ParserError } from './parser-error.js';
23
+ import { sortNodes } from './sort-nodes.js';
24
+ export class Parser {
25
+ get authoredElementName() {
26
+ return __classPrivateFieldGet(this, _Parser_authoredElementName, "f");
27
+ }
28
+ /**
29
+ * Detect value as a true if its attribute is booleanish value and omitted.
30
+ *
31
+ * Ex:
32
+ * ```jsx
33
+ * <Component aria-hidden />
34
+ * ```
35
+ *
36
+ * In the above, the `aria-hidden` is `true`.
37
+ */
38
+ get booleanish() {
39
+ return __classPrivateFieldGet(this, _Parser_booleanish, "f");
40
+ }
41
+ /**
42
+ * The end tag omittable type.
43
+ *
44
+ * - `"xml"`: Must need an end tag or must self-close
45
+ * - `"omittable"`: May omit
46
+ * - `"never"`: Never need
47
+ */
48
+ get endTag() {
49
+ return __classPrivateFieldGet(this, _Parser_endTagType, "f");
50
+ }
51
+ get rawCode() {
52
+ return __classPrivateFieldGet(this, _Parser_rawCode, "f");
53
+ }
54
+ get tagNameCaseSensitive() {
55
+ return __classPrivateFieldGet(this, _Parser_tagNameCaseSensitive, "f");
56
+ }
57
+ constructor(options, defaultState) {
58
+ _Parser_instances.add(this);
59
+ _Parser_booleanish.set(this, false);
60
+ _Parser_defaultState.set(this, void 0);
61
+ _Parser_endTagType.set(this, 'omittable');
62
+ _Parser_ignoreTags.set(this, []);
63
+ _Parser_maskChar.set(this, void 0);
64
+ _Parser_tagNameCaseSensitive.set(this, false);
65
+ _Parser_selfCloseType.set(this, 'html');
66
+ _Parser_spaceChars.set(this, defaultSpaces);
67
+ _Parser_rawTextElements.set(this, ['style', 'script']);
68
+ _Parser_authoredElementName.set(this, void 0);
69
+ _Parser_originalRawCode.set(this, '');
70
+ _Parser_rawCode.set(this, '');
71
+ _Parser_defaultDepth.set(this, 0);
72
+ _Parser_walkMethodSequentailPrevNode.set(this, null);
73
+ __classPrivateFieldSet(this, _Parser_booleanish, options?.booleanish ?? __classPrivateFieldGet(this, _Parser_booleanish, "f"), "f");
74
+ __classPrivateFieldSet(this, _Parser_endTagType, options?.endTagType ?? __classPrivateFieldGet(this, _Parser_endTagType, "f"), "f");
75
+ __classPrivateFieldSet(this, _Parser_ignoreTags, options?.ignoreTags ?? __classPrivateFieldGet(this, _Parser_ignoreTags, "f"), "f");
76
+ __classPrivateFieldSet(this, _Parser_maskChar, options?.maskChar ?? __classPrivateFieldGet(this, _Parser_maskChar, "f"), "f");
77
+ __classPrivateFieldSet(this, _Parser_tagNameCaseSensitive, options?.tagNameCaseSensitive ?? __classPrivateFieldGet(this, _Parser_tagNameCaseSensitive, "f"), "f");
78
+ __classPrivateFieldSet(this, _Parser_selfCloseType, options?.selfCloseType ?? __classPrivateFieldGet(this, _Parser_selfCloseType, "f"), "f");
79
+ __classPrivateFieldSet(this, _Parser_spaceChars, options?.spaceChars ?? __classPrivateFieldGet(this, _Parser_spaceChars, "f"), "f");
80
+ __classPrivateFieldSet(this, _Parser_rawTextElements, options?.rawTextElements ?? __classPrivateFieldGet(this, _Parser_rawTextElements, "f"), "f");
81
+ __classPrivateFieldSet(this, _Parser_defaultState, defaultState ?? null, "f");
82
+ this.state = structuredClone(__classPrivateFieldGet(this, _Parser_defaultState, "f"));
83
+ }
84
+ tokenize() {
85
+ return {
86
+ ast: [],
87
+ isFragment: false,
88
+ };
89
+ }
90
+ beforeParse(rawCode, options) {
91
+ const spaces = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_createOffsetSpaces).call(this, options);
92
+ return spaces + rawCode;
93
+ }
94
+ parse(rawCode, options) {
95
+ try {
96
+ // Initialize raw code
97
+ __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_setRawCode).call(this, rawCode, rawCode);
98
+ // Override raw code
99
+ __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_setRawCode).call(this, this.beforeParse(this.rawCode, options));
100
+ __classPrivateFieldSet(this, _Parser_authoredElementName, options?.authoredElementName, "f");
101
+ let frontMatter = null;
102
+ if (options?.ignoreFrontMatter) {
103
+ const fm = ignoreFrontMatter(this.rawCode);
104
+ __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_setRawCode).call(this, fm.code);
105
+ frontMatter = fm.frontMatter;
106
+ }
107
+ const blocks = ignoreBlock(this.rawCode, __classPrivateFieldGet(this, _Parser_ignoreTags, "f"), __classPrivateFieldGet(this, _Parser_maskChar, "f"));
108
+ __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_setRawCode).call(this, blocks.replaced);
109
+ const tokenized = this.tokenize();
110
+ const ast = tokenized.ast;
111
+ const isFragment = tokenized.isFragment;
112
+ __classPrivateFieldSet(this, _Parser_defaultDepth, options?.depth ?? __classPrivateFieldGet(this, _Parser_defaultDepth, "f"), "f");
113
+ const traversed = this.traverse(ast, null, __classPrivateFieldGet(this, _Parser_defaultDepth, "f"));
114
+ const nodeTree = this.afterTraverse([...traversed.childNodes, ...traversed.siblings]);
115
+ let nodeList = this.flattenNodes(nodeTree);
116
+ nodeList = this.afterFlattenNodes(nodeList);
117
+ nodeList = restoreNode(this, nodeList, blocks, false);
118
+ nodeList = this.afterParse(nodeList, options);
119
+ if (frontMatter) {
120
+ const newNodeList = [...nodeList];
121
+ let firstText = '';
122
+ const firstTextNode = newNodeList.shift();
123
+ if (firstTextNode && firstTextNode.type === 'text') {
124
+ firstText = firstTextNode.raw;
125
+ }
126
+ else if (firstTextNode) {
127
+ newNodeList.unshift(firstTextNode);
128
+ }
129
+ const raw = frontMatter + firstText.slice(frontMatter.length);
130
+ const token = this.sliceFragment(0, raw.length);
131
+ const fmNode = this.visitPsBlock({
132
+ ...token,
133
+ depth: 0,
134
+ parentNode: null,
135
+ raw,
136
+ nodeName: 'front-matter',
137
+ })[0];
138
+ if (!fmNode) {
139
+ throw new ParserError('Unexpected front matter', firstTextNode ?? token);
140
+ }
141
+ nodeList = [fmNode, ...newNodeList];
142
+ }
143
+ __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_reset).call(this);
144
+ return {
145
+ raw: rawCode,
146
+ nodeList,
147
+ isFragment,
148
+ };
149
+ }
150
+ catch (error) {
151
+ throw this.parseError(error);
152
+ }
153
+ }
154
+ afterParse(nodeList, options) {
155
+ return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_removeOffsetSpaces).call(this, nodeList, options);
156
+ }
157
+ parseError(error) {
158
+ return new ParserError(error, {
159
+ line: error.line ?? error.lineNumber ?? 0,
160
+ col: error.col ?? error.column ?? 0,
161
+ raw: error.raw ?? this.rawCode,
162
+ stack: error.stack,
163
+ });
164
+ }
165
+ traverse(originNodes, parentNode = null, depth) {
166
+ if (originNodes.length === 0) {
167
+ return {
168
+ childNodes: [],
169
+ siblings: [],
170
+ };
171
+ }
172
+ const childNodes = [];
173
+ const siblings = [];
174
+ for (const originNode of originNodes) {
175
+ const nodes = this.nodeize(originNode, parentNode, depth);
176
+ const after = this.afterNodeize(nodes, parentNode, depth);
177
+ childNodes.push(...after.siblings);
178
+ siblings.push(...after.ancestors);
179
+ }
180
+ return {
181
+ childNodes,
182
+ siblings,
183
+ };
184
+ }
185
+ afterTraverse(nodeTree) {
186
+ return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_siblingsCorrection).call(this, nodeTree);
187
+ }
188
+ nodeize(originNode, parentNode, depth) {
189
+ return [];
190
+ }
191
+ afterNodeize(siblings, parentNode, depth) {
192
+ const newSiblings = [];
193
+ const ancestors = [];
194
+ for (const sibling of siblings) {
195
+ if (sibling.type === 'doctype') {
196
+ if (sibling.depth === 0) {
197
+ ancestors.push(sibling);
198
+ continue;
199
+ }
200
+ throw new ParserError('Unexpected doctype', sibling);
201
+ }
202
+ if (sibling.depth === depth) {
203
+ newSiblings.push(sibling);
204
+ continue;
205
+ }
206
+ if (sibling.depth < depth) {
207
+ ancestors.push(sibling);
208
+ continue;
209
+ }
210
+ }
211
+ return {
212
+ siblings: newSiblings,
213
+ ancestors,
214
+ };
215
+ }
216
+ flattenNodes(nodeTree) {
217
+ return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_arrayize).call(this, nodeTree);
218
+ }
219
+ afterFlattenNodes(nodeList, options) {
220
+ const exposeInvalidNode = options?.exposeInvalidNode ?? true;
221
+ const exposeWhiteSpace = options?.exposeWhiteSpace ?? true;
222
+ const concatText = options?.concatText ?? true;
223
+ nodeList = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_exposeRemnantNodes).call(this, nodeList, exposeInvalidNode, exposeWhiteSpace);
224
+ nodeList = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_orphanEndTagToBogusMark).call(this, nodeList);
225
+ if (concatText) {
226
+ nodeList = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_concatText).call(this, nodeList);
227
+ }
228
+ return nodeList;
229
+ }
230
+ visitDoctype(token) {
231
+ const node = {
232
+ ...token,
233
+ ...this.createToken(token),
234
+ type: 'doctype',
235
+ nodeName: '#doctype',
236
+ };
237
+ return [node];
238
+ }
239
+ visitComment(token, options) {
240
+ const isBogus = options?.isBogus ?? !token.raw.startsWith('<!--');
241
+ const node = {
242
+ ...token,
243
+ ...this.createToken(token),
244
+ type: 'comment',
245
+ nodeName: '#comment',
246
+ isBogus,
247
+ };
248
+ return [node];
249
+ }
250
+ visitText(token, options) {
251
+ const node = {
252
+ ...token,
253
+ ...this.createToken(token),
254
+ type: 'text',
255
+ nodeName: '#text',
256
+ };
257
+ if (options?.researchTags) {
258
+ const nodes = this.parseCodeFragment(token);
259
+ const includedStartTags = nodes.some(node => node.type === 'starttag');
260
+ // Handle invalid tags as text nodes
261
+ if (options.invalidTagAsText && includedStartTags) {
262
+ return [node];
263
+ }
264
+ return nodes;
265
+ }
266
+ return [node];
267
+ }
268
+ visitElement(token, childNodes = [], options) {
269
+ const createEndTagToken = options?.createEndTagToken;
270
+ const namelessFragment = options?.namelessFragment ?? false;
271
+ const overwriteProps = options?.overwriteProps;
272
+ // Handle omitted empty tag as ghost element node
273
+ if (token.raw === '') {
274
+ const startTag = {
275
+ ...token,
276
+ ...this.createToken(token),
277
+ type: 'starttag',
278
+ elementType: 'html',
279
+ attributes: [],
280
+ childNodes: [],
281
+ parentNode: token.parentNode,
282
+ pairNode: null,
283
+ tagCloseChar: '',
284
+ tagOpenChar: '',
285
+ isGhost: true,
286
+ ...overwriteProps,
287
+ };
288
+ const siblings = this.visitChildren(childNodes, startTag);
289
+ return [startTag, ...siblings];
290
+ }
291
+ const startTag = {
292
+ ...__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseStartTag).call(this, token, {
293
+ namespace: token.namespace,
294
+ ...overwriteProps,
295
+ }, namelessFragment),
296
+ };
297
+ const siblings = this.visitChildren(childNodes, startTag);
298
+ if (createEndTagToken) {
299
+ const endTagToken = createEndTagToken(startTag);
300
+ if (endTagToken) {
301
+ const endTag = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseEndTag).call(this, endTagToken, namelessFragment);
302
+ __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_pairing).call(this, startTag, endTag);
303
+ return [startTag, endTag, ...siblings];
304
+ }
305
+ }
306
+ return [startTag, ...siblings];
307
+ }
308
+ visitPsBlock(token, childNodes = [], originBlockNode) {
309
+ const block = {
310
+ ...token,
311
+ ...this.createToken(token),
312
+ type: 'psblock',
313
+ nodeName: `#ps:${token.nodeName}`,
314
+ childNodes: [],
315
+ isBogus: false,
316
+ };
317
+ const siblings = this.visitChildren(childNodes, block);
318
+ return [block, ...siblings];
319
+ }
320
+ visitChildren(children, parentNode) {
321
+ if (children.length === 0) {
322
+ return [];
323
+ }
324
+ if (parentNode && __classPrivateFieldGet(this, _Parser_rawTextElements, "f").includes(parentNode.nodeName.toLowerCase())) {
325
+ return [];
326
+ }
327
+ const traversed = this.traverse(children, parentNode, parentNode ? parentNode.depth + 1 : 0);
328
+ this.appendChild(parentNode, ...traversed.childNodes);
329
+ return traversed.siblings;
330
+ }
331
+ visitSpreadAttr(token) {
332
+ const raw = token.raw.trim();
333
+ if (raw === '') {
334
+ return null;
335
+ }
336
+ // eslint-disable-next-line regexp/strict
337
+ if (!/^{\s*\.{3}[^.]/.test(raw)) {
338
+ return null;
339
+ }
340
+ if (!raw.endsWith('}')) {
341
+ return null;
342
+ }
343
+ const node = this.createToken(raw, token.startOffset, token.startLine, token.startCol);
344
+ return {
345
+ ...node,
346
+ ...__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_getEndLocation).call(this, node),
347
+ type: 'spread',
348
+ nodeName: '#spread',
349
+ };
350
+ }
351
+ visitAttr(token, options) {
352
+ const raw = token.raw;
353
+ const quoteSet = options?.quoteSet;
354
+ const startState = options?.startState ?? AttrState.BeforeName;
355
+ const quoteInValueChars = options?.quoteInValueChars;
356
+ const endOfUnquotedValueChars = options?.endOfUnquotedValueChars;
357
+ let startOffset = token.startOffset;
358
+ let startLine = token.startLine;
359
+ let startCol = token.startCol;
360
+ let tokens;
361
+ try {
362
+ tokens = attrTokenizer(raw, quoteSet, startState, quoteInValueChars, endOfUnquotedValueChars);
363
+ }
364
+ catch (error) {
365
+ if (error instanceof SyntaxError) {
366
+ throw new ParserError(error.message, token);
367
+ }
368
+ throw error;
369
+ }
370
+ const spacesBeforeName = this.createToken(tokens.spacesBeforeAttrName, startOffset, startLine, startCol);
371
+ startLine = spacesBeforeName.endLine;
372
+ startCol = spacesBeforeName.endCol;
373
+ startOffset = spacesBeforeName.endOffset;
374
+ const name = this.createToken(tokens.attrName, startOffset, startLine, startCol);
375
+ startLine = name.endLine;
376
+ startCol = name.endCol;
377
+ startOffset = name.endOffset;
378
+ const spacesBeforeEqual = this.createToken(tokens.spacesBeforeEqual, startOffset, startLine, startCol);
379
+ startLine = spacesBeforeEqual.endLine;
380
+ startCol = spacesBeforeEqual.endCol;
381
+ startOffset = spacesBeforeEqual.endOffset;
382
+ const equal = this.createToken(tokens.equal, startOffset, startLine, startCol);
383
+ startLine = equal.endLine;
384
+ startCol = equal.endCol;
385
+ startOffset = equal.endOffset;
386
+ const spacesAfterEqual = this.createToken(tokens.spacesAfterEqual, startOffset, startLine, startCol);
387
+ startLine = spacesAfterEqual.endLine;
388
+ startCol = spacesAfterEqual.endCol;
389
+ startOffset = spacesAfterEqual.endOffset;
390
+ const startQuote = this.createToken(tokens.quoteStart, startOffset, startLine, startCol);
391
+ startLine = startQuote.endLine;
392
+ startCol = startQuote.endCol;
393
+ startOffset = startQuote.endOffset;
394
+ const value = this.createToken(tokens.attrValue, startOffset, startLine, startCol);
395
+ startLine = value.endLine;
396
+ startCol = value.endCol;
397
+ startOffset = value.endOffset;
398
+ const endQuote = this.createToken(tokens.quoteEnd, startOffset, startLine, startCol);
399
+ const attrToken = this.createToken(tokens.attrName +
400
+ tokens.spacesBeforeEqual +
401
+ tokens.equal +
402
+ tokens.spacesAfterEqual +
403
+ tokens.quoteStart +
404
+ tokens.attrValue +
405
+ tokens.quoteEnd, name.startOffset, name.startLine, name.startCol);
406
+ const htmlAttr = {
407
+ ...attrToken,
408
+ type: 'attr',
409
+ nodeName: name.raw,
410
+ spacesBeforeName,
411
+ name,
412
+ spacesBeforeEqual,
413
+ equal,
414
+ spacesAfterEqual,
415
+ startQuote,
416
+ value,
417
+ endQuote,
418
+ isDuplicatable: false,
419
+ };
420
+ const spread = this.visitSpreadAttr(attrToken);
421
+ if (tokens.leftover) {
422
+ return {
423
+ ...(spread ?? htmlAttr),
424
+ __rightText: tokens.leftover,
425
+ };
426
+ }
427
+ return spread ?? htmlAttr;
428
+ }
429
+ parseCodeFragment(token, options) {
430
+ const nodes = [];
431
+ let raw = token.raw;
432
+ let startOffset = token.startOffset;
433
+ let startLine = token.startLine;
434
+ let startCol = token.startCol;
435
+ let depth = token.depth;
436
+ const depthStack = new Map();
437
+ while (raw) {
438
+ const parsed = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseTag).call(this, {
439
+ raw,
440
+ startOffset,
441
+ startLine,
442
+ startCol,
443
+ depth,
444
+ parentNode: null,
445
+ }, true, true, options?.namelessFragment ?? false);
446
+ if (parsed.__left) {
447
+ const token = this.createToken(parsed.__left, startOffset, startLine, startCol);
448
+ const textNode = {
449
+ ...token,
450
+ type: 'text',
451
+ depth,
452
+ nodeName: '#text',
453
+ parentNode: null,
454
+ };
455
+ nodes.push(textNode);
456
+ }
457
+ raw = parsed.__right ?? '';
458
+ if (!parsed.token) {
459
+ continue;
460
+ }
461
+ const tag = parsed.token;
462
+ startLine = tag.endLine;
463
+ startCol = tag.endCol;
464
+ startOffset = tag.endOffset;
465
+ let isSelfClose = tag.type === 'starttag' && tag.selfClosingSolidus?.raw === '/';
466
+ const isVoidElement = detectVoidElement({ localName: tag.nodeName.toLowerCase() });
467
+ switch (__classPrivateFieldGet(this, _Parser_selfCloseType, "f")) {
468
+ case 'html': {
469
+ isSelfClose = isVoidElement;
470
+ break;
471
+ }
472
+ case 'html+xml': {
473
+ isSelfClose = isSelfClose || isVoidElement;
474
+ break;
475
+ }
476
+ case 'xml': {
477
+ // eslint-disable-next-line no-self-assign
478
+ isSelfClose = isSelfClose;
479
+ break;
480
+ }
481
+ }
482
+ if (tag.type === 'starttag' && !isSelfClose) {
483
+ depthStack.set(tag.nodeName, depth);
484
+ }
485
+ if (tag.raw) {
486
+ if (tag.type === 'endtag') {
487
+ if (depthStack.has(tag.nodeName)) {
488
+ depth = depthStack.get(tag.nodeName);
489
+ depthStack.delete(tag.nodeName);
490
+ }
491
+ else {
492
+ depth = Math.max(depth - 1, __classPrivateFieldGet(this, _Parser_defaultDepth, "f"));
493
+ }
494
+ this.updateLocation(tag, {
495
+ depth,
496
+ });
497
+ }
498
+ else if (!isSelfClose) {
499
+ depth += 1;
500
+ }
501
+ /**
502
+ * The DOM parser sometimes includes HTML closing tags as a text node child of the body element.
503
+ * In that case, it is ignored.
504
+ */
505
+ if (token.parentNode?.nodeName === 'body' && tag.nodeName === 'html' && tag.type === 'endtag') {
506
+ continue;
507
+ }
508
+ nodes.push(tag);
509
+ }
510
+ }
511
+ return nodes;
512
+ }
513
+ updateLocation(node, props) {
514
+ Object.assign(node, {
515
+ startOffset: props.startOffset ?? node.startOffset,
516
+ startLine: props.startLine ?? node.startLine,
517
+ startCol: props.startCol ?? node.startCol,
518
+ endOffset: props.startOffset == null ? node.endOffset : props.startOffset + node.raw.length,
519
+ endLine: props.startLine == null ? node.endLine : getEndLine(node.raw, props.startLine),
520
+ endCol: props.startCol == null ? node.endCol : getEndCol(node.raw, props.startCol),
521
+ depth: props.depth ?? node.depth,
522
+ });
523
+ }
524
+ /**
525
+ * Set new raw code to target node.
526
+ *
527
+ * Replace the raw code and update the start/end offset/line/column.
528
+ *
529
+ * @param node target node
530
+ * @param raw new raw code
531
+ */
532
+ updateRaw(node, raw) {
533
+ const startOffset = node.startOffset;
534
+ const startLine = node.startLine;
535
+ const startCol = node.startCol;
536
+ const endOffset = startOffset + raw.length;
537
+ const endLine = getEndLine(raw, startLine);
538
+ const endCol = getEndCol(raw, startCol);
539
+ Object.assign(node, {
540
+ raw,
541
+ startOffset,
542
+ endOffset,
543
+ startLine,
544
+ endLine,
545
+ startCol,
546
+ endCol,
547
+ });
548
+ }
549
+ updateElement(el, props) {
550
+ Object.assign(el, props);
551
+ }
552
+ updateAttr(attr, props) {
553
+ Object.assign(attr, props);
554
+ }
555
+ detectElementType(nodeName, defaultPattern) {
556
+ return detectElementType(nodeName, __classPrivateFieldGet(this, _Parser_authoredElementName, "f"), defaultPattern);
557
+ }
558
+ createToken(token, startOffset, startLine, startCol) {
559
+ const props = typeof token === 'string'
560
+ ? {
561
+ raw: token,
562
+ startOffset: startOffset ?? 0,
563
+ startLine: startLine ?? 1,
564
+ startCol: startCol ?? 1,
565
+ }
566
+ : token;
567
+ return {
568
+ uuid: uuid().slice(0, 8),
569
+ ...props,
570
+ ...__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_getEndLocation).call(this, props),
571
+ };
572
+ }
573
+ sliceFragment(start, end) {
574
+ const raw = this.rawCode.slice(start, end);
575
+ return {
576
+ raw,
577
+ startOffset: start,
578
+ startLine: getLine(this.rawCode, start),
579
+ startCol: getCol(this.rawCode, start),
580
+ };
581
+ }
582
+ walk(nodeList, walker, depth = 0) {
583
+ for (const node of nodeList) {
584
+ walker(node, __classPrivateFieldGet(this, _Parser_walkMethodSequentailPrevNode, "f"), depth);
585
+ __classPrivateFieldSet(this, _Parser_walkMethodSequentailPrevNode, node, "f");
586
+ if ('childNodes' in node && node.childNodes.length > 0) {
587
+ this.walk(node.childNodes, walker, depth + 1);
588
+ }
589
+ }
590
+ if (depth === 0) {
591
+ __classPrivateFieldSet(this, _Parser_walkMethodSequentailPrevNode, null, "f");
592
+ }
593
+ }
594
+ appendChild(parentNode, ...childNodes) {
595
+ if (!parentNode || childNodes.length === 0) {
596
+ return;
597
+ }
598
+ const newChildNodes = [...parentNode.childNodes];
599
+ for (const appendingChild of childNodes) {
600
+ const currentIndex = parentNode.childNodes.findIndex(n => n.uuid === appendingChild.uuid);
601
+ Object.assign(appendingChild, { parentNode });
602
+ if (currentIndex === -1) {
603
+ newChildNodes.push(appendingChild);
604
+ continue;
605
+ }
606
+ newChildNodes.splice(currentIndex, 1, appendingChild);
607
+ }
608
+ Object.assign(parentNode, {
609
+ childNodes: __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_siblingsCorrection).call(this, newChildNodes),
610
+ });
611
+ }
612
+ replaceChild(parentNode, oldChildNode, newChildNode) {
613
+ const index = parentNode.childNodes.findIndex(childNode => childNode.uuid === oldChildNode.uuid);
614
+ if (index === -1) {
615
+ return;
616
+ }
617
+ if (Array.prototype.toSpliced == null) {
618
+ const newChildNodes = [...parentNode.childNodes];
619
+ // TODO: Use splice instead of toSpliced until we end support for Node 18
620
+ newChildNodes.splice(index, 1, newChildNode);
621
+ Object.assign(parentNode, { childNodes: newChildNodes });
622
+ return;
623
+ }
624
+ const newChildNodes = parentNode.childNodes.toSpliced(index, 1, newChildNode);
625
+ Object.assign(parentNode, { childNodes: newChildNodes });
626
+ }
627
+ }
628
+ _Parser_booleanish = new WeakMap(), _Parser_defaultState = new WeakMap(), _Parser_endTagType = new WeakMap(), _Parser_ignoreTags = new WeakMap(), _Parser_maskChar = new WeakMap(), _Parser_tagNameCaseSensitive = new WeakMap(), _Parser_selfCloseType = new WeakMap(), _Parser_spaceChars = new WeakMap(), _Parser_rawTextElements = new WeakMap(), _Parser_authoredElementName = new WeakMap(), _Parser_originalRawCode = new WeakMap(), _Parser_rawCode = new WeakMap(), _Parser_defaultDepth = new WeakMap(), _Parser_walkMethodSequentailPrevNode = new WeakMap(), _Parser_instances = new WeakSet(), _Parser_arrayize = function _Parser_arrayize(nodeTree) {
629
+ let nodeList = [];
630
+ this.walk(nodeTree, node => {
631
+ nodeList.push(node);
632
+ });
633
+ nodeList = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_removeDeprecatedNode).call(this, nodeList);
634
+ return nodeList;
635
+ }, _Parser_concatText = function _Parser_concatText(nodeList) {
636
+ const newNodeList = [];
637
+ for (const node of nodeList) {
638
+ const prevNode = newNodeList.at(-1) ?? null;
639
+ if (prevNode?.type === 'text' &&
640
+ prevNode?.nodeName === '#text' &&
641
+ node.type === 'text' &&
642
+ node.nodeName === '#text' &&
643
+ prevNode?.endOffset === node.startOffset) {
644
+ const newNode = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_concatTextNodes).call(this, prevNode, node);
645
+ newNodeList.pop();
646
+ newNodeList.push(newNode);
647
+ continue;
648
+ }
649
+ newNodeList.push(node);
650
+ }
651
+ return newNodeList;
652
+ }, _Parser_concatTextNodes = function _Parser_concatTextNodes(...nodes) {
653
+ if (nodes.length === 0) {
654
+ throw new Error('Empty node list');
655
+ }
656
+ const firstNode = nodes.at(0);
657
+ const lastNode = nodes.at(-1);
658
+ if (firstNode.uuid === lastNode.uuid) {
659
+ return firstNode;
660
+ }
661
+ const textNode = {
662
+ ...firstNode,
663
+ uuid: uuid().slice(0, 8),
664
+ raw: nodes.map(n => n.raw).join(''),
665
+ endOffset: lastNode.endOffset,
666
+ endLine: lastNode.endLine,
667
+ endCol: lastNode.endCol,
668
+ };
669
+ for (const node of nodes) {
670
+ __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_removeChild).call(this, node.parentNode, node);
671
+ }
672
+ this.appendChild(textNode.parentNode, textNode);
673
+ return textNode;
674
+ }, _Parser_convertIntoInvalidNode = function _Parser_convertIntoInvalidNode(node) {
675
+ if (node.type === 'invalid') {
676
+ return node;
677
+ }
678
+ return {
679
+ ...node,
680
+ type: 'invalid',
681
+ nodeName: '#invalid',
682
+ isBogus: true,
683
+ kind: node.type,
684
+ };
685
+ }, _Parser_createOffsetSpaces = function _Parser_createOffsetSpaces(options) {
686
+ const offsetOffset = Math.max(options?.offsetOffset ?? 0, 0);
687
+ const offsetLine = Math.max((options?.offsetLine ?? 0) - 1, 0);
688
+ const offsetColumn = Math.max((options?.offsetColumn ?? 0) - 1, 0);
689
+ const offsetSpaces = ' '.repeat(offsetOffset - offsetLine - offsetColumn);
690
+ const offsetLines = '\n'.repeat(offsetLine);
691
+ const offsetColumns = ' '.repeat(offsetColumn);
692
+ return offsetSpaces + offsetLines + offsetColumns;
693
+ }, _Parser_createRemnantNode = function _Parser_createRemnantNode(start, end, depth, parentNode, exposeInvalidNode, exposeWhitespace) {
694
+ const codeFragment = this.sliceFragment(start, end);
695
+ if (codeFragment.raw) {
696
+ const remnantNodes = this.visitText({
697
+ ...codeFragment,
698
+ depth: depth,
699
+ parentNode: parentNode,
700
+ }, { researchTags: true }).filter((node) => 'parentNode' in node);
701
+ if (remnantNodes.length > 1) {
702
+ this.appendChild(parentNode, ...remnantNodes);
703
+ return remnantNodes;
704
+ }
705
+ const remnantNode = remnantNodes[0];
706
+ if (!remnantNode) {
707
+ return null;
708
+ }
709
+ if (exposeInvalidNode && remnantNode.raw.trim() !== '') {
710
+ const invalidNode = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_convertIntoInvalidNode).call(this, remnantNode);
711
+ this.appendChild(parentNode, invalidNode);
712
+ return [remnantNode];
713
+ }
714
+ if (exposeWhitespace && remnantNode.type === 'text' && remnantNode.raw.trim() === '') {
715
+ this.appendChild(parentNode, remnantNode);
716
+ return [remnantNode];
717
+ }
718
+ }
719
+ return null;
720
+ }, _Parser_exposeRemnantNodes = function _Parser_exposeRemnantNodes(nodeList, invalidNode, whitespace) {
721
+ if (!invalidNode && !whitespace) {
722
+ return nodeList;
723
+ }
724
+ const newNodeList = [];
725
+ for (const [i, node] of nodeList.entries()) {
726
+ const sequentailPrevNode = nodeList[i - 1] ?? null;
727
+ if (!__classPrivateFieldGet(this, _Parser_rawTextElements, "f").includes(node.nodeName.toLowerCase())) {
728
+ const endOffset = sequentailPrevNode?.endOffset ?? 0;
729
+ const remnantNodes = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_createRemnantNode).call(this, endOffset, node.startOffset, node.depth, node.parentNode, invalidNode, whitespace);
730
+ if (remnantNodes) {
731
+ newNodeList.push(...remnantNodes);
732
+ }
733
+ }
734
+ newNodeList.push(node);
735
+ }
736
+ const lastNode = newNodeList.at(-1);
737
+ if (!lastNode) {
738
+ return newNodeList;
739
+ }
740
+ const remnantNodes = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_createRemnantNode).call(this, lastNode.endOffset, undefined, lastNode.depth, lastNode.parentNode, invalidNode, whitespace);
741
+ if (!remnantNodes) {
742
+ return newNodeList;
743
+ }
744
+ newNodeList.push(...remnantNodes);
745
+ return newNodeList;
746
+ }, _Parser_getEndLocation = function _Parser_getEndLocation(token) {
747
+ const endOffset = token.startOffset + token.raw.length;
748
+ return {
749
+ endOffset,
750
+ endLine: getEndLine(token.raw, token.startLine),
751
+ endCol: getEndCol(token.raw, token.startCol),
752
+ };
753
+ }, _Parser_orphanEndTagToBogusMark = function _Parser_orphanEndTagToBogusMark(nodeList) {
754
+ const newNodeList = [];
755
+ for (let node of nodeList) {
756
+ if (node.type === 'endtag') {
757
+ const endTagUUID = node.uuid;
758
+ const openTag = newNodeList.findLast((n) => n.type === 'starttag' ? n.pairNode?.uuid === endTagUUID : false);
759
+ if (!openTag) {
760
+ node = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_convertIntoInvalidNode).call(this, node);
761
+ }
762
+ }
763
+ newNodeList.push(node);
764
+ }
765
+ return newNodeList;
766
+ }, _Parser_pairing = function _Parser_pairing(startTag, endTag, appendChild = true) {
767
+ Object.assign(startTag, { pairNode: endTag });
768
+ Object.assign(endTag, { pairNode: startTag });
769
+ if (!appendChild) {
770
+ return;
771
+ }
772
+ this.appendChild(startTag.parentNode, endTag);
773
+ }, _Parser_parseEndTag = function _Parser_parseEndTag(token, namelessFragment) {
774
+ const parsed = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseTag).call(this, token, true, false, namelessFragment);
775
+ if (!parsed.token || parsed.token.type !== 'endtag') {
776
+ throw new ParserError("Expected end tag but it's not end tag", token);
777
+ }
778
+ return parsed.token;
779
+ }, _Parser_parseStartTag = function _Parser_parseStartTag(token, overwriteProps, namelessFragment) {
780
+ const parsed = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseTag).call(this, token, true, false, namelessFragment);
781
+ if (!parsed.token || parsed.token.type !== 'starttag') {
782
+ throw new ParserError("Expected start tag but it's not start tag", token);
783
+ }
784
+ const startTag = {
785
+ ...parsed.token,
786
+ ...overwriteProps,
787
+ };
788
+ this.appendChild(token.parentNode, startTag);
789
+ return startTag;
790
+ }, _Parser_parseTag = function _Parser_parseTag(token, praseAttr, failSafe, namelessFragment) {
791
+ const raw = token.raw;
792
+ const depth = token.depth;
793
+ const initialOffset = token.startOffset;
794
+ const initialLine = token.startLine;
795
+ const initialCol = token.startCol;
796
+ let offset = initialOffset;
797
+ let line = initialLine;
798
+ let col = initialCol;
799
+ let tagStartOffset = offset;
800
+ let tagStartLine = line;
801
+ let tagStartCol = col;
802
+ let state = TagState.BeforeOpenTag;
803
+ let beforeOpenTagChars = '';
804
+ let tagName = '';
805
+ let afterAttrsSpaceChars = '';
806
+ let selfClosingSolidusChar = '';
807
+ let isOpenTag = true;
808
+ const attrs = [];
809
+ const chars = [...raw];
810
+ while (chars.length > 0) {
811
+ if (state === TagState.AfterOpenTag) {
812
+ break;
813
+ }
814
+ const char = chars.shift();
815
+ stateSwitch: switch (state) {
816
+ case TagState.BeforeOpenTag: {
817
+ if (char === '<') {
818
+ const beforeOpenTag = this.createToken(beforeOpenTagChars, offset, line, col);
819
+ line = beforeOpenTag.endLine;
820
+ col = beforeOpenTag.endCol;
821
+ offset = beforeOpenTag.endOffset;
822
+ tagStartOffset = offset;
823
+ tagStartLine = line;
824
+ tagStartCol = col;
825
+ // Add `<` length
826
+ col += 1;
827
+ offset += 1;
828
+ state = TagState.FirstCharOfTagName;
829
+ break;
830
+ }
831
+ beforeOpenTagChars += char;
832
+ break;
833
+ }
834
+ case TagState.FirstCharOfTagName: {
835
+ if (/[a-z]/i.test(char)) {
836
+ tagName += char;
837
+ state = TagState.TagName;
838
+ break;
839
+ }
840
+ if (char === '/') {
841
+ isOpenTag = false;
842
+ break;
843
+ }
844
+ if (namelessFragment && char === '>') {
845
+ state = TagState.AfterOpenTag;
846
+ break;
847
+ }
848
+ chars.unshift(char);
849
+ state = TagState.AfterOpenTag;
850
+ break;
851
+ }
852
+ case TagState.TagName: {
853
+ if (__classPrivateFieldGet(this, _Parser_spaceChars, "f").includes(char)) {
854
+ chars.unshift(char);
855
+ if (!isOpenTag) {
856
+ // Add `/` of `</`(close tag) length
857
+ offset += 1;
858
+ col += 1;
859
+ }
860
+ offset += tagName.length;
861
+ col += tagName.length;
862
+ state = TagState.Attrs;
863
+ break;
864
+ }
865
+ if (char === '/') {
866
+ chars.unshift(char);
867
+ state = TagState.AfterAttrs;
868
+ break;
869
+ }
870
+ if (char === '>') {
871
+ state = TagState.AfterOpenTag;
872
+ break;
873
+ }
874
+ tagName += char;
875
+ break;
876
+ }
877
+ case TagState.Attrs: {
878
+ if (!praseAttr) {
879
+ state = TagState.AfterAttrs;
880
+ break stateSwitch;
881
+ }
882
+ let leftover = char + chars.join('');
883
+ while (leftover.trim()) {
884
+ if (leftover.trim().startsWith('/') || leftover.trim().startsWith('>')) {
885
+ chars.length = 0;
886
+ chars.push(...leftover);
887
+ state = TagState.AfterAttrs;
888
+ break stateSwitch;
889
+ }
890
+ const attr = this.visitAttr({
891
+ raw: leftover,
892
+ startOffset: offset,
893
+ startLine: line,
894
+ startCol: col,
895
+ });
896
+ line = attr.endLine;
897
+ col = attr.endCol;
898
+ offset = attr.endOffset;
899
+ if (leftover === attr.__rightText) {
900
+ throw new SyntaxError(`Invalid attribute syntax: ${leftover}`);
901
+ }
902
+ leftover = attr.__rightText == null ? '' : `${attr.__rightText}`;
903
+ delete attr.__rightText;
904
+ attrs.push(attr);
905
+ }
906
+ break;
907
+ }
908
+ case TagState.AfterAttrs: {
909
+ if (char === '>') {
910
+ state = TagState.AfterOpenTag;
911
+ break;
912
+ }
913
+ if (__classPrivateFieldGet(this, _Parser_spaceChars, "f").includes(char)) {
914
+ afterAttrsSpaceChars += char;
915
+ break;
916
+ }
917
+ if (char === '/') {
918
+ selfClosingSolidusChar = char;
919
+ break;
920
+ }
921
+ if (!praseAttr) {
922
+ break;
923
+ }
924
+ throw new SyntaxError(`Invalid tag syntax: "${raw}"`);
925
+ }
926
+ }
927
+ }
928
+ const leftover = chars.join('');
929
+ if (!failSafe && !leftover && state === TagState.TagName) {
930
+ throw new SyntaxError(`Invalid tag syntax: "${raw}"`);
931
+ }
932
+ if (!failSafe && !namelessFragment && tagName === '') {
933
+ throw new SyntaxError(`No tag name: "${raw}"`);
934
+ }
935
+ const endSpace = this.createToken(afterAttrsSpaceChars, offset, line, col);
936
+ line = endSpace.endLine;
937
+ col = endSpace.endCol;
938
+ offset = endSpace.endOffset;
939
+ const selfClosingSolidus = this.createToken(selfClosingSolidusChar, offset, line, col);
940
+ line = selfClosingSolidus.endLine;
941
+ col = selfClosingSolidus.endCol;
942
+ offset = selfClosingSolidus.endOffset;
943
+ const rawCodeFragment = raw.slice(beforeOpenTagChars.length, raw.length - leftover.length);
944
+ if (!rawCodeFragment) {
945
+ return {
946
+ __left: beforeOpenTagChars,
947
+ __right: leftover,
948
+ };
949
+ }
950
+ const tagToken = this.createToken(rawCodeFragment, tagStartOffset, tagStartLine, tagStartCol);
951
+ const commons = {
952
+ depth,
953
+ nodeName: tagName || '#jsx-fragment',
954
+ parentNode: null,
955
+ };
956
+ const tag = isOpenTag
957
+ ? {
958
+ ...tagToken,
959
+ ...commons,
960
+ type: 'starttag',
961
+ elementType: this.detectElementType(tagName),
962
+ namespace: '',
963
+ attributes: attrs,
964
+ childNodes: [],
965
+ pairNode: null,
966
+ tagOpenChar: '<',
967
+ tagCloseChar: selfClosingSolidusChar + '>',
968
+ selfClosingSolidus,
969
+ isGhost: false,
970
+ }
971
+ : {
972
+ ...tagToken,
973
+ ...commons,
974
+ type: 'endtag',
975
+ pairNode: {},
976
+ tagOpenChar: '</',
977
+ tagCloseChar: '>',
978
+ };
979
+ return {
980
+ token: tag,
981
+ __left: beforeOpenTagChars,
982
+ __right: leftover,
983
+ };
984
+ }, _Parser_removeChild = function _Parser_removeChild(parentNode, ...childNodes) {
985
+ if (!parentNode || childNodes.length === 0) {
986
+ return;
987
+ }
988
+ const newChildNodes = parentNode.childNodes.filter(n => !childNodes.includes(n));
989
+ Object.assign(parentNode, { childNodes: newChildNodes });
990
+ }, _Parser_removeDeprecatedNode = function _Parser_removeDeprecatedNode(nodeOrders) {
991
+ /**
992
+ * sorting
993
+ */
994
+ const sorted = Array.prototype.toSorted == null
995
+ ? // TODO: Use sort instead of toSorted until we end support for Node 18
996
+ [...nodeOrders].sort(sortNodes)
997
+ : nodeOrders.toSorted(sortNodes);
998
+ /**
999
+ * remove duplicated node
1000
+ */
1001
+ const stack = {};
1002
+ const removeIndexes = [];
1003
+ for (const [i, node] of sorted.entries()) {
1004
+ const id = `${node.startOffset}::${node.nodeName}`;
1005
+ if (stack[id] != null) {
1006
+ removeIndexes.push(i);
1007
+ }
1008
+ stack[id] = i;
1009
+ }
1010
+ let r = sorted.length;
1011
+ while (r-- > 0) {
1012
+ if (removeIndexes.includes(r)) {
1013
+ sorted.splice(r, 1);
1014
+ }
1015
+ }
1016
+ return sorted;
1017
+ }, _Parser_removeOffsetSpaces = function _Parser_removeOffsetSpaces(nodeList, options) {
1018
+ const offsetOffset = options?.offsetOffset ?? 0;
1019
+ const offsetLine = options?.offsetLine ?? 1;
1020
+ const offsetColumn = options?.offsetColumn ?? 1;
1021
+ if (offsetOffset === 0) {
1022
+ return nodeList;
1023
+ }
1024
+ const firstNode = nodeList.at(0);
1025
+ if (!firstNode || firstNode.type !== 'text') {
1026
+ return nodeList;
1027
+ }
1028
+ const raw = firstNode.raw.slice(offsetOffset);
1029
+ if (!raw) {
1030
+ if (Array.prototype.toSpliced == null) {
1031
+ const newNodeList = [...nodeList];
1032
+ // TODO: Use splice instead of toSpliced until we end support for Node 18
1033
+ newNodeList.splice(0, 1);
1034
+ return newNodeList;
1035
+ }
1036
+ return nodeList.toSpliced(0, 1);
1037
+ }
1038
+ this.updateRaw(firstNode, raw);
1039
+ this.updateLocation(firstNode, {
1040
+ startOffset: offsetOffset,
1041
+ startLine: offsetLine,
1042
+ startCol: offsetColumn,
1043
+ });
1044
+ return nodeList;
1045
+ }, _Parser_reset = function _Parser_reset() {
1046
+ // Reset state
1047
+ this.state = structuredClone(__classPrivateFieldGet(this, _Parser_defaultState, "f"));
1048
+ __classPrivateFieldSet(this, _Parser_defaultDepth, 0, "f");
1049
+ }, _Parser_setRawCode = function _Parser_setRawCode(rawCode, originalRawCode) {
1050
+ __classPrivateFieldSet(this, _Parser_rawCode, rawCode, "f");
1051
+ __classPrivateFieldSet(this, _Parser_originalRawCode, originalRawCode ?? __classPrivateFieldGet(this, _Parser_originalRawCode, "f"), "f");
1052
+ }, _Parser_siblingsCorrection = function _Parser_siblingsCorrection(nodes) {
1053
+ const stack = new Set();
1054
+ const newNodes = [];
1055
+ const oldNodes = Array.prototype.toSorted == null
1056
+ ? // TODO: Use sort instead of toSorted until we end support for Node 18
1057
+ [...nodes].sort(sortNodes)
1058
+ : nodes.toSorted(sortNodes);
1059
+ for (const node of oldNodes) {
1060
+ const id = `${node.startOffset}::${node.nodeName}`;
1061
+ if (stack.has(id)) {
1062
+ continue;
1063
+ }
1064
+ stack.add(id);
1065
+ if (node.type === 'endtag') {
1066
+ const openTag = newNodes.findLast(n => n.type === 'starttag' && n.nodeName === node.nodeName);
1067
+ if (openTag && openTag.type === 'starttag' && !openTag.pairNode) {
1068
+ __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_pairing).call(this, openTag, node, false);
1069
+ newNodes.push(node);
1070
+ continue;
1071
+ }
1072
+ }
1073
+ newNodes.push(node);
1074
+ }
1075
+ return newNodes;
1076
+ };