-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
parser-html.js
303 lines (275 loc) · 7.94 KB
/
parser-html.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"use strict";
const parseFrontMatter = require("../utils/front-matter");
const { HTML_ELEMENT_ATTRIBUTES, HTML_TAGS } = require("./utils");
const { hasPragma } = require("./pragma");
const createError = require("../common/parser-create-error");
const { Node } = require("./ast");
const { parseIeConditionalComment } = require("./conditional-comment");
function ngHtmlParser(
input,
{
recognizeSelfClosing,
normalizeTagName,
normalizeAttributeName,
allowHtmComponentClosingTags,
isTagNameCaseSensitive
}
) {
const parser = require("angular-html-parser");
const {
RecursiveVisitor,
visitAll,
Attribute,
CDATA,
Comment,
DocType,
Element,
Text
} = require("angular-html-parser/lib/compiler/src/ml_parser/ast");
const {
ParseSourceSpan
} = require("angular-html-parser/lib/compiler/src/parse_util");
const {
getHtmlTagDefinition
} = require("angular-html-parser/lib/compiler/src/ml_parser/html_tags");
const { rootNodes, errors } = parser.parse(input, {
canSelfClose: recognizeSelfClosing,
allowHtmComponentClosingTags,
isTagNameCaseSensitive
});
if (errors.length !== 0) {
const { msg, span } = errors[0];
const { line, col } = span.start;
throw createError(msg, { start: { line: line + 1, column: col + 1 } });
}
const addType = node => {
if (node instanceof Attribute) {
node.type = "attribute";
} else if (node instanceof CDATA) {
node.type = "cdata";
} else if (node instanceof Comment) {
node.type = "comment";
} else if (node instanceof DocType) {
node.type = "docType";
} else if (node instanceof Element) {
node.type = "element";
} else if (node instanceof Text) {
node.type = "text";
} else {
throw new Error(`Unexpected node ${JSON.stringify(node)}`);
}
};
const restoreName = node => {
const namespace = node.name.startsWith(":")
? node.name.slice(1).split(":")[0]
: null;
const rawName = node.nameSpan ? node.nameSpan.toString() : node.name;
const hasExplicitNamespace = rawName.startsWith(`${namespace}:`);
const name = hasExplicitNamespace
? rawName.slice(namespace.length + 1)
: rawName;
node.name = name;
node.namespace = namespace;
node.hasExplicitNamespace = hasExplicitNamespace;
};
const restoreNameAndValue = node => {
if (node instanceof Element) {
restoreName(node);
node.attrs.forEach(attr => {
restoreName(attr);
if (!attr.valueSpan) {
attr.value = null;
} else {
attr.value = attr.valueSpan.toString();
if (/['"]/.test(attr.value[0])) {
attr.value = attr.value.slice(1, -1);
}
}
});
} else if (node instanceof Comment) {
node.value = node.sourceSpan
.toString()
.slice("<!--".length, -"-->".length);
} else if (node instanceof Text) {
node.value = node.sourceSpan.toString();
}
};
const lowerCaseIfFn = (text, fn) => {
const lowerCasedText = text.toLowerCase();
return fn(lowerCasedText) ? lowerCasedText : text;
};
const normalizeName = node => {
if (node instanceof Element) {
if (
normalizeTagName &&
(!node.namespace ||
node.namespace === node.tagDefinition.implicitNamespacePrefix)
) {
node.name = lowerCaseIfFn(
node.name,
lowerCasedName => lowerCasedName in HTML_TAGS
);
}
if (normalizeAttributeName) {
const CURRENT_HTML_ELEMENT_ATTRIBUTES =
HTML_ELEMENT_ATTRIBUTES[node.name] || Object.create(null);
node.attrs.forEach(attr => {
if (!attr.namespace) {
attr.name = lowerCaseIfFn(
attr.name,
lowerCasedAttrName =>
node.name in HTML_ELEMENT_ATTRIBUTES &&
(lowerCasedAttrName in HTML_ELEMENT_ATTRIBUTES["*"] ||
lowerCasedAttrName in CURRENT_HTML_ELEMENT_ATTRIBUTES)
);
}
});
}
}
};
const fixSourceSpan = node => {
if (node.sourceSpan && node.endSourceSpan) {
node.sourceSpan = new ParseSourceSpan(
node.sourceSpan.start,
node.endSourceSpan.end
);
}
};
const addTagDefinition = node => {
if (node instanceof Element) {
const tagDefinition = getHtmlTagDefinition(
isTagNameCaseSensitive ? node.name : node.name.toLowerCase()
);
if (
!node.namespace ||
node.namespace === tagDefinition.implicitNamespacePrefix
) {
node.tagDefinition = tagDefinition;
} else {
node.tagDefinition = getHtmlTagDefinition(""); // the default one
}
}
};
visitAll(
new class extends RecursiveVisitor {
visit(node) {
addType(node);
restoreNameAndValue(node);
addTagDefinition(node);
normalizeName(node);
fixSourceSpan(node);
}
}(),
rootNodes
);
return rootNodes;
}
function _parse(text, options, parserOptions, shouldParseFrontMatter = true) {
const { frontMatter, content } = shouldParseFrontMatter
? parseFrontMatter(text)
: { frontMatter: null, content: text };
const rawAst = {
type: "root",
sourceSpan: { start: { offset: 0 }, end: { offset: text.length } },
children: ngHtmlParser(content, parserOptions)
};
if (frontMatter) {
rawAst.children.unshift(frontMatter);
}
const ast = new Node(rawAst);
const parseSubHtml = (subContent, startSpan) => {
const { offset } = startSpan;
const fakeContent = text.slice(0, offset).replace(/[^\r\n]/g, " ");
const realContent = subContent;
const subAst = _parse(
fakeContent + realContent,
options,
parserOptions,
false
);
const ParseSourceSpan = subAst.children[0].sourceSpan.constructor;
subAst.sourceSpan = new ParseSourceSpan(
startSpan,
subAst.children[subAst.children.length - 1].sourceSpan.end
);
const firstText = subAst.children[0];
if (firstText.length === offset) {
subAst.children.shift();
} else {
firstText.sourceSpan = new ParseSourceSpan(
firstText.sourceSpan.start.moveBy(offset),
firstText.sourceSpan.end
);
firstText.value = firstText.value.slice(offset);
}
return subAst;
};
const isFakeElement = node => node.type === "element" && !node.nameSpan;
return ast.map(node => {
if (node.children && node.children.some(isFakeElement)) {
const newChildren = [];
for (const child of node.children) {
if (isFakeElement(child)) {
Array.prototype.push.apply(newChildren, child.children);
} else {
newChildren.push(child);
}
}
return node.clone({ children: newChildren });
}
if (node.type === "comment") {
const ieConditionalComment = parseIeConditionalComment(
node,
parseSubHtml
);
if (ieConditionalComment) {
return ieConditionalComment;
}
}
return node;
});
}
function locStart(node) {
return node.sourceSpan.start.offset;
}
function locEnd(node) {
return node.sourceSpan.end.offset;
}
function createParser({
recognizeSelfClosing = false,
normalizeTagName = false,
normalizeAttributeName = false,
allowHtmComponentClosingTags = false,
isTagNameCaseSensitive = false
} = {}) {
return {
parse: (text, parsers, options) =>
_parse(text, options, {
recognizeSelfClosing,
normalizeTagName,
normalizeAttributeName,
allowHtmComponentClosingTags,
isTagNameCaseSensitive
}),
hasPragma,
astFormat: "html",
locStart,
locEnd
};
}
module.exports = {
parsers: {
html: createParser({
recognizeSelfClosing: true,
normalizeTagName: true,
normalizeAttributeName: true,
allowHtmComponentClosingTags: true
}),
angular: createParser(),
vue: createParser({
recognizeSelfClosing: true,
isTagNameCaseSensitive: true
}),
lwc: createParser()
}
};