-
-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathhtmlxparser.ts
More file actions
167 lines (147 loc) · 5.3 KB
/
htmlxparser.ts
File metadata and controls
167 lines (147 loc) · 5.3 KB
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
import { parse } from 'svelte/compiler';
import { Node } from 'estree-walker';
function parseAttributeValue(value: string): string {
return /^['"]/.test(value) ? value.slice(1, -1) : value;
}
function parseAttributes(str: string, start: number) {
const attrs: Node[] = [];
str.split(/\s+/)
.filter(Boolean)
.forEach((attr) => {
const attrStart = start + str.indexOf(attr);
const [name, value] = attr.split('=');
attrs[name] = value ? parseAttributeValue(value) : name;
attrs.push({
type: 'Attribute',
name,
value: !value || [
{
type: 'Text',
start: attrStart + attr.indexOf('=') + 1,
end: attrStart + attr.length,
raw: parseAttributeValue(value)
}
],
start: attrStart,
end: attrStart + attr.length
});
});
return attrs;
}
function extractTag(htmlx: string, tag: 'script' | 'style') {
const exp = new RegExp(`(<!--[^]*?-->)|(<${tag}([\\S\\s]*?)>)([\\S\\s]*?)<\\/${tag}>`, 'g');
const matches: Node[] = [];
let match: RegExpExecArray | null = null;
while ((match = exp.exec(htmlx)) != null) {
if (match[0].startsWith('<!--')) {
// Tag is inside comment
continue;
}
const content = match[4];
if (!content) {
// Self-closing/empty tags don't need replacement
continue;
}
const start = match.index + match[2].length;
const end = start + content.length;
const containerStart = match.index;
const containerEnd = match.index + match[0].length;
matches.push({
start: containerStart,
end: containerEnd,
name: tag,
type: tag === 'style' ? 'Style' : 'Script',
attributes: parseAttributes(match[3], containerStart + `<${tag}`.length),
content: {
type: 'Text',
start,
end,
value: content,
raw: content
}
});
}
return matches;
}
function findVerbatimElements(htmlx: string) {
return [...extractTag(htmlx, 'script'), ...extractTag(htmlx, 'style')];
}
function blankVerbatimContent(htmlx: string, verbatimElements: Node[]) {
let output = htmlx;
for (const node of verbatimElements) {
const content = node.content;
if (content) {
output =
output.substring(0, content.start) +
output
.substring(content.start, content.end)
// blank out the content
.replace(/[^\n]/g, ' ')
// excess blank space can make the svelte parser very slow (sec->min). break it up with comments (works in style/script)
.replace(/[^\n][^\n][^\n][^\n]\n/g, '/**/\n') +
output.substring(content.end);
}
}
return output;
}
export function parseHtmlx(htmlx: string, options?: { emitOnTemplateError?: boolean }) {
//Svelte tries to parse style and script tags which doesn't play well with typescript, so we blank them out.
//HTMLx spec says they should just be retained after processing as is, so this is fine
const verbatimElements = findVerbatimElements(htmlx);
const deconstructed = blankVerbatimContent(htmlx, verbatimElements);
//extract the html content parsed as htmlx this excludes our script and style tags
const parsingCode = options?.emitOnTemplateError
? blankPossiblyErrorOperatorOrPropertyAccess(deconstructed)
: deconstructed;
const htmlxAst = parse(parsingCode).html;
//restore our script and style tags as nodes to maintain validity with HTMLx
for (const s of verbatimElements) {
htmlxAst.children.push(s);
htmlxAst.start = Math.min(htmlxAst.start, s.start);
htmlxAst.end = Math.max(htmlxAst.end, s.end);
}
return { htmlxAst, tags: verbatimElements };
}
const possibleOperatorOrPropertyAccess = new Set([
'.',
'?',
'*',
'~',
'=',
'<',
'!',
'&',
'^',
'|',
',',
'+',
'-'
]);
function blankPossiblyErrorOperatorOrPropertyAccess(htmlx: string) {
let index = htmlx.indexOf('}');
let lastIndex = 0;
const { length } = htmlx;
while (index < length && index >= 0) {
let backwardIndex = index - 1;
while (backwardIndex > lastIndex) {
const char = htmlx.charAt(backwardIndex);
if (possibleOperatorOrPropertyAccess.has(char)) {
const isPlusOrMinus = char === '+' || char === '-';
const isIncrementOrDecrement =
isPlusOrMinus && htmlx.charAt(backwardIndex - 1) === char;
if (isIncrementOrDecrement) {
backwardIndex -= 2;
continue;
}
htmlx =
htmlx.substring(0, backwardIndex) + ' ' + htmlx.substring(backwardIndex + 1);
} else if (!/\s/.test(char)) {
break;
}
backwardIndex--;
}
lastIndex = index;
index = htmlx.indexOf('}', index + 1);
}
return htmlx;
}