/
vue-template-html.ts
195 lines (174 loc) · 5.19 KB
/
vue-template-html.ts
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
import { VueLanguagePlugin } from '../types';
import type * as CompilerDOM from '@vue/compiler-dom';
interface Loc {
start: { offset: number; };
end: { offset: number; };
source: string;
}
type Node = CompilerDOM.RootNode | CompilerDOM.TemplateChildNode | CompilerDOM.ExpressionNode | CompilerDOM.AttributeNode | CompilerDOM.DirectiveNode;
const plugin: VueLanguagePlugin = ({ modules }) => {
return {
version: 1,
compileSFCTemplate(lang, template, options) {
if (lang === 'html') {
const compiler = modules['@vue/compiler-dom'];
return compiler.compile(template, {
...options,
comments: true,
});
}
},
updateSFCTemplate(oldResult, change) {
const CompilerDOM = modules['@vue/compiler-dom'];
const lengthDiff = change.newText.length - (change.end - change.start);
let hitNodes: Node[] = [];
if (tryUpdateNode(oldResult.ast) && hitNodes.length) {
hitNodes = hitNodes.sort((a, b) => a.loc.source.length - b.loc.source.length);
const hitNode = hitNodes[0];
if (hitNode.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
return oldResult;
}
}
function tryUpdateNode(node: Node) {
if (withinChangeRange(node.loc)) {
hitNodes.push(node);
}
if (tryUpdateNodeLoc(node.loc)) {
if (node.type === CompilerDOM.NodeTypes.ROOT) {
for (const child of node.children) {
if (!tryUpdateNode(child)) {
return false;
}
}
}
else if (node.type === CompilerDOM.NodeTypes.ELEMENT) {
if (withinChangeRange(node.loc)) {
// if not self closing, should not hit tag name
const start = node.loc.start.offset + 2;
const end = node.loc.start.offset + node.loc.source.lastIndexOf('</');
if (!withinChangeRange({ start: { offset: start }, end: { offset: end }, source: '' })) {
return false;
}
}
for (const prop of node.props) {
if (!tryUpdateNode(prop)) {
return false;
}
}
for (const child of node.children) {
if (!tryUpdateNode(child)) {
return false;
}
}
}
else if (node.type === CompilerDOM.NodeTypes.ATTRIBUTE) {
if (node.value && !tryUpdateNode(node.value)) {
return false;
}
}
else if (node.type === CompilerDOM.NodeTypes.DIRECTIVE) {
if (node.arg && withinChangeRange(node.arg.loc) && node.name === 'slot') {
return false;
}
if (node.exp && withinChangeRange(node.exp.loc) && node.name === 'for') { // #2266
return false;
}
if (node.arg && !tryUpdateNode(node.arg)) {
return false;
}
if (node.exp && !tryUpdateNode(node.exp)) {
return false;
}
}
else if (node.type === CompilerDOM.NodeTypes.TEXT_CALL) {
if (!tryUpdateNode(node.content)) {
return false;
}
}
else if (node.type === CompilerDOM.NodeTypes.COMPOUND_EXPRESSION) {
for (const childNode of node.children) {
if (typeof childNode === 'object') {
if (!tryUpdateNode(childNode as CompilerDOM.TemplateChildNode)) {
return false;
}
}
}
}
else if (node.type === CompilerDOM.NodeTypes.IF) {
for (const branch of node.branches) {
if (branch.condition && !tryUpdateNode(branch.condition)) {
return false;
}
for (const child of branch.children) {
if (!tryUpdateNode(child)) {
return false;
}
}
}
}
else if (node.type === CompilerDOM.NodeTypes.FOR) {
for (const child of [
node.parseResult.source,
node.parseResult.value,
node.parseResult.key,
node.parseResult.index,
]) {
if (child && !tryUpdateNode(child)) {
return false;
}
}
for (const child of node.children) {
if (!tryUpdateNode(child)) {
return false;
}
}
}
else if (node.type === CompilerDOM.NodeTypes.INTERPOLATION) {
if (!tryUpdateNode(node.content)) {
return false;
}
}
else if (node.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
if (withinChangeRange(node.loc)) { // TODO: review this (slot name?)
if (node.isStatic) {
return false;
}
else {
node.content = node.loc.source;
}
}
}
return true;
}
return false;
}
function tryUpdateNodeLoc(loc: Loc) {
delete (loc as any).__endOffset;
if (withinChangeRange(loc)) {
loc.source =
loc.source.substring(0, change.start - loc.start.offset)
+ change.newText
+ loc.source.substring(change.end - loc.start.offset);
(loc as any).__endOffset = loc.end.offset;
loc.end.offset += lengthDiff;
return true;
}
else if (change.end <= loc.start.offset) {
(loc as any).__endOffset = loc.end.offset;
loc.start.offset += lengthDiff;
loc.end.offset += lengthDiff;
return true;
}
else if (change.start >= loc.end.offset) {
return true; // no need update
}
return false;
}
function withinChangeRange(loc: Loc) {
const originalLocEnd = (loc as any).__endOffset ?? loc.end.offset;
return change.start >= loc.start.offset && change.end <= originalLocEnd;
}
},
};
};
export = plugin;