@weapp-vite/wxml 0.0.0-stage → 0.0.1-dev.20261112.1
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/LICENSE +21 -0
- package/README.md +199 -2
- package/dist/core/index.d.mts +2 -0
- package/dist/core/index.mjs +1 -0
- package/dist/core-BEEC2yjm.mjs +2392 -0
- package/dist/core-DBjwtLEd.d.mts +249 -0
- package/dist/index-eR1Owa_a.d.mts +71 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.mjs +140 -0
- package/dist/presets/index.d.mts +2 -0
- package/dist/presets/index.mjs +1 -0
- package/dist/presets-Ez_Kg5w3.mjs +179 -0
- package/package.json +92 -4
|
@@ -0,0 +1,2392 @@
|
|
|
1
|
+
import { decodeXML } from "entities";
|
|
2
|
+
// src/lexical.ts
|
|
3
|
+
const lineEndPattern = /[\n\r\u2028\u2029]/g;
|
|
4
|
+
/** JS 行注释在任意行终止符处结束;正则只定位下一处,不逐字符分配结果。 */
|
|
5
|
+
function findLineEnd(source, start) {
|
|
6
|
+
lineEndPattern.lastIndex = start;
|
|
7
|
+
return lineEndPattern.exec(source)?.index ?? -1;
|
|
8
|
+
}
|
|
9
|
+
/** 词法错误携带原文偏移,由调用方选择报告或抛出。 */
|
|
10
|
+
var TemplateLexicalError = class extends Error {
|
|
11
|
+
offset;
|
|
12
|
+
recovery;
|
|
13
|
+
constructor(offset, message, recovery) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.offset = offset;
|
|
16
|
+
this.recovery = recovery;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
function isSpaceCode(code) {
|
|
20
|
+
if (code <= 32) return code === 32 || code >= 9 && code <= 13;
|
|
21
|
+
return code >= 160 && (code === 160 || code === 5760 || code >= 8192 && code <= 8202 || code === 8232 || code === 8233 || code === 8239 || code === 8287 || code === 12288 || code === 65279);
|
|
22
|
+
}
|
|
23
|
+
function isWordCode(code) {
|
|
24
|
+
return code >= 65 && code <= 90 || code >= 97 && code <= 122 || code >= 48 && code <= 57 || code === 95 || code === 36 || code >= 128;
|
|
25
|
+
}
|
|
26
|
+
function isNameCode(code) {
|
|
27
|
+
return isWordCode(code) || code === 58 || code === 46 || code === 45;
|
|
28
|
+
}
|
|
29
|
+
function isTagStartCode(code) {
|
|
30
|
+
return code >= 65 && code <= 90 || code >= 97 && code <= 122 || code === 95;
|
|
31
|
+
}
|
|
32
|
+
function skipSpace(source, start) {
|
|
33
|
+
while (isSpaceCode(source.charCodeAt(start))) start++;
|
|
34
|
+
return start;
|
|
35
|
+
}
|
|
36
|
+
// src/attributeLayer.ts
|
|
37
|
+
/** 宿主属性层只移除每对反斜杠与后续字符中的第一字符,不解释 JS 转义。 */
|
|
38
|
+
function decodeLegacyAttributeLayer(value) {
|
|
39
|
+
return value.replace(/\\([\s\S])/g, "$1");
|
|
40
|
+
}
|
|
41
|
+
/** 外层引号是否结束属性只由宿主层的连续反斜杠奇偶决定。 */
|
|
42
|
+
function legacyAttributeEnd(source, start, quote) {
|
|
43
|
+
let end = source.indexOf(quote, start);
|
|
44
|
+
while (end >= 0) {
|
|
45
|
+
let previous = end - 1;
|
|
46
|
+
while (previous >= start && source.charCodeAt(previous) === 92) previous--;
|
|
47
|
+
if ((end - previous) % 2 !== 0) return end;
|
|
48
|
+
end = source.indexOf(quote, end + 1);
|
|
49
|
+
}
|
|
50
|
+
return -1;
|
|
51
|
+
}
|
|
52
|
+
/** 无转义不建立偏移表;有转义时只记录被移除字符的逻辑偏移。 */
|
|
53
|
+
function legacyAttributeSource(source, start, end) {
|
|
54
|
+
const content = source.slice(start, end);
|
|
55
|
+
if (!content.includes("\\")) return {
|
|
56
|
+
source: content,
|
|
57
|
+
start: 0,
|
|
58
|
+
end: content.length,
|
|
59
|
+
origin: start
|
|
60
|
+
};
|
|
61
|
+
const escapes = [];
|
|
62
|
+
const value = content.replace(/\\([\s\S])/g, (_match, character, offset) => {
|
|
63
|
+
escapes.push(offset - escapes.length);
|
|
64
|
+
return character;
|
|
65
|
+
});
|
|
66
|
+
return {
|
|
67
|
+
source: value,
|
|
68
|
+
start: 0,
|
|
69
|
+
end: value.length,
|
|
70
|
+
origin: start,
|
|
71
|
+
escapes
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** 逻辑字符仍映射到包含宿主层反斜杠的原始 UTF-16 位置。 */
|
|
75
|
+
function attributeSourcePosition(view, offset) {
|
|
76
|
+
const escapes = view.escapes;
|
|
77
|
+
let lower = 0;
|
|
78
|
+
let upper = escapes?.length ?? 0;
|
|
79
|
+
while (lower < upper) {
|
|
80
|
+
const middle = lower + upper >>> 1;
|
|
81
|
+
if (escapes[middle] < offset) lower = middle + 1;
|
|
82
|
+
else upper = middle;
|
|
83
|
+
}
|
|
84
|
+
return view.origin + offset + lower;
|
|
85
|
+
}
|
|
86
|
+
/** 诊断和可恢复插值范围始终引用原始源码,而非解码后的临时视图。 */
|
|
87
|
+
function attributeSourceError(view, error) {
|
|
88
|
+
if (error instanceof TemplateLexicalError) {
|
|
89
|
+
const recovery = error.recovery;
|
|
90
|
+
throw new TemplateLexicalError(attributeSourcePosition(view, error.offset), error.message, recovery && {
|
|
91
|
+
kind: recovery.kind,
|
|
92
|
+
start: attributeSourcePosition(view, recovery.start),
|
|
93
|
+
end: attributeSourcePosition(view, recovery.end)
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
// src/context.ts
|
|
99
|
+
const expressionWord = 1;
|
|
100
|
+
const controlWord = 2;
|
|
101
|
+
const blockWord = 4;
|
|
102
|
+
const declarationPrefix = 8;
|
|
103
|
+
const lineBreakWord = 16;
|
|
104
|
+
const exportWord = 32;
|
|
105
|
+
const typeObjectWord = 64;
|
|
106
|
+
const functionWord = 128;
|
|
107
|
+
const classWord = 256;
|
|
108
|
+
const caseWord = 512;
|
|
109
|
+
const defaultWord = 1024;
|
|
110
|
+
const wordRoles = /* @__PURE__ */ new Map([
|
|
111
|
+
["return", 17],
|
|
112
|
+
["throw", expressionWord],
|
|
113
|
+
["case", 513],
|
|
114
|
+
["delete", expressionWord],
|
|
115
|
+
["void", expressionWord],
|
|
116
|
+
["typeof", expressionWord],
|
|
117
|
+
["new", expressionWord],
|
|
118
|
+
["in", expressionWord],
|
|
119
|
+
["instanceof", expressionWord],
|
|
120
|
+
["yield", 17],
|
|
121
|
+
["await", expressionWord],
|
|
122
|
+
["else", 5],
|
|
123
|
+
["do", 5],
|
|
124
|
+
["if", controlWord],
|
|
125
|
+
["while", controlWord],
|
|
126
|
+
["for", controlWord],
|
|
127
|
+
["with", controlWord],
|
|
128
|
+
["switch", controlWord],
|
|
129
|
+
["catch", controlWord],
|
|
130
|
+
["try", blockWord],
|
|
131
|
+
["finally", blockWord],
|
|
132
|
+
["async", declarationPrefix],
|
|
133
|
+
["export", 40],
|
|
134
|
+
["default", 1032],
|
|
135
|
+
["extends", typeObjectWord],
|
|
136
|
+
["keyof", typeObjectWord],
|
|
137
|
+
["readonly", typeObjectWord],
|
|
138
|
+
["is", typeObjectWord],
|
|
139
|
+
["function", functionWord],
|
|
140
|
+
["class", classWord]
|
|
141
|
+
]);
|
|
142
|
+
const lineBreakPattern = /[\n\r\u2028\u2029]/;
|
|
143
|
+
const expressionAfter = 256;
|
|
144
|
+
const statementBody = 512;
|
|
145
|
+
const functionParameters = 1024;
|
|
146
|
+
const bracketMask = 255;
|
|
147
|
+
/** 保存有效 token 的词法目标,构造体正文按声明或表达式归属恢复斜杠判界。 */
|
|
148
|
+
var TokenContext = class {
|
|
149
|
+
operand = true;
|
|
150
|
+
previousRoles = 0;
|
|
151
|
+
previousCode;
|
|
152
|
+
brackets;
|
|
153
|
+
rootStatement;
|
|
154
|
+
statement;
|
|
155
|
+
label = false;
|
|
156
|
+
arrowBody = false;
|
|
157
|
+
headers;
|
|
158
|
+
questions;
|
|
159
|
+
cases;
|
|
160
|
+
constructor(expression = false) {
|
|
161
|
+
this.previousCode = expression ? -1 : 0;
|
|
162
|
+
this.rootStatement = this.statement = !expression;
|
|
163
|
+
}
|
|
164
|
+
get depth() {
|
|
165
|
+
return this.brackets?.length ?? 0;
|
|
166
|
+
}
|
|
167
|
+
get bracket() {
|
|
168
|
+
const frame = this.brackets?.[this.brackets.length - 1];
|
|
169
|
+
return frame === void 0 ? void 0 : frame & bracketMask;
|
|
170
|
+
}
|
|
171
|
+
word(source, start, end) {
|
|
172
|
+
if (this.previousCode === 46) {
|
|
173
|
+
this.value();
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const length = end - start;
|
|
177
|
+
const roles = length >= 2 && length <= 10 ? wordRoles.get(source.slice(start, end)) ?? 0 : 0;
|
|
178
|
+
if (roles) {
|
|
179
|
+
this.keyword(roles);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
this.label = this.statement;
|
|
183
|
+
this.operand = this.statement = this.arrowBody = false;
|
|
184
|
+
this.previousRoles = 0;
|
|
185
|
+
this.previousCode = 1;
|
|
186
|
+
}
|
|
187
|
+
keyword(roles) {
|
|
188
|
+
const startsStatement = this.statement;
|
|
189
|
+
const caseLabel = startsStatement && Boolean(roles & caseWord || roles & defaultWord && !(this.previousRoles & exportWord));
|
|
190
|
+
if (roles & 384) (this.headers ??= []).push({
|
|
191
|
+
kind: roles & functionWord ? "function" : "class",
|
|
192
|
+
phase: "head",
|
|
193
|
+
depth: this.depth,
|
|
194
|
+
declaration: startsStatement,
|
|
195
|
+
angles: 0,
|
|
196
|
+
typed: false
|
|
197
|
+
});
|
|
198
|
+
if (caseLabel) (this.cases ??= []).push(this.depth);
|
|
199
|
+
this.label = startsStatement && !caseLabel && !(roles & 1447);
|
|
200
|
+
this.statement = Boolean(roles & blockWord || startsStatement && !caseLabel && roles & declarationPrefix);
|
|
201
|
+
this.previousRoles = roles;
|
|
202
|
+
this.previousCode = 1;
|
|
203
|
+
this.arrowBody = false;
|
|
204
|
+
this.operand = Boolean(roles & expressionWord);
|
|
205
|
+
}
|
|
206
|
+
value(code = 1) {
|
|
207
|
+
this.operand = false;
|
|
208
|
+
this.statement = this.label = this.arrowBody = false;
|
|
209
|
+
this.previousRoles = 0;
|
|
210
|
+
this.previousCode = code;
|
|
211
|
+
}
|
|
212
|
+
increment(code) {
|
|
213
|
+
this.statement = this.label = this.arrowBody = false;
|
|
214
|
+
this.previousRoles = 0;
|
|
215
|
+
this.previousCode = code;
|
|
216
|
+
}
|
|
217
|
+
/** 换行只恢复可开始声明的位置,不改变跨行除法的表达式目标。 */
|
|
218
|
+
lineBreak(source, start, end) {
|
|
219
|
+
if (this.operand && !(this.previousRoles & lineBreakWord)) return;
|
|
220
|
+
const frame = this.brackets?.[this.brackets.length - 1];
|
|
221
|
+
const header = this.headers?.[this.headers.length - 1];
|
|
222
|
+
if ((frame === void 0 ? this.rootStatement : Boolean(frame & statementBody)) && header?.depth !== this.depth && (source === void 0 || lineBreakPattern.test(source.slice(start, end)))) this.statement = true;
|
|
223
|
+
}
|
|
224
|
+
/** 普通括号仍用数字帧;只有函数或类的头部需要保存跨 token 的正文归属。 */
|
|
225
|
+
punctuation(code, next = 0) {
|
|
226
|
+
let matched = true;
|
|
227
|
+
if (code === 46) this.operand = this.statement = false;
|
|
228
|
+
else if (code === 91) {
|
|
229
|
+
(this.brackets ??= []).push(code);
|
|
230
|
+
this.operand = true;
|
|
231
|
+
this.statement = false;
|
|
232
|
+
} else matched = this.punctuationContext(code, next);
|
|
233
|
+
this.arrowBody = code === 62 && this.previousCode === 61;
|
|
234
|
+
this.label = false;
|
|
235
|
+
this.previousRoles = 0;
|
|
236
|
+
this.previousCode = code;
|
|
237
|
+
return matched;
|
|
238
|
+
}
|
|
239
|
+
punctuationContext(code, next) {
|
|
240
|
+
let matched = true;
|
|
241
|
+
const header = this.headers?.[this.headers.length - 1];
|
|
242
|
+
const currentHeader = header && header.depth === this.depth ? header : void 0;
|
|
243
|
+
if (code === 40) {
|
|
244
|
+
if (currentHeader?.kind === "function" && currentHeader.phase === "head" && !currentHeader.angles) {
|
|
245
|
+
currentHeader.phase = "parameters";
|
|
246
|
+
(this.brackets ??= []).push(code | functionParameters);
|
|
247
|
+
} else (this.brackets ??= []).push(code | (this.previousRoles & controlWord ? expressionAfter : 0));
|
|
248
|
+
this.operand = true;
|
|
249
|
+
this.statement = false;
|
|
250
|
+
} else if (code === 123) {
|
|
251
|
+
const typeObject = currentHeader && (currentHeader.angles > 0 || currentHeader.phase === "body" && currentHeader.typed && ([
|
|
252
|
+
58,
|
|
253
|
+
38,
|
|
254
|
+
124,
|
|
255
|
+
63,
|
|
256
|
+
61,
|
|
257
|
+
62
|
|
258
|
+
].includes(this.previousCode) || this.previousRoles & typeObjectWord));
|
|
259
|
+
if (currentHeader && !typeObject && !currentHeader.angles && (currentHeader.kind === "class" || currentHeader.phase === "body")) {
|
|
260
|
+
this.headers.pop();
|
|
261
|
+
(this.brackets ??= []).push(code | statementBody | (currentHeader.declaration ? expressionAfter : 0));
|
|
262
|
+
this.statement = true;
|
|
263
|
+
} else if (this.arrowBody && !typeObject) {
|
|
264
|
+
(this.brackets ??= []).push(code | statementBody);
|
|
265
|
+
this.statement = true;
|
|
266
|
+
} else {
|
|
267
|
+
const object = typeObject || this.operand && !this.statement;
|
|
268
|
+
(this.brackets ??= []).push(code | (object ? 0 : 768));
|
|
269
|
+
this.statement = !object;
|
|
270
|
+
}
|
|
271
|
+
this.operand = true;
|
|
272
|
+
} else if (code === 41 || code === 93 || code === 125) {
|
|
273
|
+
const expected = code === 41 ? 40 : code === 93 ? 91 : 123;
|
|
274
|
+
const frame = this.brackets?.pop();
|
|
275
|
+
matched = frame !== void 0 && (frame & bracketMask) === expected;
|
|
276
|
+
if (frame !== void 0 && frame & functionParameters) {
|
|
277
|
+
const parameters = this.headers?.[this.headers.length - 1];
|
|
278
|
+
if (parameters?.kind === "function" && parameters.phase === "parameters" && parameters.depth === this.depth) parameters.phase = "body";
|
|
279
|
+
this.operand = this.statement = false;
|
|
280
|
+
} else {
|
|
281
|
+
this.operand = frame === void 0 ? code === 125 : Boolean(frame & expressionAfter);
|
|
282
|
+
this.statement = this.operand;
|
|
283
|
+
}
|
|
284
|
+
} else {
|
|
285
|
+
if (currentHeader && currentHeader.phase !== "parameters") {
|
|
286
|
+
if (code === 60) currentHeader.angles++;
|
|
287
|
+
else if (code === 62 && this.previousCode !== 61 && currentHeader.angles) currentHeader.angles--;
|
|
288
|
+
else if (code === 58 && currentHeader.phase === "body") currentHeader.typed = true;
|
|
289
|
+
else if (!currentHeader.angles && currentHeader.phase === "head" && (code === 58 || code === 61 || code === 44 || code === 59)) this.headers.pop();
|
|
290
|
+
}
|
|
291
|
+
this.operand = code !== 46;
|
|
292
|
+
if (code === 59) {
|
|
293
|
+
const frame = this.brackets?.[this.brackets.length - 1];
|
|
294
|
+
this.statement = frame === void 0 ? this.rootStatement : Boolean(frame & statementBody);
|
|
295
|
+
} else if (code === 58) {
|
|
296
|
+
if (this.questions?.[this.questions.length - 1] === this.depth) {
|
|
297
|
+
this.questions.pop();
|
|
298
|
+
this.statement = false;
|
|
299
|
+
} else if (this.cases?.[this.cases.length - 1] === this.depth) {
|
|
300
|
+
this.cases.pop();
|
|
301
|
+
this.statement = true;
|
|
302
|
+
} else this.statement = this.label;
|
|
303
|
+
} else {
|
|
304
|
+
this.statement = false;
|
|
305
|
+
if (code === 63 && next !== 46 && next !== 63 && this.previousCode !== 63) (this.questions ??= []).push(this.depth);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return matched;
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
// src/expression.ts
|
|
312
|
+
const fastInterpolation = /\{\{[^{}[\]()"'`\x2F&]*\}\}/y;
|
|
313
|
+
function encodedQuoteAt(source, start) {
|
|
314
|
+
if (source.charCodeAt(start) !== 38) return;
|
|
315
|
+
const initial = source.charCodeAt(start + 1) | 32;
|
|
316
|
+
if (initial !== 35 && initial !== 97 && initial !== 113) return;
|
|
317
|
+
const limit = Math.min(source.length, start + 16);
|
|
318
|
+
let end = start + 1;
|
|
319
|
+
while (end < limit && source.charCodeAt(end) !== 59) end++;
|
|
320
|
+
if (end === limit) return;
|
|
321
|
+
const name = source.slice(start + 1, end).toLowerCase();
|
|
322
|
+
let code = name === "quot" ? 34 : name === "apos" ? 39 : 0;
|
|
323
|
+
if (!code && name.charCodeAt(0) === 35) {
|
|
324
|
+
const hexadecimal = name.charCodeAt(1) === 120;
|
|
325
|
+
const begin = hexadecimal ? 2 : 1;
|
|
326
|
+
if (begin === name.length) return;
|
|
327
|
+
for (let index = begin; index < name.length; index++) {
|
|
328
|
+
const digit = name.charCodeAt(index);
|
|
329
|
+
if (!(digit >= 48 && digit <= 57 || hexadecimal && digit >= 97 && digit <= 102)) return;
|
|
330
|
+
}
|
|
331
|
+
code = Number.parseInt(name.slice(begin), hexadecimal ? 16 : 10);
|
|
332
|
+
}
|
|
333
|
+
return code === 34 || code === 39 ? {
|
|
334
|
+
code,
|
|
335
|
+
end: end + 1
|
|
336
|
+
} : void 0;
|
|
337
|
+
}
|
|
338
|
+
function quotedEnd$1(source, start, syntax, limit, entityQuotes) {
|
|
339
|
+
const opening = encodedQuoteAt(source, start);
|
|
340
|
+
const quote = opening?.code ?? source.charCodeAt(start);
|
|
341
|
+
const entityLayer = syntax === "xml" || Boolean(opening);
|
|
342
|
+
for (let index = opening?.end ?? start + 1; index < limit;) {
|
|
343
|
+
const entity = entityLayer ? encodedQuoteAt(source, index) : void 0;
|
|
344
|
+
const code = entity?.code ?? source.charCodeAt(index);
|
|
345
|
+
if (code === 92) {
|
|
346
|
+
const slashStart = index;
|
|
347
|
+
while (source.charCodeAt(index) === 92) index++;
|
|
348
|
+
const delimiter = entityLayer ? encodedQuoteAt(source, index) : void 0;
|
|
349
|
+
if ((index - slashStart) % 2 !== 0) index = delimiter?.end ?? index + 1;
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
if (quote === 96 && code === 36 && source.charCodeAt(index + 1) === 123) index = scanExpression(source, index + 1, false, syntax, limit, entityQuotes);
|
|
353
|
+
else if (code === quote) return entity?.end ?? index + 1;
|
|
354
|
+
else index = entity?.end ?? index + 1;
|
|
355
|
+
}
|
|
356
|
+
throw new TemplateLexicalError(start, "未闭合的表达式字符串");
|
|
357
|
+
}
|
|
358
|
+
function regexpEnd$1(source, start, limit) {
|
|
359
|
+
let characterClass = false;
|
|
360
|
+
for (let index = start + 1; index < limit; index++) {
|
|
361
|
+
const code = source.charCodeAt(index);
|
|
362
|
+
if (code === 92) index++;
|
|
363
|
+
else if (code === 91) characterClass = true;
|
|
364
|
+
else if (code === 93) characterClass = false;
|
|
365
|
+
else if (code === 47 && !characterClass) {
|
|
366
|
+
index++;
|
|
367
|
+
while (isWordCode(source.charCodeAt(index))) index++;
|
|
368
|
+
return index;
|
|
369
|
+
} else if (code === 10 || code === 13) break;
|
|
370
|
+
}
|
|
371
|
+
throw new TemplateLexicalError(start, "未闭合的表达式正则");
|
|
372
|
+
}
|
|
373
|
+
function scanExpression(source, start, mustache, syntax, limit = source.length, entityQuotes = true) {
|
|
374
|
+
if (mustache) {
|
|
375
|
+
fastInterpolation.lastIndex = start;
|
|
376
|
+
const fast = fastInterpolation.exec(source);
|
|
377
|
+
if (fast && fast.index + fast[0].length <= limit) return fast.index + fast[0].length;
|
|
378
|
+
}
|
|
379
|
+
const context = new TokenContext(true);
|
|
380
|
+
let mismatch;
|
|
381
|
+
for (let index = start + (mustache ? 2 : 1); index < limit;) {
|
|
382
|
+
const code = source.charCodeAt(index);
|
|
383
|
+
const next = source.charCodeAt(index + 1);
|
|
384
|
+
if (code === 125 && (!mustache ? context.depth === 0 : next === 125 && context.bracket !== 123)) {
|
|
385
|
+
const end = index + (mustache ? 2 : 1);
|
|
386
|
+
if (mismatch !== void 0 || context.depth > 0) throw new TemplateLexicalError(mismatch ?? index, "不匹配的表达式括号", {
|
|
387
|
+
kind: "interpolation",
|
|
388
|
+
start,
|
|
389
|
+
end
|
|
390
|
+
});
|
|
391
|
+
return end;
|
|
392
|
+
}
|
|
393
|
+
if (isSpaceCode(code)) {
|
|
394
|
+
if (code === 10 || code === 13 || code === 8232 || code === 8233) context.lineBreak();
|
|
395
|
+
index++;
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
if (entityQuotes && code === 38 && encodedQuoteAt(source, index)) {
|
|
399
|
+
index = quotedEnd$1(source, index, syntax, limit, entityQuotes);
|
|
400
|
+
context.value();
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
if (code === 34 || code === 39 || code === 96) {
|
|
404
|
+
index = quotedEnd$1(source, index, syntax, limit, entityQuotes);
|
|
405
|
+
context.value();
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
if (code === 47 && next === 42) {
|
|
409
|
+
const end = source.indexOf("*/", index + 2);
|
|
410
|
+
if (end < 0 || end >= limit) throw new TemplateLexicalError(index, "未闭合的表达式注释");
|
|
411
|
+
context.lineBreak(source, index + 2, end);
|
|
412
|
+
index = end + 2;
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
if (code === 47 && next === 47) {
|
|
416
|
+
const end = findLineEnd(source, index + 2);
|
|
417
|
+
if (end < 0 || end >= limit) throw new TemplateLexicalError(index, "未闭合的插值表达式");
|
|
418
|
+
context.lineBreak();
|
|
419
|
+
index = end + 1;
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
if (code === 47 && context.operand) {
|
|
423
|
+
index = regexpEnd$1(source, index, limit);
|
|
424
|
+
context.value();
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (isWordCode(code)) {
|
|
428
|
+
const begin = index++;
|
|
429
|
+
while (isWordCode(source.charCodeAt(index))) index++;
|
|
430
|
+
context.word(source, begin, index);
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
if ((code === 43 || code === 45) && code === next) {
|
|
434
|
+
context.increment(code);
|
|
435
|
+
index += 2;
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
if (!context.punctuation(code, next)) {
|
|
439
|
+
if (!mustache) throw new TemplateLexicalError(index, "不匹配的表达式括号");
|
|
440
|
+
mismatch ??= index;
|
|
441
|
+
}
|
|
442
|
+
index++;
|
|
443
|
+
}
|
|
444
|
+
if (mismatch !== void 0) throw new TemplateLexicalError(mismatch, "不匹配的表达式括号");
|
|
445
|
+
throw new TemplateLexicalError(start, "未闭合的插值表达式");
|
|
446
|
+
}
|
|
447
|
+
/** 定位插值末尾;跳过字符串、注释、正则和嵌套括号,保留原文编码。 */
|
|
448
|
+
function expressionEnd(source, start, mustache = true, syntax = "xml", outerQuote) {
|
|
449
|
+
if (syntax === "legacy" && outerQuote) {
|
|
450
|
+
const boundary = legacyAttributeEnd(source, start, outerQuote);
|
|
451
|
+
const view = legacyAttributeSource(source, start, boundary < 0 ? source.length : boundary);
|
|
452
|
+
try {
|
|
453
|
+
return attributeSourcePosition(view, scanExpression(view.source, view.start, mustache, syntax, view.end, false));
|
|
454
|
+
} catch (error) {
|
|
455
|
+
attributeSourceError(view, error);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return scanExpression(source, start, mustache, syntax);
|
|
459
|
+
}
|
|
460
|
+
function collectInterpolations(source, syntax, entityQuotes = true, begin = 0, limit = source.length) {
|
|
461
|
+
const result = [];
|
|
462
|
+
let start = source.indexOf("{{", begin);
|
|
463
|
+
while (start >= 0 && start < limit) {
|
|
464
|
+
const end = scanExpression(source, start, true, syntax, limit, entityQuotes);
|
|
465
|
+
result.push({
|
|
466
|
+
start,
|
|
467
|
+
end
|
|
468
|
+
});
|
|
469
|
+
start = source.indexOf("{{", end);
|
|
470
|
+
}
|
|
471
|
+
return result;
|
|
472
|
+
}
|
|
473
|
+
/** 返回完整插值的半开范围,保留属性层的编码规则。 */
|
|
474
|
+
function interpolations(source, syntax = "xml", outerQuote) {
|
|
475
|
+
if (syntax === "legacy" && outerQuote) {
|
|
476
|
+
const boundary = legacyAttributeEnd(source, 0, outerQuote);
|
|
477
|
+
const view = legacyAttributeSource(source, 0, boundary < 0 ? source.length : boundary);
|
|
478
|
+
try {
|
|
479
|
+
return collectInterpolations(view.source, syntax, false, view.start, view.end).map((range) => ({
|
|
480
|
+
start: attributeSourcePosition(view, range.start),
|
|
481
|
+
end: attributeSourcePosition(view, range.end)
|
|
482
|
+
}));
|
|
483
|
+
} catch (error) {
|
|
484
|
+
attributeSourceError(view, error);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
return collectInterpolations(source, syntax);
|
|
488
|
+
}
|
|
489
|
+
/** 已解开宿主层的绑定不将实体样文本解释为 JavaScript 引号。 */
|
|
490
|
+
function legacyAttributeInterpolations(source) {
|
|
491
|
+
return collectInterpolations(source, "legacy", false);
|
|
492
|
+
}
|
|
493
|
+
/** 宿主先确定属性边界,绑定词法只读取解开一层后的逻辑字符。 */
|
|
494
|
+
function validateLegacyAttribute(source, start, end) {
|
|
495
|
+
const view = legacyAttributeSource(source, start, end);
|
|
496
|
+
let begin = view.source.indexOf("{{", view.start);
|
|
497
|
+
try {
|
|
498
|
+
while (begin >= 0 && begin < view.end) {
|
|
499
|
+
const boundary = scanExpression(view.source, begin, true, "legacy", view.end, false);
|
|
500
|
+
begin = view.source.indexOf("{{", boundary);
|
|
501
|
+
}
|
|
502
|
+
} catch (error) {
|
|
503
|
+
attributeSourceError(view, error);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
// src/script.ts
|
|
507
|
+
function quotedEnd(source, start, quote) {
|
|
508
|
+
const end = legacyAttributeEnd(source, start + 1, quote === 34 ? "\"" : "'");
|
|
509
|
+
return end < 0 ? source.length : end + 1;
|
|
510
|
+
}
|
|
511
|
+
function regexpEnd(source, start) {
|
|
512
|
+
let characterClass = false;
|
|
513
|
+
for (let index = start + 1; index < source.length; index++) {
|
|
514
|
+
const code = source.charCodeAt(index);
|
|
515
|
+
if (code === 92) index++;
|
|
516
|
+
else if (code === 91) characterClass = true;
|
|
517
|
+
else if (code === 93) characterClass = false;
|
|
518
|
+
else if (code === 47 && !characterClass) {
|
|
519
|
+
index++;
|
|
520
|
+
while (isWordCode(source.charCodeAt(index))) index++;
|
|
521
|
+
return index;
|
|
522
|
+
} else if (code === 10 || code === 13) return index;
|
|
523
|
+
}
|
|
524
|
+
return source.length;
|
|
525
|
+
}
|
|
526
|
+
function closingTagAt(source, start, tagName) {
|
|
527
|
+
if (!tagName || source.charCodeAt(start) !== 60 || source.charCodeAt(start + 1) !== 47) return;
|
|
528
|
+
const begin = start + 2;
|
|
529
|
+
let cursor = begin;
|
|
530
|
+
while (isNameCode(source.charCodeAt(cursor))) cursor++;
|
|
531
|
+
if (source.slice(begin, cursor).toLowerCase() !== tagName) return;
|
|
532
|
+
while (isSpaceCode(source.charCodeAt(cursor))) cursor++;
|
|
533
|
+
return source.charCodeAt(cursor) === 62 ? {
|
|
534
|
+
start,
|
|
535
|
+
end: cursor + 1
|
|
536
|
+
} : void 0;
|
|
537
|
+
}
|
|
538
|
+
function templateEnd(source, start) {
|
|
539
|
+
let index = start + 1;
|
|
540
|
+
while (index < source.length) {
|
|
541
|
+
const code = source.charCodeAt(index);
|
|
542
|
+
if (code === 92) index += 2;
|
|
543
|
+
else if (code === 96) return index + 1;
|
|
544
|
+
else if (code === 36 && source.charCodeAt(index + 1) === 123) index = scanCode(source, index + 2, void 0, true).end;
|
|
545
|
+
else index++;
|
|
546
|
+
}
|
|
547
|
+
return index;
|
|
548
|
+
}
|
|
549
|
+
function scanCode(source, start, tagName, interpolation = false) {
|
|
550
|
+
const commentClosings = [];
|
|
551
|
+
const context = new TokenContext(interpolation);
|
|
552
|
+
let index = start;
|
|
553
|
+
while (index < source.length) {
|
|
554
|
+
const code = source.charCodeAt(index);
|
|
555
|
+
const next = source.charCodeAt(index + 1);
|
|
556
|
+
if (code === 60 && next === 47) {
|
|
557
|
+
const closingTag = closingTagAt(source, index, tagName);
|
|
558
|
+
if (closingTag) return {
|
|
559
|
+
closingTag,
|
|
560
|
+
commentClosings,
|
|
561
|
+
end: closingTag.end
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
if (isSpaceCode(code)) {
|
|
565
|
+
if (code === 10 || code === 13 || code === 8232 || code === 8233) context.lineBreak();
|
|
566
|
+
index++;
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
if (code === 34 || code === 39) {
|
|
570
|
+
index = quotedEnd(source, index, code);
|
|
571
|
+
context.value(code);
|
|
572
|
+
} else if (code === 96) {
|
|
573
|
+
index = templateEnd(source, index);
|
|
574
|
+
context.value(code);
|
|
575
|
+
} else if (code === 47 && (next === 47 || next === 42)) {
|
|
576
|
+
const block = next === 42;
|
|
577
|
+
const end = block ? source.indexOf("*/", index + 2) : findLineEnd(source, index + 2);
|
|
578
|
+
const limit = end < 0 ? source.length : end;
|
|
579
|
+
if (tagName) {
|
|
580
|
+
let candidate = source.indexOf("</", index + 2);
|
|
581
|
+
while (candidate >= 0 && candidate < limit) {
|
|
582
|
+
const closing = closingTagAt(source, candidate, tagName);
|
|
583
|
+
if (closing) commentClosings.push({
|
|
584
|
+
...closing,
|
|
585
|
+
block
|
|
586
|
+
});
|
|
587
|
+
candidate = source.indexOf("</", candidate + 2);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
if (end < 0 && block) return {
|
|
591
|
+
trailingComment: index,
|
|
592
|
+
commentClosings,
|
|
593
|
+
end: source.length
|
|
594
|
+
};
|
|
595
|
+
if (block) context.lineBreak(source, index + 2, limit);
|
|
596
|
+
else if (end >= 0) context.lineBreak();
|
|
597
|
+
index = limit + (end < 0 ? 0 : block ? 2 : 1);
|
|
598
|
+
continue;
|
|
599
|
+
} else if (code === 47 && context.operand) {
|
|
600
|
+
index = regexpEnd(source, index);
|
|
601
|
+
context.value(code);
|
|
602
|
+
} else if (isWordCode(code)) {
|
|
603
|
+
const begin = index++;
|
|
604
|
+
while (isWordCode(source.charCodeAt(index))) index++;
|
|
605
|
+
context.word(source, begin, index);
|
|
606
|
+
continue;
|
|
607
|
+
} else {
|
|
608
|
+
if (interpolation && code === 125 && context.depth === 0) return { end: index + 1 };
|
|
609
|
+
if ((code === 43 || code === 45) && next === code) {
|
|
610
|
+
context.increment(code);
|
|
611
|
+
index++;
|
|
612
|
+
} else context.punctuation(code, next);
|
|
613
|
+
index++;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
return {
|
|
617
|
+
end: index,
|
|
618
|
+
commentClosings
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
/** 只收集词法候选,不加载 JS AST;歧义由调用方注入的 resolver 处理。 */
|
|
622
|
+
function scanScriptBoundaryCandidates(source, start, tagName) {
|
|
623
|
+
return scanCode(source, start, tagName.toLowerCase());
|
|
624
|
+
}
|
|
625
|
+
/** 返回明确位于脚本代码层的结束标签,不擅自采用注释内的候选。 */
|
|
626
|
+
function findScriptClosingTag(source, start, tagName) {
|
|
627
|
+
return scanScriptBoundaryCandidates(source, start, tagName).closingTag;
|
|
628
|
+
}
|
|
629
|
+
/** 查找词法确认的未闭合尾部块注释。 */
|
|
630
|
+
function findTrailingScriptComment(source) {
|
|
631
|
+
return scanCode(source, 0).trailingComment;
|
|
632
|
+
}
|
|
633
|
+
// src/tag.ts
|
|
634
|
+
const emptyAttributes = Object.freeze([]);
|
|
635
|
+
const doubleQuotedBoundary = /"|\{\{/g;
|
|
636
|
+
const singleQuotedBoundary = /'|\{\{/g;
|
|
637
|
+
/** 读取标签与属性范围;属性值始终保留未解码的原文。 */
|
|
638
|
+
function readTag(source, start, syntax = "xml", collectAttributes = true) {
|
|
639
|
+
return scanTag(source, start, syntax, collectAttributes);
|
|
640
|
+
}
|
|
641
|
+
/** 文档扫描共享单调的绑定候选,避免逐属性搜索相同后缀。 */
|
|
642
|
+
function scanTag(source, start, syntax, collectAttributes, bindings) {
|
|
643
|
+
let cursor = start + 1;
|
|
644
|
+
const closing = source.charCodeAt(cursor) === 47;
|
|
645
|
+
if (closing) cursor++;
|
|
646
|
+
const nameStart = cursor;
|
|
647
|
+
while (isNameCode(source.charCodeAt(cursor))) cursor++;
|
|
648
|
+
const nameEnd = cursor;
|
|
649
|
+
if (nameStart === nameEnd) throw new TemplateLexicalError(start, "标签缺少名称");
|
|
650
|
+
let attributes;
|
|
651
|
+
let quotedAttribute = false;
|
|
652
|
+
while (cursor < source.length) {
|
|
653
|
+
const whitespaceStart = cursor;
|
|
654
|
+
cursor = skipSpace(source, cursor);
|
|
655
|
+
const code = source.charCodeAt(cursor);
|
|
656
|
+
const selfClosing = code === 47 && source.charCodeAt(cursor + 1) === 62;
|
|
657
|
+
if (code === 62 || selfClosing) {
|
|
658
|
+
if (closing && selfClosing) throw new TemplateLexicalError(cursor, "结束标签不能自闭合");
|
|
659
|
+
return {
|
|
660
|
+
kind: "tag",
|
|
661
|
+
start,
|
|
662
|
+
end: cursor + (selfClosing ? 2 : 1),
|
|
663
|
+
name: source.slice(nameStart, nameEnd),
|
|
664
|
+
nameStart,
|
|
665
|
+
nameEnd,
|
|
666
|
+
closing,
|
|
667
|
+
selfClosing,
|
|
668
|
+
attributes: attributes ?? emptyAttributes,
|
|
669
|
+
parent: void 0,
|
|
670
|
+
matching: void 0
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
if (closing) throw new TemplateLexicalError(cursor, "结束标签不能包含属性");
|
|
674
|
+
if (whitespaceStart === cursor && !quotedAttribute) throw new TemplateLexicalError(cursor, "属性前缺少空白分隔符");
|
|
675
|
+
quotedAttribute = false;
|
|
676
|
+
const attributeStart = cursor;
|
|
677
|
+
if (source.charCodeAt(cursor) === 123 && source.charCodeAt(cursor + 1) === 123) cursor = expressionEnd(source, cursor, true, syntax);
|
|
678
|
+
else while (cursor < source.length) {
|
|
679
|
+
const attributeCode = source.charCodeAt(cursor);
|
|
680
|
+
if (isSpaceCode(attributeCode) || attributeCode === 61 || attributeCode === 47 || attributeCode === 62) break;
|
|
681
|
+
if (attributeCode === 60 || attributeCode === 34 || attributeCode === 39) throw new TemplateLexicalError(cursor, "无效的属性名称");
|
|
682
|
+
cursor++;
|
|
683
|
+
}
|
|
684
|
+
const attributeNameEnd = cursor;
|
|
685
|
+
if (cursor === attributeStart) throw new TemplateLexicalError(cursor, "属性缺少名称");
|
|
686
|
+
const name = collectAttributes ? source.slice(attributeStart, attributeNameEnd) : "";
|
|
687
|
+
cursor = skipSpace(source, cursor);
|
|
688
|
+
if (source.charCodeAt(cursor) !== 61) {
|
|
689
|
+
if (collectAttributes) (attributes ??= []).push({
|
|
690
|
+
name,
|
|
691
|
+
nameStart: attributeStart,
|
|
692
|
+
nameEnd: attributeNameEnd,
|
|
693
|
+
start: attributeStart,
|
|
694
|
+
end: attributeNameEnd
|
|
695
|
+
});
|
|
696
|
+
cursor = attributeNameEnd;
|
|
697
|
+
continue;
|
|
698
|
+
}
|
|
699
|
+
cursor = skipSpace(source, cursor + 1);
|
|
700
|
+
const rawStart = cursor;
|
|
701
|
+
const quoteCode = source.charCodeAt(cursor);
|
|
702
|
+
const quote = quoteCode === 34 ? "\"" : quoteCode === 39 ? "'" : void 0;
|
|
703
|
+
if (quote) cursor++;
|
|
704
|
+
const valueStart = cursor;
|
|
705
|
+
let closed = !quote;
|
|
706
|
+
if (quote && syntax === "legacy") {
|
|
707
|
+
const end = legacyAttributeEnd(source, valueStart, quote);
|
|
708
|
+
if (end >= 0) {
|
|
709
|
+
validateLegacyAttribute(source, valueStart, end);
|
|
710
|
+
cursor = end;
|
|
711
|
+
closed = true;
|
|
712
|
+
}
|
|
713
|
+
} else if (quote && bindings) {
|
|
714
|
+
let end = source.indexOf(quote, cursor);
|
|
715
|
+
while (true) {
|
|
716
|
+
if (bindings.next < cursor) {
|
|
717
|
+
const found = source.indexOf("{{", cursor);
|
|
718
|
+
bindings.next = found < 0 ? Number.POSITIVE_INFINITY : found;
|
|
719
|
+
}
|
|
720
|
+
if (end >= 0 && end < bindings.next) {
|
|
721
|
+
cursor = end;
|
|
722
|
+
closed = true;
|
|
723
|
+
break;
|
|
724
|
+
}
|
|
725
|
+
if (bindings.next === Number.POSITIVE_INFINITY) break;
|
|
726
|
+
cursor = expressionEnd(source, bindings.next, true, syntax, quote);
|
|
727
|
+
if (end >= 0 && end < cursor) end = source.indexOf(quote, cursor);
|
|
728
|
+
}
|
|
729
|
+
} else if (quote) {
|
|
730
|
+
const boundary = quoteCode === 34 ? doubleQuotedBoundary : singleQuotedBoundary;
|
|
731
|
+
boundary.lastIndex = cursor;
|
|
732
|
+
while (boundary.test(source)) {
|
|
733
|
+
if (source.charCodeAt(boundary.lastIndex - 1) === quoteCode) {
|
|
734
|
+
cursor = boundary.lastIndex - 1;
|
|
735
|
+
closed = true;
|
|
736
|
+
break;
|
|
737
|
+
}
|
|
738
|
+
cursor = expressionEnd(source, boundary.lastIndex - 2, true, syntax, quote);
|
|
739
|
+
boundary.lastIndex = cursor;
|
|
740
|
+
}
|
|
741
|
+
} else while (cursor < source.length) {
|
|
742
|
+
const valueCode = source.charCodeAt(cursor);
|
|
743
|
+
if (valueCode === 123 && source.charCodeAt(cursor + 1) === 123) cursor = expressionEnd(source, cursor, true, syntax, quote);
|
|
744
|
+
else if (quote ? valueCode === quoteCode : isSpaceCode(valueCode) || valueCode === 62 || valueCode === 47 && source.charCodeAt(cursor + 1) === 62) {
|
|
745
|
+
closed = true;
|
|
746
|
+
break;
|
|
747
|
+
} else cursor++;
|
|
748
|
+
}
|
|
749
|
+
if (!closed || !quote && cursor === valueStart) throw new TemplateLexicalError(attributeStart, "未闭合或为空的属性值");
|
|
750
|
+
const valueEnd = cursor;
|
|
751
|
+
if (quote) {
|
|
752
|
+
cursor++;
|
|
753
|
+
quotedAttribute = true;
|
|
754
|
+
}
|
|
755
|
+
if (collectAttributes) {
|
|
756
|
+
const value = source.slice(valueStart, valueEnd);
|
|
757
|
+
(attributes ??= []).push({
|
|
758
|
+
name,
|
|
759
|
+
nameStart: attributeStart,
|
|
760
|
+
nameEnd: attributeNameEnd,
|
|
761
|
+
start: attributeStart,
|
|
762
|
+
end: cursor,
|
|
763
|
+
valueStart,
|
|
764
|
+
valueEnd,
|
|
765
|
+
value,
|
|
766
|
+
rawValue: quote ? source.slice(rawStart, cursor) : value,
|
|
767
|
+
quote
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
throw new TemplateLexicalError(start, "未闭合的标签");
|
|
772
|
+
}
|
|
773
|
+
// src/scannerCore.ts
|
|
774
|
+
function opaqueEnd(source, start) {
|
|
775
|
+
const delimiter = source.startsWith("<!--", start) ? "-->" : source.startsWith("<![CDATA[", start) ? "]]>" : source.startsWith("<?", start) ? "?>" : void 0;
|
|
776
|
+
if (delimiter) {
|
|
777
|
+
const end = source.indexOf(delimiter, start + 2);
|
|
778
|
+
if (end < 0) throw new TemplateLexicalError(start, "未闭合的注释、CDATA 或处理指令");
|
|
779
|
+
return end + delimiter.length;
|
|
780
|
+
}
|
|
781
|
+
let quote = 0;
|
|
782
|
+
let depth = 0;
|
|
783
|
+
for (let index = start + 2; index < source.length; index++) {
|
|
784
|
+
const code = source.charCodeAt(index);
|
|
785
|
+
if (quote) {
|
|
786
|
+
if (code === quote) quote = 0;
|
|
787
|
+
} else if (code === 34 || code === 39) quote = code;
|
|
788
|
+
else if (code === 91) depth++;
|
|
789
|
+
else if (code === 93) depth--;
|
|
790
|
+
else if (code === 62 && depth === 0) return index + 1;
|
|
791
|
+
}
|
|
792
|
+
throw new TemplateLexicalError(start, "未闭合的模板声明");
|
|
793
|
+
}
|
|
794
|
+
function scanCompiledDocument(source, state, resolveScriptBoundary, buildTree, commentsOnly = false) {
|
|
795
|
+
if (typeof source !== "string") throw new TypeError("Expected template source.");
|
|
796
|
+
const { syntax, rawTagPattern, rawTagInitials } = state;
|
|
797
|
+
const tokens = [];
|
|
798
|
+
const diagnostics = [];
|
|
799
|
+
const stack = [];
|
|
800
|
+
const children = buildTree ? [] : void 0;
|
|
801
|
+
const elements = buildTree ? [] : void 0;
|
|
802
|
+
const scanEnd = commentsOnly ? source.lastIndexOf("<!--") + 1 : source.length;
|
|
803
|
+
let cursor = 0;
|
|
804
|
+
let textStart = 0;
|
|
805
|
+
let nextTag = -1;
|
|
806
|
+
const bindings = { next: -1 };
|
|
807
|
+
function appendTag(token) {
|
|
808
|
+
if (commentsOnly) return;
|
|
809
|
+
const mutable = token;
|
|
810
|
+
if (token.closing) {
|
|
811
|
+
const openIndex = stack[stack.length - 1];
|
|
812
|
+
const open = openIndex === void 0 ? void 0 : tokens[openIndex];
|
|
813
|
+
if (!open || open.name !== token.name) diagnostics.push({
|
|
814
|
+
code: "template/mismatched-tag",
|
|
815
|
+
message: `结束标签 </${token.name}> 与开始标签不匹配`,
|
|
816
|
+
severity: "error",
|
|
817
|
+
start: token.start,
|
|
818
|
+
end: token.end
|
|
819
|
+
});
|
|
820
|
+
else {
|
|
821
|
+
stack.pop();
|
|
822
|
+
mutable.matching = openIndex;
|
|
823
|
+
mutable.parent = open.parent;
|
|
824
|
+
open.matching = tokens.length;
|
|
825
|
+
if (elements) {
|
|
826
|
+
const element = elements.pop();
|
|
827
|
+
element.closing = token;
|
|
828
|
+
element.end = token.end;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
} else {
|
|
832
|
+
mutable.parent = stack[stack.length - 1];
|
|
833
|
+
if (elements) {
|
|
834
|
+
const element = {
|
|
835
|
+
kind: "element",
|
|
836
|
+
name: token.name,
|
|
837
|
+
attributes: token.attributes,
|
|
838
|
+
children: [],
|
|
839
|
+
opening: token,
|
|
840
|
+
closing: void 0,
|
|
841
|
+
selfClosing: token.selfClosing,
|
|
842
|
+
start: token.start,
|
|
843
|
+
end: token.end
|
|
844
|
+
};
|
|
845
|
+
(elements.length ? elements[elements.length - 1].children : children).push(element);
|
|
846
|
+
if (!token.selfClosing) elements.push(element);
|
|
847
|
+
}
|
|
848
|
+
if (!token.selfClosing) stack.push(tokens.length);
|
|
849
|
+
}
|
|
850
|
+
tokens.push(token);
|
|
851
|
+
}
|
|
852
|
+
function appendRaw(token) {
|
|
853
|
+
if (commentsOnly) {
|
|
854
|
+
if (token.kind === "comment") tokens.push(token);
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
if (elements) (elements.length ? elements[elements.length - 1].children : children).push({
|
|
858
|
+
kind: token.kind,
|
|
859
|
+
start: token.start,
|
|
860
|
+
end: token.end,
|
|
861
|
+
tagName: token.tagName,
|
|
862
|
+
data: source.slice(token.start, token.end)
|
|
863
|
+
});
|
|
864
|
+
tokens.push(token);
|
|
865
|
+
}
|
|
866
|
+
try {
|
|
867
|
+
while (cursor < scanEnd) {
|
|
868
|
+
if (!commentsOnly) {
|
|
869
|
+
if (nextTag < cursor) {
|
|
870
|
+
const found = source.indexOf("<", cursor);
|
|
871
|
+
nextTag = found < 0 ? Number.POSITIVE_INFINITY : found;
|
|
872
|
+
}
|
|
873
|
+
if (bindings.next < cursor) {
|
|
874
|
+
const found = source.indexOf("{{", cursor);
|
|
875
|
+
bindings.next = found < 0 ? Number.POSITIVE_INFINITY : found;
|
|
876
|
+
}
|
|
877
|
+
if (bindings.next < nextTag) {
|
|
878
|
+
cursor = expressionEnd(source, bindings.next, true, syntax);
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
if (nextTag >= scanEnd) {
|
|
882
|
+
cursor = scanEnd;
|
|
883
|
+
break;
|
|
884
|
+
}
|
|
885
|
+
cursor = nextTag;
|
|
886
|
+
}
|
|
887
|
+
const code = source.charCodeAt(cursor);
|
|
888
|
+
if (code === 123 && source.charCodeAt(cursor + 1) === 123) {
|
|
889
|
+
cursor = expressionEnd(source, cursor, true, syntax);
|
|
890
|
+
continue;
|
|
891
|
+
}
|
|
892
|
+
const next = source.charCodeAt(cursor + 1);
|
|
893
|
+
if (code !== 60 || !isTagStartCode(next) && next !== 33 && next !== 47 && next !== 63) {
|
|
894
|
+
cursor++;
|
|
895
|
+
continue;
|
|
896
|
+
}
|
|
897
|
+
if (cursor > textStart) appendRaw({
|
|
898
|
+
kind: "text",
|
|
899
|
+
start: textStart,
|
|
900
|
+
end: cursor
|
|
901
|
+
});
|
|
902
|
+
if (next === 33 || next === 63) {
|
|
903
|
+
const end = opaqueEnd(source, cursor);
|
|
904
|
+
appendRaw({
|
|
905
|
+
kind: source.startsWith("<!--", cursor) ? "comment" : source.startsWith("<![CDATA[", cursor) ? "cdata" : "declaration",
|
|
906
|
+
start: cursor,
|
|
907
|
+
end
|
|
908
|
+
});
|
|
909
|
+
cursor = end;
|
|
910
|
+
} else {
|
|
911
|
+
const tag = scanTag(source, cursor, syntax, !commentsOnly, bindings);
|
|
912
|
+
appendTag(tag);
|
|
913
|
+
cursor = tag.end;
|
|
914
|
+
if (!tag.closing && !tag.selfClosing && (rawTagInitials & 1 << tag.name.charCodeAt(0)) !== 0 && rawTagPattern.test(tag.name)) {
|
|
915
|
+
const candidates = scanScriptBoundaryCandidates(source, cursor, tag.name);
|
|
916
|
+
const close = resolveScriptBoundary ? resolveScriptBoundary(source, cursor, tag.name, candidates) : candidates.closingTag;
|
|
917
|
+
if (!close) {
|
|
918
|
+
if (candidates.commentClosings?.length) throw new TemplateLexicalError(tag.start, `脚本标签 <${tag.name}> 的结束边界存在歧义,需要 resolveScriptBoundary`);
|
|
919
|
+
throw new TemplateLexicalError(tag.start, `未闭合的脚本标签 <${tag.name}>`);
|
|
920
|
+
}
|
|
921
|
+
if (!Number.isInteger(close.start) || !Number.isInteger(close.end) || close.start < cursor || close.end > source.length || close.end <= close.start) throw new TemplateLexicalError(tag.start, "脚本边界 resolver 返回了无效范围");
|
|
922
|
+
appendRaw({
|
|
923
|
+
kind: "script",
|
|
924
|
+
tagName: tag.name,
|
|
925
|
+
start: cursor,
|
|
926
|
+
end: close.start
|
|
927
|
+
});
|
|
928
|
+
const closing = scanTag(source, close.start, syntax, !commentsOnly, bindings);
|
|
929
|
+
if (!closing.closing || closing.end !== close.end || closing.name.toLowerCase() !== tag.name.toLowerCase()) throw new TemplateLexicalError(close.start, "脚本边界 resolver 返回的范围不是对应结束标签");
|
|
930
|
+
appendTag(closing);
|
|
931
|
+
cursor = closing.end;
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
textStart = cursor;
|
|
935
|
+
}
|
|
936
|
+
if (!commentsOnly && textStart < source.length) appendRaw({
|
|
937
|
+
kind: "text",
|
|
938
|
+
start: textStart,
|
|
939
|
+
end: source.length
|
|
940
|
+
});
|
|
941
|
+
for (const index of stack) {
|
|
942
|
+
const token = tokens[index];
|
|
943
|
+
diagnostics.push({
|
|
944
|
+
code: "template/unclosed-tag",
|
|
945
|
+
message: `标签 <${token.name}> 缺少结束标签`,
|
|
946
|
+
severity: "error",
|
|
947
|
+
start: token.start,
|
|
948
|
+
end: token.end
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
} catch (error) {
|
|
952
|
+
if (!(error instanceof TemplateLexicalError)) throw error;
|
|
953
|
+
diagnostics.push({
|
|
954
|
+
code: error.recovery ? "template/invalid-interpolation" : "template/invalid-syntax",
|
|
955
|
+
message: error.message,
|
|
956
|
+
severity: "error",
|
|
957
|
+
start: error.offset,
|
|
958
|
+
end: Math.min(source.length, error.offset + 1),
|
|
959
|
+
...error.recovery ? { recovery: error.recovery } : {}
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
return buildTree ? {
|
|
963
|
+
tokens,
|
|
964
|
+
diagnostics,
|
|
965
|
+
children,
|
|
966
|
+
syntax
|
|
967
|
+
} : {
|
|
968
|
+
tokens,
|
|
969
|
+
diagnostics
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
// src/documentContext.ts
|
|
973
|
+
const contexts = /* @__PURE__ */ new WeakMap();
|
|
974
|
+
/** 配置属于解析器,文档仅保存弱关联;编辑延续同一个不可变上下文。 */
|
|
975
|
+
function bindDocument(document, context) {
|
|
976
|
+
contexts.set(document, context);
|
|
977
|
+
return document;
|
|
978
|
+
}
|
|
979
|
+
function contextFor(document) {
|
|
980
|
+
return contexts.get(document);
|
|
981
|
+
}
|
|
982
|
+
function requireContext(document) {
|
|
983
|
+
const context = contexts.get(document);
|
|
984
|
+
if (!context) throw new TypeError("This template document has no parser context; parse its source with a selected parser before editing.");
|
|
985
|
+
return context;
|
|
986
|
+
}
|
|
987
|
+
/** 文档构造由所属上下文负责,核心仅创建通用字段并绑定完整结果。 */
|
|
988
|
+
function createDocument(source, fields, context) {
|
|
989
|
+
return bindDocument(context.createDocument ? context.createDocument(source, fields) : {
|
|
990
|
+
source,
|
|
991
|
+
syntax: fields.syntax,
|
|
992
|
+
tokens: fields.tokens,
|
|
993
|
+
diagnostics: fields.diagnostics,
|
|
994
|
+
children: fields.children
|
|
995
|
+
}, context);
|
|
996
|
+
}
|
|
997
|
+
function parseContext(source, context) {
|
|
998
|
+
return createDocument(source, scanCompiledDocument(source, context.state, context.resolveScriptBoundary, true), context);
|
|
999
|
+
}
|
|
1000
|
+
// src/decode.ts
|
|
1001
|
+
/** 解码不含外层引号的词法属性值;表达式内部保留 JS 字符串的转义层。 */
|
|
1002
|
+
function decodeAttribute(value, syntax = "legacy", decodeEntities = true, quote) {
|
|
1003
|
+
if (syntax !== "legacy") return decodeEntities && value.includes("&") ? decodeXML(value) : value;
|
|
1004
|
+
const decoded = quote && value.includes("\\") ? decodeLegacyAttributeLayer(value) : value;
|
|
1005
|
+
if (!decodeEntities || !decoded.includes("&")) return decoded;
|
|
1006
|
+
if (!decoded.includes("{{")) return decodeXML(decoded);
|
|
1007
|
+
let result = "";
|
|
1008
|
+
let offset = 0;
|
|
1009
|
+
for (const range of legacyAttributeInterpolations(decoded)) {
|
|
1010
|
+
result += decodeXML(decoded.slice(offset, range.start));
|
|
1011
|
+
result += decoded.slice(range.start, range.end);
|
|
1012
|
+
offset = range.end;
|
|
1013
|
+
}
|
|
1014
|
+
return result + decodeXML(decoded.slice(offset));
|
|
1015
|
+
}
|
|
1016
|
+
// src/encoding.ts
|
|
1017
|
+
/** 显式表达式只能占据一个完整绑定,不能提前结束后混入文本或模板结构。 */
|
|
1018
|
+
function assertExpressionBoundary(source, syntax, quote) {
|
|
1019
|
+
try {
|
|
1020
|
+
if (expressionEnd(source, 0, true, syntax, quote) !== source.length) throw new TypeError("Invalid WXML expression: expected a single complete interpolation.");
|
|
1021
|
+
} catch (error) {
|
|
1022
|
+
if (!(error instanceof TemplateLexicalError)) throw error;
|
|
1023
|
+
throw new TypeError(`Invalid WXML expression: ${error.message}`);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
function encodeAttributeLayer(value, syntax) {
|
|
1027
|
+
return syntax === "legacy" ? value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") : value.replace(/"/g, """);
|
|
1028
|
+
}
|
|
1029
|
+
function encodeEntities(value) {
|
|
1030
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
1031
|
+
}
|
|
1032
|
+
/** 字面量显式拆开绑定定界符,表达式则保留独立的输入语义。 */
|
|
1033
|
+
function encodeAttribute(value, syntax) {
|
|
1034
|
+
if (typeof value === "string") return encodeAttributeLayer(encodeEntities(value), syntax).replace(/\{\{/g, "{{'{'}}{{'{'}}");
|
|
1035
|
+
let expression;
|
|
1036
|
+
if (typeof value === "number" && Number.isFinite(value)) expression = String(value);
|
|
1037
|
+
else if (typeof value === "boolean") expression = String(value);
|
|
1038
|
+
else if (value && typeof value === "object" && typeof value.expression === "string" && value.expression.trim()) expression = value.expression;
|
|
1039
|
+
else throw new TypeError("Expected a string, finite number, boolean, or nonempty expression.");
|
|
1040
|
+
return `{{${syntax === "legacy" ? encodeAttributeLayer(expression, syntax) : encodeEntities(expression).replace(/"/g, """)}}}`;
|
|
1041
|
+
}
|
|
1042
|
+
/** 文本字面量不能生成标签或意外触发绑定。 */
|
|
1043
|
+
function encodeText(value) {
|
|
1044
|
+
if (typeof value === "string") return encodeEntities(value).replace(/\{\{/g, "{{'{'}}{{'{'}}");
|
|
1045
|
+
if (value && typeof value.expression === "string" && value.expression.trim()) return `{{${value.expression}}}`;
|
|
1046
|
+
throw new TypeError("Expected a string or nonempty expression.");
|
|
1047
|
+
}
|
|
1048
|
+
/** 只转换属性外层引号,不重复编码实体或将绑定当作字符串。 */
|
|
1049
|
+
function canonicalAttribute(attribute, syntax) {
|
|
1050
|
+
if (attribute.value === void 0) return attribute.name;
|
|
1051
|
+
let value = attribute.value;
|
|
1052
|
+
if (syntax === "xml") value = value.replace(/"/g, """);
|
|
1053
|
+
else if (attribute.quote !== "\"") value = encodeAttributeLayer(decodeAttribute(value, syntax, false, attribute.quote), syntax);
|
|
1054
|
+
return `${attribute.name}="${value}"`;
|
|
1055
|
+
}
|
|
1056
|
+
// src/edit/attributes.ts
|
|
1057
|
+
/** 已受编码器保护的字面量和标量直接组合语义属性,不重新扫描未改动的开始标签。 */
|
|
1058
|
+
function editedAttributes(node, state) {
|
|
1059
|
+
const changes = state.attributes;
|
|
1060
|
+
if (!changes?.size) return node.attributes;
|
|
1061
|
+
const attributes = [];
|
|
1062
|
+
const handled = /* @__PURE__ */ new Set();
|
|
1063
|
+
for (const attribute of node.attributes) {
|
|
1064
|
+
if (!changes.has(attribute.name)) {
|
|
1065
|
+
attributes.push(attribute);
|
|
1066
|
+
continue;
|
|
1067
|
+
}
|
|
1068
|
+
if (!handled.has(attribute.name)) {
|
|
1069
|
+
const value = changes.get(attribute.name);
|
|
1070
|
+
if (value !== void 0) attributes.push(value === null ? { name: attribute.name } : {
|
|
1071
|
+
name: attribute.name,
|
|
1072
|
+
value,
|
|
1073
|
+
quote: "\""
|
|
1074
|
+
});
|
|
1075
|
+
handled.add(attribute.name);
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
for (const [name, value] of changes) if (!handled.has(name) && value !== void 0) attributes.push(value === null ? { name } : {
|
|
1079
|
+
name,
|
|
1080
|
+
value,
|
|
1081
|
+
quote: "\""
|
|
1082
|
+
});
|
|
1083
|
+
return attributes;
|
|
1084
|
+
}
|
|
1085
|
+
// src/edit/output.ts
|
|
1086
|
+
function openingTag(session, node, state, expanded) {
|
|
1087
|
+
const { source } = session.document;
|
|
1088
|
+
for (const name of state?.unvalidatedAttributes ?? []) {
|
|
1089
|
+
const value = state?.attributes?.get(name);
|
|
1090
|
+
if (typeof value === "string") assertExpressionBoundary(value, session.document.syntax, "\"");
|
|
1091
|
+
}
|
|
1092
|
+
const edits = [];
|
|
1093
|
+
if (state?.name && state.name !== node.name) edits.push({
|
|
1094
|
+
start: node.opening.nameStart,
|
|
1095
|
+
end: node.opening.nameEnd,
|
|
1096
|
+
text: state.name
|
|
1097
|
+
});
|
|
1098
|
+
const handled = /* @__PURE__ */ new Set();
|
|
1099
|
+
const attributes = state?.attributes;
|
|
1100
|
+
for (const attribute of node.attributes) {
|
|
1101
|
+
if (!attributes?.has(attribute.name)) continue;
|
|
1102
|
+
const value = attributes.get(attribute.name);
|
|
1103
|
+
const text = handled.has(attribute.name) || value === void 0 ? "" : value === null ? attribute.name : `${attribute.name}="${value}"`;
|
|
1104
|
+
edits.push({
|
|
1105
|
+
start: attribute.start,
|
|
1106
|
+
end: attribute.end,
|
|
1107
|
+
text
|
|
1108
|
+
});
|
|
1109
|
+
handled.add(attribute.name);
|
|
1110
|
+
}
|
|
1111
|
+
let additions = "";
|
|
1112
|
+
for (const [name, value] of attributes ?? []) if (!handled.has(name) && value !== void 0) additions += ` ${value === null ? name : `${name}="${value}"`}`;
|
|
1113
|
+
const endStart = node.opening.end - (node.selfClosing ? 2 : 1);
|
|
1114
|
+
if (additions || expanded) edits.push({
|
|
1115
|
+
start: endStart,
|
|
1116
|
+
end: node.opening.end,
|
|
1117
|
+
text: `${additions}${node.selfClosing && !expanded ? "/>" : ">"}`
|
|
1118
|
+
});
|
|
1119
|
+
let cursor = node.opening.start;
|
|
1120
|
+
let output = "";
|
|
1121
|
+
for (const edit of edits.sort((a, b) => a.start - b.start)) {
|
|
1122
|
+
output += source.slice(cursor, edit.start) + edit.text;
|
|
1123
|
+
cursor = edit.end;
|
|
1124
|
+
}
|
|
1125
|
+
return output + source.slice(cursor, node.opening.end);
|
|
1126
|
+
}
|
|
1127
|
+
function childrenParts(source, children, start, end) {
|
|
1128
|
+
const parts = [];
|
|
1129
|
+
let cursor = start;
|
|
1130
|
+
for (const child of children) {
|
|
1131
|
+
if (child.start > cursor) parts.push(source.slice(cursor, child.start));
|
|
1132
|
+
parts.push(child);
|
|
1133
|
+
cursor = child.end;
|
|
1134
|
+
}
|
|
1135
|
+
if (cursor < end) parts.push(source.slice(cursor, end));
|
|
1136
|
+
return parts;
|
|
1137
|
+
}
|
|
1138
|
+
function containsEdit(starts, node) {
|
|
1139
|
+
let low = 0;
|
|
1140
|
+
let high = starts.length;
|
|
1141
|
+
while (low < high) {
|
|
1142
|
+
const middle = low + high >>> 1;
|
|
1143
|
+
if (starts[middle] < node.start) low = middle + 1;
|
|
1144
|
+
else high = middle;
|
|
1145
|
+
}
|
|
1146
|
+
return low < starts.length && starts[low] < node.end;
|
|
1147
|
+
}
|
|
1148
|
+
/** 源码片段按树归属组合,父级替换自然覆盖已登记的后代编辑。 */
|
|
1149
|
+
function renderEdits(session, ranges, snippets) {
|
|
1150
|
+
const { source, children } = session.document;
|
|
1151
|
+
const starts = Array.from(session.edits.keys(), (node) => node.start).sort((left, right) => left - right);
|
|
1152
|
+
const stack = childrenParts(source, children, 0, source.length).reverse();
|
|
1153
|
+
const output = [];
|
|
1154
|
+
let length = 0;
|
|
1155
|
+
const emit = (...parts) => {
|
|
1156
|
+
for (const part of parts) {
|
|
1157
|
+
const value = typeof part === "string" ? part : part.source;
|
|
1158
|
+
if (typeof part !== "string") snippets?.push({
|
|
1159
|
+
source: value,
|
|
1160
|
+
start: length,
|
|
1161
|
+
end: length + value.length
|
|
1162
|
+
});
|
|
1163
|
+
output.push(value);
|
|
1164
|
+
if (ranges || snippets) length += value.length;
|
|
1165
|
+
}
|
|
1166
|
+
};
|
|
1167
|
+
while (stack.length) {
|
|
1168
|
+
const part = stack.pop();
|
|
1169
|
+
if (typeof part === "string") {
|
|
1170
|
+
emit(part);
|
|
1171
|
+
continue;
|
|
1172
|
+
}
|
|
1173
|
+
if (part.kind === "range-end") {
|
|
1174
|
+
ranges.set(part.node, {
|
|
1175
|
+
start: part.start,
|
|
1176
|
+
end: length
|
|
1177
|
+
});
|
|
1178
|
+
continue;
|
|
1179
|
+
}
|
|
1180
|
+
if (part.kind === "snippet") {
|
|
1181
|
+
emit(part);
|
|
1182
|
+
continue;
|
|
1183
|
+
}
|
|
1184
|
+
const state = session.edits.get(part);
|
|
1185
|
+
if (!state && !containsEdit(starts, part)) {
|
|
1186
|
+
const value = source.slice(part.start, part.end);
|
|
1187
|
+
if (ranges?.has(part)) ranges.set(part, {
|
|
1188
|
+
start: length,
|
|
1189
|
+
end: length + value.length
|
|
1190
|
+
});
|
|
1191
|
+
emit(value);
|
|
1192
|
+
continue;
|
|
1193
|
+
}
|
|
1194
|
+
emit(...state?.before ?? []);
|
|
1195
|
+
const rangeStart = length;
|
|
1196
|
+
if (state?.removed || state?.replacement !== void 0) {
|
|
1197
|
+
if (!state.removed) {
|
|
1198
|
+
if (state.literalText === false && typeof state.replacement === "string") assertExpressionBoundary(state.replacement, session.document.syntax);
|
|
1199
|
+
emit(state.replacement);
|
|
1200
|
+
}
|
|
1201
|
+
emit(...state.after ?? []);
|
|
1202
|
+
continue;
|
|
1203
|
+
}
|
|
1204
|
+
if (part.kind !== "element") {
|
|
1205
|
+
emit(source.slice(part.start, part.end), ...state?.after ?? []);
|
|
1206
|
+
continue;
|
|
1207
|
+
}
|
|
1208
|
+
const expanded = Boolean(part.selfClosing && (state?.prepend?.length || state?.append?.length));
|
|
1209
|
+
emit(openingTag(session, part, state, expanded), ...state?.prepend ?? []);
|
|
1210
|
+
const inner = part.selfClosing ? [] : childrenParts(source, part.children, part.opening.end, part.closing.start);
|
|
1211
|
+
inner.push(...state?.append ?? []);
|
|
1212
|
+
if (expanded) inner.push(`</${state?.name ?? part.name}>`);
|
|
1213
|
+
else if (part.closing) inner.push(state?.name && state.name !== part.name ? source.slice(part.closing.start, part.closing.nameStart) + state.name + source.slice(part.closing.nameEnd, part.closing.end) : source.slice(part.closing.start, part.closing.end));
|
|
1214
|
+
if (ranges?.has(part)) inner.push({
|
|
1215
|
+
kind: "range-end",
|
|
1216
|
+
node: part,
|
|
1217
|
+
start: rangeStart
|
|
1218
|
+
});
|
|
1219
|
+
inner.push(...state?.after ?? []);
|
|
1220
|
+
for (let index = inner.length - 1; index >= 0; index--) stack.push(inner[index]);
|
|
1221
|
+
}
|
|
1222
|
+
return output.join("");
|
|
1223
|
+
}
|
|
1224
|
+
// src/edit/overlay.ts
|
|
1225
|
+
const editOverlays = /* @__PURE__ */ new WeakMap();
|
|
1226
|
+
// src/edit/state.ts
|
|
1227
|
+
function stateFor(session, node) {
|
|
1228
|
+
let state = session.edits.get(node);
|
|
1229
|
+
if (!state) {
|
|
1230
|
+
state = { removed: false };
|
|
1231
|
+
session.edits.set(node, state);
|
|
1232
|
+
}
|
|
1233
|
+
return state;
|
|
1234
|
+
}
|
|
1235
|
+
function assertName(name) {
|
|
1236
|
+
if (typeof name !== "string" || !/^[A-Z_][\w:.-]*$/i.test(name)) throw new TypeError(`Invalid WXML name: ${String(name)}.`);
|
|
1237
|
+
}
|
|
1238
|
+
function assertValid(document) {
|
|
1239
|
+
const diagnostic = document.diagnostics.find((item) => item.severity === "error");
|
|
1240
|
+
if (diagnostic) throw new TypeError(`Invalid WXML document: ${diagnostic.message}`);
|
|
1241
|
+
}
|
|
1242
|
+
function validateSnippet(session, source, raw = false) {
|
|
1243
|
+
if (typeof source !== "string") throw new TypeError("Expected a WXML source string.");
|
|
1244
|
+
if (!source) return source;
|
|
1245
|
+
if (!raw) {
|
|
1246
|
+
if (session.context.resolveScriptBoundary) return {
|
|
1247
|
+
kind: "snippet",
|
|
1248
|
+
source
|
|
1249
|
+
};
|
|
1250
|
+
assertValid(parseContext(source, session.context));
|
|
1251
|
+
}
|
|
1252
|
+
return source;
|
|
1253
|
+
}
|
|
1254
|
+
// src/edit/document.ts
|
|
1255
|
+
/** 只投影公开文档字段,兼容使用原型 getter 的延迟文档与普通解析结果。 */
|
|
1256
|
+
function copyDocument(document, context = contextFor(document)) {
|
|
1257
|
+
if (context) return createDocument(document.source, document, context);
|
|
1258
|
+
return {
|
|
1259
|
+
source: document.source,
|
|
1260
|
+
syntax: document.syntax,
|
|
1261
|
+
tokens: document.tokens,
|
|
1262
|
+
diagnostics: document.diagnostics,
|
|
1263
|
+
children: document.children
|
|
1264
|
+
};
|
|
1265
|
+
}
|
|
1266
|
+
/** 延迟状态由实例独占,getter 与物化方法共享原型,避免每次编辑分配访问器闭包。 */
|
|
1267
|
+
var PatchedDocument = class {
|
|
1268
|
+
syntax;
|
|
1269
|
+
diagnostics = [];
|
|
1270
|
+
#snapshot;
|
|
1271
|
+
#context;
|
|
1272
|
+
#sourceValue;
|
|
1273
|
+
#materialized;
|
|
1274
|
+
constructor(original, snapshot) {
|
|
1275
|
+
const context = this.#context = snapshot.context;
|
|
1276
|
+
if (context.metadata) Object.assign(this, context.metadata);
|
|
1277
|
+
this.syntax = original.syntax;
|
|
1278
|
+
this.#snapshot = snapshot;
|
|
1279
|
+
}
|
|
1280
|
+
get source() {
|
|
1281
|
+
if (this.#sourceValue === void 0) {
|
|
1282
|
+
this.#sourceValue = renderEdits(this.#snapshot);
|
|
1283
|
+
this.#snapshot = void 0;
|
|
1284
|
+
}
|
|
1285
|
+
return this.#sourceValue;
|
|
1286
|
+
}
|
|
1287
|
+
get children() {
|
|
1288
|
+
return this.#readDocument().children;
|
|
1289
|
+
}
|
|
1290
|
+
get tokens() {
|
|
1291
|
+
return this.#readDocument().tokens;
|
|
1292
|
+
}
|
|
1293
|
+
toJSON() {
|
|
1294
|
+
return copyDocument(this, this.#context);
|
|
1295
|
+
}
|
|
1296
|
+
#readDocument() {
|
|
1297
|
+
if (!this.#materialized) {
|
|
1298
|
+
const document = parseContext(this.source, this.#context);
|
|
1299
|
+
assertValid(document);
|
|
1300
|
+
this.#materialized = document;
|
|
1301
|
+
editOverlays.delete(this);
|
|
1302
|
+
}
|
|
1303
|
+
return this.#materialized;
|
|
1304
|
+
}
|
|
1305
|
+
};
|
|
1306
|
+
/** 结构不变的受控修改只验证改动词法,公开 AST 与范围首次读取时统一重建。 */
|
|
1307
|
+
function createPatchedDocument(session) {
|
|
1308
|
+
const { document: original, context } = session;
|
|
1309
|
+
if (session.hasStructuralInvalidations || context.resolveScriptBoundary || original.diagnostics.length) return;
|
|
1310
|
+
const elements = /* @__PURE__ */ new Map();
|
|
1311
|
+
const text = /* @__PURE__ */ new Map();
|
|
1312
|
+
for (const [node, state] of session.edits) {
|
|
1313
|
+
if (state.removed || state.before?.length || state.after?.length || state.prepend?.length || state.append?.length) return;
|
|
1314
|
+
if (node.kind === "element") {
|
|
1315
|
+
if (state.replacement !== void 0 || state.name && state.name !== node.name && context.dialect.rawTags.some((name) => name.toLowerCase() === node.name.toLowerCase() || name.toLowerCase() === state.name.toLowerCase())) return;
|
|
1316
|
+
if (!state.unvalidatedAttributes?.size) {
|
|
1317
|
+
elements.set(node, {
|
|
1318
|
+
name: state.name ?? node.name,
|
|
1319
|
+
attributes: editedAttributes(node, state)
|
|
1320
|
+
});
|
|
1321
|
+
continue;
|
|
1322
|
+
}
|
|
1323
|
+
const source = openingTag(session, node, state, false);
|
|
1324
|
+
try {
|
|
1325
|
+
const opening = readTag(source, 0, original.syntax);
|
|
1326
|
+
if (opening.end !== source.length || opening.selfClosing !== node.selfClosing) return;
|
|
1327
|
+
elements.set(node, opening);
|
|
1328
|
+
} catch (error) {
|
|
1329
|
+
if (!(error instanceof TemplateLexicalError)) throw error;
|
|
1330
|
+
throw new TypeError(`Invalid WXML document: ${error.message}`);
|
|
1331
|
+
}
|
|
1332
|
+
} else if (state.replacement !== void 0) {
|
|
1333
|
+
if (node.kind !== "text" || typeof state.replacement !== "string") return;
|
|
1334
|
+
if (state.literalText && state.replacement) {
|
|
1335
|
+
text.set(node, state.replacement);
|
|
1336
|
+
continue;
|
|
1337
|
+
}
|
|
1338
|
+
if (state.literalText === false) assertExpressionBoundary(state.replacement, original.syntax);
|
|
1339
|
+
const replacement = parseContext(state.replacement, context);
|
|
1340
|
+
assertValid(replacement);
|
|
1341
|
+
if (replacement.children.length !== 1 || replacement.children[0]?.kind !== "text") return;
|
|
1342
|
+
text.set(node, state.replacement);
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
const document = new PatchedDocument(original, {
|
|
1346
|
+
document: original,
|
|
1347
|
+
context,
|
|
1348
|
+
active: false,
|
|
1349
|
+
hasStructuralInvalidations: session.hasStructuralInvalidations,
|
|
1350
|
+
edits: new Map(session.edits)
|
|
1351
|
+
});
|
|
1352
|
+
bindDocument(document, context);
|
|
1353
|
+
editOverlays.set(document, {
|
|
1354
|
+
original,
|
|
1355
|
+
elements,
|
|
1356
|
+
text
|
|
1357
|
+
});
|
|
1358
|
+
return document;
|
|
1359
|
+
}
|
|
1360
|
+
// src/edit/node.ts
|
|
1361
|
+
/** 操作仅在被读取时绑定,普通遍历节点不分配整套编辑闭包。 */
|
|
1362
|
+
var EditableHandle = class {
|
|
1363
|
+
session;
|
|
1364
|
+
node;
|
|
1365
|
+
parent;
|
|
1366
|
+
changes;
|
|
1367
|
+
constructor(session, node, parent) {
|
|
1368
|
+
this.session = session;
|
|
1369
|
+
this.node = node;
|
|
1370
|
+
this.parent = parent;
|
|
1371
|
+
}
|
|
1372
|
+
isInvalidated() {
|
|
1373
|
+
const structuralReplacement = this.changes?.replacement !== void 0 && this.changes.literalText === void 0;
|
|
1374
|
+
if (this.changes?.removed || structuralReplacement) return true;
|
|
1375
|
+
if (!this.session.hasStructuralInvalidations) return false;
|
|
1376
|
+
for (let current = this.parent; current; current = current.parent) {
|
|
1377
|
+
const parentStructuralReplacement = current.changes?.replacement !== void 0 && current.changes.literalText === void 0;
|
|
1378
|
+
if (current.changes?.removed || parentStructuralReplacement) return true;
|
|
1379
|
+
}
|
|
1380
|
+
return false;
|
|
1381
|
+
}
|
|
1382
|
+
assertActive() {
|
|
1383
|
+
if (!this.session.active || this.isInvalidated()) throw new Error("This WXML edit handle is no longer active.");
|
|
1384
|
+
}
|
|
1385
|
+
state() {
|
|
1386
|
+
if (!this.changes) this.changes = stateFor(this.session, this.node);
|
|
1387
|
+
return this.changes;
|
|
1388
|
+
}
|
|
1389
|
+
element() {
|
|
1390
|
+
this.assertActive();
|
|
1391
|
+
if (this.node.kind !== "element") throw new TypeError("This edit requires an element node.");
|
|
1392
|
+
return this.state();
|
|
1393
|
+
}
|
|
1394
|
+
attribute(name) {
|
|
1395
|
+
this.assertActive();
|
|
1396
|
+
assertName(name);
|
|
1397
|
+
return this.element();
|
|
1398
|
+
}
|
|
1399
|
+
insert(position, source) {
|
|
1400
|
+
this.assertActive();
|
|
1401
|
+
if ((position === "prepend" || position === "append") && this.node.kind !== "element") throw new TypeError("This edit requires an element node.");
|
|
1402
|
+
const rawOwner = this.node.kind === "script" ? this.parent : (position === "prepend" || position === "append") && this.isRawElement() ? this : void 0;
|
|
1403
|
+
const snippet = validateSnippet(this.session, source, Boolean(rawOwner));
|
|
1404
|
+
if (source) {
|
|
1405
|
+
rawOwner?.registerRawOwner();
|
|
1406
|
+
const state = this.state();
|
|
1407
|
+
(state[position] ??= []).push(snippet);
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
isRawElement() {
|
|
1411
|
+
if (this.node.kind !== "element") return false;
|
|
1412
|
+
const tag = (this.changes?.name ?? this.node.name).toLowerCase();
|
|
1413
|
+
return this.session.context.dialect.rawTags.some((name) => name.toLowerCase() === tag);
|
|
1414
|
+
}
|
|
1415
|
+
registerRawOwner() {
|
|
1416
|
+
if (this.node.kind === "element") {
|
|
1417
|
+
this.session.rawOwners ??= /* @__PURE__ */ new Map();
|
|
1418
|
+
this.session.rawOwners.set(this.node, { isInvalidated: () => this.isInvalidated() });
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
get rename() {
|
|
1422
|
+
return (name) => {
|
|
1423
|
+
const state = this.element();
|
|
1424
|
+
assertName(name);
|
|
1425
|
+
state.name = name;
|
|
1426
|
+
};
|
|
1427
|
+
}
|
|
1428
|
+
get setAttribute() {
|
|
1429
|
+
return (name, value) => {
|
|
1430
|
+
const state = this.attribute(name);
|
|
1431
|
+
const encoded = encodeAttribute(value, this.session.document.syntax);
|
|
1432
|
+
(state.attributes ??= /* @__PURE__ */ new Map()).set(name, encoded);
|
|
1433
|
+
if (typeof value === "object") {
|
|
1434
|
+
state.unvalidatedAttributes ??= /* @__PURE__ */ new Set();
|
|
1435
|
+
state.unvalidatedAttributes.add(name);
|
|
1436
|
+
} else state.unvalidatedAttributes?.delete(name);
|
|
1437
|
+
};
|
|
1438
|
+
}
|
|
1439
|
+
get setBooleanAttribute() {
|
|
1440
|
+
return (name) => {
|
|
1441
|
+
const state = this.attribute(name);
|
|
1442
|
+
(state.attributes ??= /* @__PURE__ */ new Map()).set(name, null);
|
|
1443
|
+
state.unvalidatedAttributes?.delete(name);
|
|
1444
|
+
};
|
|
1445
|
+
}
|
|
1446
|
+
get removeAttribute() {
|
|
1447
|
+
return (name) => {
|
|
1448
|
+
const state = this.attribute(name);
|
|
1449
|
+
(state.attributes ??= /* @__PURE__ */ new Map()).set(name, void 0);
|
|
1450
|
+
state.unvalidatedAttributes?.delete(name);
|
|
1451
|
+
};
|
|
1452
|
+
}
|
|
1453
|
+
get replaceText() {
|
|
1454
|
+
return (value) => {
|
|
1455
|
+
this.assertActive();
|
|
1456
|
+
if (this.node.kind !== "text") throw new TypeError("replaceText requires a text node.");
|
|
1457
|
+
const state = this.state();
|
|
1458
|
+
state.replacement = encodeText(value);
|
|
1459
|
+
state.literalText = typeof value === "string";
|
|
1460
|
+
};
|
|
1461
|
+
}
|
|
1462
|
+
get remove() {
|
|
1463
|
+
return () => {
|
|
1464
|
+
this.assertActive();
|
|
1465
|
+
this.state().removed = true;
|
|
1466
|
+
if (this.node.kind === "element") this.session.hasStructuralInvalidations = true;
|
|
1467
|
+
};
|
|
1468
|
+
}
|
|
1469
|
+
get replaceWith() {
|
|
1470
|
+
return (source) => {
|
|
1471
|
+
this.assertActive();
|
|
1472
|
+
const rawOwner = this.node.kind === "script" ? this.parent : void 0;
|
|
1473
|
+
const state = this.state();
|
|
1474
|
+
state.replacement = validateSnippet(this.session, source, Boolean(rawOwner));
|
|
1475
|
+
state.literalText = void 0;
|
|
1476
|
+
rawOwner?.registerRawOwner();
|
|
1477
|
+
if (this.node.kind === "element") this.session.hasStructuralInvalidations = true;
|
|
1478
|
+
};
|
|
1479
|
+
}
|
|
1480
|
+
get insertBefore() {
|
|
1481
|
+
return (source) => this.insert("before", source);
|
|
1482
|
+
}
|
|
1483
|
+
get insertAfter() {
|
|
1484
|
+
return (source) => this.insert("after", source);
|
|
1485
|
+
}
|
|
1486
|
+
get prepend() {
|
|
1487
|
+
return (source) => this.insert("prepend", source);
|
|
1488
|
+
}
|
|
1489
|
+
get append() {
|
|
1490
|
+
return (source) => this.insert("append", source);
|
|
1491
|
+
}
|
|
1492
|
+
};
|
|
1493
|
+
// src/edit/path/model.ts
|
|
1494
|
+
/** 完整解析树包含所有文本和空白,逻辑子列表只需保存节点;旧 callback 不建立索引。 */
|
|
1495
|
+
function createList(source, nodes, original, parent) {
|
|
1496
|
+
const roots = {
|
|
1497
|
+
items: [],
|
|
1498
|
+
version: 0,
|
|
1499
|
+
owner: parent
|
|
1500
|
+
};
|
|
1501
|
+
const tasks = [{
|
|
1502
|
+
nodes,
|
|
1503
|
+
list: roots,
|
|
1504
|
+
parent
|
|
1505
|
+
}];
|
|
1506
|
+
while (tasks.length) {
|
|
1507
|
+
const task = tasks.pop();
|
|
1508
|
+
for (const base of task.nodes) {
|
|
1509
|
+
const record = {
|
|
1510
|
+
base,
|
|
1511
|
+
source,
|
|
1512
|
+
original: original ? base : void 0,
|
|
1513
|
+
parent: task.parent,
|
|
1514
|
+
list: task.list,
|
|
1515
|
+
children: {
|
|
1516
|
+
items: [],
|
|
1517
|
+
version: 0
|
|
1518
|
+
},
|
|
1519
|
+
name: base.kind === "element" ? base.name : void 0,
|
|
1520
|
+
attributes: base.kind === "element" ? base.attributes.map((attribute) => ({
|
|
1521
|
+
name: attribute.name,
|
|
1522
|
+
value: attribute.value,
|
|
1523
|
+
quote: attribute.quote,
|
|
1524
|
+
original: attribute
|
|
1525
|
+
})) : [],
|
|
1526
|
+
data: base.kind === "element" ? void 0 : base.data,
|
|
1527
|
+
dirty: false,
|
|
1528
|
+
attached: true
|
|
1529
|
+
};
|
|
1530
|
+
record.children.owner = record;
|
|
1531
|
+
task.list.items.push(record);
|
|
1532
|
+
if (base.kind === "element" && !base.selfClosing) tasks.push({
|
|
1533
|
+
nodes: base.children,
|
|
1534
|
+
list: record.children,
|
|
1535
|
+
parent: record
|
|
1536
|
+
});
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
return roots;
|
|
1540
|
+
}
|
|
1541
|
+
function records(list) {
|
|
1542
|
+
return list.items;
|
|
1543
|
+
}
|
|
1544
|
+
function attributeView(attribute) {
|
|
1545
|
+
return Object.freeze({
|
|
1546
|
+
name: attribute.name,
|
|
1547
|
+
value: attribute.value,
|
|
1548
|
+
quote: attribute.quote
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
1551
|
+
/** 当前逻辑节点不暴露旧源码坐标,属性和子节点每次读取都来自当前树。 */
|
|
1552
|
+
function nodeView(record) {
|
|
1553
|
+
if (!record.view) record.view = record.base.kind === "element" ? Object.freeze({
|
|
1554
|
+
kind: "element",
|
|
1555
|
+
get name() {
|
|
1556
|
+
return record.name;
|
|
1557
|
+
},
|
|
1558
|
+
get attributes() {
|
|
1559
|
+
return Object.freeze(record.attributes.map(attributeView));
|
|
1560
|
+
},
|
|
1561
|
+
get children() {
|
|
1562
|
+
return Object.freeze(records(record.children).map(nodeView));
|
|
1563
|
+
},
|
|
1564
|
+
get selfClosing() {
|
|
1565
|
+
return record.base.kind === "element" && record.base.selfClosing && !record.children.items.length;
|
|
1566
|
+
}
|
|
1567
|
+
}) : Object.freeze({
|
|
1568
|
+
kind: record.base.kind,
|
|
1569
|
+
get data() {
|
|
1570
|
+
return record.data;
|
|
1571
|
+
}
|
|
1572
|
+
});
|
|
1573
|
+
return record.view;
|
|
1574
|
+
}
|
|
1575
|
+
function isAttached(record) {
|
|
1576
|
+
for (let current = record; current; current = current.parent) if (!current.attached) return false;
|
|
1577
|
+
return true;
|
|
1578
|
+
}
|
|
1579
|
+
function assertActive(session, record) {
|
|
1580
|
+
if (!session.active || !isAttached(record)) throw new Error("This WXML edit path is no longer active.");
|
|
1581
|
+
}
|
|
1582
|
+
function changed(session, record) {
|
|
1583
|
+
session.version++;
|
|
1584
|
+
for (let current = record; current; current = current.parent) current.dirty = true;
|
|
1585
|
+
}
|
|
1586
|
+
/** 新结构继承目标与执行来源的最大代数,嵌套遍历和原文标签重建不能重置谱系。 */
|
|
1587
|
+
function structuralGeneration(session, target) {
|
|
1588
|
+
let generation = 0;
|
|
1589
|
+
const sources = [target, ...session.traversals.map((traversal) => traversal.frame.record)];
|
|
1590
|
+
for (const source of sources) for (let current = source; current; current = current.parent) generation = Math.max(generation, current.generation ?? 0);
|
|
1591
|
+
return generation + 1;
|
|
1592
|
+
}
|
|
1593
|
+
function rawElement(session, record) {
|
|
1594
|
+
return record.base.kind === "element" && session.context.dialect.rawTags.some((name) => name.toLowerCase() === record.name.toLowerCase());
|
|
1595
|
+
}
|
|
1596
|
+
/** 失效状态沿父链传播,不逐节点销毁路径,也不会重新使用被替换路径的身份。 */
|
|
1597
|
+
function detach(record) {
|
|
1598
|
+
record.attached = false;
|
|
1599
|
+
}
|
|
1600
|
+
// src/edit/path/render.ts
|
|
1601
|
+
function attributeSource(record, attribute) {
|
|
1602
|
+
const original = attribute.original;
|
|
1603
|
+
if (original && !attribute.changed) return record.source.slice(original.start, original.nameStart) + attribute.name + record.source.slice(original.nameEnd, original.end);
|
|
1604
|
+
return attribute.value === void 0 ? attribute.name : `${attribute.name}="${attribute.value}"`;
|
|
1605
|
+
}
|
|
1606
|
+
function opening(record, base) {
|
|
1607
|
+
const edits = [];
|
|
1608
|
+
if (record.name !== base.name) edits.push({
|
|
1609
|
+
start: base.opening.nameStart,
|
|
1610
|
+
end: base.opening.nameEnd,
|
|
1611
|
+
text: record.name
|
|
1612
|
+
});
|
|
1613
|
+
for (const original of base.attributes) {
|
|
1614
|
+
const current = record.attributes.find((attribute) => attribute.original === original);
|
|
1615
|
+
if (!current || current.name !== original.name || current.changed) edits.push({
|
|
1616
|
+
start: original.start,
|
|
1617
|
+
end: original.end,
|
|
1618
|
+
text: current ? attributeSource(record, current) : ""
|
|
1619
|
+
});
|
|
1620
|
+
}
|
|
1621
|
+
const additions = record.attributes.filter((attribute) => !attribute.original).map((attribute) => ` ${attributeSource(record, attribute)}`).join("");
|
|
1622
|
+
const expanded = base.selfClosing && record.children.items.length > 0;
|
|
1623
|
+
if (additions || expanded) edits.push({
|
|
1624
|
+
start: base.opening.end - (base.selfClosing ? 2 : 1),
|
|
1625
|
+
end: base.opening.end,
|
|
1626
|
+
text: `${additions}${base.selfClosing && !expanded ? "/>" : ">"}`
|
|
1627
|
+
});
|
|
1628
|
+
let cursor = base.opening.start;
|
|
1629
|
+
let result = "";
|
|
1630
|
+
for (const edit of edits.sort((left, right) => left.start - right.start)) {
|
|
1631
|
+
result += record.source.slice(cursor, edit.start) + edit.text;
|
|
1632
|
+
cursor = edit.end;
|
|
1633
|
+
}
|
|
1634
|
+
return result + record.source.slice(cursor, base.opening.end);
|
|
1635
|
+
}
|
|
1636
|
+
/** 按片段来源迭代拼接;未修改子树直接复用原字节,不依赖旧坐标表示当前树。 */
|
|
1637
|
+
function renderItems(items, ranges) {
|
|
1638
|
+
const tracked = /* @__PURE__ */ new Set();
|
|
1639
|
+
for (const record of ranges?.keys() ?? []) for (let current = record; current; current = current.parent) tracked.add(current);
|
|
1640
|
+
const stack = [...items].reverse();
|
|
1641
|
+
const output = [];
|
|
1642
|
+
let size = 0;
|
|
1643
|
+
const emit = (value) => {
|
|
1644
|
+
output.push(value);
|
|
1645
|
+
size += value.length;
|
|
1646
|
+
};
|
|
1647
|
+
while (stack.length) {
|
|
1648
|
+
const part = stack.pop();
|
|
1649
|
+
if (typeof part === "string") {
|
|
1650
|
+
emit(part);
|
|
1651
|
+
continue;
|
|
1652
|
+
}
|
|
1653
|
+
if ("record" in part) {
|
|
1654
|
+
ranges.set(part.record, {
|
|
1655
|
+
start: part.start,
|
|
1656
|
+
end: size
|
|
1657
|
+
});
|
|
1658
|
+
continue;
|
|
1659
|
+
}
|
|
1660
|
+
const base = part.base;
|
|
1661
|
+
const start = size;
|
|
1662
|
+
if (!part.dirty && !tracked.has(part)) {
|
|
1663
|
+
emit(part.source.slice(base.start, base.end));
|
|
1664
|
+
continue;
|
|
1665
|
+
}
|
|
1666
|
+
if (base.kind !== "element") {
|
|
1667
|
+
emit(part.data);
|
|
1668
|
+
if (tracked.has(part)) ranges?.set(part, {
|
|
1669
|
+
start,
|
|
1670
|
+
end: size
|
|
1671
|
+
});
|
|
1672
|
+
continue;
|
|
1673
|
+
}
|
|
1674
|
+
emit(opening(part, base));
|
|
1675
|
+
if (ranges && (part.rawProtected || tracked.has(part))) stack.push({
|
|
1676
|
+
record: part,
|
|
1677
|
+
start
|
|
1678
|
+
});
|
|
1679
|
+
if (base.closing) stack.push(part.source.slice(base.closing.start, base.closing.nameStart) + part.name + part.source.slice(base.closing.nameEnd, base.closing.end));
|
|
1680
|
+
else if (part.children.items.length) stack.push(`</${part.name}>`);
|
|
1681
|
+
for (let index = part.children.items.length - 1; index >= 0; index--) stack.push(part.children.items[index]);
|
|
1682
|
+
}
|
|
1683
|
+
return output.join("");
|
|
1684
|
+
}
|
|
1685
|
+
/** 提交后统一重建公开 AST 和坐标,同时核验脚本片段未逃出所属原文标签。 */
|
|
1686
|
+
function commit(session) {
|
|
1687
|
+
const ranges = /* @__PURE__ */ new Map();
|
|
1688
|
+
const document = parseContext(renderItems(session.roots.items, ranges), session.context);
|
|
1689
|
+
assertValid(document);
|
|
1690
|
+
if (ranges.size) {
|
|
1691
|
+
const expected = new Map([...ranges.values()].map((range) => [range.start, range.end]));
|
|
1692
|
+
const stack = [...document.children];
|
|
1693
|
+
while (stack.length && expected.size) {
|
|
1694
|
+
const node = stack.pop();
|
|
1695
|
+
const end = expected.get(node.start);
|
|
1696
|
+
if (end !== void 0) {
|
|
1697
|
+
if (node.kind !== "element" || node.end !== end || node.children.length !== 1 || node.children[0]?.kind !== "script") throw new TypeError("Raw WXML edits must stay inside their original script owner.");
|
|
1698
|
+
expected.delete(node.start);
|
|
1699
|
+
}
|
|
1700
|
+
if (node.kind === "element") stack.push(...node.children);
|
|
1701
|
+
}
|
|
1702
|
+
if (expected.size) throw new TypeError("Raw WXML edits must stay inside their original script owner.");
|
|
1703
|
+
}
|
|
1704
|
+
return document;
|
|
1705
|
+
}
|
|
1706
|
+
// src/edit/path/context.ts
|
|
1707
|
+
/** 只有自定义边界判定需要全文投影;普通属性修改和默认解析不支付该成本。 */
|
|
1708
|
+
function projectSource(session, record, operation, snippet) {
|
|
1709
|
+
const ranges = /* @__PURE__ */ new Map([[record, {
|
|
1710
|
+
start: 0,
|
|
1711
|
+
end: 0
|
|
1712
|
+
}]]);
|
|
1713
|
+
const current = renderItems(session.roots.items, ranges);
|
|
1714
|
+
const range = ranges.get(record);
|
|
1715
|
+
const head = record.base.kind === "element" ? opening(record, record.base) : "";
|
|
1716
|
+
let start = range.start;
|
|
1717
|
+
let end = start;
|
|
1718
|
+
if (operation === "after") start = end = range.end;
|
|
1719
|
+
else if (operation === "prepend") start = end = range.start + head.length;
|
|
1720
|
+
else if (operation === "append") start = end = range.start + head.length + renderItems(record.children.items).length;
|
|
1721
|
+
else if (operation === "replace" || operation === "current") end = range.end;
|
|
1722
|
+
if (operation === "current") return {
|
|
1723
|
+
source: current,
|
|
1724
|
+
offset: start
|
|
1725
|
+
};
|
|
1726
|
+
if ((operation === "prepend" || operation === "append") && record.base.kind === "element" && record.base.selfClosing && !record.children.items.length) {
|
|
1727
|
+
const expanded = `${head.slice(0, -2)}>`;
|
|
1728
|
+
return {
|
|
1729
|
+
source: `${current.slice(0, range.start)}${expanded}${snippet}</${record.name}>${current.slice(range.end)}`,
|
|
1730
|
+
offset: range.start + expanded.length
|
|
1731
|
+
};
|
|
1732
|
+
}
|
|
1733
|
+
return {
|
|
1734
|
+
source: current.slice(0, start) + snippet + current.slice(end),
|
|
1735
|
+
offset: start
|
|
1736
|
+
};
|
|
1737
|
+
}
|
|
1738
|
+
function shiftCandidates(candidates, offset) {
|
|
1739
|
+
const shift = (range) => ({
|
|
1740
|
+
...range,
|
|
1741
|
+
start: range.start + offset,
|
|
1742
|
+
end: range.end + offset
|
|
1743
|
+
});
|
|
1744
|
+
return {
|
|
1745
|
+
closingTag: candidates.closingTag && shift(candidates.closingTag),
|
|
1746
|
+
trailingComment: candidates.trailingComment === void 0 ? void 0 : candidates.trailingComment + offset,
|
|
1747
|
+
commentClosings: candidates.commentClosings?.map(shift)
|
|
1748
|
+
};
|
|
1749
|
+
}
|
|
1750
|
+
/** 片段内部坐标保持局部,用户 resolver 始终接收完整投影源码和完整坐标。 */
|
|
1751
|
+
function parseFragment(session, source, projected) {
|
|
1752
|
+
const resolver = session.context.resolveScriptBoundary;
|
|
1753
|
+
if (!resolver || !projected) return parseContext(source, session.context);
|
|
1754
|
+
return parseContext(source, {
|
|
1755
|
+
...session.context,
|
|
1756
|
+
resolveScriptBoundary: (_source, start, name, candidates) => {
|
|
1757
|
+
const wasActive = session.active;
|
|
1758
|
+
session.active = false;
|
|
1759
|
+
try {
|
|
1760
|
+
const resolved = resolver(projected.source, projected.offset + start, name, shiftCandidates(candidates, projected.offset));
|
|
1761
|
+
return resolved && {
|
|
1762
|
+
start: resolved.start - projected.offset,
|
|
1763
|
+
end: resolved.end - projected.offset
|
|
1764
|
+
};
|
|
1765
|
+
} finally {
|
|
1766
|
+
session.active = wasActive;
|
|
1767
|
+
}
|
|
1768
|
+
}
|
|
1769
|
+
});
|
|
1770
|
+
}
|
|
1771
|
+
// src/edit/path/mutations.ts
|
|
1772
|
+
function requireElement(session, record) {
|
|
1773
|
+
assertActive(session, record);
|
|
1774
|
+
if (record.base.kind !== "element") throw new TypeError("This edit requires an element node.");
|
|
1775
|
+
}
|
|
1776
|
+
function setAttribute(session, record, name, value, boolean = false) {
|
|
1777
|
+
requireElement(session, record);
|
|
1778
|
+
assertName(name);
|
|
1779
|
+
const encoded = boolean || value === void 0 ? void 0 : encodeAttribute(value, session.document.syntax);
|
|
1780
|
+
const matches = record.attributes.filter((attribute) => attribute.name === name);
|
|
1781
|
+
if (value === void 0 && !boolean) {
|
|
1782
|
+
if (!matches.length) return;
|
|
1783
|
+
record.attributes = record.attributes.filter((attribute) => attribute.name !== name);
|
|
1784
|
+
} else if (matches.length) {
|
|
1785
|
+
const first = matches[0];
|
|
1786
|
+
if (matches.length === 1 && first.value === encoded && first.quote === (boolean ? void 0 : "\"")) return;
|
|
1787
|
+
first.value = encoded;
|
|
1788
|
+
first.quote = boolean ? void 0 : "\"";
|
|
1789
|
+
first.changed = true;
|
|
1790
|
+
record.attributes = record.attributes.filter((attribute) => attribute.name !== name || attribute === first);
|
|
1791
|
+
} else record.attributes.push({
|
|
1792
|
+
name,
|
|
1793
|
+
value: encoded,
|
|
1794
|
+
quote: boolean ? void 0 : "\"",
|
|
1795
|
+
changed: true
|
|
1796
|
+
});
|
|
1797
|
+
changed(session, record);
|
|
1798
|
+
}
|
|
1799
|
+
function renameAttribute(session, record, name, nextName) {
|
|
1800
|
+
requireElement(session, record);
|
|
1801
|
+
assertName(name);
|
|
1802
|
+
assertName(nextName);
|
|
1803
|
+
if (name === nextName) return;
|
|
1804
|
+
const matches = record.attributes.filter((attribute) => attribute.name === name);
|
|
1805
|
+
if (matches.length && record.attributes.some((attribute) => attribute.name === nextName)) throw new TypeError(`Cannot rename WXML attribute to an existing name: ${nextName}.`);
|
|
1806
|
+
for (const attribute of matches) attribute.name = nextName;
|
|
1807
|
+
if (matches.length) changed(session, record);
|
|
1808
|
+
}
|
|
1809
|
+
function rename(session, record, name) {
|
|
1810
|
+
requireElement(session, record);
|
|
1811
|
+
assertName(name);
|
|
1812
|
+
if (record.name === name) return;
|
|
1813
|
+
const wasRaw = rawElement(session, record);
|
|
1814
|
+
const previous = record.name;
|
|
1815
|
+
const wasDirty = record.dirty;
|
|
1816
|
+
record.name = name;
|
|
1817
|
+
if (wasRaw !== rawElement(session, record)) {
|
|
1818
|
+
record.dirty = true;
|
|
1819
|
+
try {
|
|
1820
|
+
const source = renderItems([record]);
|
|
1821
|
+
const parsed = parseFragment(session, source, session.context.resolveScriptBoundary ? projectSource(session, record, "current", source) : void 0);
|
|
1822
|
+
assertValid(parsed);
|
|
1823
|
+
const replacement = parsed.children[0];
|
|
1824
|
+
if (parsed.children.length !== 1 || replacement?.kind !== "element") throw new TypeError("Renaming a raw tag must preserve its element boundary.");
|
|
1825
|
+
const fresh = records(createList(source, [replacement], false))[0];
|
|
1826
|
+
for (const child of records(record.children)) detach(child);
|
|
1827
|
+
record.base = fresh.base;
|
|
1828
|
+
record.source = fresh.source;
|
|
1829
|
+
record.children = fresh.children;
|
|
1830
|
+
record.children.owner = record;
|
|
1831
|
+
record.attributes = fresh.attributes;
|
|
1832
|
+
for (const child of records(record.children)) child.parent = record;
|
|
1833
|
+
record.rawProtected = void 0;
|
|
1834
|
+
} catch (error) {
|
|
1835
|
+
record.name = previous;
|
|
1836
|
+
record.dirty = wasDirty;
|
|
1837
|
+
throw error;
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
changed(session, record);
|
|
1841
|
+
}
|
|
1842
|
+
function replaceText(session, record, value) {
|
|
1843
|
+
assertActive(session, record);
|
|
1844
|
+
if (record.base.kind !== "text") throw new TypeError("replaceText requires a text node.");
|
|
1845
|
+
const encoded = encodeText(value);
|
|
1846
|
+
if (encoded !== record.data) {
|
|
1847
|
+
record.data = encoded;
|
|
1848
|
+
changed(session, record);
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
function rawOwner(session, record, inside) {
|
|
1852
|
+
return inside && rawElement(session, record) ? record : record.base.kind === "script" ? record.parent : void 0;
|
|
1853
|
+
}
|
|
1854
|
+
function insertedList(session, source, parent, owner, record, operation) {
|
|
1855
|
+
if (typeof source !== "string") throw new TypeError("Expected a WXML source string.");
|
|
1856
|
+
if (!source) return {
|
|
1857
|
+
items: [],
|
|
1858
|
+
version: 0,
|
|
1859
|
+
owner: parent
|
|
1860
|
+
};
|
|
1861
|
+
if (owner) return createList(source, [{
|
|
1862
|
+
kind: "script",
|
|
1863
|
+
start: 0,
|
|
1864
|
+
end: source.length,
|
|
1865
|
+
data: source
|
|
1866
|
+
}], false, parent);
|
|
1867
|
+
const document = parseFragment(session, source, session.context.resolveScriptBoundary ? projectSource(session, record, operation, source) : void 0);
|
|
1868
|
+
assertValid(document);
|
|
1869
|
+
return createList(source, document.children, false, parent);
|
|
1870
|
+
}
|
|
1871
|
+
function structure(session, record, operation, source = "") {
|
|
1872
|
+
assertActive(session, record);
|
|
1873
|
+
const inside = operation === "prepend" || operation === "append";
|
|
1874
|
+
if (inside) requireElement(session, record);
|
|
1875
|
+
const list = inside ? record.children : record.list;
|
|
1876
|
+
const parent = inside ? record : record.parent;
|
|
1877
|
+
const owner = rawOwner(session, record, inside);
|
|
1878
|
+
if (operation === "replace" && renderItems([record]) === source) return [session.makePath(record)];
|
|
1879
|
+
const added = operation === "remove" ? {
|
|
1880
|
+
items: [],
|
|
1881
|
+
version: 0
|
|
1882
|
+
} : insertedList(session, source, parent, owner, record, operation);
|
|
1883
|
+
const generation = structuralGeneration(session, record);
|
|
1884
|
+
if (added.items.length && generation > 1024) throw new Error("WXML edit structural generation limit exceeded; repeated insertion or replacement must terminate.");
|
|
1885
|
+
const index = inside ? operation === "prepend" ? 0 : list.items.length : list.items.indexOf(record) + (operation === "after" ? 1 : 0);
|
|
1886
|
+
const remove = operation === "replace" || operation === "remove";
|
|
1887
|
+
if (!remove && !added.items.length) return [];
|
|
1888
|
+
if (remove) detach(record);
|
|
1889
|
+
for (const item of added.items) {
|
|
1890
|
+
item.list = list;
|
|
1891
|
+
const stack = [item];
|
|
1892
|
+
while (stack.length) {
|
|
1893
|
+
const inserted = stack.pop();
|
|
1894
|
+
inserted.generation = generation;
|
|
1895
|
+
stack.push(...records(inserted.children));
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
list.items.splice(index, remove ? 1 : 0, ...added.items);
|
|
1899
|
+
list.version++;
|
|
1900
|
+
changed(session, parent);
|
|
1901
|
+
if (owner) {
|
|
1902
|
+
owner.rawProtected = true;
|
|
1903
|
+
changed(session, owner);
|
|
1904
|
+
}
|
|
1905
|
+
return added.items.map(session.makePath);
|
|
1906
|
+
}
|
|
1907
|
+
// src/edit/path/traverse.ts
|
|
1908
|
+
const visitorNames = {
|
|
1909
|
+
element: "Element",
|
|
1910
|
+
text: "Text",
|
|
1911
|
+
comment: "Comment",
|
|
1912
|
+
cdata: "Cdata",
|
|
1913
|
+
declaration: "Declaration",
|
|
1914
|
+
script: "Script"
|
|
1915
|
+
};
|
|
1916
|
+
function validateVisitors(visitors) {
|
|
1917
|
+
const known = /* @__PURE__ */ new Set([
|
|
1918
|
+
"enter",
|
|
1919
|
+
"exit",
|
|
1920
|
+
...Object.values(visitorNames)
|
|
1921
|
+
]);
|
|
1922
|
+
for (const [name, visitor] of Object.entries(visitors)) {
|
|
1923
|
+
if (!known.has(name)) throw new TypeError(`Unknown WXML edit visitor: ${name}.`);
|
|
1924
|
+
if (visitor === void 0 || typeof visitor === "function") continue;
|
|
1925
|
+
if (name === "enter" || name === "exit" || !visitor || typeof visitor !== "object" || Array.isArray(visitor)) throw new TypeError(`Expected a synchronous WXML edit visitor for ${name}.`);
|
|
1926
|
+
for (const [phase, callback] of Object.entries(visitor)) if (phase !== "enter" && phase !== "exit" || callback !== void 0 && typeof callback !== "function") throw new TypeError(`Expected enter/exit WXML edit callbacks for ${name}.`);
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
function invoke(callback, record, session, state) {
|
|
1930
|
+
const result = callback?.(session.makePath(record), state);
|
|
1931
|
+
if (result !== null && (typeof result === "object" || typeof result === "function") && typeof result.then === "function") {
|
|
1932
|
+
Promise.resolve(result).catch(() => {});
|
|
1933
|
+
throw new TypeError("WXML edit visitors must be synchronous; Promise results are not supported.");
|
|
1934
|
+
}
|
|
1935
|
+
return result === false;
|
|
1936
|
+
}
|
|
1937
|
+
function callbacks(visitors, record, phase) {
|
|
1938
|
+
const typed = visitors[visitorNames[record.base.kind]];
|
|
1939
|
+
const callback = typeof typed === "function" ? phase === "enter" ? typed : void 0 : typed?.[phase];
|
|
1940
|
+
return phase === "enter" ? [visitors.enter, callback] : [callback, visitors.exit];
|
|
1941
|
+
}
|
|
1942
|
+
function makeFrame(list, record) {
|
|
1943
|
+
return {
|
|
1944
|
+
record,
|
|
1945
|
+
phase: record ? "enter" : "children",
|
|
1946
|
+
list,
|
|
1947
|
+
cursor: 0,
|
|
1948
|
+
version: list.version,
|
|
1949
|
+
seen: /* @__PURE__ */ new Set(),
|
|
1950
|
+
skipped: false
|
|
1951
|
+
};
|
|
1952
|
+
}
|
|
1953
|
+
/** 当前子列表决定后续访问;已访问兄弟保留身份,新插入或替换的路径会补入队列。 */
|
|
1954
|
+
function traverse(session, list, visitors, state, owner) {
|
|
1955
|
+
if (!visitors || typeof visitors !== "object" || Array.isArray(visitors)) throw new TypeError("Expected synchronous WXML edit visitors.");
|
|
1956
|
+
validateVisitors(visitors);
|
|
1957
|
+
const context = {
|
|
1958
|
+
stopped: false,
|
|
1959
|
+
frames: [],
|
|
1960
|
+
requeues: [],
|
|
1961
|
+
requeueCounts: /* @__PURE__ */ new Map()
|
|
1962
|
+
};
|
|
1963
|
+
const stack = [makeFrame(list)];
|
|
1964
|
+
context.frames = stack;
|
|
1965
|
+
session.traversals.push(context);
|
|
1966
|
+
try {
|
|
1967
|
+
while ((stack.length || context.requeues.length) && !context.stopped) {
|
|
1968
|
+
if (owner && !isAttached(owner)) break;
|
|
1969
|
+
if (!stack.length) {
|
|
1970
|
+
const queued = context.requeues.shift();
|
|
1971
|
+
if (isAttached(queued)) stack.push(makeFrame(queued.children, queued));
|
|
1972
|
+
continue;
|
|
1973
|
+
}
|
|
1974
|
+
const frame = stack[stack.length - 1];
|
|
1975
|
+
context.frame = frame;
|
|
1976
|
+
const record = frame.record;
|
|
1977
|
+
if (record && !isAttached(record)) {
|
|
1978
|
+
stack.pop();
|
|
1979
|
+
continue;
|
|
1980
|
+
}
|
|
1981
|
+
if (frame.phase !== "children") {
|
|
1982
|
+
const phase = frame.phase;
|
|
1983
|
+
for (const callback of callbacks(visitors, record, phase)) {
|
|
1984
|
+
if (context.stopped || !isAttached(record)) break;
|
|
1985
|
+
if (invoke(callback, record, session, state)) frame.skipped = true;
|
|
1986
|
+
}
|
|
1987
|
+
if (phase === "exit") {
|
|
1988
|
+
frame.exited = true;
|
|
1989
|
+
frame.phase = "children";
|
|
1990
|
+
} else {
|
|
1991
|
+
frame.phase = "children";
|
|
1992
|
+
frame.list = record.children;
|
|
1993
|
+
frame.version = frame.list.version;
|
|
1994
|
+
}
|
|
1995
|
+
continue;
|
|
1996
|
+
}
|
|
1997
|
+
if (frame.skipped) {
|
|
1998
|
+
if (frame.exited) stack.pop();
|
|
1999
|
+
else frame.phase = "exit";
|
|
2000
|
+
continue;
|
|
2001
|
+
}
|
|
2002
|
+
const container = record ?? owner;
|
|
2003
|
+
if (container && frame.list !== container.children) {
|
|
2004
|
+
frame.list = container.children;
|
|
2005
|
+
frame.version = -1;
|
|
2006
|
+
}
|
|
2007
|
+
if (frame.version !== frame.list.version) {
|
|
2008
|
+
frame.version = frame.list.version;
|
|
2009
|
+
frame.cursor = 0;
|
|
2010
|
+
}
|
|
2011
|
+
let next;
|
|
2012
|
+
while (frame.cursor < frame.list.items.length) {
|
|
2013
|
+
const item = frame.list.items[frame.cursor++];
|
|
2014
|
+
if (isAttached(item) && !frame.seen.has(item)) {
|
|
2015
|
+
next = item;
|
|
2016
|
+
break;
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
if (next) {
|
|
2020
|
+
frame.seen.add(next);
|
|
2021
|
+
stack.push(makeFrame(next.children, next));
|
|
2022
|
+
} else if (record) {
|
|
2023
|
+
if (frame.exited) stack.pop();
|
|
2024
|
+
else frame.phase = "exit";
|
|
2025
|
+
} else stack.pop();
|
|
2026
|
+
}
|
|
2027
|
+
} finally {
|
|
2028
|
+
session.traversals.pop();
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
function traversalFor(session) {
|
|
2032
|
+
return session.traversals[session.traversals.length - 1];
|
|
2033
|
+
}
|
|
2034
|
+
/** 保护按单路径累计,不限制整棵树的大小;无源码进展的重复排队单独设限。 */
|
|
2035
|
+
function requeue(session, record) {
|
|
2036
|
+
const traversal = traversalFor(session);
|
|
2037
|
+
const count = traversal.requeueCounts.get(record) ?? {
|
|
2038
|
+
total: 0,
|
|
2039
|
+
unchanged: 0,
|
|
2040
|
+
version: session.version
|
|
2041
|
+
};
|
|
2042
|
+
count.total++;
|
|
2043
|
+
count.unchanged = count.version === session.version ? count.unchanged + 1 : 0;
|
|
2044
|
+
count.version = session.version;
|
|
2045
|
+
traversal.requeueCounts.set(record, count);
|
|
2046
|
+
if (count.total > 1e4 || count.unchanged > 32) throw new Error("WXML edit requeue limit exceeded for one path; repeated requeue must make progress and terminate.");
|
|
2047
|
+
if (!traversal.requeues.includes(record)) traversal.requeues.push(record);
|
|
2048
|
+
}
|
|
2049
|
+
// src/edit/path/path.ts
|
|
2050
|
+
/** 每个逻辑节点只创建一个路径,新增节点与旧节点使用相同的编辑生命周期。 */
|
|
2051
|
+
var LiveEditPath = class {
|
|
2052
|
+
session;
|
|
2053
|
+
record;
|
|
2054
|
+
constructor(session, record) {
|
|
2055
|
+
this.session = session;
|
|
2056
|
+
this.record = record;
|
|
2057
|
+
}
|
|
2058
|
+
get node() {
|
|
2059
|
+
return nodeView(this.record);
|
|
2060
|
+
}
|
|
2061
|
+
get originalNode() {
|
|
2062
|
+
return this.record.original;
|
|
2063
|
+
}
|
|
2064
|
+
get originalRange() {
|
|
2065
|
+
const original = this.record.original;
|
|
2066
|
+
return original ? Object.freeze({
|
|
2067
|
+
start: original.start,
|
|
2068
|
+
end: original.end
|
|
2069
|
+
}) : void 0;
|
|
2070
|
+
}
|
|
2071
|
+
get parentPath() {
|
|
2072
|
+
return this.record.parent ? this.session.makePath(this.record.parent) : void 0;
|
|
2073
|
+
}
|
|
2074
|
+
get key() {
|
|
2075
|
+
return records(this.record.list).indexOf(this.record);
|
|
2076
|
+
}
|
|
2077
|
+
get removed() {
|
|
2078
|
+
return !isAttached(this.record);
|
|
2079
|
+
}
|
|
2080
|
+
get(key) {
|
|
2081
|
+
if (key !== "children") throw new TypeError("Only children paths are supported.");
|
|
2082
|
+
return Object.freeze(records(this.record.children).map(this.session.makePath));
|
|
2083
|
+
}
|
|
2084
|
+
getSibling(index) {
|
|
2085
|
+
const sibling = records(this.record.list)[index];
|
|
2086
|
+
return sibling ? this.session.makePath(sibling) : void 0;
|
|
2087
|
+
}
|
|
2088
|
+
findParent(predicate) {
|
|
2089
|
+
for (let parent = this.parentPath; parent; parent = parent.parentPath) if (predicate(parent)) return parent;
|
|
2090
|
+
}
|
|
2091
|
+
isElement(name) {
|
|
2092
|
+
return this.record.base.kind === "element" && (name === void 0 || this.record.name === name);
|
|
2093
|
+
}
|
|
2094
|
+
isText() {
|
|
2095
|
+
return this.record.base.kind === "text";
|
|
2096
|
+
}
|
|
2097
|
+
hasAttribute(name) {
|
|
2098
|
+
return this.record.base.kind === "element" && this.record.attributes.some((attribute) => attribute.name === name);
|
|
2099
|
+
}
|
|
2100
|
+
getAttribute(name) {
|
|
2101
|
+
const attribute = this.record.base.kind === "element" ? this.record.attributes.find((attribute) => attribute.name === name) : void 0;
|
|
2102
|
+
return attribute ? attributeView(attribute) : void 0;
|
|
2103
|
+
}
|
|
2104
|
+
rename(name) {
|
|
2105
|
+
rename(this.session, this.record, name);
|
|
2106
|
+
}
|
|
2107
|
+
renameAttribute(name, nextName) {
|
|
2108
|
+
renameAttribute(this.session, this.record, name, nextName);
|
|
2109
|
+
}
|
|
2110
|
+
setAttribute(name, value) {
|
|
2111
|
+
if (value === void 0) throw new TypeError("Expected a string, finite number, boolean, or nonempty expression.");
|
|
2112
|
+
setAttribute(this.session, this.record, name, value);
|
|
2113
|
+
}
|
|
2114
|
+
setBooleanAttribute(name) {
|
|
2115
|
+
setAttribute(this.session, this.record, name, void 0, true);
|
|
2116
|
+
}
|
|
2117
|
+
removeAttribute(name) {
|
|
2118
|
+
setAttribute(this.session, this.record, name, void 0);
|
|
2119
|
+
}
|
|
2120
|
+
replaceText(value) {
|
|
2121
|
+
replaceText(this.session, this.record, value);
|
|
2122
|
+
}
|
|
2123
|
+
remove() {
|
|
2124
|
+
structure(this.session, this.record, "remove");
|
|
2125
|
+
}
|
|
2126
|
+
replaceWith(source) {
|
|
2127
|
+
return Object.freeze(structure(this.session, this.record, "replace", source));
|
|
2128
|
+
}
|
|
2129
|
+
insertBefore(source) {
|
|
2130
|
+
return Object.freeze(structure(this.session, this.record, "before", source));
|
|
2131
|
+
}
|
|
2132
|
+
insertAfter(source) {
|
|
2133
|
+
return Object.freeze(structure(this.session, this.record, "after", source));
|
|
2134
|
+
}
|
|
2135
|
+
prepend(source) {
|
|
2136
|
+
return Object.freeze(structure(this.session, this.record, "prepend", source));
|
|
2137
|
+
}
|
|
2138
|
+
append(source) {
|
|
2139
|
+
return Object.freeze(structure(this.session, this.record, "append", source));
|
|
2140
|
+
}
|
|
2141
|
+
traverse(visitors, state) {
|
|
2142
|
+
assertActive(this.session, this.record);
|
|
2143
|
+
traverse(this.session, this.record.children, visitors, state, this.record);
|
|
2144
|
+
}
|
|
2145
|
+
skip() {
|
|
2146
|
+
assertActive(this.session, this.record);
|
|
2147
|
+
const frame = traversalFor(this.session).frame;
|
|
2148
|
+
if (frame?.record !== this.record) throw new Error("skip() requires the current WXML edit traversal frame.");
|
|
2149
|
+
frame.skipped = true;
|
|
2150
|
+
}
|
|
2151
|
+
stop() {
|
|
2152
|
+
assertActive(this.session, this.record);
|
|
2153
|
+
traversalFor(this.session).stopped = true;
|
|
2154
|
+
}
|
|
2155
|
+
requeue() {
|
|
2156
|
+
assertActive(this.session, this.record);
|
|
2157
|
+
requeue(this.session, this.record);
|
|
2158
|
+
}
|
|
2159
|
+
};
|
|
2160
|
+
// src/edit/path/index.ts
|
|
2161
|
+
/** 路径式会话拥有独立当前树,退出 visitor 后统一提交源码与公开坐标。 */
|
|
2162
|
+
function editPaths(document, visitors, state, context) {
|
|
2163
|
+
assertValid(document);
|
|
2164
|
+
const session = {
|
|
2165
|
+
document,
|
|
2166
|
+
context,
|
|
2167
|
+
roots: createList(document.source, document.children, true),
|
|
2168
|
+
active: true,
|
|
2169
|
+
version: 0,
|
|
2170
|
+
traversals: [],
|
|
2171
|
+
makePath(record) {
|
|
2172
|
+
return record.path ??= new LiveEditPath(session, record);
|
|
2173
|
+
}
|
|
2174
|
+
};
|
|
2175
|
+
try {
|
|
2176
|
+
traverse(session, session.roots, visitors, state);
|
|
2177
|
+
session.active = false;
|
|
2178
|
+
return commit(session);
|
|
2179
|
+
} finally {
|
|
2180
|
+
session.active = false;
|
|
2181
|
+
session.traversals.length = 0;
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
// src/edit/snippets.ts
|
|
2185
|
+
/** 已绑定会话复用完整文档已选定的脚本边界,不向用户 resolver 传入片段上下文。 */
|
|
2186
|
+
function validateSnippetsInContext(document, snippets, context) {
|
|
2187
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
2188
|
+
for (let index = 0; index < document.tokens.length; index++) {
|
|
2189
|
+
const token = document.tokens[index];
|
|
2190
|
+
if (token.kind !== "tag" || token.closing || token.matching === void 0 || document.tokens[index + 1]?.kind !== "script") continue;
|
|
2191
|
+
const closing = document.tokens[token.matching];
|
|
2192
|
+
boundaries.set(token.end, {
|
|
2193
|
+
name: token.name.toLowerCase(),
|
|
2194
|
+
closing
|
|
2195
|
+
});
|
|
2196
|
+
}
|
|
2197
|
+
for (const snippet of snippets) {
|
|
2198
|
+
const result = scanCompiledDocument(snippet.source, context.state, (_source, start, name) => {
|
|
2199
|
+
const boundary = boundaries.get(snippet.start + start);
|
|
2200
|
+
if (!boundary || boundary.name !== name.toLowerCase() || boundary.closing.start < snippet.start || boundary.closing.end > snippet.end) return;
|
|
2201
|
+
return {
|
|
2202
|
+
start: boundary.closing.start - snippet.start,
|
|
2203
|
+
end: boundary.closing.end - snippet.start
|
|
2204
|
+
};
|
|
2205
|
+
}, true);
|
|
2206
|
+
assertValid({
|
|
2207
|
+
source: snippet.source,
|
|
2208
|
+
syntax: result.syntax,
|
|
2209
|
+
tokens: result.tokens,
|
|
2210
|
+
diagnostics: result.diagnostics,
|
|
2211
|
+
children: result.children
|
|
2212
|
+
});
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
/** 先解析最终源码,再独立验证仍存活的结构片段;被祖先覆盖的片段不会进入校验集合。 */
|
|
2216
|
+
function parseCompleteEdits(session, ranges) {
|
|
2217
|
+
const snippets = session.context.resolveScriptBoundary ? [] : void 0;
|
|
2218
|
+
const document = parseContext(renderEdits(session, ranges, snippets), session.context);
|
|
2219
|
+
assertValid(document);
|
|
2220
|
+
if (snippets?.length) validateSnippetsInContext(document, snippets, session.context);
|
|
2221
|
+
return document;
|
|
2222
|
+
}
|
|
2223
|
+
// src/edit/raw.ts
|
|
2224
|
+
/** 原始脚本片段按完整宿主上下文解析,且不能越过原有宿主边界插入结构。 */
|
|
2225
|
+
function parseRawEdits(session) {
|
|
2226
|
+
const ranges = /* @__PURE__ */ new Map();
|
|
2227
|
+
for (const [node, owner] of session.rawOwners ?? []) if (!owner.isInvalidated()) ranges.set(node, {
|
|
2228
|
+
start: 0,
|
|
2229
|
+
end: 0
|
|
2230
|
+
});
|
|
2231
|
+
const document = parseCompleteEdits(session, ranges);
|
|
2232
|
+
const expected = new Map([...ranges.values()].map((range) => [range.start, range]));
|
|
2233
|
+
const stack = [...document.children];
|
|
2234
|
+
while (stack.length && expected.size) {
|
|
2235
|
+
const node = stack.pop();
|
|
2236
|
+
const range = expected.get(node.start);
|
|
2237
|
+
if (range) {
|
|
2238
|
+
if (node.kind !== "element" || node.end !== range.end || node.children.length !== 1 || node.children[0]?.kind !== "script") throw new TypeError("Raw WXML edits must stay inside their original script owner.");
|
|
2239
|
+
expected.delete(node.start);
|
|
2240
|
+
}
|
|
2241
|
+
if (node.kind === "element") stack.push(...node.children);
|
|
2242
|
+
}
|
|
2243
|
+
if (expected.size) throw new TypeError("Raw WXML edits must stay inside their original script owner.");
|
|
2244
|
+
return document;
|
|
2245
|
+
}
|
|
2246
|
+
// src/edit/index.ts
|
|
2247
|
+
/** 兼容入口复用已读取的解析器上下文,编辑生命周期与核心入口一致。 */
|
|
2248
|
+
function editWithContext(document, visitor, context, state) {
|
|
2249
|
+
if (visitor && typeof visitor === "object" && !Array.isArray(visitor)) return editPaths(document, visitor, state, context);
|
|
2250
|
+
assertValid(document);
|
|
2251
|
+
if (typeof visitor !== "function") throw new TypeError("Expected a synchronous WXML edit visitor.");
|
|
2252
|
+
const session = {
|
|
2253
|
+
document,
|
|
2254
|
+
context,
|
|
2255
|
+
active: true,
|
|
2256
|
+
hasStructuralInvalidations: false,
|
|
2257
|
+
edits: /* @__PURE__ */ new Map()
|
|
2258
|
+
};
|
|
2259
|
+
const stack = document.children.map((node) => new EditableHandle(session, node)).reverse();
|
|
2260
|
+
try {
|
|
2261
|
+
while (stack.length) {
|
|
2262
|
+
const handle = stack.pop();
|
|
2263
|
+
if (session.hasStructuralInvalidations && handle.isInvalidated()) continue;
|
|
2264
|
+
const { node } = handle;
|
|
2265
|
+
const result = visitor(handle);
|
|
2266
|
+
if (result !== null && (typeof result === "object" || typeof result === "function") && typeof result.then === "function") {
|
|
2267
|
+
Promise.resolve(result).catch(() => {});
|
|
2268
|
+
throw new TypeError("WXML edit visitors must be synchronous; Promise results are not supported.");
|
|
2269
|
+
}
|
|
2270
|
+
if (result === false || node.kind !== "element" || session.hasStructuralInvalidations && handle.isInvalidated()) continue;
|
|
2271
|
+
for (let index = node.children.length - 1; index >= 0; index--) {
|
|
2272
|
+
const child = node.children[index];
|
|
2273
|
+
stack.push(new EditableHandle(session, child, handle));
|
|
2274
|
+
}
|
|
2275
|
+
}
|
|
2276
|
+
session.active = false;
|
|
2277
|
+
if (!session.edits.size) return copyDocument(document, context);
|
|
2278
|
+
const next = session.rawOwners?.size ? parseRawEdits(session) : createPatchedDocument(session) ?? parseCompleteEdits(session);
|
|
2279
|
+
assertValid(next);
|
|
2280
|
+
return next;
|
|
2281
|
+
} finally {
|
|
2282
|
+
session.active = false;
|
|
2283
|
+
session.edits.clear();
|
|
2284
|
+
session.rawOwners?.clear();
|
|
2285
|
+
}
|
|
2286
|
+
}
|
|
2287
|
+
function edit(document, visitor, state) {
|
|
2288
|
+
return editWithContext(document, visitor, requireContext(document), state);
|
|
2289
|
+
}
|
|
2290
|
+
// src/dialect.ts
|
|
2291
|
+
const states = /* @__PURE__ */ new WeakMap();
|
|
2292
|
+
/** 校验并冻结方言声明,匹配结构不通过公开对象暴露。 */
|
|
2293
|
+
function compileDialect(definition) {
|
|
2294
|
+
if (!definition || typeof definition !== "object" || Array.isArray(definition)) throw new TypeError("Expected a dialect definition.");
|
|
2295
|
+
const { id, syntax, rawTags } = definition;
|
|
2296
|
+
if (typeof id !== "string" || !id) throw new TypeError("Expected a nonempty dialect id.");
|
|
2297
|
+
if (syntax !== "legacy" && syntax !== "xml") throw new TypeError(`Unsupported template syntax: ${String(syntax)}.`);
|
|
2298
|
+
if (!Array.isArray(rawTags)) throw new TypeError("Expected rawTags to be an array of nonempty names.");
|
|
2299
|
+
const names = Object.freeze(rawTags.slice());
|
|
2300
|
+
let rawTagInitials = 0;
|
|
2301
|
+
for (const name of names) {
|
|
2302
|
+
if (typeof name !== "string" || !name) throw new TypeError("Expected rawTags to be an array of nonempty names.");
|
|
2303
|
+
rawTagInitials |= 1 << name.charCodeAt(0);
|
|
2304
|
+
}
|
|
2305
|
+
const matcher = new RegExp(`^(?:${names.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})$`, "i");
|
|
2306
|
+
const compiled = Object.freeze({
|
|
2307
|
+
id,
|
|
2308
|
+
syntax,
|
|
2309
|
+
rawTags: names
|
|
2310
|
+
});
|
|
2311
|
+
states.set(compiled, Object.freeze({
|
|
2312
|
+
syntax,
|
|
2313
|
+
rawTagPattern: matcher,
|
|
2314
|
+
rawTagInitials
|
|
2315
|
+
}));
|
|
2316
|
+
return compiled;
|
|
2317
|
+
}
|
|
2318
|
+
/** 绑定解析器或兼容上下文时校验一次,后续扫描直接使用私有状态。 */
|
|
2319
|
+
function compiledState(dialect) {
|
|
2320
|
+
const state = states.get(dialect);
|
|
2321
|
+
if (!state) throw new TypeError("Expected a compiled dialect.");
|
|
2322
|
+
return state;
|
|
2323
|
+
}
|
|
2324
|
+
// src/serialize.ts
|
|
2325
|
+
/** 保留模式输出当前文档源码;规范模式逐节点输出并保留所有文本。 */
|
|
2326
|
+
function serialize(document, options = {}) {
|
|
2327
|
+
if (document.diagnostics.length && document.diagnostics.some((diagnostic) => diagnostic.severity === "error")) throw new TypeError("Cannot serialize an invalid WXML document.");
|
|
2328
|
+
if (options.mode === void 0 || options.mode === "preserve") return document.source;
|
|
2329
|
+
if (options.mode !== "canonical") throw new TypeError(`Unknown WXML serialization mode: ${String(options.mode)}.`);
|
|
2330
|
+
const overlay = editOverlays.get(document);
|
|
2331
|
+
let output = "";
|
|
2332
|
+
const stack = [...(overlay?.original ?? document).children].reverse();
|
|
2333
|
+
while (stack.length) {
|
|
2334
|
+
const node = stack.pop();
|
|
2335
|
+
if (typeof node === "string") output += node;
|
|
2336
|
+
else if (node.kind !== "element") output += overlay?.text.get(node) ?? node.data;
|
|
2337
|
+
else {
|
|
2338
|
+
const element = overlay?.elements.get(node) ?? node;
|
|
2339
|
+
output += `<${element.name}`;
|
|
2340
|
+
for (const attribute of element.attributes) output += ` ${canonicalAttribute(attribute, document.syntax)}`;
|
|
2341
|
+
output += node.selfClosing ? " />" : ">";
|
|
2342
|
+
if (!node.selfClosing) {
|
|
2343
|
+
stack.push(`</${element.name}>`);
|
|
2344
|
+
for (let index = node.children.length - 1; index >= 0; index--) stack.push(node.children[index]);
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
}
|
|
2348
|
+
return output;
|
|
2349
|
+
}
|
|
2350
|
+
// src/walk.ts
|
|
2351
|
+
/** 迭代遍历文档,enter 返回 false 时跳过子树,避免深模板递归溢出。 */
|
|
2352
|
+
function walk(document, visitor) {
|
|
2353
|
+
const roots = Array.isArray(document) ? document : document.children;
|
|
2354
|
+
const stack = [];
|
|
2355
|
+
for (let index = roots.length - 1; index >= 0; index--) stack.push({ node: roots[index] });
|
|
2356
|
+
while (stack.length) {
|
|
2357
|
+
const frame = stack.pop();
|
|
2358
|
+
if (frame.leave) {
|
|
2359
|
+
visitor.leave?.(frame.node, frame.parent);
|
|
2360
|
+
continue;
|
|
2361
|
+
}
|
|
2362
|
+
const descend = visitor.enter?.(frame.node, frame.parent) !== false;
|
|
2363
|
+
if (visitor.leave) stack.push({
|
|
2364
|
+
...frame,
|
|
2365
|
+
leave: true
|
|
2366
|
+
});
|
|
2367
|
+
if (descend && frame.node.kind === "element") for (let index = frame.node.children.length - 1; index >= 0; index--) stack.push({
|
|
2368
|
+
node: frame.node.children[index],
|
|
2369
|
+
parent: frame.node
|
|
2370
|
+
});
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2373
|
+
// src/core.ts
|
|
2374
|
+
/** 将编译方言绑定一次,同一解析器复用不可变配置和 matcher。 */
|
|
2375
|
+
function createParser(dialect, options = {}) {
|
|
2376
|
+
const state = compiledState(dialect);
|
|
2377
|
+
if (!options || typeof options !== "object" || Array.isArray(options)) throw new TypeError("Expected parser options.");
|
|
2378
|
+
const resolver = options.resolveScriptBoundary;
|
|
2379
|
+
if (resolver !== void 0 && typeof resolver !== "function") throw new TypeError("Expected resolveScriptBoundary to be a function.");
|
|
2380
|
+
const context = Object.freeze({
|
|
2381
|
+
dialect,
|
|
2382
|
+
state,
|
|
2383
|
+
resolveScriptBoundary: resolver
|
|
2384
|
+
});
|
|
2385
|
+
return Object.freeze({
|
|
2386
|
+
dialect,
|
|
2387
|
+
scan: (source) => scanCompiledDocument(source, state, context.resolveScriptBoundary, false),
|
|
2388
|
+
scanComments: (source) => scanCompiledDocument(source, state, context.resolveScriptBoundary, false, true),
|
|
2389
|
+
parse: (source) => parseContext(source, context)
|
|
2390
|
+
});
|
|
2391
|
+
}
|
|
2392
|
+
export { scanScriptBoundaryCandidates as _, compiledState as a, TemplateLexicalError as b, decodeAttribute as c, parseContext as d, requireContext as f, findTrailingScriptComment as g, findScriptClosingTag as h, compileDialect as i, bindDocument as l, readTag as m, walk as n, edit as o, scanCompiledDocument as p, serialize as r, editWithContext as s, createParser as t, contextFor as u, expressionEnd as v, interpolations as y };
|