@wcstack/router 3.4.0 → 3.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +16 -6
- package/README.md +16 -6
- package/dist/auto.min.js +1 -1
- package/dist/auto.min.js.map +1 -1
- package/dist/index.d.ts +15 -2
- package/dist/index.esm.js +358 -167
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -24,6 +24,10 @@ function deepFreeze(obj) {
|
|
|
24
24
|
function deepClone(obj) {
|
|
25
25
|
if (obj === null || typeof obj !== "object")
|
|
26
26
|
return obj;
|
|
27
|
+
// basenameFileExtensions is an array: clone it as one, or getConfig() would hand out
|
|
28
|
+
// { "0": ".html" }, which for…of cannot iterate. deepFreeze freezes it like any object.
|
|
29
|
+
if (Array.isArray(obj))
|
|
30
|
+
return obj.map((item) => deepClone(item));
|
|
27
31
|
const clone = {};
|
|
28
32
|
for (const key of Object.keys(obj)) {
|
|
29
33
|
clone[key] = deepClone(obj[key]);
|
|
@@ -39,7 +43,40 @@ function getConfig() {
|
|
|
39
43
|
}
|
|
40
44
|
return frozenConfig;
|
|
41
45
|
}
|
|
46
|
+
// The defaults as shipped, for warnInvalid: a value 3.x lets through unchecked must not change
|
|
47
|
+
// what a later call is compared against.
|
|
48
|
+
const defaults = { ..._config, tagNames: { ..._config.tagNames } };
|
|
49
|
+
const warned$1 = new Set();
|
|
50
|
+
/**
|
|
51
|
+
* 3.5 forward-compat check. Warns, once per key per page, about an option that 4.0's
|
|
52
|
+
* bootstrapRouter throws on: one this package does not have, a value of another type than its
|
|
53
|
+
* default (null, or an array for an object, included), or a tag name it does not define or that is
|
|
54
|
+
* not a string. An undefined value is left out, as in 4.0. It only warns: setConfig then goes on
|
|
55
|
+
* exactly as in 3.x. Bootstrap-time only, never on a hot path.
|
|
56
|
+
*/
|
|
57
|
+
function warnInvalid(given, known, path = "") {
|
|
58
|
+
for (const [key, value] of Object.entries(given)) {
|
|
59
|
+
const name = path + key;
|
|
60
|
+
const current = known[key];
|
|
61
|
+
if (value === undefined) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (!Object.prototype.hasOwnProperty.call(known, key) ||
|
|
65
|
+
value === null ||
|
|
66
|
+
typeof value !== typeof current ||
|
|
67
|
+
Array.isArray(value) !== Array.isArray(current)) {
|
|
68
|
+
if (!warned$1.has(name)) {
|
|
69
|
+
warned$1.add(name);
|
|
70
|
+
console.warn(`[@wcstack/router] bootstrapRouter: "${name}" is not one of its options, or not of the option's type. 3.x ignores it or applies it unchecked; 4.0 throws on it.`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else if (name === "tagNames") {
|
|
74
|
+
warnInvalid(value, current, "tagNames.");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
42
78
|
function setConfig(partialConfig) {
|
|
79
|
+
warnInvalid(partialConfig, defaults);
|
|
43
80
|
if (partialConfig.tagNames) {
|
|
44
81
|
Object.assign(_config.tagNames, partialConfig.tagNames);
|
|
45
82
|
}
|
|
@@ -561,6 +598,25 @@ class RouteCore extends EventTarget {
|
|
|
561
598
|
}
|
|
562
599
|
}
|
|
563
600
|
|
|
601
|
+
/**
|
|
602
|
+
* SSR ハイドレーションマーカー(docs/ssr-router-design.md §3.3 / §4)。
|
|
603
|
+
*
|
|
604
|
+
* サーバー(_renderForSsr)が書き、クライアント(_hydrateFromSsr / Link の採用)が
|
|
605
|
+
* 読む。キーは route の absolutePath — placeholder の UUID はパースごとに再生成され
|
|
606
|
+
* サーバーとクライアントで一致しないため、同一 template から決定的に導ける
|
|
607
|
+
* absolutePath だけが突合キーになれる。
|
|
608
|
+
*/
|
|
609
|
+
/** サーバー描画済み outlet の目印(要素属性) */
|
|
610
|
+
const SSR_OUTLET_ATTR = 'data-wcs-ssr';
|
|
611
|
+
/** Link がサーバーで生成した anchor の目印(要素属性)。クライアントが採用して外す */
|
|
612
|
+
const SSR_LINK_ATTR = 'data-wcs-ssr-link';
|
|
613
|
+
/** route placeholder コメントの安定キー形式(`@@wcs-route-ph:<absolutePath>`) */
|
|
614
|
+
const ROUTE_PH_PREFIX = '@@wcs-route-ph:';
|
|
615
|
+
/** 表示中ルート内容の開始マーカー(`@@wcs-route-start:<absolutePath>`) */
|
|
616
|
+
const ROUTE_START_PREFIX = '@@wcs-route-start:';
|
|
617
|
+
/** 表示中ルート内容の終了マーカー(`@@wcs-route-end:<absolutePath>`) */
|
|
618
|
+
const ROUTE_END_PREFIX = '@@wcs-route-end:';
|
|
619
|
+
|
|
564
620
|
// NOTE: `static wcBindable` は宣言しない — RouteCore.ts 冒頭の NOTE を参照
|
|
565
621
|
// (docs/router-state-contract-design.md §5.1 / D2)。
|
|
566
622
|
class Route extends HTMLElement {
|
|
@@ -571,6 +627,9 @@ class Route extends HTMLElement {
|
|
|
571
627
|
_uuid = getUUID();
|
|
572
628
|
_placeHolder = document.createComment(`@@route:${this._uuid}`);
|
|
573
629
|
_childNodeArray;
|
|
630
|
+
_endMarker;
|
|
631
|
+
/** 隠している間の内容(IRoute.held) */
|
|
632
|
+
held = null;
|
|
574
633
|
_childIndex = 0;
|
|
575
634
|
_initialized = false;
|
|
576
635
|
_routes;
|
|
@@ -597,6 +656,13 @@ class Route extends HTMLElement {
|
|
|
597
656
|
get placeHolder() {
|
|
598
657
|
return this._placeHolder;
|
|
599
658
|
}
|
|
659
|
+
/** ルートの内容の終わり(IRoute.endMarker。文面は SSR の終了マーカーと同じ) */
|
|
660
|
+
get endMarker() {
|
|
661
|
+
if (typeof this._endMarker === 'undefined') {
|
|
662
|
+
this._endMarker = document.createComment(`${ROUTE_END_PREFIX}${this.absolutePath}`);
|
|
663
|
+
}
|
|
664
|
+
return this._endMarker;
|
|
665
|
+
}
|
|
600
666
|
get childNodeArray() {
|
|
601
667
|
if (typeof this._childNodeArray === 'undefined') {
|
|
602
668
|
this._childNodeArray = Array.from(this.childNodes);
|
|
@@ -608,9 +674,13 @@ class Route extends HTMLElement {
|
|
|
608
674
|
* サーバー描画済みの DOM ノード列をこのルートの内容として引き取る。
|
|
609
675
|
* 以後の hideRoute / showRoute は採用ノードに対して従来どおり動く。
|
|
610
676
|
* template 由来の fresh クローン(自身の childNodes)は不要になるため破棄する。
|
|
677
|
+
* `endMarker`(サーバーの終了マーカー)は、以後このルートの範囲の終わりになる。
|
|
611
678
|
*/
|
|
612
|
-
adoptChildNodes(nodes) {
|
|
679
|
+
adoptChildNodes(nodes, endMarker) {
|
|
613
680
|
this._childNodeArray = [...nodes];
|
|
681
|
+
if (endMarker) {
|
|
682
|
+
this._endMarker = endMarker;
|
|
683
|
+
}
|
|
614
684
|
while (this.firstChild) {
|
|
615
685
|
this.removeChild(this.firstChild);
|
|
616
686
|
}
|
|
@@ -1102,6 +1172,182 @@ function assignParams(element, params) {
|
|
|
1102
1172
|
}
|
|
1103
1173
|
}
|
|
1104
1174
|
|
|
1175
|
+
// ===========================================================================
|
|
1176
|
+
// AUTO-GENERATED FILE - DO NOT EDIT.
|
|
1177
|
+
// Generated from /protocol/binder.ts by scripts/sync-protocol-types.mjs.
|
|
1178
|
+
// Run `node scripts/sync-protocol-types.mjs` after editing the source.
|
|
1179
|
+
// ===========================================================================
|
|
1180
|
+
// binder protocol — how a package that inserts DOM hands those nodes to whoever
|
|
1181
|
+
// owns data bindings on the page.
|
|
1182
|
+
//
|
|
1183
|
+
// The dual of transition-runner: that one hands a *mutation* to whoever animates
|
|
1184
|
+
// it, this one hands *new nodes* to whoever binds them.
|
|
1185
|
+
//
|
|
1186
|
+
// A `data-wcs` binding exists only for nodes @wcstack/state walked when it built
|
|
1187
|
+
// its bindings. Nodes that arrive later — the content of a route that was not
|
|
1188
|
+
// active at that moment, a <wcs-head> child reflected into <head> — were never
|
|
1189
|
+
// walked, so their bindings silently do nothing, however often they are inserted.
|
|
1190
|
+
// @wcstack/router must not depend on @wcstack/state (zero runtime dependencies,
|
|
1191
|
+
// independently publishable), so state installs a binder on a well-known global
|
|
1192
|
+
// symbol and inserters look it up lazily.
|
|
1193
|
+
//
|
|
1194
|
+
// No binder installed means nothing happens — byte-for-byte the behavior these
|
|
1195
|
+
// packages had before the protocol existed.
|
|
1196
|
+
//
|
|
1197
|
+
// docs/binder-protocol-design.md is the normative description.
|
|
1198
|
+
//
|
|
1199
|
+
// SINGLE SOURCE OF TRUTH: edit only this file (/protocol/binder.ts), then run
|
|
1200
|
+
// `node scripts/sync-protocol-types.mjs` to regenerate the per-package copies
|
|
1201
|
+
// (packages/<pkg>/src/protocol/binder.ts). Those copies are generated — do not edit them.
|
|
1202
|
+
/**
|
|
1203
|
+
* Global key the binder installs itself under. `Symbol.for` so independently
|
|
1204
|
+
* loaded copies of this file (two CDN bundles on one page) still agree.
|
|
1205
|
+
*/
|
|
1206
|
+
const BINDER_KEY = Symbol.for("wcstack.binder");
|
|
1207
|
+
/**
|
|
1208
|
+
* The installed binder, or null when there is none or it speaks a version this
|
|
1209
|
+
* reader does not.
|
|
1210
|
+
*
|
|
1211
|
+
* Looked up on every call rather than cached, for the same reason
|
|
1212
|
+
* transition-runner does: the page's composition can change at any point, and a
|
|
1213
|
+
* stale cache would keep calling into a binder that is no longer there.
|
|
1214
|
+
*/
|
|
1215
|
+
function getBinder() {
|
|
1216
|
+
const candidate = globalThis[BINDER_KEY];
|
|
1217
|
+
if (candidate === undefined || candidate === null)
|
|
1218
|
+
return null;
|
|
1219
|
+
if (candidate.protocol !== "wcs-binder")
|
|
1220
|
+
return null;
|
|
1221
|
+
if (typeof candidate.version !== "number" || candidate.version < 1)
|
|
1222
|
+
return null;
|
|
1223
|
+
if (typeof candidate.bind !== "function")
|
|
1224
|
+
return null;
|
|
1225
|
+
return candidate;
|
|
1226
|
+
}
|
|
1227
|
+
/**
|
|
1228
|
+
* Subtrees offered before a binder existed, and the set of everything a binder
|
|
1229
|
+
* has taken. Both live on global symbols so that independently loaded copies of
|
|
1230
|
+
* this file — the router's and state's — share one queue.
|
|
1231
|
+
*
|
|
1232
|
+
* The queue is needed because of load order: the router's auto bundle runs
|
|
1233
|
+
* before state's, so `<wcs-head>` reflects its children into `<head>` while
|
|
1234
|
+
* there is still nothing to bind them. Offering them to a binder that arrives
|
|
1235
|
+
* later is the difference between working and silently blank.
|
|
1236
|
+
*/
|
|
1237
|
+
const PENDING_KEY = Symbol.for("wcstack.binder.pending");
|
|
1238
|
+
const TAKEN_KEY = Symbol.for("wcstack.binder.taken");
|
|
1239
|
+
function pendingQueue() {
|
|
1240
|
+
const globals = globalThis;
|
|
1241
|
+
let queue = globals[PENDING_KEY];
|
|
1242
|
+
if (queue === undefined) {
|
|
1243
|
+
queue = [];
|
|
1244
|
+
globals[PENDING_KEY] = queue;
|
|
1245
|
+
}
|
|
1246
|
+
return queue;
|
|
1247
|
+
}
|
|
1248
|
+
function takenSet() {
|
|
1249
|
+
const globals = globalThis;
|
|
1250
|
+
let taken = globals[TAKEN_KEY];
|
|
1251
|
+
if (taken === undefined) {
|
|
1252
|
+
taken = new WeakSet();
|
|
1253
|
+
globals[TAKEN_KEY] = taken;
|
|
1254
|
+
}
|
|
1255
|
+
return taken;
|
|
1256
|
+
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Hand `subtree` to the installed binder, or hold it for one that arrives later.
|
|
1259
|
+
*
|
|
1260
|
+
* Returns whether a binder took it *now*. A `false` does not yet mean the markup
|
|
1261
|
+
* is doomed — check {@link wasBoundBy} once module scripts have run.
|
|
1262
|
+
*
|
|
1263
|
+
* `options` goes to the binder with the subtree. A subtree held for a later binder
|
|
1264
|
+
* is handed over without it: that binder's first walk of the page binds what is
|
|
1265
|
+
* still in place there.
|
|
1266
|
+
*/
|
|
1267
|
+
function bindSubtree(subtree, options) {
|
|
1268
|
+
const binder = getBinder();
|
|
1269
|
+
if (binder === null) {
|
|
1270
|
+
pendingQueue().push(subtree);
|
|
1271
|
+
return false;
|
|
1272
|
+
}
|
|
1273
|
+
takenSet().add(subtree);
|
|
1274
|
+
binder.bind(subtree, options);
|
|
1275
|
+
return true;
|
|
1276
|
+
}
|
|
1277
|
+
/** Whether any binder has taken this subtree. */
|
|
1278
|
+
function wasBoundBy(subtree) {
|
|
1279
|
+
return takenSet().has(subtree);
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
/**
|
|
1283
|
+
* binder への宣言: router はルートの範囲(placeholder 〜 終了マーカー)を持ち運ぶ
|
|
1284
|
+
* (hideRoute / showRoute)。範囲の中に描かれたもの(直下の構造テンプレートの行・枝)も
|
|
1285
|
+
* 一緒に出入りするので、binder はそうしたテンプレートを描いてよい(docs/binder-protocol-design.md §2)。
|
|
1286
|
+
*/
|
|
1287
|
+
const ROUTE_RANGE = { range: true };
|
|
1288
|
+
/**
|
|
1289
|
+
* 文書に置いたノードの要素を、範囲を持ち運ぶ宣言付きで binder へ渡す(挿入の後に渡す — D5)。
|
|
1290
|
+
* binder が居なければ渡さない: 保留キューに溜めず、後から来る state の最初の走査が拾う。
|
|
1291
|
+
*/
|
|
1292
|
+
function offerToBinder(nodes) {
|
|
1293
|
+
if (getBinder() === null)
|
|
1294
|
+
return;
|
|
1295
|
+
for (const node of nodes) {
|
|
1296
|
+
if (node.nodeType === 1) {
|
|
1297
|
+
bindSubtree(node, ROUTE_RANGE);
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
/** The placeholders and end marks of a route's descendants */
|
|
1302
|
+
function innerMarks(route, marks) {
|
|
1303
|
+
for (const child of route.routeChildNodes) {
|
|
1304
|
+
marks.add(child.placeHolder).add(child.endMarker);
|
|
1305
|
+
innerMarks(child, marks);
|
|
1306
|
+
}
|
|
1307
|
+
return marks;
|
|
1308
|
+
}
|
|
1309
|
+
/**
|
|
1310
|
+
* The nodes between a route's placeholder and its end mark, in order. When the end mark is not
|
|
1311
|
+
* after the placeholder (other code moved or removed it), the range ends at the last of the route's
|
|
1312
|
+
* own nodes still beside the placeholder, and never runs past another route's mark (a sibling's
|
|
1313
|
+
* placeholder, a parent's end mark): what follows cannot be told from what is not the route's, and
|
|
1314
|
+
* an own node beyond goes back as one moved out of the range. Read in full before anything is
|
|
1315
|
+
* moved: moving runs disconnectedCallback, which may change what follows the placeholder.
|
|
1316
|
+
*/
|
|
1317
|
+
function rangeOf(route) {
|
|
1318
|
+
const placeHolder = route.placeHolder;
|
|
1319
|
+
const end = route.endMarker;
|
|
1320
|
+
const marked = end.parentNode !== null && end.parentNode === placeHolder.parentNode &&
|
|
1321
|
+
(placeHolder.compareDocumentPosition(end) & 4 /* DOCUMENT_POSITION_FOLLOWING */) !== 0;
|
|
1322
|
+
const inner = marked ? null : innerMarks(route, new Set());
|
|
1323
|
+
const nodes = [];
|
|
1324
|
+
let length = 0;
|
|
1325
|
+
for (let node = placeHolder.nextSibling; node !== null && node !== end; node = node.nextSibling) {
|
|
1326
|
+
if (inner !== null && node.nodeType === 8 && /^@@(route:|wcs-route-)/.test(node.data) && !inner.has(node)) {
|
|
1327
|
+
break;
|
|
1328
|
+
}
|
|
1329
|
+
nodes.push(node);
|
|
1330
|
+
if (marked || route.childNodeArray.includes(node)) {
|
|
1331
|
+
length = nodes.length;
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
nodes.length = length;
|
|
1335
|
+
return nodes;
|
|
1336
|
+
}
|
|
1337
|
+
/**
|
|
1338
|
+
* Moves `nodes` into `target` in order. Each move may run a disconnectedCallback, which may move or
|
|
1339
|
+
* remove a node that comes later in the list (a `<wcs-link>` removes its anchor): such a node is
|
|
1340
|
+
* left where that callback put it.
|
|
1341
|
+
*/
|
|
1342
|
+
function moveInto(target, nodes) {
|
|
1343
|
+
const from = nodes.map((node) => node.parentNode);
|
|
1344
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
1345
|
+
if (nodes[i].parentNode === from[i]) {
|
|
1346
|
+
target.appendChild(nodes[i]);
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1105
1351
|
class LayoutOutlet extends HTMLElement {
|
|
1106
1352
|
_layout = null;
|
|
1107
1353
|
_initialized = false;
|
|
@@ -1188,6 +1434,10 @@ class LayoutOutlet extends HTMLElement {
|
|
|
1188
1434
|
}
|
|
1189
1435
|
this.appendChild(fragmentForTemplate);
|
|
1190
1436
|
}
|
|
1437
|
+
// 置いた後で binder へ渡す(D5)。遷移で入ったルートの内容は router が渡す時点では文書の外で、
|
|
1438
|
+
// レイアウトのテンプレート自身の束縛もここでしか渡せない(shadow root の中は対象外)。light DOM は
|
|
1439
|
+
// 初期化の前は空なので、子が置いたもの。写して回す(渡した直下の for: が行を足すと番号がずれる)
|
|
1440
|
+
offerToBinder(Array.from(this.childNodes));
|
|
1191
1441
|
}
|
|
1192
1442
|
finally {
|
|
1193
1443
|
this._initializing = false;
|
|
@@ -1237,14 +1487,16 @@ const CSP_GUIDE = "https://github.com/wcstack/wcstack/blob/main/docs/csp.md";
|
|
|
1237
1487
|
* 真ならブロック確定として対処方法を書き、偽なら構文エラー等と区別できないので
|
|
1238
1488
|
* 元のエラーを主にして CSP は参照先を添えるに留める。
|
|
1239
1489
|
*
|
|
1240
|
-
*
|
|
1241
|
-
*
|
|
1490
|
+
* blob: の import は、router を読み込んだ `<script>` の nonce を引き継ぐので、そこに nonce を
|
|
1491
|
+
* 付ければ通る(docs/csp.md §5)。ガードは state と違ってインライン専用(`<wcs-route>` 直下の
|
|
1492
|
+
* `<script>`)なので、`src=` に逃がすという回避策が無い。nonce を付けられない構成で CSP を敷くなら、
|
|
1493
|
+
* blob: の許可が必須になる。
|
|
1242
1494
|
*/
|
|
1243
1495
|
function describeImportFailure(error, firstError, cspBlocked) {
|
|
1244
1496
|
if (cspBlocked) {
|
|
1245
1497
|
return `The guard <script> was blocked by Content-Security-Policy. ` +
|
|
1246
|
-
`Guard scripts are inline-only and are evaluated through a blob: URL
|
|
1247
|
-
`
|
|
1498
|
+
`Guard scripts are inline-only and are evaluated through a blob: URL: ` +
|
|
1499
|
+
`give the page's nonce to the <script> that loads @wcstack/router, or allow blob: in script-src. See ${CSP_GUIDE}`;
|
|
1248
1500
|
}
|
|
1249
1501
|
return `loadGuardHandler: failed to import guard script. ` +
|
|
1250
1502
|
`data: URL error: ${error?.message ?? String(error)}` +
|
|
@@ -1292,6 +1544,9 @@ async function importModule(script, route) {
|
|
|
1292
1544
|
scriptModule = await import(`data:application/javascript;base64,${b64}`);
|
|
1293
1545
|
}
|
|
1294
1546
|
catch (e) {
|
|
1547
|
+
// Firefox は違反イベントを import の失敗より後(次のタスク)に出す(Chromium と WebKit は先)。
|
|
1548
|
+
// 判定の前に 1 タスク待って、どのエンジンでも CSP を観測できるようにする
|
|
1549
|
+
await new Promise((resolve) => setTimeout(resolve));
|
|
1295
1550
|
// 両 import が失敗した場合、Blob URL 側の元エラーを cause として失わないように包む
|
|
1296
1551
|
// (Blob URL も失敗していなければ firstError は null)
|
|
1297
1552
|
raiseError(describeImportFailure(e, firstError, cspBlocked), { cause: firstError ?? e });
|
|
@@ -1730,11 +1985,42 @@ function normalizeBasename(path) {
|
|
|
1730
1985
|
return p;
|
|
1731
1986
|
}
|
|
1732
1987
|
|
|
1733
|
-
|
|
1734
|
-
|
|
1988
|
+
/**
|
|
1989
|
+
* ルートの内容を `route.held` へ持ち出す: placeholder から `route.endMarker` までの範囲。
|
|
1990
|
+
* state が描いた行・枝とアンカーは childNodeArray に無いので、範囲で持つ。範囲の外へ
|
|
1991
|
+
* 移された自分のノード(body のダイアログ)は元の順の位置で持ち、親の無いもの
|
|
1992
|
+
* (アンカーに置き換えられた template)は戻さない。すでに持っていれば何もしない
|
|
1993
|
+
* (重なったナビゲーションが二度隠しても、持っている内容を失わない)。
|
|
1994
|
+
*/
|
|
1995
|
+
function holdRoute(route) {
|
|
1996
|
+
if (route.held !== null) {
|
|
1997
|
+
return;
|
|
1998
|
+
}
|
|
1999
|
+
// Decide which nodes go, and in which order, before moving any: a move runs disconnectedCallback.
|
|
2000
|
+
const nodes = rangeOf(route);
|
|
2001
|
+
const top = new Set(nodes);
|
|
2002
|
+
const outer = [...nodes, ...route.childNodeArray];
|
|
2003
|
+
let prev = null;
|
|
1735
2004
|
for (const node of route.childNodeArray) {
|
|
1736
|
-
|
|
2005
|
+
if (top.has(node)) {
|
|
2006
|
+
prev = node;
|
|
2007
|
+
}
|
|
2008
|
+
else if (node.parentNode !== null && !outer.some((other) => other !== node && other.contains(node))) {
|
|
2009
|
+
// an own node moved out of the range goes back where it was written (one inside another goes with it)
|
|
2010
|
+
nodes.splice(prev === null ? 0 : nodes.indexOf(prev) + 1, 0, node);
|
|
2011
|
+
top.add(node);
|
|
2012
|
+
prev = node;
|
|
2013
|
+
}
|
|
1737
2014
|
}
|
|
2015
|
+
const held = document.createDocumentFragment();
|
|
2016
|
+
moveInto(held, nodes);
|
|
2017
|
+
held.appendChild(route.endMarker);
|
|
2018
|
+
route.held = held;
|
|
2019
|
+
}
|
|
2020
|
+
/** ルートの内容を隠す: パラメータを消し、内容を持ち出す */
|
|
2021
|
+
function hideRoute(route) {
|
|
2022
|
+
route.clearParams();
|
|
2023
|
+
holdRoute(route);
|
|
1738
2024
|
}
|
|
1739
2025
|
|
|
1740
2026
|
/**
|
|
@@ -1773,19 +2059,30 @@ function assignRouteParams(route, matchResult) {
|
|
|
1773
2059
|
}
|
|
1774
2060
|
}
|
|
1775
2061
|
}
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
2062
|
+
/** Routes placed at least once: their written nodes are never placed again */
|
|
2063
|
+
const placed = new WeakSet();
|
|
2064
|
+
/**
|
|
2065
|
+
* ルートの内容を placeholder の後ろへ置く: `route.held` があればそれを、初めてなら元のノードと
|
|
2066
|
+
* `route.endMarker` を。表示中のルート(パラメータの変化)は、隠すときと同じく持ち出してすぐ戻す。
|
|
2067
|
+
* パラメータは先に割り当てておく(assignRouteParams)。
|
|
2068
|
+
*/
|
|
2069
|
+
function placeRoute(route) {
|
|
2070
|
+
const placeHolder = route.placeHolder;
|
|
2071
|
+
if (placeHolder.parentNode === null)
|
|
2072
|
+
return;
|
|
2073
|
+
if (placed.has(route) || route.endMarker.parentNode !== null) {
|
|
2074
|
+
// Shown before (again on a parameter change, or back from held): take it out as hiding does —
|
|
2075
|
+
// nothing to do when it is held — and put it back, so its custom elements reconnect and read the
|
|
2076
|
+
// new params. Never its written nodes again, even when other code removed the end mark: a
|
|
2077
|
+
// template the state replaced with its anchor would come back.
|
|
2078
|
+
holdRoute(route);
|
|
2079
|
+
placeHolder.after(route.held);
|
|
2080
|
+
route.held = null;
|
|
1787
2081
|
}
|
|
1788
|
-
|
|
2082
|
+
else {
|
|
2083
|
+
placeHolder.after(...route.childNodeArray, route.endMarker);
|
|
2084
|
+
}
|
|
2085
|
+
placed.add(route);
|
|
1789
2086
|
}
|
|
1790
2087
|
|
|
1791
2088
|
// ===========================================================================
|
|
@@ -1850,109 +2147,6 @@ function runTransition(source, mutate, types) {
|
|
|
1850
2147
|
return runner.run(mutate, { source, types });
|
|
1851
2148
|
}
|
|
1852
2149
|
|
|
1853
|
-
// ===========================================================================
|
|
1854
|
-
// AUTO-GENERATED FILE - DO NOT EDIT.
|
|
1855
|
-
// Generated from /protocol/binder.ts by scripts/sync-protocol-types.mjs.
|
|
1856
|
-
// Run `node scripts/sync-protocol-types.mjs` after editing the source.
|
|
1857
|
-
// ===========================================================================
|
|
1858
|
-
// binder protocol — how a package that inserts DOM hands those nodes to whoever
|
|
1859
|
-
// owns data bindings on the page.
|
|
1860
|
-
//
|
|
1861
|
-
// The dual of transition-runner: that one hands a *mutation* to whoever animates
|
|
1862
|
-
// it, this one hands *new nodes* to whoever binds them.
|
|
1863
|
-
//
|
|
1864
|
-
// A `data-wcs` binding exists only for nodes @wcstack/state walked when it built
|
|
1865
|
-
// its bindings. Nodes that arrive later — the content of a route that was not
|
|
1866
|
-
// active at that moment, a <wcs-head> child reflected into <head> — were never
|
|
1867
|
-
// walked, so their bindings silently do nothing, however often they are inserted.
|
|
1868
|
-
// @wcstack/router must not depend on @wcstack/state (zero runtime dependencies,
|
|
1869
|
-
// independently publishable), so state installs a binder on a well-known global
|
|
1870
|
-
// symbol and inserters look it up lazily.
|
|
1871
|
-
//
|
|
1872
|
-
// No binder installed means nothing happens — byte-for-byte the behavior these
|
|
1873
|
-
// packages had before the protocol existed.
|
|
1874
|
-
//
|
|
1875
|
-
// docs/binder-protocol-design.md is the normative description.
|
|
1876
|
-
//
|
|
1877
|
-
// SINGLE SOURCE OF TRUTH: edit only this file (/protocol/binder.ts), then run
|
|
1878
|
-
// `node scripts/sync-protocol-types.mjs` to regenerate the per-package copies
|
|
1879
|
-
// (packages/<pkg>/src/protocol/binder.ts). Those copies are generated — do not edit them.
|
|
1880
|
-
/**
|
|
1881
|
-
* Global key the binder installs itself under. `Symbol.for` so independently
|
|
1882
|
-
* loaded copies of this file (two CDN bundles on one page) still agree.
|
|
1883
|
-
*/
|
|
1884
|
-
const BINDER_KEY = Symbol.for("wcstack.binder");
|
|
1885
|
-
/**
|
|
1886
|
-
* The installed binder, or null when there is none or it speaks a version this
|
|
1887
|
-
* reader does not.
|
|
1888
|
-
*
|
|
1889
|
-
* Looked up on every call rather than cached, for the same reason
|
|
1890
|
-
* transition-runner does: the page's composition can change at any point, and a
|
|
1891
|
-
* stale cache would keep calling into a binder that is no longer there.
|
|
1892
|
-
*/
|
|
1893
|
-
function getBinder() {
|
|
1894
|
-
const candidate = globalThis[BINDER_KEY];
|
|
1895
|
-
if (candidate === undefined || candidate === null)
|
|
1896
|
-
return null;
|
|
1897
|
-
if (candidate.protocol !== "wcs-binder")
|
|
1898
|
-
return null;
|
|
1899
|
-
if (typeof candidate.version !== "number" || candidate.version < 1)
|
|
1900
|
-
return null;
|
|
1901
|
-
if (typeof candidate.bind !== "function")
|
|
1902
|
-
return null;
|
|
1903
|
-
return candidate;
|
|
1904
|
-
}
|
|
1905
|
-
/**
|
|
1906
|
-
* Subtrees offered before a binder existed, and the set of everything a binder
|
|
1907
|
-
* has taken. Both live on global symbols so that independently loaded copies of
|
|
1908
|
-
* this file — the router's and state's — share one queue.
|
|
1909
|
-
*
|
|
1910
|
-
* The queue is needed because of load order: the router's auto bundle runs
|
|
1911
|
-
* before state's, so `<wcs-head>` reflects its children into `<head>` while
|
|
1912
|
-
* there is still nothing to bind them. Offering them to a binder that arrives
|
|
1913
|
-
* later is the difference between working and silently blank.
|
|
1914
|
-
*/
|
|
1915
|
-
const PENDING_KEY = Symbol.for("wcstack.binder.pending");
|
|
1916
|
-
const TAKEN_KEY = Symbol.for("wcstack.binder.taken");
|
|
1917
|
-
function pendingQueue() {
|
|
1918
|
-
const globals = globalThis;
|
|
1919
|
-
let queue = globals[PENDING_KEY];
|
|
1920
|
-
if (queue === undefined) {
|
|
1921
|
-
queue = [];
|
|
1922
|
-
globals[PENDING_KEY] = queue;
|
|
1923
|
-
}
|
|
1924
|
-
return queue;
|
|
1925
|
-
}
|
|
1926
|
-
function takenSet() {
|
|
1927
|
-
const globals = globalThis;
|
|
1928
|
-
let taken = globals[TAKEN_KEY];
|
|
1929
|
-
if (taken === undefined) {
|
|
1930
|
-
taken = new WeakSet();
|
|
1931
|
-
globals[TAKEN_KEY] = taken;
|
|
1932
|
-
}
|
|
1933
|
-
return taken;
|
|
1934
|
-
}
|
|
1935
|
-
/**
|
|
1936
|
-
* Hand `subtree` to the installed binder, or hold it for one that arrives later.
|
|
1937
|
-
*
|
|
1938
|
-
* Returns whether a binder took it *now*. A `false` does not yet mean the markup
|
|
1939
|
-
* is doomed — check {@link wasBoundBy} once module scripts have run.
|
|
1940
|
-
*/
|
|
1941
|
-
function bindSubtree(subtree) {
|
|
1942
|
-
const binder = getBinder();
|
|
1943
|
-
if (binder === null) {
|
|
1944
|
-
pendingQueue().push(subtree);
|
|
1945
|
-
return false;
|
|
1946
|
-
}
|
|
1947
|
-
takenSet().add(subtree);
|
|
1948
|
-
binder.bind(subtree);
|
|
1949
|
-
return true;
|
|
1950
|
-
}
|
|
1951
|
-
/** Whether any binder has taken this subtree. */
|
|
1952
|
-
function wasBoundBy(subtree) {
|
|
1953
|
-
return takenSet().has(subtree);
|
|
1954
|
-
}
|
|
1955
|
-
|
|
1956
2150
|
/**
|
|
1957
2151
|
* 「後から差し込んだノードのバインドは効かない」ことを loud に報告する。
|
|
1958
2152
|
*
|
|
@@ -2034,8 +2228,16 @@ function bindRouteContent(route) {
|
|
|
2034
2228
|
for (const node of route.childNodeArray) {
|
|
2035
2229
|
if (node.nodeType !== 1)
|
|
2036
2230
|
continue;
|
|
2037
|
-
if (
|
|
2231
|
+
if (node.isConnected) {
|
|
2232
|
+
if (bindSubtree(node, ROUTE_RANGE))
|
|
2233
|
+
continue;
|
|
2234
|
+
}
|
|
2235
|
+
else if (getBinder() !== null) {
|
|
2236
|
+
// Not in the document: the content of a route inside a <wcs-layout> whose template is still
|
|
2237
|
+
// loading. A binder must not take a detached subtree (3.x binds it detached, and its event
|
|
2238
|
+
// bindings fail as disconnected); the layout outlet hands it over once it has placed it.
|
|
2038
2239
|
continue;
|
|
2240
|
+
}
|
|
2039
2241
|
warnUnboundMarkup(node, `<${node.tagName.toLowerCase()}> inside a route`, `Load @wcstack/state on this page, or render data-driven markup outside ` +
|
|
2040
2242
|
`<wcs-router> — bind the router's \`path\` into state and gate the markup ` +
|
|
2041
2243
|
`with <template data-wcs="if: …">. See examples/router-i18n.`);
|
|
@@ -2113,16 +2315,29 @@ async function showRouteContent(routerNode, matchResult, lastRoutes) {
|
|
|
2113
2315
|
hideRoute(route);
|
|
2114
2316
|
}
|
|
2115
2317
|
}
|
|
2116
|
-
|
|
2318
|
+
// The routes to show (new ones, ones whose params changed, and every one under the first of
|
|
2319
|
+
// them) get their params first, all of them: putting a parent back moves its children's content
|
|
2320
|
+
// too, and their connectedCallback must already see the new params.
|
|
2321
|
+
const shown = [];
|
|
2117
2322
|
for (const route of matchResult.routes) {
|
|
2118
|
-
if (!lastRouteSet.has(route) || route.shouldChange(matchResult.params)
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2323
|
+
if (shown.length > 0 || !lastRouteSet.has(route) || route.shouldChange(matchResult.params)) {
|
|
2324
|
+
shown.push(route);
|
|
2325
|
+
assignRouteParams(route, matchResult);
|
|
2326
|
+
}
|
|
2327
|
+
}
|
|
2328
|
+
let moved = false;
|
|
2329
|
+
for (const route of shown) {
|
|
2330
|
+
const again = lastRouteSet.has(route);
|
|
2331
|
+
// A route shown again under one shown again was moved along with it: not twice.
|
|
2332
|
+
if (!(again && moved)) {
|
|
2333
|
+
placeRoute(route);
|
|
2334
|
+
}
|
|
2335
|
+
moved ||= again;
|
|
2336
|
+
// 挿入の後。初回描画(lastRoutes が空)の内容は state のバインド構築時に
|
|
2337
|
+
// document に居るので、そこは binder に渡す必要も報告する必要も無い。
|
|
2338
|
+
// `bind()` 自体は冪等なので渡しても壊れないが、渡さないほうが安い。
|
|
2339
|
+
if (lastRoutes.length > 0 && !again) {
|
|
2340
|
+
bindRouteContent(route);
|
|
2126
2341
|
}
|
|
2127
2342
|
}
|
|
2128
2343
|
};
|
|
@@ -2317,25 +2532,6 @@ function inSsr() {
|
|
|
2317
2532
|
return html ? html.hasAttribute('data-wcs-server') : false;
|
|
2318
2533
|
}
|
|
2319
2534
|
|
|
2320
|
-
/**
|
|
2321
|
-
* SSR ハイドレーションマーカー(docs/ssr-router-design.md §3.3 / §4)。
|
|
2322
|
-
*
|
|
2323
|
-
* サーバー(_renderForSsr)が書き、クライアント(_hydrateFromSsr / Link の採用)が
|
|
2324
|
-
* 読む。キーは route の absolutePath — placeholder の UUID はパースごとに再生成され
|
|
2325
|
-
* サーバーとクライアントで一致しないため、同一 template から決定的に導ける
|
|
2326
|
-
* absolutePath だけが突合キーになれる。
|
|
2327
|
-
*/
|
|
2328
|
-
/** サーバー描画済み outlet の目印(要素属性) */
|
|
2329
|
-
const SSR_OUTLET_ATTR = 'data-wcs-ssr';
|
|
2330
|
-
/** Link がサーバーで生成した anchor の目印(要素属性)。クライアントが採用して外す */
|
|
2331
|
-
const SSR_LINK_ATTR = 'data-wcs-ssr-link';
|
|
2332
|
-
/** route placeholder コメントの安定キー形式(`@@wcs-route-ph:<absolutePath>`) */
|
|
2333
|
-
const ROUTE_PH_PREFIX = '@@wcs-route-ph:';
|
|
2334
|
-
/** 表示中ルート内容の開始マーカー(`@@wcs-route-start:<absolutePath>`) */
|
|
2335
|
-
const ROUTE_START_PREFIX = '@@wcs-route-start:';
|
|
2336
|
-
/** 表示中ルート内容の終了マーカー(`@@wcs-route-end:<absolutePath>`) */
|
|
2337
|
-
const ROUTE_END_PREFIX = '@@wcs-route-end:';
|
|
2338
|
-
|
|
2339
2535
|
const EMPTY_RECORD = Object.freeze({});
|
|
2340
2536
|
/**
|
|
2341
2537
|
* AppRoutes - Root component for @wcstack/router
|
|
@@ -3101,13 +3297,12 @@ class Router extends HTMLElement {
|
|
|
3101
3297
|
nodes.push(node);
|
|
3102
3298
|
node = node.nextSibling;
|
|
3103
3299
|
}
|
|
3104
|
-
|
|
3300
|
+
// 終了マーカーは残し、以後このルートの範囲の終わりとして使う(hideRoute)
|
|
3301
|
+
route.adoptChildNodes(nodes, end);
|
|
3105
3302
|
}
|
|
3106
|
-
//
|
|
3303
|
+
// 開始マーカーの除去と目印の撤去(終了マーカーは各ルートの範囲の終わりとして残る)
|
|
3107
3304
|
for (const { comment } of startByPath.values())
|
|
3108
3305
|
comment.remove();
|
|
3109
|
-
for (const { comment } of endByPath.values())
|
|
3110
|
-
comment.remove();
|
|
3111
3306
|
this.outlet.removeAttribute(SSR_OUTLET_ATTR);
|
|
3112
3307
|
// 表示済み状態の確立。内容は既に見えているので挿入はしない — パラメータ
|
|
3113
3308
|
// 配送(setParams / data-bind / active イベント)だけ CSR と同じ規則で行う。
|
|
@@ -3180,14 +3375,8 @@ class Router extends HTMLElement {
|
|
|
3180
3375
|
});
|
|
3181
3376
|
this.outlet.setAttribute(SSR_OUTLET_ATTR, '');
|
|
3182
3377
|
for (const route of this.outlet.lastRoutes) {
|
|
3183
|
-
//
|
|
3184
|
-
|
|
3185
|
-
const contentNodes = route.childNodeArray;
|
|
3186
|
-
const start = document.createComment(`${ROUTE_START_PREFIX}${route.absolutePath}`);
|
|
3187
|
-
const end = document.createComment(`${ROUTE_END_PREFIX}${route.absolutePath}`);
|
|
3188
|
-
parentNode.insertBefore(start, contentNodes[0] ?? route.placeHolder.nextSibling);
|
|
3189
|
-
const last = contentNodes[contentNodes.length - 1];
|
|
3190
|
-
parentNode.insertBefore(end, last ? last.nextSibling : start.nextSibling);
|
|
3378
|
+
// 終了マーカーは showRoute が置いた route.endMarker をそのまま使う
|
|
3379
|
+
route.placeHolder.after(document.createComment(`${ROUTE_START_PREFIX}${route.absolutePath}`));
|
|
3191
3380
|
}
|
|
3192
3381
|
}
|
|
3193
3382
|
/**
|
|
@@ -3198,8 +3387,10 @@ class Router extends HTMLElement {
|
|
|
3198
3387
|
_offerInitialContentToBinder() {
|
|
3199
3388
|
for (const route of this.outlet.lastRoutes) {
|
|
3200
3389
|
for (const node of route.childNodeArray) {
|
|
3201
|
-
|
|
3202
|
-
|
|
3390
|
+
// A node not in the document (a route inside a <wcs-layout> whose template is still
|
|
3391
|
+
// loading) is handed over by the layout outlet once placed, or found by state's first scan
|
|
3392
|
+
if (node.nodeType === Node.ELEMENT_NODE && node.isConnected) {
|
|
3393
|
+
bindSubtree(node, ROUTE_RANGE);
|
|
3203
3394
|
}
|
|
3204
3395
|
}
|
|
3205
3396
|
}
|
|
@@ -3857,7 +4048,7 @@ function bootstrapRouter(config, registry) {
|
|
|
3857
4048
|
registerComponents(registry);
|
|
3858
4049
|
}
|
|
3859
4050
|
|
|
3860
|
-
var version = "3.
|
|
4051
|
+
var version = "3.5.1";
|
|
3861
4052
|
var pkg = {
|
|
3862
4053
|
version: version};
|
|
3863
4054
|
|