@mpcore/simulator 0.6.2 → 0.7.0
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/README.md +15 -1
- package/dist/index.d.mts +254 -170
- package/dist/index.mjs +842 -222
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { dirname, join, normalize, posix, relative } from "pathe";
|
|
2
|
+
import { WEVU_NATIVE_INSTANCE_KEY, WEVU_PAGE_SCROLL_EVENT_CONTRACT_KEY, WEVU_ROUTE_EVENT_CONTRACT_KEY } from "@weapp-core/constants";
|
|
2
3
|
import fs from "node:fs";
|
|
3
4
|
import path from "node:path";
|
|
4
5
|
import { parseExpression } from "@babel/parser";
|
|
5
6
|
import postcss from "postcss";
|
|
6
|
-
import { WEVU_NATIVE_INSTANCE_KEY } from "@weapp-core/constants";
|
|
7
7
|
import { parseDocument } from "htmlparser2";
|
|
8
8
|
import { selectAll } from "css-select";
|
|
9
9
|
import { AttributeAction, SelectorType, parse } from "css-what";
|
|
@@ -241,7 +241,13 @@ var StartupNavigationQueue = class {
|
|
|
241
241
|
}
|
|
242
242
|
}
|
|
243
243
|
run(option, operation) {
|
|
244
|
-
if (!this.collecting)
|
|
244
|
+
if (!this.collecting) {
|
|
245
|
+
const submitted = option && { ...option };
|
|
246
|
+
this.scheduler.queueMicrotask(() => {
|
|
247
|
+
this.scheduler.setTimeout(() => operation(submitted), 0);
|
|
248
|
+
});
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
245
251
|
const defer = (callback) => callback ? (...args) => {
|
|
246
252
|
this.scheduler.setTimeout(() => callback(...args), 0);
|
|
247
253
|
} : void 0;
|
|
@@ -500,6 +506,14 @@ function createHeadlessWx(driver, runtimeConsole = console) {
|
|
|
500
506
|
navigateBack: true,
|
|
501
507
|
navigateTo: true,
|
|
502
508
|
nextTick: true,
|
|
509
|
+
offBeforeAppRoute: true,
|
|
510
|
+
offBeforePageUnload: true,
|
|
511
|
+
offAppRoute: true,
|
|
512
|
+
offAppRouteDone: true,
|
|
513
|
+
onBeforeAppRoute: true,
|
|
514
|
+
onBeforePageUnload: true,
|
|
515
|
+
onAppRoute: true,
|
|
516
|
+
onAppRouteDone: true,
|
|
503
517
|
offAppHide: true,
|
|
504
518
|
offAppShow: true,
|
|
505
519
|
offNetworkStatusChange: true,
|
|
@@ -547,6 +561,8 @@ function createHeadlessWx(driver, runtimeConsole = console) {
|
|
|
547
561
|
};
|
|
548
562
|
return {
|
|
549
563
|
...eventBus,
|
|
564
|
+
[WEVU_ROUTE_EVENT_CONTRACT_KEY]: 1,
|
|
565
|
+
[WEVU_PAGE_SCROLL_EVENT_CONTRACT_KEY]: 1,
|
|
550
566
|
canIUse: (schema) => typeof schema === "string" && schema.trim() !== "" && resolveCapabilityValue(capabilityTree, schema.trim()) != null,
|
|
551
567
|
canvasToTempFilePath: (option) => invokeWxApi(() => driver.canvasToTempFilePath(option), option),
|
|
552
568
|
chooseImage: (option) => invokeWxApi(() => driver.chooseImage(option ?? {}), option),
|
|
@@ -674,6 +690,14 @@ function createHeadlessWx(driver, runtimeConsole = console) {
|
|
|
674
690
|
driver.navigateTo(deferred);
|
|
675
691
|
}, deferred)),
|
|
676
692
|
nextTick: (callback) => driver.nextTick(callback),
|
|
693
|
+
offBeforeAppRoute: (listener) => driver.offBeforeAppRoute(listener),
|
|
694
|
+
offBeforePageUnload: (listener) => driver.offBeforePageUnload(listener),
|
|
695
|
+
offAppRoute: (listener) => driver.offAppRoute(listener),
|
|
696
|
+
offAppRouteDone: (listener) => driver.offAppRouteDone(listener),
|
|
697
|
+
onBeforeAppRoute: (listener) => driver.onBeforeAppRoute(listener),
|
|
698
|
+
onBeforePageUnload: (listener) => driver.onBeforePageUnload(listener),
|
|
699
|
+
onAppRoute: (listener) => driver.onAppRoute(listener),
|
|
700
|
+
onAppRouteDone: (listener) => driver.onAppRouteDone(listener),
|
|
677
701
|
offAppHide: (callback) => driver.offAppHide(callback),
|
|
678
702
|
offAppShow: (callback) => driver.offAppShow(callback),
|
|
679
703
|
offNetworkStatusChange: (callback) => driver.offNetworkStatusChange(callback),
|
|
@@ -1118,6 +1142,7 @@ var RuntimeScheduler = class {
|
|
|
1118
1142
|
intervals = /* @__PURE__ */ new Set();
|
|
1119
1143
|
timeouts = /* @__PURE__ */ new Set();
|
|
1120
1144
|
closed = false;
|
|
1145
|
+
canceledHandle;
|
|
1121
1146
|
onError;
|
|
1122
1147
|
constructor(onError) {
|
|
1123
1148
|
this.onError = onError;
|
|
@@ -1139,7 +1164,7 @@ var RuntimeScheduler = class {
|
|
|
1139
1164
|
this.timeouts.clear();
|
|
1140
1165
|
}
|
|
1141
1166
|
queueMicrotask(handler) {
|
|
1142
|
-
if (this.closed)
|
|
1167
|
+
if (this.closed) return;
|
|
1143
1168
|
queueMicrotask(() => {
|
|
1144
1169
|
if (this.closed) return;
|
|
1145
1170
|
try {
|
|
@@ -1150,7 +1175,7 @@ var RuntimeScheduler = class {
|
|
|
1150
1175
|
});
|
|
1151
1176
|
}
|
|
1152
1177
|
setInterval(handler, timeout, ...args) {
|
|
1153
|
-
if (this.closed)
|
|
1178
|
+
if (this.closed) return this.getCanceledHandle();
|
|
1154
1179
|
const handle = setInterval(() => {
|
|
1155
1180
|
try {
|
|
1156
1181
|
handler(...args);
|
|
@@ -1162,7 +1187,7 @@ var RuntimeScheduler = class {
|
|
|
1162
1187
|
return handle;
|
|
1163
1188
|
}
|
|
1164
1189
|
setTimeout(handler, timeout, ...args) {
|
|
1165
|
-
if (this.closed)
|
|
1190
|
+
if (this.closed) return this.getCanceledHandle();
|
|
1166
1191
|
const handle = setTimeout(() => {
|
|
1167
1192
|
this.timeouts.delete(handle);
|
|
1168
1193
|
try {
|
|
@@ -1174,6 +1199,14 @@ var RuntimeScheduler = class {
|
|
|
1174
1199
|
this.timeouts.add(handle);
|
|
1175
1200
|
return handle;
|
|
1176
1201
|
}
|
|
1202
|
+
/** 已销毁 realm 的 Promise 回调不能复活任务,也不能向外泄漏异步拒绝。 */
|
|
1203
|
+
getCanceledHandle() {
|
|
1204
|
+
if (!this.canceledHandle) {
|
|
1205
|
+
this.canceledHandle = setTimeout(() => {}, 0);
|
|
1206
|
+
clearTimeout(this.canceledHandle);
|
|
1207
|
+
}
|
|
1208
|
+
return this.canceledHandle;
|
|
1209
|
+
}
|
|
1177
1210
|
};
|
|
1178
1211
|
//#endregion
|
|
1179
1212
|
//#region src/kernel/runtimeKernel.ts
|
|
@@ -1852,7 +1885,7 @@ const loadingPages = /* @__PURE__ */ new WeakSet();
|
|
|
1852
1885
|
function isPageBeforeReady(page) {
|
|
1853
1886
|
return loadingPages.has(page);
|
|
1854
1887
|
}
|
|
1855
|
-
function runInitialPageLifecycles(page, query, scheduler, isAlive, renderComponents) {
|
|
1888
|
+
function runInitialPageLifecycles(page, query, scheduler, isAlive, renderComponents, onReady) {
|
|
1856
1889
|
loadingPages.add(page);
|
|
1857
1890
|
try {
|
|
1858
1891
|
renderComponents?.();
|
|
@@ -1867,7 +1900,10 @@ function runInitialPageLifecycles(page, query, scheduler, isAlive, renderCompone
|
|
|
1867
1900
|
scheduler.setTimeout(() => {
|
|
1868
1901
|
if (!isAlive()) return;
|
|
1869
1902
|
page.onReady?.();
|
|
1870
|
-
if (isAlive())
|
|
1903
|
+
if (isAlive()) {
|
|
1904
|
+
page.onRouteDone?.({});
|
|
1905
|
+
onReady?.();
|
|
1906
|
+
}
|
|
1871
1907
|
}, 0);
|
|
1872
1908
|
}
|
|
1873
1909
|
//#endregion
|
|
@@ -1939,6 +1975,48 @@ function linkRenderedParents(root, parent = null) {
|
|
|
1939
1975
|
for (const child of root.children ?? []) linkRenderedParents(child, root);
|
|
1940
1976
|
}
|
|
1941
1977
|
//#endregion
|
|
1978
|
+
//#region src/view/templateInterpolation.ts
|
|
1979
|
+
/** 按表达式词法寻找插值边界,字符串和对象内部的大括号不结束绑定。 */
|
|
1980
|
+
function templateInterpolations(source) {
|
|
1981
|
+
const ranges = [];
|
|
1982
|
+
let offset = 0;
|
|
1983
|
+
while (offset < source.length) {
|
|
1984
|
+
const start = source.indexOf("{{", offset);
|
|
1985
|
+
if (start < 0) break;
|
|
1986
|
+
let quote;
|
|
1987
|
+
let depth = 0;
|
|
1988
|
+
let closed = false;
|
|
1989
|
+
for (let i = start + 2; i < source.length; i++) {
|
|
1990
|
+
const char = source[i];
|
|
1991
|
+
if (quote) {
|
|
1992
|
+
if (char === "\\") i++;
|
|
1993
|
+
else if (char === quote) quote = void 0;
|
|
1994
|
+
} else if (char === "'" || char === "\"") quote = char;
|
|
1995
|
+
else if (char === "{") depth++;
|
|
1996
|
+
else if (char === "}") {
|
|
1997
|
+
if (depth === 0 && source[i + 1] === "}") {
|
|
1998
|
+
ranges.push({
|
|
1999
|
+
start,
|
|
2000
|
+
end: i + 2,
|
|
2001
|
+
expression: source.slice(start + 2, i)
|
|
2002
|
+
});
|
|
2003
|
+
offset = i + 2;
|
|
2004
|
+
closed = true;
|
|
2005
|
+
break;
|
|
2006
|
+
}
|
|
2007
|
+
depth--;
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
if (!closed) break;
|
|
2011
|
+
}
|
|
2012
|
+
return ranges;
|
|
2013
|
+
}
|
|
2014
|
+
function isTemplateExpression(value) {
|
|
2015
|
+
const trimmed = value.trim();
|
|
2016
|
+
const ranges = templateInterpolations(trimmed);
|
|
2017
|
+
return ranges.length === 1 && ranges[0].start === 0 && ranges[0].end === trimmed.length;
|
|
2018
|
+
}
|
|
2019
|
+
//#endregion
|
|
1942
2020
|
//#region src/view/templateExpression.ts
|
|
1943
2021
|
const expressionCache = /* @__PURE__ */ new Map();
|
|
1944
2022
|
const BLOCKED_MEMBER_KEYS = /* @__PURE__ */ new Set([
|
|
@@ -1947,13 +2025,9 @@ const BLOCKED_MEMBER_KEYS = /* @__PURE__ */ new Set([
|
|
|
1947
2025
|
"prototype"
|
|
1948
2026
|
]);
|
|
1949
2027
|
const NUMERIC_DOT_PATH_RE = /\.(\d+)(?=[.[\s,}\]]|$)/g;
|
|
1950
|
-
function isMustacheOnly$3(value) {
|
|
1951
|
-
const trimmed = value.trim();
|
|
1952
|
-
return trimmed.startsWith("{{") && trimmed.endsWith("}}") && !trimmed.includes("{{", 2);
|
|
1953
|
-
}
|
|
1954
2028
|
function unwrapMustacheExpression(expression) {
|
|
1955
2029
|
const normalized = expression.trim();
|
|
1956
|
-
if (
|
|
2030
|
+
if (isTemplateExpression(normalized)) return normalized.slice(2, -2).trim();
|
|
1957
2031
|
return normalized;
|
|
1958
2032
|
}
|
|
1959
2033
|
function parseTemplateExpression(expression) {
|
|
@@ -2221,6 +2295,21 @@ function resolveMiniProgramComponent(ownerFilePath, request, miniprogramRootPath
|
|
|
2221
2295
|
return npmPath ? relative(miniprogramRootPath, npmPath).slice(0, -3) : localPath;
|
|
2222
2296
|
}
|
|
2223
2297
|
//#endregion
|
|
2298
|
+
//#region src/view/nodeDataset.ts
|
|
2299
|
+
const DATASET_NAME_RE = /-([a-z])/g;
|
|
2300
|
+
function isDatasetAttribute(attributeName) {
|
|
2301
|
+
return attributeName.startsWith("data-") && !attributeName.startsWith("data-sim-");
|
|
2302
|
+
}
|
|
2303
|
+
function toDatasetKey(attributeName) {
|
|
2304
|
+
return attributeName.slice(5).replace(DATASET_NAME_RE, (_match, char) => char.toUpperCase());
|
|
2305
|
+
}
|
|
2306
|
+
function collectNodeDataset(node) {
|
|
2307
|
+
if (node.dataset) return { ...node.dataset };
|
|
2308
|
+
const dataset = {};
|
|
2309
|
+
for (const [key, value] of Object.entries(node.attribs ?? {})) if (isDatasetAttribute(key)) dataset[toDatasetKey(key)] = value;
|
|
2310
|
+
return dataset;
|
|
2311
|
+
}
|
|
2312
|
+
//#endregion
|
|
2224
2313
|
//#region src/view/eventBinding.ts
|
|
2225
2314
|
const LEGACY_EVENT_NAME_RE = /^\w+$/;
|
|
2226
2315
|
const EVENT_BINDING_PREFIXES = [
|
|
@@ -2274,6 +2363,29 @@ function collectMiniProgramEventBindings(attributes = {}) {
|
|
|
2274
2363
|
function resolveMiniProgramEventBinding(attributes, eventName) {
|
|
2275
2364
|
return collectMiniProgramEventBindings(attributes).get(eventName) ?? null;
|
|
2276
2365
|
}
|
|
2366
|
+
/** 冒泡时保留原始 target,currentTarget 则属于当前执行绑定的节点。 */
|
|
2367
|
+
function createMiniProgramEventPayload(node, eventName, event, origin = node) {
|
|
2368
|
+
const dataset = collectNodeDataset(node);
|
|
2369
|
+
const nodeId = node.attribs?.id ?? "";
|
|
2370
|
+
const isOrigin = node === origin;
|
|
2371
|
+
return {
|
|
2372
|
+
bubbles: false,
|
|
2373
|
+
capturePhase: false,
|
|
2374
|
+
composed: false,
|
|
2375
|
+
currentTarget: {
|
|
2376
|
+
dataset: isOrigin ? event.currentTarget?.dataset ?? event.dataset ?? dataset : dataset,
|
|
2377
|
+
id: isOrigin ? event.currentTarget?.id ?? event.id ?? nodeId : nodeId
|
|
2378
|
+
},
|
|
2379
|
+
detail: event.detail,
|
|
2380
|
+
mark: event.mark,
|
|
2381
|
+
target: {
|
|
2382
|
+
dataset: event.target?.dataset ?? event.dataset ?? (isOrigin ? dataset : collectNodeDataset(origin)),
|
|
2383
|
+
id: event.target?.id ?? event.id ?? origin.attribs?.id ?? ""
|
|
2384
|
+
},
|
|
2385
|
+
timeStamp: Date.now(),
|
|
2386
|
+
type: eventName
|
|
2387
|
+
};
|
|
2388
|
+
}
|
|
2277
2389
|
//#endregion
|
|
2278
2390
|
//#region src/view/selectorQueryScope.ts
|
|
2279
2391
|
const MPCORE_COMPONENT_SCOPE_ID = "__mpcoreComponentScopeId";
|
|
@@ -2334,21 +2446,6 @@ function resolveSelectorQueryScopeSnapshot(scope, snapshots) {
|
|
|
2334
2446
|
return null;
|
|
2335
2447
|
}
|
|
2336
2448
|
//#endregion
|
|
2337
|
-
//#region src/view/nodeDataset.ts
|
|
2338
|
-
const DATASET_NAME_RE = /-([a-z])/g;
|
|
2339
|
-
function isDatasetAttribute(attributeName) {
|
|
2340
|
-
return attributeName.startsWith("data-") && !attributeName.startsWith("data-sim-");
|
|
2341
|
-
}
|
|
2342
|
-
function toDatasetKey(attributeName) {
|
|
2343
|
-
return attributeName.slice(5).replace(DATASET_NAME_RE, (_match, char) => char.toUpperCase());
|
|
2344
|
-
}
|
|
2345
|
-
function collectNodeDataset(node) {
|
|
2346
|
-
if (node.dataset) return { ...node.dataset };
|
|
2347
|
-
const dataset = {};
|
|
2348
|
-
for (const [key, value] of Object.entries(node.attribs ?? {})) if (isDatasetAttribute(key)) dataset[toDatasetKey(key)] = value;
|
|
2349
|
-
return dataset;
|
|
2350
|
-
}
|
|
2351
|
-
//#endregion
|
|
2352
2449
|
//#region src/view/templateImports.ts
|
|
2353
2450
|
function createImportedTemplateState(root, filePath, loadImport, loadWxs) {
|
|
2354
2451
|
const documents = /* @__PURE__ */ new Map();
|
|
@@ -2400,29 +2497,87 @@ function createImportedTemplateState(root, filePath, loadImport, loadWxs) {
|
|
|
2400
2497
|
}
|
|
2401
2498
|
//#endregion
|
|
2402
2499
|
//#region src/view/templateText.ts
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2500
|
+
function interpolateTemplateText(input, data, emptyNull = false) {
|
|
2501
|
+
let output = "";
|
|
2502
|
+
let offset = 0;
|
|
2503
|
+
for (const range of templateInterpolations(input)) {
|
|
2504
|
+
const value = resolveTemplateExpression(data, range.expression);
|
|
2505
|
+
output += input.slice(offset, range.start) + (value === void 0 || emptyNull && value === null ? "" : String(value));
|
|
2506
|
+
offset = range.end;
|
|
2507
|
+
}
|
|
2508
|
+
return output + input.slice(offset);
|
|
2509
|
+
}
|
|
2510
|
+
//#endregion
|
|
2511
|
+
//#region src/view/templateAttributes.ts
|
|
2512
|
+
function decodeAttribute(raw, quote) {
|
|
2513
|
+
const source = raw.replaceAll(`\\${quote}`, quote);
|
|
2514
|
+
let value = "";
|
|
2515
|
+
let offset = 0;
|
|
2516
|
+
for (const range of templateInterpolations(source)) {
|
|
2517
|
+
value += source.slice(offset, range.start).replace(/\\([\s\S])/g, "$1");
|
|
2518
|
+
value += source.slice(range.start, range.end);
|
|
2519
|
+
offset = range.end;
|
|
2520
|
+
}
|
|
2521
|
+
return value + source.slice(offset).replace(/\\([\s\S])/g, "$1");
|
|
2522
|
+
}
|
|
2523
|
+
/** 先保存 WXML 属性词法值,再交给 HTML parser 构建标签树,避免反斜杠引号提前截断属性。 */
|
|
2524
|
+
function maskTemplateAttributes(source) {
|
|
2525
|
+
const attributes = /* @__PURE__ */ new Map();
|
|
2526
|
+
if (!source.includes("\\")) return {
|
|
2527
|
+
source,
|
|
2528
|
+
attributes
|
|
2529
|
+
};
|
|
2530
|
+
let prefix = "__mpcore_quoted_attribute_";
|
|
2531
|
+
while (source.includes(prefix)) prefix += "_";
|
|
2532
|
+
let output = "";
|
|
2533
|
+
let offset = 0;
|
|
2534
|
+
for (let i = 0; i < source.length; i++) {
|
|
2535
|
+
if (source.startsWith("<!--", i)) {
|
|
2536
|
+
const end = source.indexOf("-->", i + 4);
|
|
2537
|
+
i = end < 0 ? source.length : end + 2;
|
|
2538
|
+
continue;
|
|
2539
|
+
}
|
|
2540
|
+
if (source[i] !== "<" || !/[a-z_]/i.test(source[i + 1] ?? "")) continue;
|
|
2541
|
+
for (i++; i < source.length && source[i] !== ">"; i++) {
|
|
2542
|
+
if (source[i] !== "\"" && source[i] !== "'") continue;
|
|
2543
|
+
const quote = source[i];
|
|
2544
|
+
const start = i + 1;
|
|
2545
|
+
for (i++; i < source.length && source[i] !== quote; i++) if (source[i] === "\\") i++;
|
|
2546
|
+
if (i >= source.length) break;
|
|
2547
|
+
const raw = source.slice(start, i);
|
|
2548
|
+
if (!raw.includes("\\")) continue;
|
|
2549
|
+
const token = `${prefix}${attributes.size}`;
|
|
2550
|
+
attributes.set(token, decodeAttribute(raw, quote));
|
|
2551
|
+
output += source.slice(offset, start) + token;
|
|
2552
|
+
offset = i;
|
|
2553
|
+
}
|
|
2554
|
+
}
|
|
2555
|
+
return {
|
|
2556
|
+
source: output + source.slice(offset),
|
|
2557
|
+
attributes
|
|
2558
|
+
};
|
|
2409
2559
|
}
|
|
2410
2560
|
//#endregion
|
|
2411
2561
|
//#region src/view/wxsDocument.ts
|
|
2412
2562
|
const INLINE_WXS_RE = /<wxs\b([^>]*)(?<!\/)>([\s\S]*?)<\/wxs\s*>/g;
|
|
2413
2563
|
function parseWxsTemplateDocument(templateSource) {
|
|
2414
2564
|
const scripts = [];
|
|
2415
|
-
const
|
|
2565
|
+
const masked = maskTemplateAttributes(templateSource.replace(INLINE_WXS_RE, (_match, attributes, script) => {
|
|
2416
2566
|
return `<wxs data-sim-wxs="${scripts.push(script) - 1}"${attributes}>wxs-source</wxs>`;
|
|
2417
|
-
});
|
|
2418
|
-
const document = parseDocument(`<page>${source}</page>`, {
|
|
2567
|
+
}));
|
|
2568
|
+
const document = parseDocument(`<page>${masked.source}</page>`, {
|
|
2419
2569
|
xmlMode: false,
|
|
2420
2570
|
decodeEntities: false,
|
|
2421
2571
|
lowerCaseAttributeNames: false,
|
|
2422
2572
|
lowerCaseTags: false,
|
|
2423
2573
|
recognizeSelfClosing: true
|
|
2424
2574
|
});
|
|
2575
|
+
if (!scripts.length && !masked.attributes.size) return document;
|
|
2576
|
+
const hasAttributes = masked.attributes.size > 0;
|
|
2425
2577
|
const restore = (node) => {
|
|
2578
|
+
if (hasAttributes && node.attribs) {
|
|
2579
|
+
for (const [name, token] of Object.entries(node.attribs)) if (masked.attributes.has(token)) node.attribs[name] = masked.attributes.get(token);
|
|
2580
|
+
}
|
|
2426
2581
|
if (node.name === "wxs" && Object.hasOwn(node.attribs ?? {}, "data-sim-wxs")) {
|
|
2427
2582
|
node.children[0].data = scripts[Number(node.attribs["data-sim-wxs"])];
|
|
2428
2583
|
delete node.attribs["data-sim-wxs"];
|
|
@@ -2434,7 +2589,6 @@ function parseWxsTemplateDocument(templateSource) {
|
|
|
2434
2589
|
}
|
|
2435
2590
|
//#endregion
|
|
2436
2591
|
//#region src/browser/render/shared.ts
|
|
2437
|
-
const TEMPLATE_INTERPOLATION_RE$2 = /\{\{([^{}]+)\}\}/g;
|
|
2438
2592
|
const LEADING_SLASH_RE$5 = /^\/+/;
|
|
2439
2593
|
const EVENT_BINDING_ATTRS$1 = [
|
|
2440
2594
|
"bindtap",
|
|
@@ -2454,8 +2608,7 @@ const STRUCTURAL_ATTRS$1 = [
|
|
|
2454
2608
|
const CLASS_SPLIT_RE$1 = /\s+/;
|
|
2455
2609
|
const JS_FILE_RE$1 = /\.js$/;
|
|
2456
2610
|
function isMustacheOnly$2(value) {
|
|
2457
|
-
|
|
2458
|
-
return trimmed.startsWith("{{") && trimmed.endsWith("}}") && !trimmed.includes("{{", 2);
|
|
2611
|
+
return isTemplateExpression(value);
|
|
2459
2612
|
}
|
|
2460
2613
|
function collectDataset$1(node, source) {
|
|
2461
2614
|
if (node.dataset) return collectNodeDataset(node);
|
|
@@ -2480,17 +2633,11 @@ function cloneNode$1(node) {
|
|
|
2480
2633
|
children: node.children?.map((child) => cloneNode$1(child))
|
|
2481
2634
|
};
|
|
2482
2635
|
}
|
|
2483
|
-
function resolveValueByPath$2(source, expression) {
|
|
2484
|
-
return resolveTemplateExpression(source, expression) ?? "";
|
|
2485
|
-
}
|
|
2486
2636
|
function resolveRawValueByPath$2(source, expression) {
|
|
2487
2637
|
return resolveTemplateExpression(source, expression);
|
|
2488
2638
|
}
|
|
2489
2639
|
function interpolateTemplate$2(input, data) {
|
|
2490
|
-
return input
|
|
2491
|
-
const value = resolveValueByPath$2(data, expression);
|
|
2492
|
-
return typeof value === "string" ? value : String(value);
|
|
2493
|
-
});
|
|
2640
|
+
return interpolateTemplateText(input, data, true);
|
|
2494
2641
|
}
|
|
2495
2642
|
function readTemplateSource$1(files, filePath) {
|
|
2496
2643
|
const templateSource = readBrowserVirtualFile(files, filePath);
|
|
@@ -2615,7 +2762,7 @@ function resolveUsingComponents$1(context, ownerJsonPath, ownerFilePath) {
|
|
|
2615
2762
|
return /* @__PURE__ */ new Map();
|
|
2616
2763
|
}
|
|
2617
2764
|
}
|
|
2618
|
-
function resolveComponentGenerics$1(context, hostNode, ownerJsonPath, ownerFilePath, componentFilePath) {
|
|
2765
|
+
function resolveComponentGenerics$1(context, hostNode, ownerJsonPath, ownerFilePath, componentFilePath, ownerGenerics) {
|
|
2619
2766
|
const componentJsonPath = join(context.project.miniprogramRootPath, `${componentFilePath.replace(JS_FILE_RE$1, "")}.json`);
|
|
2620
2767
|
const componentGenerics = readComponentConfig$1(context.files, componentJsonPath).componentGenerics;
|
|
2621
2768
|
if (!componentGenerics || typeof componentGenerics !== "object" || Array.isArray(componentGenerics)) return;
|
|
@@ -2623,7 +2770,7 @@ function resolveComponentGenerics$1(context, hostNode, ownerJsonPath, ownerFileP
|
|
|
2623
2770
|
const resolved = /* @__PURE__ */ new Map();
|
|
2624
2771
|
for (const [genericName, definition] of Object.entries(componentGenerics)) {
|
|
2625
2772
|
const selectedAlias = hostNode.attribs?.[`generic:${genericName}`];
|
|
2626
|
-
const selectedPath = selectedAlias ? ownerComponents.get(selectedAlias) : void 0;
|
|
2773
|
+
const selectedPath = selectedAlias ? ownerGenerics?.get(selectedAlias) ?? ownerComponents.get(selectedAlias) : void 0;
|
|
2627
2774
|
if (selectedPath) {
|
|
2628
2775
|
resolved.set(genericName, selectedPath);
|
|
2629
2776
|
continue;
|
|
@@ -2685,8 +2832,9 @@ function syncComponentProperties$1(instance, definition, nextProperties, binding
|
|
|
2685
2832
|
const previousSnapshot = instance.__propertySnapshots?.[key];
|
|
2686
2833
|
if (hasComponentPropertyValueChanged(instance.properties[key], previousSnapshot, nextValue, bindingAffected)) {
|
|
2687
2834
|
previousProperties[key] = instance.properties[key];
|
|
2688
|
-
|
|
2689
|
-
|
|
2835
|
+
const deliveredValue = cloneValue(nextValue);
|
|
2836
|
+
instance.properties[key] = deliveredValue;
|
|
2837
|
+
if (Object.hasOwn(definition.properties ?? {}, key)) instance.data[key] = deliveredValue;
|
|
2690
2838
|
changedRootKeys.push(key);
|
|
2691
2839
|
}
|
|
2692
2840
|
instance.__propertySnapshots ??= {};
|
|
@@ -2882,7 +3030,7 @@ function renderNodeTree$1(node, scope, context, ownerJsonPath, ownerFilePath, in
|
|
|
2882
3030
|
const componentScopeId = `${instancePath}/${clonedNode.name}`;
|
|
2883
3031
|
const ownerScopeId = scope.getScopeId().includes("/") ? scope.getScopeId() : void 0;
|
|
2884
3032
|
const { nextProperties, bindingExpressions } = resolveComponentProperties$1(clonedNode, scope, componentEntry.definition);
|
|
2885
|
-
const genericComponents = resolveComponentGenerics$1(context, clonedNode, ownerJsonPath, ownerFilePath, componentEntry.filePath);
|
|
3033
|
+
const genericComponents = resolveComponentGenerics$1(context, clonedNode, ownerJsonPath, ownerFilePath, componentEntry.filePath, scope.genericComponents);
|
|
2886
3034
|
const slots = collectComponentSlots$1(clonedNode, scope, ownerJsonPath, ownerFilePath, componentScopeId, templateRenderState);
|
|
2887
3035
|
let componentInstance = context.componentCache.get(componentScopeId);
|
|
2888
3036
|
if (!componentInstance) componentInstance = createBrowserComponentInstance(componentScopeId, context, clonedNode, componentEntry, nextProperties, ownerScopeId, (instance) => {
|
|
@@ -2911,6 +3059,7 @@ function renderNodeTree$1(node, scope, context, ownerJsonPath, ownerFilePath, in
|
|
|
2911
3059
|
return renderedComponentRoot;
|
|
2912
3060
|
}
|
|
2913
3061
|
applyNodeBindings$1(clonedNode, scope);
|
|
3062
|
+
clonedNode.attribs["data-sim-node"] = instancePath;
|
|
2914
3063
|
clonedNode.children = renderChildren$1(clonedNode.children ?? [], scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/${clonedNode.name}`, seenComponentScopes, templateRenderState);
|
|
2915
3064
|
return clonedNode;
|
|
2916
3065
|
}
|
|
@@ -3188,6 +3337,18 @@ var HeadlessAppLifecycle = class {
|
|
|
3188
3337
|
}
|
|
3189
3338
|
};
|
|
3190
3339
|
//#endregion
|
|
3340
|
+
//#region src/runtime/packageAsset.ts
|
|
3341
|
+
const assetExtension = /\.(?:png|jpe?g|gif|svg|cer|mp3|aac|m4a|mp4|wav|ogg|silk|wasm|br|cur|ico|skel|crt|cert|gltf|glb|bin|ktx|hdr|exr|xnet|pem)$/i;
|
|
3342
|
+
/** 包内静态资源只读且每次访问宿主当前产物,不缓存为用户文件,也不允许越过包根目录。 */
|
|
3343
|
+
function createPackageAssetReader(read) {
|
|
3344
|
+
return (filePath) => {
|
|
3345
|
+
if (filePath.includes("\\") || filePath.includes(":") || filePath.includes("\0")) return;
|
|
3346
|
+
const relative = posix.normalize(filePath.replace(/^\/+/, ""));
|
|
3347
|
+
if (relative === ".." || relative.startsWith("../") || !assetExtension.test(relative)) return;
|
|
3348
|
+
return read(relative);
|
|
3349
|
+
};
|
|
3350
|
+
}
|
|
3351
|
+
//#endregion
|
|
3191
3352
|
//#region src/runtime/pageInstance.ts
|
|
3192
3353
|
const ARRAY_INDEX_PATH_RE = /\[(\d+)\]/g;
|
|
3193
3354
|
const ARRAY_INDEX_SEGMENT_RE = /^\d+$/;
|
|
@@ -3280,6 +3441,87 @@ function createPageInstance(route, definition, options = {}, pageState = {}) {
|
|
|
3280
3441
|
return instance;
|
|
3281
3442
|
}
|
|
3282
3443
|
//#endregion
|
|
3444
|
+
//#region src/runtime/routeEvents.ts
|
|
3445
|
+
/** 两种模拟器共用路由身份与监听状态,完成事件由页面就绪和宿主提交驱动。 */
|
|
3446
|
+
var HeadlessRouteEvents = class {
|
|
3447
|
+
requestRender;
|
|
3448
|
+
isCurrentPage;
|
|
3449
|
+
bus = createHeadlessUniEventBus();
|
|
3450
|
+
pages = /* @__PURE__ */ new WeakMap();
|
|
3451
|
+
nextRouteId = 0;
|
|
3452
|
+
nextWebviewId = 0;
|
|
3453
|
+
activeEvent;
|
|
3454
|
+
constructor(requestRender, isCurrentPage) {
|
|
3455
|
+
this.requestRender = requestRender;
|
|
3456
|
+
this.isCurrentPage = isCurrentPage;
|
|
3457
|
+
}
|
|
3458
|
+
on(name, listener) {
|
|
3459
|
+
this.bus.$on(name, listener);
|
|
3460
|
+
}
|
|
3461
|
+
off(name, listener) {
|
|
3462
|
+
this.bus.$off(name, listener);
|
|
3463
|
+
}
|
|
3464
|
+
begin(path, query, openType, renderer, page) {
|
|
3465
|
+
const previous = page ? this.pages.get(page) : void 0;
|
|
3466
|
+
const event = {
|
|
3467
|
+
path,
|
|
3468
|
+
query: { ...query },
|
|
3469
|
+
openType: this.nextRouteId === 0 ? "appLaunch" : openType,
|
|
3470
|
+
renderer: previous?.event.renderer ?? (renderer === "skyline" || renderer === "xr-frame" ? renderer : "webview"),
|
|
3471
|
+
routeEventId: String(++this.nextRouteId),
|
|
3472
|
+
webviewId: previous?.webviewId ?? ++this.nextWebviewId,
|
|
3473
|
+
timeStamp: Date.now()
|
|
3474
|
+
};
|
|
3475
|
+
this.activeEvent = event;
|
|
3476
|
+
this.bus.$emit("beforeAppRoute", event);
|
|
3477
|
+
return event;
|
|
3478
|
+
}
|
|
3479
|
+
attach(page, event) {
|
|
3480
|
+
const state = this.pages.get(page);
|
|
3481
|
+
this.pages.set(page, {
|
|
3482
|
+
webviewId: event.webviewId,
|
|
3483
|
+
ready: state?.ready ?? false,
|
|
3484
|
+
event,
|
|
3485
|
+
committed: false
|
|
3486
|
+
});
|
|
3487
|
+
}
|
|
3488
|
+
commit(page, event) {
|
|
3489
|
+
const state = this.pages.get(page);
|
|
3490
|
+
if (!state || state.event !== event || this.activeEvent !== event || !this.isCurrentPage(page)) return;
|
|
3491
|
+
state.committed = true;
|
|
3492
|
+
this.bus.$emit("appRoute", event);
|
|
3493
|
+
if (state.ready) this.complete(page, state);
|
|
3494
|
+
}
|
|
3495
|
+
ready(page) {
|
|
3496
|
+
const state = this.pages.get(page);
|
|
3497
|
+
if (!state) return;
|
|
3498
|
+
state.ready = true;
|
|
3499
|
+
if (state.committed) this.complete(page, state);
|
|
3500
|
+
}
|
|
3501
|
+
complete(page, state) {
|
|
3502
|
+
this.requestRender(() => {
|
|
3503
|
+
if (this.pages.get(page) !== state || this.activeEvent !== state.event || !this.isCurrentPage(page)) return;
|
|
3504
|
+
this.bus.$emit("appRouteDone", state.event);
|
|
3505
|
+
});
|
|
3506
|
+
}
|
|
3507
|
+
beforeUnload(page, event) {
|
|
3508
|
+
const state = this.pages.get(page);
|
|
3509
|
+
if (event && state) this.bus.$emit("beforePageUnload", {
|
|
3510
|
+
...event,
|
|
3511
|
+
path: page.route,
|
|
3512
|
+
query: { ...page.options },
|
|
3513
|
+
renderer: state.event.renderer,
|
|
3514
|
+
webviewId: state.webviewId,
|
|
3515
|
+
page
|
|
3516
|
+
});
|
|
3517
|
+
this.pages.delete(page);
|
|
3518
|
+
}
|
|
3519
|
+
close() {
|
|
3520
|
+
this.activeEvent = void 0;
|
|
3521
|
+
this.bus.$off();
|
|
3522
|
+
}
|
|
3523
|
+
};
|
|
3524
|
+
//#endregion
|
|
3283
3525
|
//#region src/runtime/systemInfo.ts
|
|
3284
3526
|
function createDefaultLocationResult() {
|
|
3285
3527
|
return {
|
|
@@ -4074,7 +4316,7 @@ function createHeadlessWxState(scheduler = nativeScheduler, options = {}) {
|
|
|
4074
4316
|
const readFile = (filePath, encoding) => {
|
|
4075
4317
|
normalizeEncoding(encoding);
|
|
4076
4318
|
const normalizedPath = normalizeFsPath(filePath);
|
|
4077
|
-
const fileContent = files.get(normalizedPath);
|
|
4319
|
+
const fileContent = files.get(normalizedPath) ?? options.readPackageFile?.(normalizedPath);
|
|
4078
4320
|
if (fileContent == null) throw new Error(`readFile:fail no such file or directory, open '${normalizedPath}'`);
|
|
4079
4321
|
return {
|
|
4080
4322
|
data: fileContent,
|
|
@@ -5159,27 +5401,6 @@ function queryXPathElements(root, expression) {
|
|
|
5159
5401
|
function escapeText$2(text) {
|
|
5160
5402
|
return text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
5161
5403
|
}
|
|
5162
|
-
function createEventPayload$1(node, eventName, event) {
|
|
5163
|
-
const dataset = collectNodeDataset(node);
|
|
5164
|
-
const nodeId = node.attribs?.id ?? "";
|
|
5165
|
-
return {
|
|
5166
|
-
bubbles: false,
|
|
5167
|
-
capturePhase: false,
|
|
5168
|
-
composed: false,
|
|
5169
|
-
currentTarget: {
|
|
5170
|
-
dataset: event.currentTarget?.dataset ?? event.dataset ?? dataset,
|
|
5171
|
-
id: event.currentTarget?.id ?? event.id ?? nodeId
|
|
5172
|
-
},
|
|
5173
|
-
detail: event.detail,
|
|
5174
|
-
mark: event.mark,
|
|
5175
|
-
target: {
|
|
5176
|
-
dataset: event.target?.dataset ?? event.dataset ?? dataset,
|
|
5177
|
-
id: event.target?.id ?? event.id ?? nodeId
|
|
5178
|
-
},
|
|
5179
|
-
timeStamp: Date.now(),
|
|
5180
|
-
type: eventName
|
|
5181
|
-
};
|
|
5182
|
-
}
|
|
5183
5404
|
function createValueEventDetail(value, detail) {
|
|
5184
5405
|
return {
|
|
5185
5406
|
value,
|
|
@@ -5289,9 +5510,18 @@ var HeadlessTestingNodeHandle = class HeadlessTestingNodeHandle {
|
|
|
5289
5510
|
if (!this.interactions) throw new Error("Node interactions are not available for this headless testing node.");
|
|
5290
5511
|
const normalizedEventName = eventName.trim();
|
|
5291
5512
|
if (!normalizedEventName) throw new Error("Event name must be a non-empty string in headless testing runtime.");
|
|
5513
|
+
let handlerResults;
|
|
5514
|
+
const nativeHandled = this.interactions.dispatchNativeEvent?.(this.node, normalizedEventName, event, normalizedEventName === "tap" ? (result) => {
|
|
5515
|
+
handlerResults ??= [];
|
|
5516
|
+
handlerResults.push(result);
|
|
5517
|
+
} : void 0);
|
|
5518
|
+
if (normalizedEventName === "tap" && nativeHandled) return handlerResults ? (await Promise.all(handlerResults))[0] : void 0;
|
|
5292
5519
|
const binding = resolveMiniProgramEventBinding(this.node.attribs, normalizedEventName);
|
|
5293
|
-
if (!binding?.method)
|
|
5294
|
-
|
|
5520
|
+
if (!binding?.method) {
|
|
5521
|
+
if (nativeHandled) return;
|
|
5522
|
+
throw new Error(`No ${normalizedEventName} binding was found on <${this.node.name ?? "unknown"}> in headless testing runtime.`);
|
|
5523
|
+
}
|
|
5524
|
+
return await this.interactions.callMethod(this.node.attribs?.["data-sim-scope"] ?? null, binding.method, createMiniProgramEventPayload(this.node, normalizedEventName, event));
|
|
5295
5525
|
}
|
|
5296
5526
|
async tap(event = {}) {
|
|
5297
5527
|
return await this.trigger("tap", event);
|
|
@@ -5322,7 +5552,7 @@ var HeadlessTestingNodeHandle = class HeadlessTestingNodeHandle {
|
|
|
5322
5552
|
//#endregion
|
|
5323
5553
|
//#region src/view/render.ts
|
|
5324
5554
|
const LEADING_SLASH_RE$4 = /^\/+/;
|
|
5325
|
-
const TEMPLATE_INTERPOLATION_RE
|
|
5555
|
+
const TEMPLATE_INTERPOLATION_RE = /\{\{([^{}]+)\}\}/g;
|
|
5326
5556
|
function resolveRawValueByPath$1(source, expression) {
|
|
5327
5557
|
const normalized = expression.trim();
|
|
5328
5558
|
if (!normalized) return;
|
|
@@ -5334,12 +5564,12 @@ function resolveRawValueByPath$1(source, expression) {
|
|
|
5334
5564
|
}
|
|
5335
5565
|
return current;
|
|
5336
5566
|
}
|
|
5337
|
-
function resolveValueByPath
|
|
5567
|
+
function resolveValueByPath(source, expression) {
|
|
5338
5568
|
return resolveRawValueByPath$1(source, expression) ?? "";
|
|
5339
5569
|
}
|
|
5340
5570
|
function interpolateTemplate$1(input, data) {
|
|
5341
|
-
return input.replace(TEMPLATE_INTERPOLATION_RE
|
|
5342
|
-
const value = resolveValueByPath
|
|
5571
|
+
return input.replace(TEMPLATE_INTERPOLATION_RE, (_match, expression) => {
|
|
5572
|
+
const value = resolveValueByPath(data, expression);
|
|
5343
5573
|
return typeof value === "string" ? value : String(value);
|
|
5344
5574
|
});
|
|
5345
5575
|
}
|
|
@@ -5398,35 +5628,9 @@ function renderPageTree(project, page) {
|
|
|
5398
5628
|
};
|
|
5399
5629
|
}
|
|
5400
5630
|
//#endregion
|
|
5401
|
-
//#region src/view/
|
|
5402
|
-
const LEADING_MARK_PREFIX_RE = /^mark[:\-]?/;
|
|
5403
|
-
const MARK_NAME_RE = /[:\-]([a-z])/g;
|
|
5631
|
+
//#region src/view/nodeGeometry.ts
|
|
5404
5632
|
const NUMERIC_LIKE_VALUE_RE$1 = /-?\d+(?:\.\d+)?/;
|
|
5405
|
-
|
|
5406
|
-
return attributeName.replace(LEADING_MARK_PREFIX_RE, "").replace(MARK_NAME_RE, (_match, char) => char.toUpperCase());
|
|
5407
|
-
}
|
|
5408
|
-
function collectMark(node) {
|
|
5409
|
-
const mark = {};
|
|
5410
|
-
for (const [key, value] of Object.entries(node.attribs ?? {})) {
|
|
5411
|
-
if (!(key.startsWith("mark:") || key.startsWith("mark-"))) continue;
|
|
5412
|
-
mark[toMarkKey(key)] = value;
|
|
5413
|
-
}
|
|
5414
|
-
return mark;
|
|
5415
|
-
}
|
|
5416
|
-
function createScopedRoot(node) {
|
|
5417
|
-
return {
|
|
5418
|
-
children: [node],
|
|
5419
|
-
type: "root"
|
|
5420
|
-
};
|
|
5421
|
-
}
|
|
5422
|
-
function findNodeByScopeId(root, scopeId) {
|
|
5423
|
-
if (root.attribs?.["data-sim-scope"] === scopeId) return root;
|
|
5424
|
-
for (const child of root.children ?? []) {
|
|
5425
|
-
const match = findNodeByScopeId(child, scopeId);
|
|
5426
|
-
if (match) return match;
|
|
5427
|
-
}
|
|
5428
|
-
return null;
|
|
5429
|
-
}
|
|
5633
|
+
const PIXEL_LENGTH_RE = /^\d+(?:\.\d+)?px$|^0$/;
|
|
5430
5634
|
function parseStyleDeclarations$1(styleValue) {
|
|
5431
5635
|
const declarations = {};
|
|
5432
5636
|
if (!styleValue) return declarations;
|
|
@@ -5461,18 +5665,229 @@ function resolveRect$1(node, windowInfo) {
|
|
|
5461
5665
|
width
|
|
5462
5666
|
};
|
|
5463
5667
|
}
|
|
5668
|
+
/** 仅支持显式 px 视口和单个 view 内容盒;未知样式布局不作为已测量滚动范围。 */
|
|
5669
|
+
function resolveNativeScrollBounds(node) {
|
|
5670
|
+
const viewport = parseStyleDeclarations$1(node.attribs?.style);
|
|
5671
|
+
let content;
|
|
5672
|
+
const visit = (parent) => {
|
|
5673
|
+
for (const child of parent.children ?? []) {
|
|
5674
|
+
if (child.type === "comment" || child.type === "text" && !child.data?.trim()) continue;
|
|
5675
|
+
if (child.name === "block") {
|
|
5676
|
+
if (!visit(child)) return false;
|
|
5677
|
+
} else {
|
|
5678
|
+
if (content || child.name !== "view") return false;
|
|
5679
|
+
content = child;
|
|
5680
|
+
}
|
|
5681
|
+
}
|
|
5682
|
+
return true;
|
|
5683
|
+
};
|
|
5684
|
+
if (!visit(node)) return {
|
|
5685
|
+
maxTop: 0,
|
|
5686
|
+
maxLeft: 0
|
|
5687
|
+
};
|
|
5688
|
+
const box = parseStyleDeclarations$1(content?.attribs?.style);
|
|
5689
|
+
const supported = content && !viewport.display && !box.display && !box.position && !box.transform;
|
|
5690
|
+
const extent = (dimension) => {
|
|
5691
|
+
const viewportValue = viewport[dimension];
|
|
5692
|
+
const contentValue = box[dimension];
|
|
5693
|
+
if (!supported || !viewportValue || !contentValue || !PIXEL_LENGTH_RE.test(viewportValue) || !PIXEL_LENGTH_RE.test(contentValue)) return 0;
|
|
5694
|
+
const size = parseNumericLikeValue$1(viewportValue);
|
|
5695
|
+
const contentSize = parseNumericLikeValue$1(contentValue);
|
|
5696
|
+
return size > 0 && Number.isFinite(size) && Number.isFinite(contentSize) ? Math.max(0, contentSize - size) : 0;
|
|
5697
|
+
};
|
|
5698
|
+
return {
|
|
5699
|
+
maxTop: extent("height"),
|
|
5700
|
+
maxLeft: extent("width")
|
|
5701
|
+
};
|
|
5702
|
+
}
|
|
5703
|
+
//#endregion
|
|
5704
|
+
//#region src/view/nativeNode.ts
|
|
5705
|
+
const pageScrollViews = /* @__PURE__ */ new WeakMap();
|
|
5706
|
+
function coordinate(value) {
|
|
5707
|
+
const number = Number(value);
|
|
5708
|
+
return Number.isFinite(number) ? Math.max(0, number) : 0;
|
|
5709
|
+
}
|
|
5710
|
+
function nodeKey(node) {
|
|
5711
|
+
return node.attribs?.["data-sim-node"] ?? "";
|
|
5712
|
+
}
|
|
5713
|
+
/** 原生滚动位置独立于绑定值;未改变的绑定不能覆盖用户滚动。 */
|
|
5714
|
+
function syncNativeScrollViews(page, root, emit) {
|
|
5715
|
+
let states = pageScrollViews.get(page);
|
|
5716
|
+
let present;
|
|
5717
|
+
const visit = (node) => {
|
|
5718
|
+
if (node.name === "scroll-view") {
|
|
5719
|
+
const key = nodeKey(node);
|
|
5720
|
+
present ??= /* @__PURE__ */ new Set();
|
|
5721
|
+
present.add(key);
|
|
5722
|
+
if (!states) {
|
|
5723
|
+
states = /* @__PURE__ */ new Map();
|
|
5724
|
+
pageScrollViews.set(page, states);
|
|
5725
|
+
}
|
|
5726
|
+
const boundTop = coordinate(node.attribs?.["scroll-top"]);
|
|
5727
|
+
const boundLeft = coordinate(node.attribs?.["scroll-left"]);
|
|
5728
|
+
const bounds = resolveNativeScrollBounds(node);
|
|
5729
|
+
const scrollY = node.attribs?.["scroll-y"];
|
|
5730
|
+
const scrollX = node.attribs?.["scroll-x"];
|
|
5731
|
+
const maxTop = scrollY !== void 0 && scrollY !== "false" && scrollY !== "0" ? bounds.maxTop : 0;
|
|
5732
|
+
const maxLeft = scrollX !== void 0 && scrollX !== "false" && scrollX !== "0" ? bounds.maxLeft : 0;
|
|
5733
|
+
let state = states.get(key);
|
|
5734
|
+
const previousTop = state?.scrollTop ?? 0;
|
|
5735
|
+
const previousLeft = state?.scrollLeft ?? 0;
|
|
5736
|
+
if (!state) {
|
|
5737
|
+
state = {
|
|
5738
|
+
boundTop,
|
|
5739
|
+
boundLeft,
|
|
5740
|
+
scrollTop: boundTop,
|
|
5741
|
+
scrollLeft: boundLeft,
|
|
5742
|
+
maxTop,
|
|
5743
|
+
maxLeft
|
|
5744
|
+
};
|
|
5745
|
+
states.set(key, state);
|
|
5746
|
+
} else {
|
|
5747
|
+
if (state.boundTop !== boundTop) {
|
|
5748
|
+
state.scrollTop = boundTop;
|
|
5749
|
+
state.boundTop = boundTop;
|
|
5750
|
+
}
|
|
5751
|
+
if (state.boundLeft !== boundLeft) {
|
|
5752
|
+
state.scrollLeft = boundLeft;
|
|
5753
|
+
state.boundLeft = boundLeft;
|
|
5754
|
+
}
|
|
5755
|
+
}
|
|
5756
|
+
state.maxTop = maxTop;
|
|
5757
|
+
state.maxLeft = maxLeft;
|
|
5758
|
+
state.scrollTop = Math.min(state.scrollTop, maxTop);
|
|
5759
|
+
state.scrollLeft = Math.min(state.scrollLeft, maxLeft);
|
|
5760
|
+
if (state.scrollTop !== previousTop || state.scrollLeft !== previousLeft) {
|
|
5761
|
+
const binding = resolveMiniProgramEventBinding(node.attribs, "scroll");
|
|
5762
|
+
if (binding?.method) {
|
|
5763
|
+
const target = {
|
|
5764
|
+
id: node.attribs?.id ?? "",
|
|
5765
|
+
dataset: collectNodeDataset(node)
|
|
5766
|
+
};
|
|
5767
|
+
emit(node.attribs?.["data-sim-scope"] ?? null, binding.method, {
|
|
5768
|
+
type: "scroll",
|
|
5769
|
+
timeStamp: Date.now(),
|
|
5770
|
+
detail: {
|
|
5771
|
+
scrollTop: state.scrollTop,
|
|
5772
|
+
scrollLeft: state.scrollLeft
|
|
5773
|
+
},
|
|
5774
|
+
target,
|
|
5775
|
+
currentTarget: target
|
|
5776
|
+
});
|
|
5777
|
+
}
|
|
5778
|
+
}
|
|
5779
|
+
}
|
|
5780
|
+
for (const child of node.children ?? []) visit(child);
|
|
5781
|
+
};
|
|
5782
|
+
visit(root);
|
|
5783
|
+
if (states) {
|
|
5784
|
+
for (const key of states.keys()) if (!present?.has(key)) states.delete(key);
|
|
5785
|
+
}
|
|
5786
|
+
}
|
|
5787
|
+
function getNativeScrollOffset(page, node) {
|
|
5788
|
+
const state = pageScrollViews.get(page)?.get(nodeKey(node));
|
|
5789
|
+
return {
|
|
5790
|
+
scrollTop: state?.scrollTop ?? 0,
|
|
5791
|
+
scrollLeft: state?.scrollLeft ?? 0
|
|
5792
|
+
};
|
|
5793
|
+
}
|
|
5794
|
+
/** 原生交互更新宿主状态,导航仍交给统一 wx 路由入口。 */
|
|
5795
|
+
function dispatchNativeNodeEvent(page, node, eventName, event, wx, resolveMethod, onHandlerResult) {
|
|
5796
|
+
if (eventName === "scroll" && node.name === "scroll-view") {
|
|
5797
|
+
const detail = event.detail;
|
|
5798
|
+
const state = pageScrollViews.get(page)?.get(nodeKey(node));
|
|
5799
|
+
if (state) {
|
|
5800
|
+
if (detail?.scrollTop !== void 0) state.scrollTop = Math.min(coordinate(detail.scrollTop), state.maxTop);
|
|
5801
|
+
if (detail?.scrollLeft !== void 0) state.scrollLeft = Math.min(coordinate(detail.scrollLeft), state.maxLeft);
|
|
5802
|
+
}
|
|
5803
|
+
event.detail = Object.assign(event.detail && typeof event.detail === "object" ? event.detail : {}, getNativeScrollOffset(page, node));
|
|
5804
|
+
return true;
|
|
5805
|
+
}
|
|
5806
|
+
if (eventName !== "tap") return false;
|
|
5807
|
+
let current = node;
|
|
5808
|
+
let navigator;
|
|
5809
|
+
let handlers;
|
|
5810
|
+
let handled = false;
|
|
5811
|
+
while (current) {
|
|
5812
|
+
if (!navigator && current.name === "navigator") navigator = current;
|
|
5813
|
+
const binding = resolveMiniProgramEventBinding(current.attribs, "tap");
|
|
5814
|
+
if (binding?.method) {
|
|
5815
|
+
const call = resolveMethod(current.attribs?.["data-sim-scope"] ?? null, binding.method);
|
|
5816
|
+
handlers ??= [];
|
|
5817
|
+
handlers.push({
|
|
5818
|
+
node: current,
|
|
5819
|
+
call
|
|
5820
|
+
});
|
|
5821
|
+
handled = true;
|
|
5822
|
+
}
|
|
5823
|
+
if (binding?.stopAfter) {
|
|
5824
|
+
handled = true;
|
|
5825
|
+
break;
|
|
5826
|
+
}
|
|
5827
|
+
current = current.parent;
|
|
5828
|
+
}
|
|
5829
|
+
if (handlers) for (const handler of handlers) {
|
|
5830
|
+
const result = handler.call(createMiniProgramEventPayload(handler.node, "tap", event, node));
|
|
5831
|
+
onHandlerResult?.(result);
|
|
5832
|
+
}
|
|
5833
|
+
if (!navigator) return handled;
|
|
5834
|
+
const openType = navigator.attribs?.["open-type"] ?? "navigate";
|
|
5835
|
+
const url = navigator.attribs?.url ?? "";
|
|
5836
|
+
switch (openType) {
|
|
5837
|
+
case "navigate":
|
|
5838
|
+
wx.navigateTo({ url });
|
|
5839
|
+
break;
|
|
5840
|
+
case "redirect":
|
|
5841
|
+
wx.redirectTo({ url });
|
|
5842
|
+
break;
|
|
5843
|
+
case "switchTab":
|
|
5844
|
+
wx.switchTab({ url });
|
|
5845
|
+
break;
|
|
5846
|
+
case "reLaunch":
|
|
5847
|
+
wx.reLaunch({ url });
|
|
5848
|
+
break;
|
|
5849
|
+
case "navigateBack":
|
|
5850
|
+
wx.navigateBack({ delta: Number(navigator.attribs?.delta ?? 1) });
|
|
5851
|
+
break;
|
|
5852
|
+
default: return handled;
|
|
5853
|
+
}
|
|
5854
|
+
return true;
|
|
5855
|
+
}
|
|
5856
|
+
//#endregion
|
|
5857
|
+
//#region src/view/selectorQuery.ts
|
|
5858
|
+
const LEADING_MARK_PREFIX_RE = /^mark[:\-]?/;
|
|
5859
|
+
const MARK_NAME_RE = /[:\-]([a-z])/g;
|
|
5860
|
+
function toMarkKey(attributeName) {
|
|
5861
|
+
return attributeName.replace(LEADING_MARK_PREFIX_RE, "").replace(MARK_NAME_RE, (_match, char) => char.toUpperCase());
|
|
5862
|
+
}
|
|
5863
|
+
function collectMark(node) {
|
|
5864
|
+
const mark = {};
|
|
5865
|
+
for (const [key, value] of Object.entries(node.attribs ?? {})) {
|
|
5866
|
+
if (!(key.startsWith("mark:") || key.startsWith("mark-"))) continue;
|
|
5867
|
+
mark[toMarkKey(key)] = value;
|
|
5868
|
+
}
|
|
5869
|
+
return mark;
|
|
5870
|
+
}
|
|
5871
|
+
function createScopedRoot(node) {
|
|
5872
|
+
return {
|
|
5873
|
+
children: [node],
|
|
5874
|
+
type: "root"
|
|
5875
|
+
};
|
|
5876
|
+
}
|
|
5877
|
+
function findNodeByScopeId(root, scopeId) {
|
|
5878
|
+
if (root.attribs?.["data-sim-scope"] === scopeId) return root;
|
|
5879
|
+
for (const child of root.children ?? []) {
|
|
5880
|
+
const match = findNodeByScopeId(child, scopeId);
|
|
5881
|
+
if (match) return match;
|
|
5882
|
+
}
|
|
5883
|
+
return null;
|
|
5884
|
+
}
|
|
5464
5885
|
function resolveSelectorScrollTop(root, selector) {
|
|
5465
5886
|
const normalizedSelector = selector?.trim();
|
|
5466
5887
|
if (!normalizedSelector) return null;
|
|
5467
5888
|
const match = querySelectorAll(root, normalizedSelector)[0];
|
|
5468
5889
|
return match ? resolveRect$1(match).top : null;
|
|
5469
5890
|
}
|
|
5470
|
-
function resolveScrollOffset(node) {
|
|
5471
|
-
return {
|
|
5472
|
-
scrollLeft: parseNumericLikeValue$1(node.attribs?.["data-sim-scroll-left"]),
|
|
5473
|
-
scrollTop: parseNumericLikeValue$1(node.attribs?.["data-sim-scroll-top"])
|
|
5474
|
-
};
|
|
5475
|
-
}
|
|
5476
5891
|
function resolvePropertyValue(node, propertyName) {
|
|
5477
5892
|
const normalizedPropertyName = propertyName.trim();
|
|
5478
5893
|
if (!normalizedPropertyName) return;
|
|
@@ -5505,7 +5920,7 @@ function resolveFieldsResult(node, fields, options) {
|
|
|
5505
5920
|
result.height = rect.height;
|
|
5506
5921
|
}
|
|
5507
5922
|
}
|
|
5508
|
-
if (fields.scrollOffset) Object.assign(result,
|
|
5923
|
+
if (fields.scrollOffset) Object.assign(result, getNativeScrollOffset(options.page, node));
|
|
5509
5924
|
if (Array.isArray(fields.properties) && fields.properties.length > 0) Object.assign(result, pickProperties(node, fields.properties));
|
|
5510
5925
|
if (Array.isArray(fields.computedStyle) && fields.computedStyle.length > 0) Object.assign(result, pickComputedStyle(node, fields.computedStyle));
|
|
5511
5926
|
if (fields.context) result.context = options.resolveContext?.(node) ?? { type: "unsupported-context" };
|
|
@@ -6352,6 +6767,7 @@ var BrowserHeadlessSession = class {
|
|
|
6352
6767
|
componentScopes = /* @__PURE__ */ new Map();
|
|
6353
6768
|
selectorQueryScopeSnapshots = /* @__PURE__ */ new WeakMap();
|
|
6354
6769
|
renderRequestPending = false;
|
|
6770
|
+
renderInProgress = false;
|
|
6355
6771
|
renderRequestCallbacks = [];
|
|
6356
6772
|
tabBarRoutes;
|
|
6357
6773
|
tabPages = /* @__PURE__ */ new Map();
|
|
@@ -6365,6 +6781,7 @@ var BrowserHeadlessSession = class {
|
|
|
6365
6781
|
launchOptions = createAppLaunchOptions("", {});
|
|
6366
6782
|
kernel = new RuntimeKernel();
|
|
6367
6783
|
startupNavigation = new StartupNavigationQueue(this.kernel.scheduler);
|
|
6784
|
+
routeEvents = new HeadlessRouteEvents((callback) => this.requestRender(callback), (page) => this.currentPageInstance === page && !this.kernel.isClosed);
|
|
6368
6785
|
wxState;
|
|
6369
6786
|
pullDownRefreshState = {
|
|
6370
6787
|
active: false,
|
|
@@ -6372,7 +6789,10 @@ var BrowserHeadlessSession = class {
|
|
|
6372
6789
|
};
|
|
6373
6790
|
constructor(options) {
|
|
6374
6791
|
this.files = options.files;
|
|
6375
|
-
this.wxState = createHeadlessWxState(this.kernel.scheduler, {
|
|
6792
|
+
this.wxState = createHeadlessWxState(this.kernel.scheduler, {
|
|
6793
|
+
strictMocks: options.strictHostMocks,
|
|
6794
|
+
readPackageFile: createPackageAssetReader((relative) => readBrowserVirtualFile(this.files, join(this.project.miniprogramRootPath, relative)))
|
|
6795
|
+
});
|
|
6376
6796
|
this.onRender = options.onRender;
|
|
6377
6797
|
this.project = options.project ?? createBrowserProject(options.files);
|
|
6378
6798
|
this.registries = createHostRegistries();
|
|
@@ -6441,6 +6861,14 @@ var BrowserHeadlessSession = class {
|
|
|
6441
6861
|
saveImageToPhotosAlbum: (option) => this.wxState.saveImageToPhotosAlbum(option),
|
|
6442
6862
|
saveVideoToPhotosAlbum: (option) => this.wxState.saveVideoToPhotosAlbum(option),
|
|
6443
6863
|
nextTick: (callback) => this.kernel.scheduler.queueMicrotask(() => callback?.()),
|
|
6864
|
+
offBeforeAppRoute: (listener) => this.routeEvents.off("beforeAppRoute", listener),
|
|
6865
|
+
offBeforePageUnload: (listener) => this.routeEvents.off("beforePageUnload", listener),
|
|
6866
|
+
offAppRoute: (listener) => this.routeEvents.off("appRoute", listener),
|
|
6867
|
+
offAppRouteDone: (listener) => this.routeEvents.off("appRouteDone", listener),
|
|
6868
|
+
onBeforeAppRoute: (listener) => this.routeEvents.on("beforeAppRoute", listener),
|
|
6869
|
+
onBeforePageUnload: (listener) => this.routeEvents.on("beforePageUnload", listener),
|
|
6870
|
+
onAppRoute: (listener) => this.routeEvents.on("appRoute", listener),
|
|
6871
|
+
onAppRouteDone: (listener) => this.routeEvents.on("appRouteDone", listener),
|
|
6444
6872
|
offAppHide: (callback) => this.appLifecycle.offAppHide(callback),
|
|
6445
6873
|
offAppShow: (callback) => this.appLifecycle.offAppShow(callback),
|
|
6446
6874
|
offNetworkStatusChange: (callback) => this.wxState.offNetworkStatusChange(callback),
|
|
@@ -6491,14 +6919,21 @@ var BrowserHeadlessSession = class {
|
|
|
6491
6919
|
}
|
|
6492
6920
|
close() {
|
|
6493
6921
|
if (this.kernel.isClosed) return;
|
|
6494
|
-
|
|
6922
|
+
let failure;
|
|
6923
|
+
try {
|
|
6924
|
+
this.unloadAllPages(void 0, true);
|
|
6925
|
+
} catch (error) {
|
|
6926
|
+
failure = { error };
|
|
6927
|
+
}
|
|
6495
6928
|
this.canvasContexts.clear();
|
|
6496
6929
|
this.renderRequestCallbacks.length = 0;
|
|
6497
6930
|
this.renderRequestPending = false;
|
|
6498
6931
|
this.appLifecycle.close();
|
|
6932
|
+
this.routeEvents.close();
|
|
6499
6933
|
this.wxState.close();
|
|
6500
6934
|
this.moduleLoader.close();
|
|
6501
6935
|
this.kernel.close();
|
|
6936
|
+
if (failure) throw failure.error;
|
|
6502
6937
|
}
|
|
6503
6938
|
getDiagnostics() {
|
|
6504
6939
|
return this.kernel.diagnostics.getEntries();
|
|
@@ -6843,6 +7278,11 @@ var BrowserHeadlessSession = class {
|
|
|
6843
7278
|
selectOwnerComponent: (scopeId) => this.selectOwnerComponent(scopeId)
|
|
6844
7279
|
}
|
|
6845
7280
|
}, current);
|
|
7281
|
+
syncNativeScrollViews(current, rendered.root, (scopeId, method, event) => {
|
|
7282
|
+
this.kernel.scheduler.queueMicrotask(() => {
|
|
7283
|
+
if (this.currentPageInstance === current) this.callScopeMethod(scopeId, method, event);
|
|
7284
|
+
});
|
|
7285
|
+
});
|
|
6846
7286
|
current.__lastChangedKeys__ = [];
|
|
6847
7287
|
const componentScopePrefix = `page:${stripLeadingSlash$1(current.route)}`;
|
|
6848
7288
|
for (const [scopeId, instance] of this.componentCache.entries()) if (scopeId.startsWith(componentScopePrefix)) this.selectorQueryScopeSnapshots.set(instance, {
|
|
@@ -6852,38 +7292,63 @@ var BrowserHeadlessSession = class {
|
|
|
6852
7292
|
});
|
|
6853
7293
|
return rendered;
|
|
6854
7294
|
}
|
|
7295
|
+
dispatchNativeNodeEvent(node, eventName, event, onHandlerResult) {
|
|
7296
|
+
const current = this.requireCurrentPage("dispatchNativeNodeEvent()");
|
|
7297
|
+
if (eventName === "tap") this.renderCurrentPage();
|
|
7298
|
+
return dispatchNativeNodeEvent(current, node, eventName, event, this.moduleLoader.wx, (scopeId, method) => this.resolveScopeMethod(current, scopeId, method), onHandlerResult);
|
|
7299
|
+
}
|
|
6855
7300
|
requestRender(callback) {
|
|
6856
7301
|
this.assertActive();
|
|
6857
7302
|
if (callback) this.renderRequestCallbacks.push(callback);
|
|
6858
7303
|
if (this.renderRequestPending) return;
|
|
6859
7304
|
this.renderRequestPending = true;
|
|
6860
|
-
|
|
7305
|
+
if (this.renderInProgress) return;
|
|
7306
|
+
Promise.resolve().then(async () => {
|
|
6861
7307
|
if (this.kernel.isClosed) return;
|
|
6862
7308
|
this.renderRequestPending = false;
|
|
7309
|
+
this.renderInProgress = true;
|
|
6863
7310
|
const callbacks = this.renderRequestCallbacks.splice(0);
|
|
6864
|
-
|
|
6865
|
-
|
|
6866
|
-
|
|
7311
|
+
try {
|
|
7312
|
+
if (this.currentPageInstance) this.renderCurrentPage();
|
|
7313
|
+
const rendered = this.onRender?.();
|
|
7314
|
+
if (rendered && typeof rendered.then === "function") await rendered;
|
|
7315
|
+
for (const cb of callbacks) {
|
|
7316
|
+
if (this.kernel.isClosed) break;
|
|
7317
|
+
cb();
|
|
7318
|
+
}
|
|
7319
|
+
} finally {
|
|
7320
|
+
this.renderInProgress = false;
|
|
7321
|
+
if (this.renderRequestPending && !this.kernel.isClosed) {
|
|
7322
|
+
this.renderRequestPending = false;
|
|
7323
|
+
this.requestRender();
|
|
7324
|
+
}
|
|
7325
|
+
}
|
|
6867
7326
|
});
|
|
6868
7327
|
}
|
|
6869
7328
|
callScopeMethod(scopeId, methodName, event) {
|
|
6870
7329
|
const current = this.requireCurrentPage(`scope method "${methodName}"`);
|
|
6871
7330
|
this.renderCurrentPage();
|
|
6872
|
-
|
|
7331
|
+
return this.resolveScopeMethod(current, scopeId, methodName)(event);
|
|
7332
|
+
}
|
|
7333
|
+
/** 解析本次交互的实例,后续回调不随重渲染或导航切换所有者。 */
|
|
7334
|
+
resolveScopeMethod(current, scopeId, methodName) {
|
|
7335
|
+
if (!scopeId || scopeId === `page:${stripLeadingSlash$1(current.route)}`) return (event) => {
|
|
6873
7336
|
const method = current[methodName];
|
|
6874
7337
|
if (typeof method !== "function") throw new TypeError(`Method "${methodName}" does not exist on browser simulator page ${current.route}.`);
|
|
6875
7338
|
return method.call(current, event);
|
|
6876
|
-
}
|
|
7339
|
+
};
|
|
6877
7340
|
const instance = this.componentCache.get(scopeId);
|
|
6878
7341
|
if (!instance) throw new Error(`Unknown scope "${scopeId}" in browser simulator runtime.`);
|
|
6879
|
-
|
|
6880
|
-
|
|
6881
|
-
|
|
6882
|
-
|
|
7342
|
+
return (event) => {
|
|
7343
|
+
instance.__lastInteractionEvent__ = {
|
|
7344
|
+
currentTarget: event.currentTarget,
|
|
7345
|
+
mark: event.mark,
|
|
7346
|
+
target: event.target
|
|
7347
|
+
};
|
|
7348
|
+
const method = instance[methodName];
|
|
7349
|
+
if (typeof method !== "function") throw new TypeError(`Method "${methodName}" does not exist on browser simulator component scope "${scopeId}".`);
|
|
7350
|
+
return method.call(instance, event);
|
|
6883
7351
|
};
|
|
6884
|
-
const method = instance[methodName];
|
|
6885
|
-
if (typeof method !== "function") throw new TypeError(`Method "${methodName}" does not exist on browser simulator component scope "${scopeId}".`);
|
|
6886
|
-
return method.call(instance, event);
|
|
6887
7352
|
}
|
|
6888
7353
|
callScopeMethodDirect(scopeId, methodName, ...args) {
|
|
6889
7354
|
const normalizedScopeId = scopeId.trim();
|
|
@@ -6971,11 +7436,13 @@ var BrowserHeadlessSession = class {
|
|
|
6971
7436
|
const target = this.resolveNavigationTarget(url);
|
|
6972
7437
|
const launchedPage = this.bootstrapNavigation(createAppLaunchOptions(target.normalizedRoute, target.query));
|
|
6973
7438
|
if (launchedPage) return launchedPage;
|
|
6974
|
-
this.
|
|
6975
|
-
|
|
7439
|
+
const event = this.beginRoute(target, "reLaunch");
|
|
7440
|
+
this.unloadAllPages(event);
|
|
7441
|
+
const pageInstance = this.createFreshPage(target, event);
|
|
6976
7442
|
this.pages.push(pageInstance);
|
|
6977
7443
|
this.currentPageInstance = pageInstance;
|
|
6978
7444
|
this.runInitialPageLifecycles(pageInstance, target.query);
|
|
7445
|
+
this.routeEvents.commit(pageInstance, event);
|
|
6979
7446
|
return pageInstance;
|
|
6980
7447
|
}
|
|
6981
7448
|
navigateTo(url) {
|
|
@@ -6984,12 +7451,14 @@ var BrowserHeadlessSession = class {
|
|
|
6984
7451
|
if (launchedPage) return launchedPage;
|
|
6985
7452
|
if (this.pages.length >= PAGE_STACK_LIMIT$1) throw new Error(`Cannot navigateTo() beyond a ${PAGE_STACK_LIMIT$1}-page stack in browser simulator runtime.`);
|
|
6986
7453
|
if (this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.navigateTo() cannot open a tabBar page in browser simulator runtime: ${url}`);
|
|
7454
|
+
const event = this.beginRoute(target, "navigateTo");
|
|
6987
7455
|
this.currentPageInstance?.onHide?.();
|
|
6988
7456
|
if (this.currentPageInstance) this.runPageComponentLifetime(this.currentPageInstance.route, "hide");
|
|
6989
|
-
const pageInstance = this.createFreshPage(target);
|
|
7457
|
+
const pageInstance = this.createFreshPage(target, event);
|
|
6990
7458
|
this.pages.push(pageInstance);
|
|
6991
7459
|
this.currentPageInstance = pageInstance;
|
|
6992
7460
|
this.runInitialPageLifecycles(pageInstance, target.query);
|
|
7461
|
+
this.routeEvents.commit(pageInstance, event);
|
|
6993
7462
|
return pageInstance;
|
|
6994
7463
|
}
|
|
6995
7464
|
redirectTo(url) {
|
|
@@ -6997,15 +7466,17 @@ var BrowserHeadlessSession = class {
|
|
|
6997
7466
|
const launchedPage = this.bootstrapNavigation(createAppLaunchOptions(target.normalizedRoute, target.query));
|
|
6998
7467
|
if (launchedPage) return launchedPage;
|
|
6999
7468
|
if (this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.redirectTo() cannot open a tabBar page in browser simulator runtime: ${url}`);
|
|
7469
|
+
const event = this.beginRoute(target, "redirectTo");
|
|
7000
7470
|
const current = this.currentPageInstance;
|
|
7001
7471
|
if (current) {
|
|
7472
|
+
this.unloadPage(current, event);
|
|
7002
7473
|
this.pages.pop();
|
|
7003
|
-
this.unloadPage(current);
|
|
7004
7474
|
}
|
|
7005
|
-
const pageInstance = this.createFreshPage(target);
|
|
7475
|
+
const pageInstance = this.createFreshPage(target, event);
|
|
7006
7476
|
this.pages.push(pageInstance);
|
|
7007
7477
|
this.currentPageInstance = pageInstance;
|
|
7008
7478
|
this.runInitialPageLifecycles(pageInstance, target.query);
|
|
7479
|
+
this.routeEvents.commit(pageInstance, event);
|
|
7009
7480
|
return pageInstance;
|
|
7010
7481
|
}
|
|
7011
7482
|
navigateBack(delta = 1) {
|
|
@@ -7013,12 +7484,17 @@ var BrowserHeadlessSession = class {
|
|
|
7013
7484
|
if (launchedPage) return launchedPage;
|
|
7014
7485
|
if (this.pages.length <= 1) return this.currentPageInstance;
|
|
7015
7486
|
const removableCount = Math.min(Number.isFinite(delta) ? Math.max(1, Math.trunc(delta)) : 1, this.pages.length - 1);
|
|
7016
|
-
const
|
|
7017
|
-
|
|
7018
|
-
|
|
7487
|
+
const nextPage = this.pages[this.pages.length - removableCount - 1];
|
|
7488
|
+
const event = this.routeEvents.begin(nextPage.route, nextPage.options, "navigateBack", void 0, nextPage);
|
|
7489
|
+
this.routeEvents.attach(nextPage, event);
|
|
7490
|
+
for (let index = 0; index < removableCount; index++) {
|
|
7491
|
+
this.unloadPage(this.pages.at(-1), event);
|
|
7492
|
+
this.pages.pop();
|
|
7493
|
+
}
|
|
7019
7494
|
this.currentPageInstance = nextPage;
|
|
7020
7495
|
nextPage?.onShow?.();
|
|
7021
7496
|
if (nextPage) this.runPageComponentLifetime(nextPage.route, "show");
|
|
7497
|
+
this.routeEvents.commit(nextPage, event);
|
|
7022
7498
|
return nextPage;
|
|
7023
7499
|
}
|
|
7024
7500
|
switchTab(url) {
|
|
@@ -7036,6 +7512,8 @@ var BrowserHeadlessSession = class {
|
|
|
7036
7512
|
const current = this.currentPageInstance;
|
|
7037
7513
|
const cachedTarget = this.tabPages.get(target.routeRecord.route) ?? null;
|
|
7038
7514
|
if (current === cachedTarget && current) return () => {};
|
|
7515
|
+
const event = this.beginRoute(target, "switchTab", cachedTarget ?? void 0);
|
|
7516
|
+
if (cachedTarget) this.routeEvents.attach(cachedTarget, event);
|
|
7039
7517
|
if (current && current !== cachedTarget) {
|
|
7040
7518
|
current.onHide?.();
|
|
7041
7519
|
this.runPageComponentLifetime(current.route, "hide");
|
|
@@ -7045,7 +7523,7 @@ var BrowserHeadlessSession = class {
|
|
|
7045
7523
|
let nextPage = cachedTarget;
|
|
7046
7524
|
let shouldRunInitialLifecycles = false;
|
|
7047
7525
|
if (!nextPage) {
|
|
7048
|
-
nextPage = this.createFreshPage(target);
|
|
7526
|
+
nextPage = this.createFreshPage(target, event);
|
|
7049
7527
|
this.tabPages.set(target.routeRecord.route, nextPage);
|
|
7050
7528
|
shouldRunInitialLifecycles = true;
|
|
7051
7529
|
}
|
|
@@ -7058,7 +7536,8 @@ var BrowserHeadlessSession = class {
|
|
|
7058
7536
|
this.runPageComponentLifetime(nextPage.route, "show");
|
|
7059
7537
|
}
|
|
7060
7538
|
return () => {
|
|
7061
|
-
for (const page of pagesToUnload) this.unloadPage(page);
|
|
7539
|
+
for (const page of pagesToUnload) this.unloadPage(page, event);
|
|
7540
|
+
this.routeEvents.commit(nextPage, event);
|
|
7062
7541
|
};
|
|
7063
7542
|
}
|
|
7064
7543
|
pageScrollTo(option) {
|
|
@@ -7182,17 +7661,20 @@ var BrowserHeadlessSession = class {
|
|
|
7182
7661
|
windowInfo: this.getWindowInfo()
|
|
7183
7662
|
});
|
|
7184
7663
|
}
|
|
7185
|
-
|
|
7664
|
+
beginRoute(target, openType, page) {
|
|
7665
|
+
return this.routeEvents.begin(target.normalizedRoute, target.query, openType, target.pageConfig?.renderer ?? this.project.appConfig.renderer, page);
|
|
7666
|
+
}
|
|
7667
|
+
createFreshPage(target, event) {
|
|
7186
7668
|
const resourcePath = target.routeRecord.resourcePath ?? target.routeRecord.route;
|
|
7187
7669
|
const pageModulePath = join(this.project.miniprogramRootPath, `${resourcePath}.js`);
|
|
7188
|
-
const pageConfigPath = join(this.project.miniprogramRootPath, `${resourcePath}.json`);
|
|
7189
7670
|
const pageDefinition = this.moduleLoader.executePageModule(pageModulePath, target.routeRecord.route);
|
|
7190
|
-
const pageConfig =
|
|
7671
|
+
const pageConfig = target.pageConfig;
|
|
7191
7672
|
const pageInstance = createPageInstance(target.routeRecord.route, pageDefinition, target.query, {
|
|
7192
7673
|
background: resolveBackgroundSnapshot(this.project.appConfig, pageConfig),
|
|
7193
7674
|
navigationBar: resolveNavigationBarSnapshot(this.project.appConfig, pageConfig),
|
|
7194
7675
|
requestRender: (callback) => this.requestRender(callback)
|
|
7195
7676
|
});
|
|
7677
|
+
this.routeEvents.attach(pageInstance, event);
|
|
7196
7678
|
pageInstance.createIntersectionObserver = (options) => this.createIntersectionObserver(pageInstance, options);
|
|
7197
7679
|
pageInstance.createMediaQueryObserver = () => this.createMediaQueryObserver(pageInstance);
|
|
7198
7680
|
pageInstance.selectComponent = (selector) => resolveNativeComponentSelection(this.selectComponent(selector));
|
|
@@ -7205,7 +7687,7 @@ var BrowserHeadlessSession = class {
|
|
|
7205
7687
|
return pageInstance;
|
|
7206
7688
|
}
|
|
7207
7689
|
runInitialPageLifecycles(pageInstance, query) {
|
|
7208
|
-
runInitialPageLifecycles(pageInstance, query, this.kernel.scheduler, () => this.pages.includes(pageInstance) || this.tabPages.get(pageInstance.route) === pageInstance, () => this.renderCurrentPage());
|
|
7690
|
+
runInitialPageLifecycles(pageInstance, query, this.kernel.scheduler, () => this.pages.includes(pageInstance) || this.tabPages.get(pageInstance.route) === pageInstance, () => this.renderCurrentPage(), () => this.routeEvents.ready(pageInstance));
|
|
7209
7691
|
}
|
|
7210
7692
|
isTabBarRoute(route) {
|
|
7211
7693
|
return this.tabBarRoutes.has(stripLeadingSlash$1(route));
|
|
@@ -7228,7 +7710,8 @@ var BrowserHeadlessSession = class {
|
|
|
7228
7710
|
return {
|
|
7229
7711
|
normalizedRoute,
|
|
7230
7712
|
query,
|
|
7231
|
-
routeRecord
|
|
7713
|
+
routeRecord,
|
|
7714
|
+
pageConfig: readJsonObject$2(this.files, join(this.project.miniprogramRootPath, `${routeRecord.resourcePath ?? routeRecord.route}.json`))
|
|
7232
7715
|
};
|
|
7233
7716
|
}
|
|
7234
7717
|
requireTabBarItem(index, action) {
|
|
@@ -7237,21 +7720,41 @@ var BrowserHeadlessSession = class {
|
|
|
7237
7720
|
if (!item) throw new Error(`Cannot call ${action} with unknown tabBar index ${index} in browser simulator runtime.`);
|
|
7238
7721
|
return item;
|
|
7239
7722
|
}
|
|
7240
|
-
unloadAllPages() {
|
|
7723
|
+
unloadAllPages(event, closing = false) {
|
|
7241
7724
|
const pagesToUnload = /* @__PURE__ */ new Set([...this.pages, ...this.tabPages.values()]);
|
|
7242
|
-
|
|
7725
|
+
let failure;
|
|
7726
|
+
for (const page of [...pagesToUnload].reverse()) try {
|
|
7727
|
+
this.unloadPage(page, event, closing);
|
|
7728
|
+
} catch (error) {
|
|
7729
|
+
if (!closing) throw error;
|
|
7730
|
+
failure ??= { error };
|
|
7731
|
+
}
|
|
7243
7732
|
this.pages.length = 0;
|
|
7244
7733
|
this.tabPages.clear();
|
|
7245
7734
|
this.componentCache.clear();
|
|
7246
7735
|
this.componentScopes.clear();
|
|
7247
7736
|
this.clearMediaQueryObservers();
|
|
7248
7737
|
this.currentPageInstance = null;
|
|
7738
|
+
if (failure) throw failure.error;
|
|
7249
7739
|
}
|
|
7250
|
-
unloadPage(page) {
|
|
7251
|
-
|
|
7740
|
+
unloadPage(page, event, closing = false) {
|
|
7741
|
+
let failure;
|
|
7742
|
+
try {
|
|
7743
|
+
this.routeEvents.beforeUnload(page, event);
|
|
7744
|
+
page.onUnload?.();
|
|
7745
|
+
} catch (error) {
|
|
7746
|
+
if (!closing) throw error;
|
|
7747
|
+
failure = { error };
|
|
7748
|
+
}
|
|
7252
7749
|
this.clearMediaQueryObservers(page);
|
|
7253
|
-
|
|
7750
|
+
try {
|
|
7751
|
+
this.detachPageComponents(page.route, closing);
|
|
7752
|
+
} catch (error) {
|
|
7753
|
+
if (!closing) throw error;
|
|
7754
|
+
failure ??= { error };
|
|
7755
|
+
}
|
|
7254
7756
|
this.tabPages.delete(stripLeadingSlash$1(page.route));
|
|
7757
|
+
if (failure) throw failure.error;
|
|
7255
7758
|
}
|
|
7256
7759
|
requireCurrentPage(action) {
|
|
7257
7760
|
this.bootstrap();
|
|
@@ -7259,15 +7762,27 @@ var BrowserHeadlessSession = class {
|
|
|
7259
7762
|
if (!current) throw new Error(`Cannot call ${action} without an active page in browser simulator runtime.`);
|
|
7260
7763
|
return current;
|
|
7261
7764
|
}
|
|
7262
|
-
detachPageComponents(route) {
|
|
7765
|
+
detachPageComponents(route, closing = false) {
|
|
7263
7766
|
const prefix = `page:${stripLeadingSlash$1(route)}`;
|
|
7264
7767
|
const removed = [...this.componentCache].filter(([scopeId]) => scopeId.startsWith(prefix));
|
|
7265
|
-
|
|
7266
|
-
|
|
7768
|
+
let failure;
|
|
7769
|
+
for (const [, instance] of removed) try {
|
|
7770
|
+
runComponentLifecycle(instance, "detached");
|
|
7771
|
+
} catch (error) {
|
|
7772
|
+
if (!closing) throw error;
|
|
7773
|
+
failure ??= { error };
|
|
7774
|
+
}
|
|
7775
|
+
try {
|
|
7776
|
+
detachComponentRelations(removed.map(([, instance]) => instance));
|
|
7777
|
+
} catch (error) {
|
|
7778
|
+
if (!closing) throw error;
|
|
7779
|
+
failure ??= { error };
|
|
7780
|
+
}
|
|
7267
7781
|
for (const [scopeId] of removed) {
|
|
7268
7782
|
this.componentCache.delete(scopeId);
|
|
7269
7783
|
this.componentScopes.delete(scopeId);
|
|
7270
7784
|
}
|
|
7785
|
+
if (failure) throw failure.error;
|
|
7271
7786
|
}
|
|
7272
7787
|
notifyMediaQueryObservers() {
|
|
7273
7788
|
for (const entry of [...this.mediaQueryObservers]) {
|
|
@@ -7537,7 +8052,6 @@ function getRuntimeWxsLoader(owner, artifactSource) {
|
|
|
7537
8052
|
}
|
|
7538
8053
|
//#endregion
|
|
7539
8054
|
//#region src/runtime/render/shared.ts
|
|
7540
|
-
const TEMPLATE_INTERPOLATION_RE = /\{\{([^{}]+)\}\}/g;
|
|
7541
8055
|
const LEADING_SLASH_RE$2 = /^\/+/;
|
|
7542
8056
|
const EVENT_BINDING_ATTRS = [
|
|
7543
8057
|
"bindtap",
|
|
@@ -7557,8 +8071,7 @@ const STRUCTURAL_ATTRS = [
|
|
|
7557
8071
|
const CLASS_SPLIT_RE = /\s+/;
|
|
7558
8072
|
const JS_FILE_RE = /\.js$/;
|
|
7559
8073
|
function isMustacheOnly(value) {
|
|
7560
|
-
|
|
7561
|
-
return trimmed.startsWith("{{") && trimmed.endsWith("}}") && !trimmed.includes("{{", 2);
|
|
8074
|
+
return isTemplateExpression(value);
|
|
7562
8075
|
}
|
|
7563
8076
|
function collectDataset(node, source) {
|
|
7564
8077
|
if (node.dataset) return collectNodeDataset(node);
|
|
@@ -7583,17 +8096,11 @@ function cloneNode(node) {
|
|
|
7583
8096
|
children: node.children?.map((child) => cloneNode(child))
|
|
7584
8097
|
};
|
|
7585
8098
|
}
|
|
7586
|
-
function resolveValueByPath(source, expression) {
|
|
7587
|
-
return resolveTemplateExpression(source, expression) ?? "";
|
|
7588
|
-
}
|
|
7589
8099
|
function resolveRawValueByPath(source, expression) {
|
|
7590
8100
|
return resolveTemplateExpression(source, expression);
|
|
7591
8101
|
}
|
|
7592
8102
|
function interpolateTemplate(input, data) {
|
|
7593
|
-
return input
|
|
7594
|
-
const value = resolveValueByPath(data, expression);
|
|
7595
|
-
return typeof value === "string" ? value : String(value);
|
|
7596
|
-
});
|
|
8103
|
+
return interpolateTemplateText(input, data, true);
|
|
7597
8104
|
}
|
|
7598
8105
|
function readTemplateSource(artifactSource, filePath) {
|
|
7599
8106
|
const source = artifactSource.readText(filePath);
|
|
@@ -7720,7 +8227,7 @@ function resolveUsingComponents(context, ownerJsonPath, ownerFilePath) {
|
|
|
7720
8227
|
return /* @__PURE__ */ new Map();
|
|
7721
8228
|
}
|
|
7722
8229
|
}
|
|
7723
|
-
function resolveComponentGenerics(context, hostNode, ownerJsonPath, ownerFilePath, componentFilePath) {
|
|
8230
|
+
function resolveComponentGenerics(context, hostNode, ownerJsonPath, ownerFilePath, componentFilePath, ownerGenerics) {
|
|
7724
8231
|
const componentJsonPath = path.resolve(context.project.miniprogramRootPath, `${componentFilePath.replace(JS_FILE_RE, "")}.json`);
|
|
7725
8232
|
const componentGenerics = readComponentConfig(context.artifactSource, componentJsonPath).componentGenerics;
|
|
7726
8233
|
if (!componentGenerics || typeof componentGenerics !== "object" || Array.isArray(componentGenerics)) return;
|
|
@@ -7728,7 +8235,7 @@ function resolveComponentGenerics(context, hostNode, ownerJsonPath, ownerFilePat
|
|
|
7728
8235
|
const resolved = /* @__PURE__ */ new Map();
|
|
7729
8236
|
for (const [genericName, definition] of Object.entries(componentGenerics)) {
|
|
7730
8237
|
const selectedAlias = hostNode.attribs?.[`generic:${genericName}`];
|
|
7731
|
-
const selectedPath = selectedAlias ? ownerComponents.get(selectedAlias) : void 0;
|
|
8238
|
+
const selectedPath = selectedAlias ? ownerGenerics?.get(selectedAlias) ?? ownerComponents.get(selectedAlias) : void 0;
|
|
7732
8239
|
if (selectedPath) {
|
|
7733
8240
|
resolved.set(genericName, selectedPath);
|
|
7734
8241
|
continue;
|
|
@@ -7790,8 +8297,9 @@ function syncComponentProperties(instance, definition, nextProperties, bindingEx
|
|
|
7790
8297
|
const previousSnapshot = instance.__propertySnapshots?.[key];
|
|
7791
8298
|
if (hasComponentPropertyValueChanged(instance.properties[key], previousSnapshot, nextValue, bindingAffected)) {
|
|
7792
8299
|
previousProperties[key] = instance.properties[key];
|
|
7793
|
-
|
|
7794
|
-
|
|
8300
|
+
const deliveredValue = cloneValue(nextValue);
|
|
8301
|
+
instance.properties[key] = deliveredValue;
|
|
8302
|
+
if (Object.hasOwn(definition.properties ?? {}, key)) instance.data[key] = deliveredValue;
|
|
7795
8303
|
changedRootKeys.push(key);
|
|
7796
8304
|
}
|
|
7797
8305
|
instance.__propertySnapshots ??= {};
|
|
@@ -7989,7 +8497,7 @@ function renderNodeTree(node, scope, context, ownerJsonPath, ownerFilePath, inst
|
|
|
7989
8497
|
const componentScopeId = `${instancePath}/${clonedNode.name}`;
|
|
7990
8498
|
const ownerScopeId = scope.getScopeId().includes("/") ? scope.getScopeId() : void 0;
|
|
7991
8499
|
const { nextProperties, bindingExpressions } = resolveComponentProperties(clonedNode, scope, componentEntry.definition);
|
|
7992
|
-
const genericComponents = resolveComponentGenerics(context, clonedNode, ownerJsonPath, ownerFilePath, componentEntry.filePath);
|
|
8500
|
+
const genericComponents = resolveComponentGenerics(context, clonedNode, ownerJsonPath, ownerFilePath, componentEntry.filePath, scope.genericComponents);
|
|
7993
8501
|
const slots = collectComponentSlots(clonedNode, scope, ownerJsonPath, ownerFilePath, componentScopeId, templateRenderState);
|
|
7994
8502
|
let componentInstance = context.componentCache.get(componentScopeId);
|
|
7995
8503
|
if (!componentInstance) componentInstance = createRuntimeComponentInstance(componentScopeId, context, clonedNode, componentEntry, nextProperties, ownerScopeId, (instance) => {
|
|
@@ -8378,6 +8886,7 @@ var HeadlessSession = class {
|
|
|
8378
8886
|
appInstance = null;
|
|
8379
8887
|
appLifecycle = new HeadlessAppLifecycle();
|
|
8380
8888
|
moduleLoader;
|
|
8889
|
+
routeEvents = new HeadlessRouteEvents((callback) => this.requestRender(callback), (page) => this.currentPageInstance === page && !this.kernel.isClosed);
|
|
8381
8890
|
eventListeners = /* @__PURE__ */ new Map();
|
|
8382
8891
|
registries;
|
|
8383
8892
|
currentPageInstance = null;
|
|
@@ -8405,7 +8914,10 @@ var HeadlessSession = class {
|
|
|
8405
8914
|
constructor(options) {
|
|
8406
8915
|
if (!options.project && !options.projectPath) throw new Error("Headless runtime requires projectPath or project.");
|
|
8407
8916
|
this.project = options.project ?? loadProject(options.projectPath);
|
|
8408
|
-
this.wxState = createHeadlessWxState(this.kernel.scheduler, {
|
|
8917
|
+
this.wxState = createHeadlessWxState(this.kernel.scheduler, {
|
|
8918
|
+
strictMocks: options.strictHostMocks,
|
|
8919
|
+
readPackageFile: createPackageAssetReader((relative) => this.project.artifactSource.readText(path.join(this.project.miniprogramRootPath, relative)))
|
|
8920
|
+
});
|
|
8409
8921
|
this.registries = createHostRegistries();
|
|
8410
8922
|
const rawTabBarList = Array.isArray(this.project.appConfig.tabBar?.list) ? this.project.appConfig.tabBar.list : [];
|
|
8411
8923
|
this.tabBarRoutes = /* @__PURE__ */ new Set();
|
|
@@ -8472,6 +8984,14 @@ var HeadlessSession = class {
|
|
|
8472
8984
|
saveImageToPhotosAlbum: (option) => this.wxState.saveImageToPhotosAlbum(option),
|
|
8473
8985
|
saveVideoToPhotosAlbum: (option) => this.wxState.saveVideoToPhotosAlbum(option),
|
|
8474
8986
|
nextTick: (callback) => this.kernel.scheduler.queueMicrotask(() => callback?.()),
|
|
8987
|
+
offBeforeAppRoute: (listener) => this.routeEvents.off("beforeAppRoute", listener),
|
|
8988
|
+
offBeforePageUnload: (listener) => this.routeEvents.off("beforePageUnload", listener),
|
|
8989
|
+
offAppRoute: (listener) => this.routeEvents.off("appRoute", listener),
|
|
8990
|
+
offAppRouteDone: (listener) => this.routeEvents.off("appRouteDone", listener),
|
|
8991
|
+
onBeforeAppRoute: (listener) => this.routeEvents.on("beforeAppRoute", listener),
|
|
8992
|
+
onBeforePageUnload: (listener) => this.routeEvents.on("beforePageUnload", listener),
|
|
8993
|
+
onAppRoute: (listener) => this.routeEvents.on("appRoute", listener),
|
|
8994
|
+
onAppRouteDone: (listener) => this.routeEvents.on("appRouteDone", listener),
|
|
8475
8995
|
offAppHide: (callback) => this.appLifecycle.offAppHide(callback),
|
|
8476
8996
|
offAppShow: (callback) => this.appLifecycle.offAppShow(callback),
|
|
8477
8997
|
offNetworkStatusChange: (callback) => this.wxState.offNetworkStatusChange(callback),
|
|
@@ -8524,15 +9044,28 @@ var HeadlessSession = class {
|
|
|
8524
9044
|
}
|
|
8525
9045
|
close() {
|
|
8526
9046
|
if (this.kernel.isClosed) return;
|
|
8527
|
-
|
|
9047
|
+
let failure;
|
|
9048
|
+
try {
|
|
9049
|
+
this.unloadAllPages(void 0, true);
|
|
9050
|
+
} catch (error) {
|
|
9051
|
+
failure = { error };
|
|
9052
|
+
}
|
|
8528
9053
|
this.canvasContexts.clear();
|
|
8529
9054
|
this.renderRequestCallbacks.length = 0;
|
|
8530
9055
|
this.renderRequestPending = false;
|
|
8531
9056
|
this.appLifecycle.close();
|
|
9057
|
+
this.routeEvents.close();
|
|
8532
9058
|
this.wxState.close();
|
|
8533
9059
|
this.moduleLoader.close();
|
|
8534
|
-
this.eventListeners.clear();
|
|
8535
9060
|
this.kernel.close();
|
|
9061
|
+
try {
|
|
9062
|
+
this.emit("close");
|
|
9063
|
+
} catch (error) {
|
|
9064
|
+
failure ??= { error };
|
|
9065
|
+
} finally {
|
|
9066
|
+
this.eventListeners.clear();
|
|
9067
|
+
}
|
|
9068
|
+
if (failure) throw failure.error;
|
|
8536
9069
|
}
|
|
8537
9070
|
getDiagnostics() {
|
|
8538
9071
|
return this.kernel.diagnostics.getEntries();
|
|
@@ -8549,7 +9082,14 @@ var HeadlessSession = class {
|
|
|
8549
9082
|
if (listeners?.size === 0) this.eventListeners.delete(eventName);
|
|
8550
9083
|
}
|
|
8551
9084
|
emit(eventName, ...args) {
|
|
8552
|
-
|
|
9085
|
+
let failure;
|
|
9086
|
+
for (const handler of [...this.eventListeners.get(eventName) ?? []]) try {
|
|
9087
|
+
handler(...args);
|
|
9088
|
+
} catch (error) {
|
|
9089
|
+
if (eventName !== "close") throw error;
|
|
9090
|
+
failure ??= { error };
|
|
9091
|
+
}
|
|
9092
|
+
if (failure) throw failure.error;
|
|
8553
9093
|
}
|
|
8554
9094
|
get isClosed() {
|
|
8555
9095
|
return this.kernel.isClosed;
|
|
@@ -8631,6 +9171,11 @@ var HeadlessSession = class {
|
|
|
8631
9171
|
selectOwnerComponent: (scopeId) => this.selectOwnerComponent(scopeId)
|
|
8632
9172
|
}
|
|
8633
9173
|
}, current);
|
|
9174
|
+
syncNativeScrollViews(current, rendered.root, (scopeId, method, event) => {
|
|
9175
|
+
this.kernel.scheduler.queueMicrotask(() => {
|
|
9176
|
+
if (this.currentPageInstance === current) this.callScopeMethod(scopeId, method, event);
|
|
9177
|
+
});
|
|
9178
|
+
});
|
|
8634
9179
|
const componentScopePrefix = `page:${stripLeadingSlash(current.route)}`;
|
|
8635
9180
|
for (const [scopeId, instance] of this.componentCache.entries()) if (scopeId.startsWith(componentScopePrefix)) this.selectorQueryScopeSnapshots.set(instance, {
|
|
8636
9181
|
page: current,
|
|
@@ -8639,6 +9184,11 @@ var HeadlessSession = class {
|
|
|
8639
9184
|
});
|
|
8640
9185
|
return rendered;
|
|
8641
9186
|
}
|
|
9187
|
+
dispatchNativeNodeEvent(node, eventName, event, onHandlerResult) {
|
|
9188
|
+
const current = this.requireCurrentPage("dispatchNativeNodeEvent()");
|
|
9189
|
+
if (eventName === "tap") this.renderCurrentPage();
|
|
9190
|
+
return dispatchNativeNodeEvent(current, node, eventName, event, this.moduleLoader.wx, (scopeId, method) => this.resolveScopeMethod(current, scopeId, method), onHandlerResult);
|
|
9191
|
+
}
|
|
8642
9192
|
requestRender(callback) {
|
|
8643
9193
|
this.assertActive();
|
|
8644
9194
|
if (callback) this.renderRequestCallbacks.push(callback);
|
|
@@ -8686,21 +9236,27 @@ var HeadlessSession = class {
|
|
|
8686
9236
|
callScopeMethod(scopeId, methodName, event) {
|
|
8687
9237
|
const current = this.requireCurrentPage(`scope method "${methodName}"`);
|
|
8688
9238
|
this.renderCurrentPage();
|
|
8689
|
-
|
|
9239
|
+
return this.resolveScopeMethod(current, scopeId, methodName)(event);
|
|
9240
|
+
}
|
|
9241
|
+
/** 解析本次交互的实例,后续回调不随重渲染或导航切换所有者。 */
|
|
9242
|
+
resolveScopeMethod(current, scopeId, methodName) {
|
|
9243
|
+
if (!scopeId || scopeId === `page:${stripLeadingSlash(current.route)}`) return (event) => {
|
|
8690
9244
|
const method = current[methodName];
|
|
8691
9245
|
if (typeof method !== "function") throw new TypeError(`Method "${methodName}" does not exist on headless page ${current.route}.`);
|
|
8692
9246
|
return method.call(current, event);
|
|
8693
|
-
}
|
|
9247
|
+
};
|
|
8694
9248
|
const instance = this.componentCache.get(scopeId);
|
|
8695
9249
|
if (!instance) throw new Error(`Unknown scope "${scopeId}" in headless runtime.`);
|
|
8696
|
-
|
|
8697
|
-
|
|
8698
|
-
|
|
8699
|
-
|
|
9250
|
+
return (event) => {
|
|
9251
|
+
instance.__lastInteractionEvent__ = {
|
|
9252
|
+
currentTarget: event.currentTarget,
|
|
9253
|
+
mark: event.mark,
|
|
9254
|
+
target: event.target
|
|
9255
|
+
};
|
|
9256
|
+
const method = instance[methodName];
|
|
9257
|
+
if (typeof method !== "function") throw new TypeError(`Method "${methodName}" does not exist on headless component scope "${scopeId}".`);
|
|
9258
|
+
return method.call(instance, event);
|
|
8700
9259
|
};
|
|
8701
|
-
const method = instance[methodName];
|
|
8702
|
-
if (typeof method !== "function") throw new TypeError(`Method "${methodName}" does not exist on headless component scope "${scopeId}".`);
|
|
8703
|
-
return method.call(instance, event);
|
|
8704
9260
|
}
|
|
8705
9261
|
callScopeMethodDirect(scopeId, methodName, ...args) {
|
|
8706
9262
|
const normalizedScopeId = scopeId.trim();
|
|
@@ -8981,11 +9537,13 @@ var HeadlessSession = class {
|
|
|
8981
9537
|
const target = this.resolveNavigationTarget(url);
|
|
8982
9538
|
const launchedPage = this.bootstrapNavigation(createAppLaunchOptions(target.normalizedRoute, target.query));
|
|
8983
9539
|
if (launchedPage) return launchedPage;
|
|
8984
|
-
this.
|
|
8985
|
-
|
|
9540
|
+
const event = this.beginRoute(target, "reLaunch");
|
|
9541
|
+
this.unloadAllPages(event);
|
|
9542
|
+
const pageInstance = this.createFreshPage(target, event);
|
|
8986
9543
|
this.pages.push(pageInstance);
|
|
8987
9544
|
this.currentPageInstance = pageInstance;
|
|
8988
9545
|
this.runInitialPageLifecycles(pageInstance, target.query);
|
|
9546
|
+
this.routeEvents.commit(pageInstance, event);
|
|
8989
9547
|
return pageInstance;
|
|
8990
9548
|
}
|
|
8991
9549
|
navigateTo(url) {
|
|
@@ -8994,12 +9552,14 @@ var HeadlessSession = class {
|
|
|
8994
9552
|
if (launchedPage) return launchedPage;
|
|
8995
9553
|
if (this.pages.length >= PAGE_STACK_LIMIT) throw new Error(`Cannot navigateTo() beyond a ${PAGE_STACK_LIMIT}-page stack in headless runtime.`);
|
|
8996
9554
|
if (this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.navigateTo() cannot open a tabBar page in headless runtime: ${url}`);
|
|
9555
|
+
const event = this.beginRoute(target, "navigateTo");
|
|
8997
9556
|
this.currentPageInstance?.onHide?.();
|
|
8998
9557
|
if (this.currentPageInstance) this.runPageComponentLifetime(this.currentPageInstance.route, "hide");
|
|
8999
|
-
const pageInstance = this.createFreshPage(target);
|
|
9558
|
+
const pageInstance = this.createFreshPage(target, event);
|
|
9000
9559
|
this.pages.push(pageInstance);
|
|
9001
9560
|
this.currentPageInstance = pageInstance;
|
|
9002
9561
|
this.runInitialPageLifecycles(pageInstance, target.query);
|
|
9562
|
+
this.routeEvents.commit(pageInstance, event);
|
|
9003
9563
|
return pageInstance;
|
|
9004
9564
|
}
|
|
9005
9565
|
redirectTo(url) {
|
|
@@ -9007,15 +9567,17 @@ var HeadlessSession = class {
|
|
|
9007
9567
|
const launchedPage = this.bootstrapNavigation(createAppLaunchOptions(target.normalizedRoute, target.query));
|
|
9008
9568
|
if (launchedPage) return launchedPage;
|
|
9009
9569
|
if (this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.redirectTo() cannot open a tabBar page in headless runtime: ${url}`);
|
|
9570
|
+
const event = this.beginRoute(target, "redirectTo");
|
|
9010
9571
|
const current = this.currentPageInstance;
|
|
9011
9572
|
if (current) {
|
|
9573
|
+
this.unloadPage(current, event);
|
|
9012
9574
|
this.pages.pop();
|
|
9013
|
-
this.unloadPage(current);
|
|
9014
9575
|
}
|
|
9015
|
-
const pageInstance = this.createFreshPage(target);
|
|
9576
|
+
const pageInstance = this.createFreshPage(target, event);
|
|
9016
9577
|
this.pages.push(pageInstance);
|
|
9017
9578
|
this.currentPageInstance = pageInstance;
|
|
9018
9579
|
this.runInitialPageLifecycles(pageInstance, target.query);
|
|
9580
|
+
this.routeEvents.commit(pageInstance, event);
|
|
9019
9581
|
return pageInstance;
|
|
9020
9582
|
}
|
|
9021
9583
|
navigateBack(delta = 1) {
|
|
@@ -9023,12 +9585,17 @@ var HeadlessSession = class {
|
|
|
9023
9585
|
if (launchedPage) return launchedPage;
|
|
9024
9586
|
if (this.pages.length <= 1) return this.currentPageInstance;
|
|
9025
9587
|
const removableCount = Math.min(Number.isFinite(delta) ? Math.max(1, Math.trunc(delta)) : 1, this.pages.length - 1);
|
|
9026
|
-
const
|
|
9027
|
-
|
|
9028
|
-
|
|
9588
|
+
const nextPage = this.pages[this.pages.length - removableCount - 1];
|
|
9589
|
+
const event = this.routeEvents.begin(nextPage.route, nextPage.options, "navigateBack", void 0, nextPage);
|
|
9590
|
+
this.routeEvents.attach(nextPage, event);
|
|
9591
|
+
for (let index = 0; index < removableCount; index++) {
|
|
9592
|
+
this.unloadPage(this.pages.at(-1), event);
|
|
9593
|
+
this.pages.pop();
|
|
9594
|
+
}
|
|
9029
9595
|
this.currentPageInstance = nextPage;
|
|
9030
9596
|
nextPage?.onShow?.();
|
|
9031
9597
|
if (nextPage) this.runPageComponentLifetime(nextPage.route, "show");
|
|
9598
|
+
this.routeEvents.commit(nextPage, event);
|
|
9032
9599
|
return nextPage;
|
|
9033
9600
|
}
|
|
9034
9601
|
switchTab(url) {
|
|
@@ -9046,6 +9613,8 @@ var HeadlessSession = class {
|
|
|
9046
9613
|
const current = this.currentPageInstance;
|
|
9047
9614
|
const cachedTarget = this.tabPages.get(target.routeRecord.route) ?? null;
|
|
9048
9615
|
if (current === cachedTarget && current) return () => {};
|
|
9616
|
+
const event = this.beginRoute(target, "switchTab", cachedTarget ?? void 0);
|
|
9617
|
+
if (cachedTarget) this.routeEvents.attach(cachedTarget, event);
|
|
9049
9618
|
if (current && current !== cachedTarget) {
|
|
9050
9619
|
current.onHide?.();
|
|
9051
9620
|
this.runPageComponentLifetime(current.route, "hide");
|
|
@@ -9055,7 +9624,7 @@ var HeadlessSession = class {
|
|
|
9055
9624
|
let nextPage = cachedTarget;
|
|
9056
9625
|
let shouldRunInitialLifecycles = false;
|
|
9057
9626
|
if (!nextPage) {
|
|
9058
|
-
nextPage = this.createFreshPage(target);
|
|
9627
|
+
nextPage = this.createFreshPage(target, event);
|
|
9059
9628
|
this.tabPages.set(target.routeRecord.route, nextPage);
|
|
9060
9629
|
shouldRunInitialLifecycles = true;
|
|
9061
9630
|
}
|
|
@@ -9068,7 +9637,8 @@ var HeadlessSession = class {
|
|
|
9068
9637
|
this.runPageComponentLifetime(nextPage.route, "show");
|
|
9069
9638
|
}
|
|
9070
9639
|
return () => {
|
|
9071
|
-
for (const page of pagesToUnload) this.unloadPage(page);
|
|
9640
|
+
for (const page of pagesToUnload) this.unloadPage(page, event);
|
|
9641
|
+
this.routeEvents.commit(nextPage, event);
|
|
9072
9642
|
};
|
|
9073
9643
|
}
|
|
9074
9644
|
pageScrollTo(option) {
|
|
@@ -9163,17 +9733,20 @@ var HeadlessSession = class {
|
|
|
9163
9733
|
windowInfo: this.getWindowInfo()
|
|
9164
9734
|
});
|
|
9165
9735
|
}
|
|
9166
|
-
|
|
9736
|
+
beginRoute(target, openType, page) {
|
|
9737
|
+
return this.routeEvents.begin(target.normalizedRoute, target.query, openType, target.pageConfig?.renderer ?? this.project.appConfig.renderer, page);
|
|
9738
|
+
}
|
|
9739
|
+
createFreshPage(target, event) {
|
|
9167
9740
|
const resourcePath = target.routeRecord.resourcePath ?? target.routeRecord.route;
|
|
9168
9741
|
const pageModulePath = path.resolve(this.project.miniprogramRootPath, `${resourcePath}.js`);
|
|
9169
|
-
const pageConfigPath = path.resolve(this.project.miniprogramRootPath, `${resourcePath}.json`);
|
|
9170
9742
|
const pageDefinition = this.moduleLoader.executePageModule(pageModulePath, target.routeRecord.route);
|
|
9171
|
-
const pageConfig =
|
|
9743
|
+
const pageConfig = target.pageConfig;
|
|
9172
9744
|
const pageInstance = createPageInstance(`/${target.routeRecord.route}`, pageDefinition, target.query, {
|
|
9173
9745
|
background: resolveBackgroundSnapshot(this.project.appConfig, pageConfig),
|
|
9174
9746
|
navigationBar: resolveNavigationBarSnapshot(this.project.appConfig, pageConfig),
|
|
9175
9747
|
requestRender: (callback) => this.requestRender(callback)
|
|
9176
9748
|
});
|
|
9749
|
+
this.routeEvents.attach(pageInstance, event);
|
|
9177
9750
|
pageInstance.createIntersectionObserver = (options) => this.createIntersectionObserver(pageInstance, options);
|
|
9178
9751
|
pageInstance.createMediaQueryObserver = () => this.createMediaQueryObserver(pageInstance);
|
|
9179
9752
|
pageInstance.selectComponent = (selector) => resolveNativeComponentSelection(this.selectComponent(selector));
|
|
@@ -9186,7 +9759,7 @@ var HeadlessSession = class {
|
|
|
9186
9759
|
return pageInstance;
|
|
9187
9760
|
}
|
|
9188
9761
|
runInitialPageLifecycles(pageInstance, query) {
|
|
9189
|
-
runInitialPageLifecycles(pageInstance, query, this.kernel.scheduler, () => this.pages.includes(pageInstance) || this.tabPages.get(pageInstance.route) === pageInstance, () => this.renderCurrentPage());
|
|
9762
|
+
runInitialPageLifecycles(pageInstance, query, this.kernel.scheduler, () => this.pages.includes(pageInstance) || this.tabPages.get(pageInstance.route) === pageInstance, () => this.renderCurrentPage(), () => this.routeEvents.ready(pageInstance));
|
|
9190
9763
|
}
|
|
9191
9764
|
isTabBarRoute(route) {
|
|
9192
9765
|
return this.tabBarRoutes.has(stripLeadingSlash(route));
|
|
@@ -9215,24 +9788,45 @@ var HeadlessSession = class {
|
|
|
9215
9788
|
return {
|
|
9216
9789
|
normalizedRoute,
|
|
9217
9790
|
query,
|
|
9218
|
-
routeRecord
|
|
9791
|
+
routeRecord,
|
|
9792
|
+
pageConfig: readJsonObject(this.project, path.resolve(this.project.miniprogramRootPath, `${routeRecord.resourcePath ?? routeRecord.route}.json`))
|
|
9219
9793
|
};
|
|
9220
9794
|
}
|
|
9221
|
-
unloadAllPages() {
|
|
9795
|
+
unloadAllPages(event, closing = false) {
|
|
9222
9796
|
const pagesToUnload = /* @__PURE__ */ new Set([...this.pages, ...this.tabPages.values()]);
|
|
9223
|
-
|
|
9797
|
+
let failure;
|
|
9798
|
+
for (const page of [...pagesToUnload].reverse()) try {
|
|
9799
|
+
this.unloadPage(page, event, closing);
|
|
9800
|
+
} catch (error) {
|
|
9801
|
+
if (!closing) throw error;
|
|
9802
|
+
failure ??= { error };
|
|
9803
|
+
}
|
|
9224
9804
|
this.pages.length = 0;
|
|
9225
9805
|
this.tabPages.clear();
|
|
9226
9806
|
this.componentCache.clear();
|
|
9227
9807
|
this.componentScopes.clear();
|
|
9228
9808
|
this.clearMediaQueryObservers();
|
|
9229
9809
|
this.currentPageInstance = null;
|
|
9810
|
+
if (failure) throw failure.error;
|
|
9230
9811
|
}
|
|
9231
|
-
unloadPage(page) {
|
|
9232
|
-
|
|
9812
|
+
unloadPage(page, event, closing = false) {
|
|
9813
|
+
let failure;
|
|
9814
|
+
try {
|
|
9815
|
+
this.routeEvents.beforeUnload(page, event);
|
|
9816
|
+
page.onUnload?.();
|
|
9817
|
+
} catch (error) {
|
|
9818
|
+
if (!closing) throw error;
|
|
9819
|
+
failure = { error };
|
|
9820
|
+
}
|
|
9233
9821
|
this.clearMediaQueryObservers(page);
|
|
9234
|
-
|
|
9822
|
+
try {
|
|
9823
|
+
this.detachPageComponents(page.route, closing);
|
|
9824
|
+
} catch (error) {
|
|
9825
|
+
if (!closing) throw error;
|
|
9826
|
+
failure ??= { error };
|
|
9827
|
+
}
|
|
9235
9828
|
this.tabPages.delete(stripLeadingSlash(page.route));
|
|
9829
|
+
if (failure) throw failure.error;
|
|
9236
9830
|
}
|
|
9237
9831
|
requireCurrentPage(action) {
|
|
9238
9832
|
this.bootstrap();
|
|
@@ -9260,15 +9854,27 @@ var HeadlessSession = class {
|
|
|
9260
9854
|
const directlyOwned = rootScopeId ? matches.filter(([, scope]) => scope.ownerScopeId === rootScopeId) : [];
|
|
9261
9855
|
return (directlyOwned.length > 0 ? directlyOwned : matches).map(([candidateScopeId]) => this.componentCache.get(candidateScopeId)).filter(Boolean);
|
|
9262
9856
|
}
|
|
9263
|
-
detachPageComponents(route) {
|
|
9857
|
+
detachPageComponents(route, closing = false) {
|
|
9264
9858
|
const prefix = `page:${stripLeadingSlash(route)}`;
|
|
9265
9859
|
const removed = [...this.componentCache].filter(([scopeId]) => scopeId.startsWith(prefix));
|
|
9266
|
-
|
|
9267
|
-
|
|
9860
|
+
let failure;
|
|
9861
|
+
for (const [, instance] of removed) try {
|
|
9862
|
+
runComponentLifecycle(instance, "detached");
|
|
9863
|
+
} catch (error) {
|
|
9864
|
+
if (!closing) throw error;
|
|
9865
|
+
failure ??= { error };
|
|
9866
|
+
}
|
|
9867
|
+
try {
|
|
9868
|
+
detachComponentRelations(removed.map(([, instance]) => instance));
|
|
9869
|
+
} catch (error) {
|
|
9870
|
+
if (!closing) throw error;
|
|
9871
|
+
failure ??= { error };
|
|
9872
|
+
}
|
|
9268
9873
|
for (const [scopeId] of removed) {
|
|
9269
9874
|
this.componentCache.delete(scopeId);
|
|
9270
9875
|
this.componentScopes.delete(scopeId);
|
|
9271
9876
|
}
|
|
9877
|
+
if (failure) throw failure.error;
|
|
9272
9878
|
}
|
|
9273
9879
|
notifyMediaQueryObservers() {
|
|
9274
9880
|
for (const entry of [...this.mediaQueryObservers]) {
|
|
@@ -9428,6 +10034,7 @@ function createPageRootNodeHandle(options) {
|
|
|
9428
10034
|
const tree = session?.getCurrentPages().includes(page) ? session.renderCurrentPage() : renderPageTree(project, page);
|
|
9429
10035
|
return new HeadlessTestingNodeHandle(tree.root.type === "root" && !options.includeHostRoots ? tree.root.children?.[0] ?? tree.root : tree.root, {
|
|
9430
10036
|
assertActive: options.assertActive,
|
|
10037
|
+
dispatchNativeEvent: (node, eventName, event, onHandlerResult) => session?.getCurrentPages().at(-1) === page ? session.dispatchNativeNodeEvent(node, eventName, event, onHandlerResult) : false,
|
|
9431
10038
|
callMethod: (scopeId, methodName, event) => {
|
|
9432
10039
|
if (session?.getCurrentPages().includes(page)) return session.callScopeMethod(scopeId, methodName, event);
|
|
9433
10040
|
const method = page[methodName];
|
|
@@ -9867,9 +10474,13 @@ var HeadlessTestingScopeHandle = class HeadlessTestingScopeHandle {
|
|
|
9867
10474
|
var HeadlessTestingSessionHandle = class {
|
|
9868
10475
|
project;
|
|
9869
10476
|
session;
|
|
10477
|
+
pendingNavigations = /* @__PURE__ */ new Set();
|
|
9870
10478
|
constructor(project, session) {
|
|
9871
10479
|
this.project = project;
|
|
9872
10480
|
this.session = session;
|
|
10481
|
+
this.session.on("close", () => {
|
|
10482
|
+
for (const reject of this.pendingNavigations) reject(/* @__PURE__ */ new Error("The headless testing session closed before navigation completed."));
|
|
10483
|
+
});
|
|
9873
10484
|
}
|
|
9874
10485
|
async close() {
|
|
9875
10486
|
this.session.close();
|
|
@@ -9938,23 +10549,23 @@ var HeadlessTestingSessionHandle = class {
|
|
|
9938
10549
|
}, `Timed out waiting for ${normalizedCount} component(s) matching "${normalizedSelector}" in headless testing runtime.`, options);
|
|
9939
10550
|
}
|
|
9940
10551
|
async reLaunch(route) {
|
|
9941
|
-
this.invokeNavigation("reLaunch", { url: route });
|
|
10552
|
+
await this.invokeNavigation("reLaunch", { url: route });
|
|
9942
10553
|
return await this.requireCurrentPage("reLaunch", route);
|
|
9943
10554
|
}
|
|
9944
10555
|
async navigateTo(route) {
|
|
9945
|
-
this.invokeNavigation("navigateTo", { url: route });
|
|
10556
|
+
await this.invokeNavigation("navigateTo", { url: route });
|
|
9946
10557
|
return await this.requireCurrentPage("navigateTo", route);
|
|
9947
10558
|
}
|
|
9948
10559
|
async redirectTo(route) {
|
|
9949
|
-
this.invokeNavigation("redirectTo", { url: route });
|
|
10560
|
+
await this.invokeNavigation("redirectTo", { url: route });
|
|
9950
10561
|
return await this.requireCurrentPage("redirectTo", route);
|
|
9951
10562
|
}
|
|
9952
10563
|
async navigateBack(delta = 1) {
|
|
9953
|
-
this.invokeNavigation("navigateBack", { delta });
|
|
10564
|
+
await this.invokeNavigation("navigateBack", { delta });
|
|
9954
10565
|
return await this.currentPage();
|
|
9955
10566
|
}
|
|
9956
10567
|
async switchTab(route) {
|
|
9957
|
-
this.invokeNavigation("switchTab", { url: route });
|
|
10568
|
+
await this.invokeNavigation("switchTab", { url: route });
|
|
9958
10569
|
return await this.requireCurrentPage("switchTab", route);
|
|
9959
10570
|
}
|
|
9960
10571
|
async waitForCurrentPage(route, options = {}) {
|
|
@@ -9991,21 +10602,30 @@ var HeadlessTestingSessionHandle = class {
|
|
|
9991
10602
|
this.session.triggerRouteDone(options);
|
|
9992
10603
|
}
|
|
9993
10604
|
invokeNavigation(methodName, options) {
|
|
9994
|
-
|
|
9995
|
-
|
|
9996
|
-
|
|
9997
|
-
|
|
9998
|
-
|
|
9999
|
-
|
|
10605
|
+
return new Promise((resolve, reject) => {
|
|
10606
|
+
const rejectNavigation = (error) => {
|
|
10607
|
+
this.pendingNavigations.delete(rejectNavigation);
|
|
10608
|
+
reject(error);
|
|
10609
|
+
};
|
|
10610
|
+
this.pendingNavigations.add(rejectNavigation);
|
|
10611
|
+
try {
|
|
10612
|
+
this.session.callWxMethod(methodName, {
|
|
10613
|
+
...options,
|
|
10614
|
+
success: () => {
|
|
10615
|
+
this.pendingNavigations.delete(rejectNavigation);
|
|
10616
|
+
resolve();
|
|
10617
|
+
},
|
|
10618
|
+
fail: (error) => {
|
|
10619
|
+
const details = error && typeof error === "object" ? error : void 0;
|
|
10620
|
+
const reason = typeof details?.errMsg === "string" ? details.errMsg : typeof details?.message === "string" ? details.message : String(error);
|
|
10621
|
+
const target = options.url ? ` route "${options.url}"` : "";
|
|
10622
|
+
rejectNavigation(new Error(`Failed to ${methodName}${target} through the headless wx runtime: ${reason}`, { cause: error }));
|
|
10623
|
+
}
|
|
10624
|
+
});
|
|
10625
|
+
} catch (error) {
|
|
10626
|
+
rejectNavigation(error);
|
|
10000
10627
|
}
|
|
10001
10628
|
});
|
|
10002
|
-
if (failure.received) {
|
|
10003
|
-
const error = failure.error;
|
|
10004
|
-
const details = error && typeof error === "object" ? error : void 0;
|
|
10005
|
-
const reason = typeof details?.errMsg === "string" ? details.errMsg : typeof details?.message === "string" ? details.message : String(error);
|
|
10006
|
-
const target = options.url ? ` route "${options.url}"` : "";
|
|
10007
|
-
throw new Error(`Failed to ${methodName}${target} through the headless wx runtime: ${reason}`, { cause: error });
|
|
10008
|
-
}
|
|
10009
10629
|
}
|
|
10010
10630
|
async requireCurrentPage(methodName, route) {
|
|
10011
10631
|
const page = await this.currentPage();
|
|
@@ -10040,4 +10660,4 @@ async function launch(options) {
|
|
|
10040
10660
|
}
|
|
10041
10661
|
}
|
|
10042
10662
|
//#endregion
|
|
10043
|
-
export { BrowserHeadlessSession, HEADLESS_PLUGIN_VIRTUAL_ROOT, HeadlessSession, HeadlessTestingNodeHandle, HeadlessTestingPageHandle, HeadlessTestingScopeHandle, HeadlessTestingSessionHandle, RuntimeDiagnostics, RuntimeKernel, RuntimeScheduler, cloneValue, coerceComponentPropertyValue, collectMiniProgramEventBindings, createAppInstance, createArtifactProject, createBrowserHeadlessSession, createBrowserModuleLoader, createBrowserProject, createBrowserVirtualFiles, createBrowserVirtualFilesFromDirectory, createComponentInstance, createFileSystemArtifactSource, createHeadlessSession, createHeadlessUniEventBus, createHeadlessWx, createHeadlessWxState, createHostRegistries, createLogicalNode, createMemoryArtifactSource, createMiniProgramHostConfig, createOverlayArtifactSource, createPageInstance, createPluginArtifactSource, createPluginVirtualRoot, createProjectDescriptor, executeSelectorQueryRequests, getPageInstanceId, hasBrowserVirtualFile, hasComponentPropertyValueChanged, launch, loadProject, normalizeBrowserFilePath, normalizeComponentPageDefinition, normalizeComponentPropertyValue, querySelectorAll, readBrowserVirtualFile, readRenderedNodes, readRenderedSelectorNodes, registerAppDefinition, registerComponentDefinition, registerExportedComponentDefinition, registerPageDefinition, renderBrowserPageTree, renderPageTree, renderRuntimePageTree, resolveBrowserPageStyles, resolveMiniProgramEventBinding, resolvePluginRequest, resolveRoutesFromAppConfig, resolveSelectorQueryScopeRoot, resolveSelectorScrollTop, runComponentLifecycle, runComponentObservers, runComponentPageLifetime, waitForPageComponent, waitForPageComponents, waitForPageData, waitForPageSelector, waitForPageText, waitForPageTextGone, waitForRenderedPage, wechat };
|
|
10663
|
+
export { BrowserHeadlessSession, HEADLESS_PLUGIN_VIRTUAL_ROOT, HeadlessSession, HeadlessTestingNodeHandle, HeadlessTestingPageHandle, HeadlessTestingScopeHandle, HeadlessTestingSessionHandle, RuntimeDiagnostics, RuntimeKernel, RuntimeScheduler, cloneValue, coerceComponentPropertyValue, collectMiniProgramEventBindings, createAppInstance, createArtifactProject, createBrowserHeadlessSession, createBrowserModuleLoader, createBrowserProject, createBrowserVirtualFiles, createBrowserVirtualFilesFromDirectory, createComponentInstance, createFileSystemArtifactSource, createHeadlessSession, createHeadlessUniEventBus, createHeadlessWx, createHeadlessWxState, createHostRegistries, createLogicalNode, createMemoryArtifactSource, createMiniProgramEventPayload, createMiniProgramHostConfig, createOverlayArtifactSource, createPageInstance, createPluginArtifactSource, createPluginVirtualRoot, createProjectDescriptor, executeSelectorQueryRequests, getPageInstanceId, hasBrowserVirtualFile, hasComponentPropertyValueChanged, launch, loadProject, normalizeBrowserFilePath, normalizeComponentPageDefinition, normalizeComponentPropertyValue, querySelectorAll, readBrowserVirtualFile, readRenderedNodes, readRenderedSelectorNodes, registerAppDefinition, registerComponentDefinition, registerExportedComponentDefinition, registerPageDefinition, renderBrowserPageTree, renderPageTree, renderRuntimePageTree, resolveBrowserPageStyles, resolveMiniProgramEventBinding, resolvePluginRequest, resolveRoutesFromAppConfig, resolveSelectorQueryScopeRoot, resolveSelectorScrollTop, runComponentLifecycle, runComponentObservers, runComponentPageLifetime, waitForPageComponent, waitForPageComponents, waitForPageData, waitForPageSelector, waitForPageText, waitForPageTextGone, waitForRenderedPage, wechat };
|