@wcstack/router 4.29.0 → 4.31.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/dist/index.d.ts CHANGED
@@ -785,6 +785,7 @@ declare class Link extends HTMLElement implements ILink {
785
785
  * 通常は registerComponents() で Router を Link より先に upgrade することを推奨する。
786
786
  */
787
787
  get router(): Router;
788
+ private _findRouter;
788
789
  private _initialize;
789
790
  /**
790
791
  * URL pathname を正規化する。Router と共通実装を使うことで
@@ -799,6 +800,11 @@ declare class Link extends HTMLElement implements ILink {
799
800
  private _isInternalTarget;
800
801
  private _setAnchorHref;
801
802
  connectedCallback(): void;
803
+ /**
804
+ * a `to` assigned before the element was defined (state loaded before the router) is an own property that
805
+ * hides the accessor: take it in through the setter
806
+ */
807
+ private _adoptOwnTo;
802
808
  /**
803
809
  * サーバーが生成した目印付き anchor(直後の兄弟)。クライアントの採用対象
804
810
  */
package/dist/index.esm.js CHANGED
@@ -78,7 +78,7 @@ function setConfig(partialConfig) {
78
78
  }
79
79
 
80
80
  var name = "@wcstack/router";
81
- var version = "4.29.0";
81
+ var version = "4.31.0";
82
82
  var pkg = {
83
83
  name: name,
84
84
  version: version};
@@ -4186,7 +4186,7 @@ class Router extends HTMLElement {
4186
4186
  const MIRRORED_ATTRIBUTES = ['title', 'rel', 'target', 'download', 'hreflang', 'lang', 'dir'];
4187
4187
  class Link extends HTMLElement {
4188
4188
  static get observedAttributes() {
4189
- return ['to', 'match', 'query-keys', ...MIRRORED_ATTRIBUTES];
4189
+ return ['to', 'match', 'query-keys', 'current', ...MIRRORED_ATTRIBUTES];
4190
4190
  }
4191
4191
  _childNodeArray = [];
4192
4192
  _uuid = getUUID();
@@ -4221,21 +4221,18 @@ class Link extends HTMLElement {
4221
4221
  * 通常は registerComponents() で Router を Link より先に upgrade することを推奨する。
4222
4222
  */
4223
4223
  get router() {
4224
+ return this._findRouter() ?? raiseError(`${config.tagNames.link} is not connected to a router.`);
4225
+ }
4226
+ _findRouter() {
4224
4227
  if (this._router) {
4225
4228
  return this._router;
4226
4229
  }
4227
4230
  // DOM 祖先走査で最寄りの Router を探す(マルチ Router 対応)
4228
- const ancestor = this.closest(config.tagNames.router);
4229
- if (ancestor) {
4230
- return (this._router = ancestor);
4231
- }
4232
4231
  // 祖先にない場合は ownerDocument 内の Router を探す
4233
- const root = this.getRootNode();
4234
- const found = root.querySelector?.(config.tagNames.router);
4235
- if (found) {
4236
- return (this._router = found);
4237
- }
4238
- raiseError(`${config.tagNames.link} is not connected to a router.`);
4232
+ const found = this.closest(config.tagNames.router)
4233
+ ?? this.getRootNode().querySelector?.(config.tagNames.router)
4234
+ ?? null;
4235
+ return found === null ? null : (this._router = found);
4239
4236
  }
4240
4237
  _initialize() {
4241
4238
  // サーバーでは書かない: 直列化されると style 属性になり、厳しい style-src が拒む(docs/seam-apps.md、
@@ -4287,7 +4284,10 @@ class Link extends HTMLElement {
4287
4284
  const joined = pathname === ""
4288
4285
  ? window.location.pathname
4289
4286
  : this._joinInternalPath(this.router.basename, pathname);
4290
- anchor.href = joined + effectiveSearch(search) + hash;
4287
+ const href = joined + effectiveSearch(search) + hash;
4288
+ // 書くのは変わったときだけ: _updateActiveState がロケーションの変化のたびに引き直す
4289
+ if (anchor.getAttribute('href') !== href)
4290
+ anchor.href = href;
4291
4291
  }
4292
4292
  else {
4293
4293
  try {
@@ -4320,8 +4320,31 @@ class Link extends HTMLElement {
4320
4320
  });
4321
4321
  return;
4322
4322
  }
4323
+ // ルーターがまだ文書に無い: リンクをルーターより前に書いたページを、タグを定義した後で流し込んだ
4324
+ // (happy-dom の innerHTML は開始タグの時点で接続を呼ぶ — @wcstack/testing の mount())。流し込みは同期で
4325
+ // 終わるので、1 microtask 譲ってから組み立てる。子もそのときに揃う。譲っても無ければ従来どおり投げる。
4326
+ // 以前はその場で投げ、mount() が失敗した(Advent Calendar の制作の記録、docs/v4-direction-2026-10.md §5.27)
4327
+ this._adoptOwnTo();
4328
+ if (!this._initialized && this._isInternalTarget(this.to) && this._findRouter() === null) {
4329
+ queueMicrotask(() => {
4330
+ if (this.isConnected && !this._initialized)
4331
+ this._connect();
4332
+ });
4333
+ return;
4334
+ }
4323
4335
  this._connect();
4324
4336
  }
4337
+ /**
4338
+ * a `to` assigned before the element was defined (state loaded before the router) is an own property that
4339
+ * hides the accessor: take it in through the setter
4340
+ */
4341
+ _adoptOwnTo() {
4342
+ if (Object.prototype.hasOwnProperty.call(this, "to")) {
4343
+ const value = this.to;
4344
+ delete this.to;
4345
+ this.to = value;
4346
+ }
4347
+ }
4325
4348
  /**
4326
4349
  * サーバーが生成した目印付き anchor(直後の兄弟)。クライアントの採用対象
4327
4350
  */
@@ -4333,13 +4356,7 @@ class Link extends HTMLElement {
4333
4356
  return null;
4334
4357
  }
4335
4358
  _connect() {
4336
- // a `to` assigned before the element was defined (state loaded before the router) is an own property that
4337
- // hides the accessor: take it in through the setter
4338
- if (Object.prototype.hasOwnProperty.call(this, "to")) {
4339
- const value = this.to;
4340
- delete this.to;
4341
- this.to = value;
4342
- }
4359
+ this._adoptOwnTo();
4343
4360
  if (!this._initialized) {
4344
4361
  this._initialize();
4345
4362
  }
@@ -4444,7 +4461,7 @@ class Link extends HTMLElement {
4444
4461
  this._router = null;
4445
4462
  }
4446
4463
  attributeChangedCallback(name, oldValue, newValue) {
4447
- if ((name === 'match' || name === 'query-keys') && oldValue !== newValue) {
4464
+ if ((name === 'match' || name === 'query-keys' || name === 'current') && oldValue !== newValue) {
4448
4465
  // anchor 生成前(upgrade の属性の通知 — まだ router の下に居ないことがある)は接続時の判定に任せる
4449
4466
  if (this._anchorElement) {
4450
4467
  this._updateActiveState();
@@ -4472,9 +4489,10 @@ class Link extends HTMLElement {
4472
4489
  }
4473
4490
  }
4474
4491
  _updateActiveState = () => {
4475
- // クエリのみリンク(to="?k=v")の href は現在 pathname に依存するため、
4476
- // active 判定と同じリスナー経路でロケーション変更に追従させる(§4.1)。
4477
- if (this._path.startsWith('?') && this._anchorElement) {
4492
+ // 内部の行き先の href は、active 判定と同じリスナー経路で引き直す。クエリのみリンク(to="?k=v")の href は
4493
+ // 現在 pathname に依存し(§4.1)、ルーターの初期化より先に接続したリンクは basename をまだ知らなかった —
4494
+ // `/app/next` が `/next` になった(ルーターは初期化の終わりにこの通知を出す。Advent Calendar の制作の記録)
4495
+ if (this._anchorElement && this._isInternalTarget(this._path)) {
4478
4496
  this._setAnchorHref(this._anchorElement, this._path);
4479
4497
  }
4480
4498
  // active 判定は pathname の比較(§1.1 欠陥 7 の修理)に、`to` のクエリを足す(`to` にクエリが無ければ
@@ -4518,7 +4536,10 @@ class Link extends HTMLElement {
4518
4536
  this._anchorElement.classList.add('active');
4519
4537
  // 修理・既定オン(docs/a11y-design.md §3-3): active class と同じ事実の ARIA 表現。
4520
4538
  // 鮮度保証は active class と同一(同じ分岐・同じ呼び出し経路)。
4521
- this._anchorElement.setAttribute('aria-current', exact ? 'page' : 'true');
4539
+ // `current`: active のときに付ける aria-current の値を名指す("page" なら、下のパスでも「現在のページ」—
4540
+ // 「一覧」のリンクを詳細のページでもそう読ませたいナビ。docs/seam-apps.md、street-reports)。無ければ
4541
+ // 今までどおり: そのページそのものは "page"、下のパスや query-keys の一致は "true"
4542
+ this._anchorElement.setAttribute('aria-current', this.getAttribute('current') || (exact ? 'page' : 'true'));
4522
4543
  }
4523
4544
  else {
4524
4545
  this._anchorElement.classList.remove('active');