@markuplint/astro-parser 3.12.0 → 4.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/parse.js DELETED
@@ -1,347 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parse = void 0;
4
- const html_parser_1 = require("@markuplint/html-parser");
5
- const parser_utils_1 = require("@markuplint/parser-utils");
6
- const astro_parser_1 = require("./astro-parser");
7
- const attr_tokenizer_1 = require("./attr-tokenizer");
8
- const parse = (rawCode, options = {}) => {
9
- var _a, _b;
10
- const ast = (0, astro_parser_1.astroParse)(rawCode);
11
- const htmlRawNodeList = traverse(ast, null, 'http://www.w3.org/1999/xhtml', rawCode, 0, options);
12
- const firstOffset = (_b = (_a = htmlRawNodeList[0]) === null || _a === void 0 ? void 0 : _a.startOffset) !== null && _b !== void 0 ? _b : 0;
13
- if (firstOffset > 0) {
14
- const head = rawCode.slice(0, firstOffset);
15
- const ast = (0, html_parser_1.parse)(head, {
16
- ...options,
17
- ignoreFrontMatter: true,
18
- });
19
- const headNodes = ast.nodeList.filter(node => {
20
- return !node.isGhost;
21
- });
22
- htmlRawNodeList.unshift(...headNodes);
23
- }
24
- const nodeList = (0, parser_utils_1.flattenNodes)(htmlRawNodeList, rawCode);
25
- return {
26
- nodeList,
27
- isFragment: true,
28
- };
29
- };
30
- exports.parse = parse;
31
- function traverse(
32
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
33
- rootNode, parentNode = null, scopeNS, rawHtml, offset, options, inExpression) {
34
- const nodeList = [];
35
- if (!('children' in rootNode)) {
36
- return nodeList;
37
- }
38
- if (rootNode.children.length === 0) {
39
- return nodeList;
40
- }
41
- let prevNode = null;
42
- for (const astNode of rootNode.children) {
43
- const node = nodeize(astNode, prevNode, parentNode, scopeNS, rawHtml, offset, options, inExpression);
44
- if (!node) {
45
- continue;
46
- }
47
- const nodes = Array.isArray(node) ? node : [node];
48
- for (const node of nodes) {
49
- if (prevNode) {
50
- if (node.type !== 'endtag') {
51
- prevNode.nextNode = node;
52
- }
53
- node.prevNode = prevNode;
54
- }
55
- prevNode = node;
56
- nodeList.push(node);
57
- }
58
- }
59
- return nodeList;
60
- }
61
- function nodeize(
62
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
63
- originNode,
64
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
65
- prevNode,
66
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
67
- parentNode, scopeNS, rawHtml, offset = 0, options, inExpression) {
68
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
69
- if (!originNode.position) {
70
- throw new TypeError("Node doesn't have position");
71
- }
72
- const nextNode = null;
73
- const startOffset = originNode.position.start.offset + offset;
74
- const endOffset = ((_b = (_a = originNode.position.end) === null || _a === void 0 ? void 0 : _a.offset) !== null && _b !== void 0 ? _b : originNode.position.start.offset) + offset;
75
- const { startLine, endLine, startCol, endCol, raw } = (0, parser_utils_1.sliceFragment)(rawHtml, startOffset, endOffset);
76
- if (scopeNS === 'http://www.w3.org/1999/xhtml' &&
77
- originNode.type === 'element' &&
78
- ((_c = originNode.name) === null || _c === void 0 ? void 0 : _c.toLowerCase()) === 'svg') {
79
- scopeNS = 'http://www.w3.org/2000/svg';
80
- }
81
- else if (scopeNS === 'http://www.w3.org/2000/svg' && parentNode && parentNode.nodeName === 'foreignObject') {
82
- scopeNS = 'http://www.w3.org/1999/xhtml';
83
- }
84
- switch (originNode.type) {
85
- case 'text': {
86
- if (inExpression) {
87
- return null;
88
- }
89
- const node = {
90
- uuid: (0, parser_utils_1.uuid)(),
91
- raw,
92
- startOffset,
93
- endOffset,
94
- startLine,
95
- endLine,
96
- startCol,
97
- endCol,
98
- nodeName: '#text',
99
- type: 'text',
100
- parentNode,
101
- prevNode,
102
- nextNode,
103
- isFragment: false,
104
- isGhost: false,
105
- };
106
- return node;
107
- }
108
- case 'expression': {
109
- let _endOffset = endOffset;
110
- let _startLine = startLine;
111
- let _endLine = endLine;
112
- let _startCol = startCol;
113
- let _endCol = endCol;
114
- let _raw = raw;
115
- let closeExpression = null;
116
- const firstChild = originNode.children[0];
117
- const lastChild = originNode.children[originNode.children.length - 1];
118
- if (firstChild && lastChild && firstChild !== lastChild) {
119
- _endOffset = (_f = (_e = (_d = firstChild.position) === null || _d === void 0 ? void 0 : _d.end) === null || _e === void 0 ? void 0 : _e.offset) !== null && _f !== void 0 ? _f : endOffset;
120
- const startLoc = (0, parser_utils_1.sliceFragment)(rawHtml, startOffset, _endOffset);
121
- _startLine = startLoc.startLine;
122
- _endLine = startLoc.endLine;
123
- _startCol = startLoc.startCol;
124
- _endCol = startLoc.endCol;
125
- _raw = startLoc.raw;
126
- const closeStartOffset = (_h = (_g = lastChild.position) === null || _g === void 0 ? void 0 : _g.start.offset) !== null && _h !== void 0 ? _h : startOffset;
127
- const closeLoc = (0, parser_utils_1.sliceFragment)(rawHtml, closeStartOffset, endOffset);
128
- closeExpression = {
129
- uuid: (0, parser_utils_1.uuid)(),
130
- raw: closeLoc.raw,
131
- startOffset: closeStartOffset,
132
- endOffset: closeLoc.endOffset,
133
- startLine: closeLoc.startLine,
134
- endLine: closeLoc.endLine,
135
- startCol: closeLoc.startCol,
136
- endCol: closeLoc.endCol,
137
- nodeName: 'MustacheTag',
138
- type: 'psblock',
139
- parentNode,
140
- prevNode,
141
- nextNode,
142
- isFragment: false,
143
- isGhost: false,
144
- };
145
- }
146
- const node = {
147
- uuid: (0, parser_utils_1.uuid)(),
148
- raw: _raw,
149
- startOffset,
150
- endOffset: _endOffset,
151
- startLine: _startLine,
152
- endLine: _endLine,
153
- startCol: _startCol,
154
- endCol: _endCol,
155
- nodeName: 'MustacheTag',
156
- type: 'psblock',
157
- parentNode,
158
- prevNode,
159
- nextNode,
160
- isFragment: false,
161
- isGhost: false,
162
- };
163
- if (originNode.children.length > 0) {
164
- node.childNodes = traverse(originNode, parentNode, scopeNS, rawHtml, offset, options, true);
165
- }
166
- if (closeExpression) {
167
- return [node, closeExpression];
168
- }
169
- return node;
170
- }
171
- case 'comment': {
172
- return {
173
- uuid: (0, parser_utils_1.uuid)(),
174
- raw,
175
- startOffset,
176
- endOffset,
177
- startLine,
178
- endLine,
179
- startCol,
180
- endCol,
181
- nodeName: '#comment',
182
- type: 'comment',
183
- parentNode,
184
- prevNode,
185
- nextNode,
186
- isFragment: false,
187
- isGhost: false,
188
- };
189
- }
190
- case 'component':
191
- case 'custom-element':
192
- case 'fragment':
193
- case 'element': {
194
- if (((_j = originNode.name) === null || _j === void 0 ? void 0 : _j.toLowerCase()) === '!doctype') {
195
- return {
196
- uuid: (0, parser_utils_1.uuid)(),
197
- raw,
198
- name: originNode.name,
199
- publicId: '',
200
- systemId: '',
201
- startOffset,
202
- endOffset,
203
- startLine,
204
- endLine,
205
- startCol,
206
- endCol,
207
- nodeName: '#doctype',
208
- type: 'doctype',
209
- parentNode,
210
- prevNode,
211
- nextNode,
212
- isFragment: false,
213
- isGhost: false,
214
- };
215
- }
216
- return parseElement(originNode, scopeNS, rawHtml, startLine, startCol, startOffset, parentNode, prevNode, nextNode, offset, options);
217
- }
218
- default: {
219
- return null;
220
- }
221
- }
222
- }
223
- function parseElement(
224
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
225
- originNode, scopeNS, rawHtml, startLine, startCol, startOffset,
226
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
227
- parentNode,
228
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
229
- prevNode,
230
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
231
- nextNode, offset, options) {
232
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
233
- if (!originNode.position) {
234
- throw new TypeError("Node doesn't have position");
235
- }
236
- let startTagRaw;
237
- let childrenStart;
238
- let childrenEnd;
239
- if (originNode.children[0]) {
240
- childrenStart = ((_c = (_b = (_a = originNode.children[0].position) === null || _a === void 0 ? void 0 : _a.start) === null || _b === void 0 ? void 0 : _b.offset) !== null && _c !== void 0 ? _c : 0) + offset;
241
- childrenEnd = ((_g = (_f = (_e = (_d = originNode.children[originNode.children.length - 1]) === null || _d === void 0 ? void 0 : _d.position) === null || _e === void 0 ? void 0 : _e.end) === null || _f === void 0 ? void 0 : _f.offset) !== null && _g !== void 0 ? _g : 0) + offset;
242
- const startTagStartOffset = originNode.position.start.offset + offset;
243
- const startTagEndOffset = childrenStart;
244
- startTagRaw = rawHtml.slice(startTagStartOffset, startTagEndOffset);
245
- }
246
- else {
247
- childrenStart = offset + ((_k = (_j = (_h = originNode.position.end) === null || _h === void 0 ? void 0 : _h.offset) !== null && _j !== void 0 ? _j : nextNode === null || nextNode === void 0 ? void 0 : nextNode.endOffset) !== null && _k !== void 0 ? _k : rawHtml.length - offset);
248
- childrenEnd = childrenStart;
249
- const startTagStartOffset = originNode.position.start.offset + offset;
250
- let startTagEndOffset = childrenStart;
251
- startTagRaw = rawHtml.slice(startTagStartOffset, startTagEndOffset);
252
- const expectedCloseTag = `</${originNode.name}>`;
253
- if (startTagRaw.includes(expectedCloseTag)) {
254
- childrenStart -= expectedCloseTag.length;
255
- childrenEnd = childrenStart;
256
- startTagRaw = startTagRaw.replace(expectedCloseTag, '');
257
- }
258
- else {
259
- let startTagRawMasked = startTagRaw;
260
- for (const attr of originNode.attributes) {
261
- startTagRawMasked = startTagRawMasked.replace(attr.value, ' '.repeat(attr.value.length));
262
- }
263
- const closeChars = '>';
264
- const closeOffset = startTagRawMasked.indexOf(closeChars) + closeChars.length;
265
- startTagEndOffset = startTagStartOffset + closeOffset;
266
- startTagRaw = rawHtml.slice(startTagStartOffset, startTagEndOffset);
267
- }
268
- // console.log({
269
- // originNode,
270
- // attrs: originNode.attributes,
271
- // startTagRaw,
272
- // startTagStartOffset,
273
- // startTagEndOffset,
274
- // expectedCloseTag,
275
- // childrenStart,
276
- // childrenEnd,
277
- // });
278
- }
279
- const tagTokens = (0, html_parser_1.parseRawTag)(startTagRaw, startLine, startCol, startOffset);
280
- const tagName = tagTokens.tagName;
281
- let endTag = null;
282
- if (childrenEnd < ((_m = (_l = originNode.position.end) === null || _l === void 0 ? void 0 : _l.offset) !== null && _m !== void 0 ? _m : 0) + offset) {
283
- const endTagLoc = (0, parser_utils_1.sliceFragment)(rawHtml, childrenEnd, ((_p = (_o = originNode.position.end) === null || _o === void 0 ? void 0 : _o.offset) !== null && _p !== void 0 ? _p : 0) + offset);
284
- const endTagTokens = (0, html_parser_1.parseRawTag)(endTagLoc.raw, endTagLoc.startLine, endTagLoc.startCol, endTagLoc.startOffset);
285
- const endTagName = endTagTokens.tagName;
286
- endTag = {
287
- uuid: (0, parser_utils_1.uuid)(),
288
- raw: endTagLoc.raw,
289
- startOffset: endTagLoc.startOffset,
290
- endOffset: endTagLoc.endOffset,
291
- startLine: endTagLoc.startLine,
292
- endLine: endTagLoc.endLine,
293
- startCol: endTagLoc.startCol,
294
- endCol: endTagLoc.endCol,
295
- nodeName: endTagName,
296
- type: 'endtag',
297
- namespace: scopeNS,
298
- attributes: endTagTokens.attrs,
299
- parentNode,
300
- prevNode,
301
- nextNode,
302
- pearNode: null,
303
- isFragment: false,
304
- isGhost: false,
305
- tagOpenChar: '</',
306
- tagCloseChar: '>',
307
- };
308
- }
309
- const startTag = {
310
- uuid: (0, parser_utils_1.uuid)(),
311
- raw: startTagRaw,
312
- startOffset,
313
- endOffset: startOffset + startTagRaw.length,
314
- startLine,
315
- endLine: (0, parser_utils_1.getEndLine)(startTagRaw, startLine),
316
- startCol,
317
- endCol: (0, parser_utils_1.getEndCol)(startTagRaw, startCol),
318
- nodeName: tagName,
319
- type: 'starttag',
320
- namespace: scopeNS,
321
- elementType: (0, parser_utils_1.detectElementType)(tagName, options === null || options === void 0 ? void 0 : options.authoredElementName, /^[A-Z]|\./),
322
- attributes: originNode.attributes.map((
323
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
324
- attr, i) => {
325
- var _a;
326
- return (0, attr_tokenizer_1.attrTokenizer)(attr, (_a = originNode.attributes[i + 1]) !== null && _a !== void 0 ? _a : null, rawHtml, startTagRaw, startOffset + startTagRaw.length);
327
- }),
328
- hasSpreadAttr: false,
329
- parentNode,
330
- prevNode,
331
- nextNode,
332
- pearNode: endTag,
333
- selfClosingSolidus: tagTokens.selfClosingSolidus,
334
- endSpace: tagTokens.endSpace,
335
- isFragment: false,
336
- isGhost: false,
337
- tagOpenChar: '<',
338
- tagCloseChar: '>',
339
- };
340
- if (endTag) {
341
- endTag.pearNode = startTag;
342
- }
343
- startTag.childNodes = ['style', 'script'].includes(tagName)
344
- ? undefined
345
- : traverse(originNode, startTag, scopeNS, rawHtml, offset, options);
346
- return startTag;
347
- }