@wcstack/state 2.1.1 → 2.2.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
@@ -78,6 +78,7 @@ declare const setByAddressSymbol: unique symbol;
78
78
  declare const connectedCallbackSymbol: unique symbol;
79
79
  declare const disconnectedCallbackSymbol: unique symbol;
80
80
  declare const updatedCallbackSymbol: unique symbol;
81
+ declare const errorCallbackSymbol: unique symbol;
81
82
 
82
83
  interface IStateProxy extends IState {
83
84
  [setLoopContextSymbol](loopContext: ILoopContext | null, callback: () => any): any;
@@ -87,10 +88,13 @@ interface IStateProxy extends IState {
87
88
  [connectedCallbackSymbol](): Promise<void>;
88
89
  [disconnectedCallbackSymbol](): void;
89
90
  [updatedCallbackSymbol](updatedAbsAddressList: IAbsoluteStateAddress[]): void;
91
+ [errorCallbackSymbol](error: unknown, info: IBindingErrorInfo): void;
90
92
  }
91
93
  type Mutability = "readonly" | "writable";
92
94
 
93
95
  interface IStateElement {
96
+ /** DOM connection state; optional for non-DOM state implementations. */
97
+ readonly isConnected?: boolean;
94
98
  /**
95
99
  * state のロードが完了しているか。`initializePromise` の同期版で、
96
100
  * DCC のアクセサが「今すぐ読み書きしてよいか」を判断するのに使う。
@@ -157,6 +161,12 @@ interface IStateElement {
157
161
  * optional なのはテスト用モック互換のため(undefined は「不明=集計する」)。
158
162
  */
159
163
  readonly hasUpdatedCallback?: boolean;
164
+ /**
165
+ * state が $errorCallback を定義しているか。true のとき drain は隔離したバインディング
166
+ * 適用失敗を console.error の代わりに $errorCallback へ配送する(devtools sink へは常に流す)。
167
+ * optional なのはテスト用モック互換のため(undefined は「未定義=console.error」)。
168
+ */
169
+ readonly hasErrorCallback?: boolean;
160
170
  /**
161
171
  * 他行を読む getter(隣接項目参照など)が検出されたリストパスの集合。
162
172
  * これらのリストは walkDependency の diff-filter 展開の対象外(全行展開)。
@@ -345,6 +355,19 @@ interface IConfig {
345
355
  */
346
356
  readonly sameValueGuard: boolean;
347
357
  }
358
+ /**
359
+ * `$errorCallback(error, info)` の第 2 引数 — 適用に失敗したバインディングの識別情報。
360
+ * 値と DOM は巻き戻されない(隔離規範)。作者はここで「どのバインディングが」を知り、
361
+ * 自分の state にエラーを書いてページ内で受ける。
362
+ */
363
+ interface IBindingErrorInfo {
364
+ /** バインドされた state パス(`data-wcs` に書かれた形。`items.*.name` などワイルドカードのまま) */
365
+ readonly path: string;
366
+ /** バインディング種別(text / property / attribute / class / style / for / if …) */
367
+ readonly bindingType: BindingType;
368
+ /** バインディングが付いたノード(テキストバインドでは Text ノード) */
369
+ readonly node: Node;
370
+ }
348
371
  interface IWritableConfig {
349
372
  bindAttributeName?: string;
350
373
  commentTextPrefix?: string;
@@ -366,6 +389,41 @@ declare function bootstrapState(config?: IWritableConfig, registry?: CustomEleme
366
389
 
367
390
  declare function getConfig(): IConfig;
368
391
 
392
+ /**
393
+ * Trusted Types (`require-trusted-types-for 'script'`) 対応。正本は docs/csp.md §7。
394
+ *
395
+ * state が HTML sink に流すのは **状態の値**(`innerHTML: path` などのプロパティ
396
+ * バインド)で、ユーザー入力が混ざり得る文字列そのもの。ここに identity policy を
397
+ * 噛ませて通すのは TT の無効化と同義なので、state は自前の policy を作らない。
398
+ * 利用側が sanitizer を持つ policy を注入したときだけ通し、無ければ従来どおり
399
+ * ブラウザに弾かせる(ただし何を設定すれば直るかは必ず言う)。
400
+ *
401
+ * 注入口は全 @wcstack パッケージ共通のグローバルスロット。buildless(CDN 一発)でも
402
+ * inline script 1 本で差し込める:
403
+ *
404
+ * ```js
405
+ * globalThis[Symbol.for("wcstack.trustedTypes.policy")] =
406
+ * trustedTypes.createPolicy("my-app", { createHTML: (s) => DOMPurify.sanitize(s) });
407
+ * ```
408
+ *
409
+ * バンドラ経由なら `setTrustedTypesPolicy()` を使う。値は毎回スロットから読むので
410
+ * 後から差し替えても効く(identity policy を作る router / worker 側だけは
411
+ * `createPolicy` の重複を避けるため生成結果をシングルトンで保持する)。
412
+ */
413
+ interface IWcsTrustedTypesPolicy {
414
+ createHTML?(input: string): unknown;
415
+ createScriptURL?(input: string): unknown;
416
+ }
417
+ /** 利用側が policy を差し込むグローバルスロット(全 @wcstack パッケージ共通)。 */
418
+ declare const TRUSTED_TYPES_POLICY_SLOT: unique symbol;
419
+ /**
420
+ * 利用側が注入した policy を返す。state はここに identity policy をフォールバック
421
+ * させない(それをやると TT を無効化することになる)。
422
+ */
423
+ declare function getTrustedTypesPolicy(): IWcsTrustedTypesPolicy | null;
424
+ /** 利用側 policy を設定する(`null` で解除)。最初のバインド適用前に呼ぶこと。 */
425
+ declare function setTrustedTypesPolicy(policy: IWcsTrustedTypesPolicy | null): void;
426
+
369
427
  /**
370
428
  * 指定された rootNode のバインディング初期化が完了するまで待機する Promise を返す。
371
429
  */
@@ -1010,6 +1068,8 @@ declare class State extends HTMLElementBase implements IStateElement {
1010
1068
  static get observedAttributes(): string[];
1011
1069
  private __state;
1012
1070
  private _hasUpdatedCallback;
1071
+ /** $errorCallback の有無(_hasUpdatedCallback と同じく state セット時に確定。ルートのみ) */
1072
+ private _hasErrorCallback;
1013
1073
  /** enable-ssr のスナップショットから初期化された(D14: ボリュームはデータを採用する) */
1014
1074
  private _hydratedFromSsr;
1015
1075
  private _crossRowListPaths;
@@ -1153,6 +1213,7 @@ declare class State extends HTMLElementBase implements IStateElement {
1153
1213
  createState(mutability: Mutability, callback: (state: IStateProxy) => void): void;
1154
1214
  nextVersion(): number;
1155
1215
  get hasUpdatedCallback(): boolean;
1216
+ get hasErrorCallback(): boolean;
1156
1217
  get crossRowListPaths(): ReadonlySet<string>;
1157
1218
  addCrossRowListPath(path: string): void;
1158
1219
  get indexDependentGetterPaths(): ReadonlySet<string>;
@@ -1167,5 +1228,5 @@ declare global {
1167
1228
  }
1168
1229
  }
1169
1230
 
1170
- export { Ssr, VERSION, WCS_MANIFEST_VERSION, analyzeContract, bootstrapState, buildBindings, builtinFilterMeta, defineState, getBindingsReady, getConfig, getWcsManifest };
1171
- export type { ContractEvent, FilterArgType, FilterResultType, IContractManifest, IFilterMeta, ISsrElement, IWcsManifest, IWritableConfig, IWritableTagNames, WcsPathValue, WcsPaths, WcsStateApi, WcsThis };
1231
+ export { Ssr, TRUSTED_TYPES_POLICY_SLOT, VERSION, WCS_MANIFEST_VERSION, analyzeContract, bootstrapState, buildBindings, builtinFilterMeta, defineState, getBindingsReady, getConfig, getTrustedTypesPolicy, getWcsManifest, setTrustedTypesPolicy };
1232
+ export type { ContractEvent, FilterArgType, FilterResultType, IBindingErrorInfo, IContractManifest, IFilterMeta, ISsrElement, IWcsManifest, IWcsTrustedTypesPolicy, IWritableConfig, IWritableTagNames, WcsPathValue, WcsPaths, WcsStateApi, WcsThis };