@kanbun-skam/skam-web-component 0.2.0 → 0.3.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 +93 -34
- package/dist/auto.d.ts +1 -2
- package/dist/auto.js +11 -8
- package/dist/index.d.ts +69 -65
- package/dist/index.js +1 -10
- package/dist/skam-renderer-Bz03E4hy.js +652 -0
- package/package.json +5 -5
- package/dist/chunk-HPIH7MRS.js +0 -454
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
import { SKAM_XML_MEDIA_TYPE, extractXmlFromScriptElement, extractXmlFromScriptElement as extractXmlFromScriptElement$1, parse } from "@kanbun-skam/skam-xml-parser";
|
|
2
|
+
import { PROFILES, assertHideQuizOption, mount, resolveRenderProfile, validateRenderOptions } from "@kanbun-skam/skam-html-renderer";
|
|
3
|
+
//#region src/attribute-map.ts
|
|
4
|
+
/**
|
|
5
|
+
* HTML属性 → RenderOptions 変換ユーティリティ
|
|
6
|
+
*/
|
|
7
|
+
const VALID_WRITING_MODES = ["vertical", "horizontal"];
|
|
8
|
+
const VALID_COPYABLE_ELEMENTS = [
|
|
9
|
+
"ruby",
|
|
10
|
+
"okurigana",
|
|
11
|
+
"soegana",
|
|
12
|
+
"kaeriten",
|
|
13
|
+
"okototen"
|
|
14
|
+
];
|
|
15
|
+
function parseWritingMode(value) {
|
|
16
|
+
if (value != null && VALID_WRITING_MODES.includes(value)) return value;
|
|
17
|
+
return "vertical";
|
|
18
|
+
}
|
|
19
|
+
const MAX_ECHO = 64;
|
|
20
|
+
/**
|
|
21
|
+
* profile 属性を解決する。プリセット名か JSON を受け付け、不正なら TypeError を投げる。
|
|
22
|
+
* 属性が無い (null) ときは呼ばない。空文字・null・配列・数値は不正で、full には落とさない。
|
|
23
|
+
*/
|
|
24
|
+
function resolveProfileAttribute(value) {
|
|
25
|
+
if (Object.hasOwn(PROFILES, value)) return resolveRenderProfile({ base: value });
|
|
26
|
+
let parsed;
|
|
27
|
+
try {
|
|
28
|
+
parsed = JSON.parse(value);
|
|
29
|
+
} catch {
|
|
30
|
+
throw new TypeError(`profile must be a preset name or a JSON object (got ${JSON.stringify(value.slice(0, MAX_ECHO))})`);
|
|
31
|
+
}
|
|
32
|
+
return resolveRenderProfile(parsed);
|
|
33
|
+
}
|
|
34
|
+
function parseBooleanAttr(value) {
|
|
35
|
+
return value !== null;
|
|
36
|
+
}
|
|
37
|
+
function parseCopyable(value) {
|
|
38
|
+
if (value == null) return void 0;
|
|
39
|
+
if (value === "all") return "all";
|
|
40
|
+
const elements = value.split(/\s+/).filter((v) => VALID_COPYABLE_ELEMENTS.includes(v));
|
|
41
|
+
return elements.length > 0 ? elements : void 0;
|
|
42
|
+
}
|
|
43
|
+
function findUnknownCopyable(value) {
|
|
44
|
+
if (value == null || value === "all") return void 0;
|
|
45
|
+
return value.split(/\s+/).find((v) => v !== "" && !VALID_COPYABLE_ELEMENTS.includes(v));
|
|
46
|
+
}
|
|
47
|
+
/** html-renderer の検証規則をそのまま使い、1 つの属性だけを検査する */
|
|
48
|
+
function checkAttribute(attribute, probe) {
|
|
49
|
+
try {
|
|
50
|
+
validateRenderOptions(probe);
|
|
51
|
+
return;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
return {
|
|
54
|
+
attribute,
|
|
55
|
+
error: toTypeError(error)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function toTypeError(error) {
|
|
60
|
+
return error instanceof TypeError ? error : new TypeError(String(error));
|
|
61
|
+
}
|
|
62
|
+
/** 属性が真偽値の書き方か。"true" / "false" / 空文字 (属性の存在) だけを受け付ける */
|
|
63
|
+
function parseReadingLayerFlag(value) {
|
|
64
|
+
if (value === "" || value === "true") return true;
|
|
65
|
+
if (value === "false") return false;
|
|
66
|
+
throw new TypeError(`include-reading-layer must be "true", "false" or empty (got ${JSON.stringify(value.slice(0, MAX_ECHO))})`);
|
|
67
|
+
}
|
|
68
|
+
/** reading-layer-source 属性を解決する。属性が無い (null) ときは呼ばない。空文字は不正 */
|
|
69
|
+
function parseReadingLayerSource(value) {
|
|
70
|
+
if (value === "readings" || value === "display") return value;
|
|
71
|
+
throw new TypeError("reading-layer-source must be \"readings\" or \"display\"");
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* hide-quiz 属性を解決する。属性が無い (null) ときは呼ばない。
|
|
75
|
+
* 空の値は「すべて隠す」(true)、それ以外は JSON として読み、html-renderer の検査に通す。
|
|
76
|
+
* エラーのメッセージに属性の値は含めない
|
|
77
|
+
*/
|
|
78
|
+
function parseHideQuiz(value) {
|
|
79
|
+
if (value === "") return true;
|
|
80
|
+
let parsed;
|
|
81
|
+
try {
|
|
82
|
+
parsed = JSON.parse(value);
|
|
83
|
+
} catch {
|
|
84
|
+
throw new TypeError("hide-quiz must be empty or a JSON array of quiz names");
|
|
85
|
+
}
|
|
86
|
+
const checked = assertHideQuizOption(parsed);
|
|
87
|
+
if (checked === void 0) throw new TypeError("hide-quiz must be empty or a JSON array of quiz names");
|
|
88
|
+
return checked;
|
|
89
|
+
}
|
|
90
|
+
function applyHiddenAnnotationAttributes(attrs, options, blocked) {
|
|
91
|
+
if (attrs.profile !== null) try {
|
|
92
|
+
options.profile = resolveProfileAttribute(attrs.profile);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
blocked.push({
|
|
95
|
+
attribute: "profile",
|
|
96
|
+
error: toTypeError(error)
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
const readingLayer = attrs["include-reading-layer"];
|
|
100
|
+
if (readingLayer === null) options.includeReadingLayer = true;
|
|
101
|
+
else try {
|
|
102
|
+
options.includeReadingLayer = parseReadingLayerFlag(readingLayer);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
blocked.push({
|
|
105
|
+
attribute: "include-reading-layer",
|
|
106
|
+
error: toTypeError(error)
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
const readingLayerSource = attrs["reading-layer-source"] ?? null;
|
|
110
|
+
if (readingLayerSource !== null) try {
|
|
111
|
+
options.readingLayerSource = parseReadingLayerSource(readingLayerSource);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
blocked.push({
|
|
114
|
+
attribute: "reading-layer-source",
|
|
115
|
+
error: toTypeError(error)
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
const hideQuiz = attrs["hide-quiz"] ?? null;
|
|
119
|
+
if (hideQuiz !== null) try {
|
|
120
|
+
options.hideQuiz = parseHideQuiz(hideQuiz);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
blocked.push({
|
|
123
|
+
attribute: "hide-quiz",
|
|
124
|
+
error: toTypeError(error)
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function checkWritingModeAndCopyable(attrs) {
|
|
129
|
+
const invalid = [];
|
|
130
|
+
const writingMode = attrs["writing-mode"];
|
|
131
|
+
if (writingMode !== null && !VALID_WRITING_MODES.includes(writingMode)) invalid.push({
|
|
132
|
+
attribute: "writing-mode",
|
|
133
|
+
error: /* @__PURE__ */ new TypeError("writing-mode must be \"vertical\" or \"horizontal\"")
|
|
134
|
+
});
|
|
135
|
+
if (findUnknownCopyable(attrs.copyable) !== void 0) invalid.push({
|
|
136
|
+
attribute: "copyable",
|
|
137
|
+
error: /* @__PURE__ */ new TypeError("copyable must be \"all\" or a space-separated list of known element names")
|
|
138
|
+
});
|
|
139
|
+
return invalid;
|
|
140
|
+
}
|
|
141
|
+
function buildRenderOptions(attrs) {
|
|
142
|
+
const options = {
|
|
143
|
+
writingMode: parseWritingMode(attrs["writing-mode"]),
|
|
144
|
+
useLayer: false
|
|
145
|
+
};
|
|
146
|
+
if (parseBooleanAttr(attrs.inline)) options.inline = true;
|
|
147
|
+
const blocked = [];
|
|
148
|
+
applyHiddenAnnotationAttributes(attrs, options, blocked);
|
|
149
|
+
const copyable = parseCopyable(attrs.copyable);
|
|
150
|
+
if (copyable) options.copyable = copyable;
|
|
151
|
+
const invalid = checkWritingModeAndCopyable(attrs);
|
|
152
|
+
const classPrefix = attrs["class-prefix"];
|
|
153
|
+
if (classPrefix != null) {
|
|
154
|
+
const problem = checkAttribute("class-prefix", { classPrefix });
|
|
155
|
+
if (problem) invalid.push(problem);
|
|
156
|
+
else {
|
|
157
|
+
options.classPrefix = classPrefix;
|
|
158
|
+
options.variablePrefix = classPrefix;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const displayLang = attrs["display-lang"];
|
|
162
|
+
if (displayLang != null) {
|
|
163
|
+
const problem = checkAttribute("display-lang", { displayLang });
|
|
164
|
+
if (problem) invalid.push(problem);
|
|
165
|
+
else options.displayLang = displayLang;
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
options,
|
|
169
|
+
invalid,
|
|
170
|
+
blocked
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region src/xml-extraction.ts
|
|
175
|
+
/**
|
|
176
|
+
* XML テキスト取得ロジック
|
|
177
|
+
*
|
|
178
|
+
* 優先順位:
|
|
179
|
+
* 1. xmlContent プロパティ(プログラマティック設定値)
|
|
180
|
+
* 2. <script type="application/vnd.berlysia.skam+xml"> 子要素の textContent
|
|
181
|
+
* 3. Light DOM の textContent(フォールバック)
|
|
182
|
+
*/
|
|
183
|
+
/**
|
|
184
|
+
* Light DOM の textContent からXMLを取得する(フォールバック)
|
|
185
|
+
*/
|
|
186
|
+
function extractXmlFromLightDOM(host) {
|
|
187
|
+
const text = host.textContent;
|
|
188
|
+
if (text && text.trim()) return text;
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* XML テキストを優先順位に従って取得する
|
|
193
|
+
*
|
|
194
|
+
* @param xmlContent - プログラマティックに設定された XML (undefined = 未設定)
|
|
195
|
+
* @param host - カスタムエレメントのホスト要素
|
|
196
|
+
* @returns XML テキストまたは null
|
|
197
|
+
*/
|
|
198
|
+
function extractXml(xmlContent, host) {
|
|
199
|
+
if (xmlContent !== void 0) return xmlContent || null;
|
|
200
|
+
const fromScript = extractXmlFromScriptElement(host);
|
|
201
|
+
if (fromScript) return fromScript;
|
|
202
|
+
return extractXmlFromLightDOM(host);
|
|
203
|
+
}
|
|
204
|
+
//#endregion
|
|
205
|
+
//#region src/font-loader.ts
|
|
206
|
+
/**
|
|
207
|
+
* Google Fonts からの Noto Serif JP 自動ロード
|
|
208
|
+
*
|
|
209
|
+
* document.head に <link> を注入する冪等ユーティリティ。
|
|
210
|
+
* Shadow DOM 内のフォント CSS Variable 設定も提供する。
|
|
211
|
+
*/
|
|
212
|
+
const DEFAULT_FONT_FAMILY = "Noto Serif JP";
|
|
213
|
+
const GOOGLE_FONTS_URL = "https://fonts.googleapis.com/css2?family=Noto+Serif+JP:wght@400;700&display=swap";
|
|
214
|
+
const LINK_SELECTOR = "link[href*=\"fonts.googleapis.com\"][href*=\"Noto+Serif+JP\"]";
|
|
215
|
+
/**
|
|
216
|
+
* Google Fonts CSS <link> を document.head に注入する(冪等)。
|
|
217
|
+
* 既に同等の <link> が存在する場合はスキップ (既存の <link> の nonce は変えない)。
|
|
218
|
+
* `nonce` を渡すと、新しく作る <link> にだけ setAttribute で設定する。
|
|
219
|
+
* 内容属性として設定すると、ブラウザは CSP が見る nonce の値も更新する。
|
|
220
|
+
*/
|
|
221
|
+
function injectGoogleFontsLink(nonce) {
|
|
222
|
+
if (typeof document === "undefined") return;
|
|
223
|
+
if (!document.querySelector(LINK_SELECTOR)) {
|
|
224
|
+
const link = document.createElement("link");
|
|
225
|
+
link.rel = "stylesheet";
|
|
226
|
+
link.href = GOOGLE_FONTS_URL;
|
|
227
|
+
if (nonce) link.setAttribute("nonce", nonce);
|
|
228
|
+
document.head.appendChild(link);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Shadow DOM に注入するフォント CSS Variable のスタイル文字列を生成する。
|
|
233
|
+
* :host に設定することで、外部 CSS からのオーバーライドが可能。
|
|
234
|
+
*/
|
|
235
|
+
function buildFontStyle(variablePrefix) {
|
|
236
|
+
const family = `"${DEFAULT_FONT_FAMILY}", serif`;
|
|
237
|
+
return `:host {
|
|
238
|
+
--${variablePrefix}-font-family: ${family};
|
|
239
|
+
--${variablePrefix}-font-family-ruby: ${family};
|
|
240
|
+
}`;
|
|
241
|
+
}
|
|
242
|
+
//#endregion
|
|
243
|
+
//#region src/skam-renderer.ts
|
|
244
|
+
/**
|
|
245
|
+
* <skam-renderer> カスタムエレメント
|
|
246
|
+
*
|
|
247
|
+
* SKAM の HTML レンダリングパイプラインを Web Component として提供する。
|
|
248
|
+
* Shadow DOM でスタイルをカプセル化し、CSS Variables による外部カスタマイズを可能にする。
|
|
249
|
+
*/
|
|
250
|
+
/** mount が shadow root に入れる描画 CSS の <style> の印。mount (ADR-034 KD4) と同じ文字列 */
|
|
251
|
+
const STYLE_MARKER_SELECTOR = "style[data-skam-css]";
|
|
252
|
+
const ERROR_CSS = `
|
|
253
|
+
[role="alert"] {
|
|
254
|
+
color: #c00;
|
|
255
|
+
padding: 0.5em;
|
|
256
|
+
font-family: monospace;
|
|
257
|
+
white-space: pre-wrap;
|
|
258
|
+
}
|
|
259
|
+
`;
|
|
260
|
+
/**
|
|
261
|
+
* Shadow DOM 内の CSS 変数をホスト要素から継承可能にするオーバーライド CSS を生成する。
|
|
262
|
+
*
|
|
263
|
+
* 問題: HTML レンダラーが生成する CSS は `:where(.skam-document)` に変数デフォルト値を
|
|
264
|
+
* 直接宣言する。Shadow DOM ではホスト要素に設定した CSS 変数は継承で子に伝わるが、
|
|
265
|
+
* 子要素に直接宣言があると継承値より優先される。
|
|
266
|
+
*
|
|
267
|
+
* 解決: `:host` にデフォルト値を設定し、`.skam-document` で `inherit` に上書きする。
|
|
268
|
+
* `.skam-document` (specificity 0,1,0) は `:where(.skam-document)` (0,0,0) に勝つ。
|
|
269
|
+
* ユーザーのインラインスタイルは `:host` ルールより優先される。
|
|
270
|
+
*
|
|
271
|
+
* 注意: --font-family / --font-family-ruby は font-loader が :host で設定するため、
|
|
272
|
+
* ここでは :host のデフォルトを宣言しない(cascade 順序で上書きしてしまう)。
|
|
273
|
+
* .document 側は inherit のままで問題ない(生成 CSS も元から inherit)。
|
|
274
|
+
*/
|
|
275
|
+
function generateVarOverrideCss(classPrefix, variablePrefix) {
|
|
276
|
+
const vp = variablePrefix;
|
|
277
|
+
return `
|
|
278
|
+
:host {
|
|
279
|
+
--${vp}-color-fg: currentColor;
|
|
280
|
+
--${vp}-color-kaeriten: currentColor;
|
|
281
|
+
--${vp}-color-ruby: currentColor;
|
|
282
|
+
--${vp}-color-emphasis: currentColor;
|
|
283
|
+
--${vp}-glyph-size: 1em;
|
|
284
|
+
--${vp}-ruby-ratio: 0.5;
|
|
285
|
+
--${vp}-line-height: 2;
|
|
286
|
+
--${vp}-letter-spacing: 0;
|
|
287
|
+
}
|
|
288
|
+
.${classPrefix}-document {
|
|
289
|
+
--${vp}-color-fg: inherit;
|
|
290
|
+
--${vp}-color-kaeriten: inherit;
|
|
291
|
+
--${vp}-color-ruby: inherit;
|
|
292
|
+
--${vp}-color-emphasis: inherit;
|
|
293
|
+
--${vp}-font-family: inherit;
|
|
294
|
+
--${vp}-font-family-ruby: inherit;
|
|
295
|
+
--${vp}-glyph-size: inherit;
|
|
296
|
+
--${vp}-ruby-ratio: inherit;
|
|
297
|
+
--${vp}-line-height: inherit;
|
|
298
|
+
--${vp}-letter-spacing: inherit;
|
|
299
|
+
}`;
|
|
300
|
+
}
|
|
301
|
+
const BaseElement = typeof HTMLElement === "undefined" ? class {} : HTMLElement;
|
|
302
|
+
var SkamRendererElement = class extends BaseElement {
|
|
303
|
+
static observedAttributes = [
|
|
304
|
+
"writing-mode",
|
|
305
|
+
"profile",
|
|
306
|
+
"inline",
|
|
307
|
+
"include-reading-layer",
|
|
308
|
+
"reading-layer-source",
|
|
309
|
+
"hide-quiz",
|
|
310
|
+
"copyable",
|
|
311
|
+
"class-prefix",
|
|
312
|
+
"display-lang",
|
|
313
|
+
"auto-font"
|
|
314
|
+
];
|
|
315
|
+
#shadow;
|
|
316
|
+
#mainStyle;
|
|
317
|
+
#errorStyle;
|
|
318
|
+
#fontStyle;
|
|
319
|
+
#contentDiv;
|
|
320
|
+
/** shadow の <style> に設定済みの nonce。#error-css の中身を入れ直すかの判断に使う */
|
|
321
|
+
#appliedNonce;
|
|
322
|
+
#xmlContent;
|
|
323
|
+
#accessibleLabels;
|
|
324
|
+
#trustedTypesPolicy;
|
|
325
|
+
/** skam-error で通知した不正な accessibleLabels の参照。同じ参照では再通知しない */
|
|
326
|
+
#reportedLabels = null;
|
|
327
|
+
#observer = null;
|
|
328
|
+
#rafId = null;
|
|
329
|
+
/** 最後に skam-error で通知した不正値 (属性名 → 値)。同じ値では再通知しない */
|
|
330
|
+
#reportedInvalid = /* @__PURE__ */ new Map();
|
|
331
|
+
/** fail closed で通知済みの属性 → 値。同じ値では再通知しない */
|
|
332
|
+
#reportedBlocked = /* @__PURE__ */ new Map();
|
|
333
|
+
/**
|
|
334
|
+
* 直前に通知した致命的エラー (抽出した XML・name・message)。
|
|
335
|
+
* 同じ内容で同じエラーが続く間は、再描画のたびに通知しない。ハンドラが内容を書き換えたときの
|
|
336
|
+
* エラー → 再描画 → エラーの連鎖と、MutationObserver による重複通知を避けるため。
|
|
337
|
+
*/
|
|
338
|
+
#reportedFatal = null;
|
|
339
|
+
/** #reportedFatal と対になる alert の要素。同じエラーの間は使い回して DOM に触らない */
|
|
340
|
+
#fatalAlert = null;
|
|
341
|
+
constructor() {
|
|
342
|
+
super();
|
|
343
|
+
this.#shadow = this.attachShadow({ mode: "open" });
|
|
344
|
+
this.#mainStyle = document.createElement("style");
|
|
345
|
+
this.#mainStyle.id = "main-css";
|
|
346
|
+
this.#errorStyle = document.createElement("style");
|
|
347
|
+
this.#errorStyle.id = "error-css";
|
|
348
|
+
this.#errorStyle.textContent = ERROR_CSS;
|
|
349
|
+
this.#fontStyle = document.createElement("style");
|
|
350
|
+
this.#fontStyle.id = "font-css";
|
|
351
|
+
this.#contentDiv = document.createElement("div");
|
|
352
|
+
this.#contentDiv.id = "content";
|
|
353
|
+
this.#shadow.appendChild(this.#fontStyle);
|
|
354
|
+
this.#shadow.appendChild(this.#mainStyle);
|
|
355
|
+
this.#shadow.appendChild(this.#errorStyle);
|
|
356
|
+
this.#shadow.appendChild(this.#contentDiv);
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* XML コンテンツをプログラマティックに設定する。
|
|
360
|
+
* undefined の場合は <script type="application/vnd.berlysia.skam+xml"> や Light DOM にフォールバック。
|
|
361
|
+
* 空文字列 "" も「設定済み」とみなし、フォールバックしない。
|
|
362
|
+
*/
|
|
363
|
+
get xmlContent() {
|
|
364
|
+
return this.#xmlContent;
|
|
365
|
+
}
|
|
366
|
+
set xmlContent(value) {
|
|
367
|
+
this.#xmlContent = value;
|
|
368
|
+
this.#scheduleRender();
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* 読み上げ層の言葉を上書きするラベル。関数を含むので属性では渡せない。
|
|
372
|
+
* `reading-layer-source="display"` のときだけ描画に効く。不正な値はラベルなしで描画し、
|
|
373
|
+
* `recoverable: true` の skam-error で通知する。変えるときは新しいオブジェクトを代入する
|
|
374
|
+
* (同じ参照の再代入は再描画しない)。null は指定なしとして扱う。
|
|
375
|
+
*
|
|
376
|
+
* @experimental
|
|
377
|
+
*/
|
|
378
|
+
get accessibleLabels() {
|
|
379
|
+
return this.#accessibleLabels;
|
|
380
|
+
}
|
|
381
|
+
set accessibleLabels(value) {
|
|
382
|
+
const next = value ?? void 0;
|
|
383
|
+
if (Object.is(next, this.#accessibleLabels)) return;
|
|
384
|
+
this.#accessibleLabels = next;
|
|
385
|
+
this.#scheduleRender();
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Trusted Types が必須のページで、文字列を DOM の sink に渡すための policy。JS のプロパティだけで、
|
|
389
|
+
* 属性は無い。値は検証せず、parse (ブラウザ版) と mount の両方に渡す。`createHTML` は元の XML と
|
|
390
|
+
* 描画の HTML の両方で呼ばれ、XML はそのまま返すこと。何を検査するかは利用者の責任で、
|
|
391
|
+
* 描画の HTML の安全性は render のエスケープに依る。変えるときは新しいオブジェクトを代入する
|
|
392
|
+
* (同じ参照の再代入は再描画しない)。null は指定なしとして扱う。
|
|
393
|
+
* customElements.define の前にインスタンスへ代入した値は、このアクセサを通らない。
|
|
394
|
+
*
|
|
395
|
+
* @experimental
|
|
396
|
+
*/
|
|
397
|
+
get trustedTypesPolicy() {
|
|
398
|
+
return this.#trustedTypesPolicy;
|
|
399
|
+
}
|
|
400
|
+
set trustedTypesPolicy(value) {
|
|
401
|
+
const next = value ?? void 0;
|
|
402
|
+
if (Object.is(next, this.#trustedTypesPolicy)) return;
|
|
403
|
+
this.#trustedTypesPolicy = next;
|
|
404
|
+
this.#scheduleRender();
|
|
405
|
+
}
|
|
406
|
+
connectedCallback() {
|
|
407
|
+
this.#ensureFont(this.#readNonce());
|
|
408
|
+
this.#observer = new MutationObserver(() => {
|
|
409
|
+
this.#scheduleRender();
|
|
410
|
+
});
|
|
411
|
+
this.#observer.observe(this, {
|
|
412
|
+
childList: true,
|
|
413
|
+
subtree: true,
|
|
414
|
+
characterData: true
|
|
415
|
+
});
|
|
416
|
+
this.#scheduleRender();
|
|
417
|
+
}
|
|
418
|
+
disconnectedCallback() {
|
|
419
|
+
this.#observer?.disconnect();
|
|
420
|
+
this.#observer = null;
|
|
421
|
+
if (this.#rafId !== null) {
|
|
422
|
+
cancelAnimationFrame(this.#rafId);
|
|
423
|
+
this.#rafId = null;
|
|
424
|
+
}
|
|
425
|
+
this.#reportedInvalid.clear();
|
|
426
|
+
this.#reportedBlocked.clear();
|
|
427
|
+
this.#reportedLabels = null;
|
|
428
|
+
this.#clearFatal();
|
|
429
|
+
}
|
|
430
|
+
attributeChangedCallback(_name, _oldValue, _newValue) {
|
|
431
|
+
this.#scheduleRender();
|
|
432
|
+
}
|
|
433
|
+
#scheduleRender() {
|
|
434
|
+
if (this.#rafId !== null) return;
|
|
435
|
+
this.#rafId = requestAnimationFrame(() => {
|
|
436
|
+
this.#rafId = null;
|
|
437
|
+
this.#performRender();
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
#performRender() {
|
|
441
|
+
if (!this.isConnected) return;
|
|
442
|
+
const nonce = this.#readNonce();
|
|
443
|
+
this.#applyNonce(nonce);
|
|
444
|
+
for (const style of Array.from(this.#shadow.querySelectorAll(STYLE_MARKER_SELECTOR))) style.remove();
|
|
445
|
+
const attrs = this.#getAttributeValues();
|
|
446
|
+
const { options, invalid, blocked } = buildRenderOptions(attrs);
|
|
447
|
+
if (blocked.length > 0) {
|
|
448
|
+
this.#clearFatal();
|
|
449
|
+
this.#renderBlocked(blocked, attrs);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
this.#reportedBlocked.clear();
|
|
453
|
+
const xml = extractXml(this.#xmlContent, this);
|
|
454
|
+
if (!xml) {
|
|
455
|
+
this.#clearFatal();
|
|
456
|
+
this.#contentDiv.replaceChildren();
|
|
457
|
+
this.#mainStyle.textContent = "";
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
try {
|
|
461
|
+
const labels = this.#accessibleLabels;
|
|
462
|
+
let labelsError = null;
|
|
463
|
+
if (labels !== void 0) try {
|
|
464
|
+
validateRenderOptions({ accessibleLabels: labels });
|
|
465
|
+
options.accessibleLabels = labels;
|
|
466
|
+
} catch (error) {
|
|
467
|
+
labelsError = error instanceof TypeError ? error : new TypeError(String(error));
|
|
468
|
+
}
|
|
469
|
+
const policy = this.#trustedTypesPolicy;
|
|
470
|
+
const doc = policy === void 0 ? parse(xml) : parse(xml, { trustedTypesPolicy: policy });
|
|
471
|
+
const classPrefix = options.classPrefix ?? "skam";
|
|
472
|
+
const variablePrefix = options.variablePrefix ?? "skam";
|
|
473
|
+
this.#mainStyle.textContent = generateVarOverrideCss(classPrefix, variablePrefix);
|
|
474
|
+
this.#ensureFont(nonce);
|
|
475
|
+
this.#updateFontStyle(variablePrefix);
|
|
476
|
+
const mountOptions = { ...options };
|
|
477
|
+
if (nonce !== void 0) mountOptions.nonce = nonce;
|
|
478
|
+
if (policy !== void 0) mountOptions.trustedTypesPolicy = policy;
|
|
479
|
+
mount(this.#contentDiv, doc, mountOptions);
|
|
480
|
+
this.#reportInvalid(invalid, attrs);
|
|
481
|
+
this.#reportInvalidLabels(labels, labelsError);
|
|
482
|
+
this.dispatchEvent(new CustomEvent("skam-render", {
|
|
483
|
+
detail: { document: doc },
|
|
484
|
+
bubbles: true,
|
|
485
|
+
composed: true
|
|
486
|
+
}));
|
|
487
|
+
this.#clearFatal();
|
|
488
|
+
} catch (error) {
|
|
489
|
+
this.#handleError(error, xml);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* 既定値に戻した属性を回復可能なエラーとして通知する (内容は消さない)。
|
|
494
|
+
* 同じ不正値が残っている間は再描画しても再通知しない。ハンドラが属性や内容を
|
|
495
|
+
* 書き換えたときに、エラー → 再描画 → エラーの連鎖が起きるのを避けるため。
|
|
496
|
+
*/
|
|
497
|
+
#reportInvalid(invalid, attrs) {
|
|
498
|
+
const invalidNames = new Set(invalid.map((i) => i.attribute));
|
|
499
|
+
for (const name of this.#reportedInvalid.keys()) if (!invalidNames.has(name)) this.#reportedInvalid.delete(name);
|
|
500
|
+
for (const { attribute, error } of invalid) {
|
|
501
|
+
const value = attrs[attribute] ?? "";
|
|
502
|
+
if (this.#reportedInvalid.get(attribute) === value) continue;
|
|
503
|
+
this.#reportedInvalid.set(attribute, value);
|
|
504
|
+
this.dispatchEvent(new CustomEvent("skam-error", {
|
|
505
|
+
detail: {
|
|
506
|
+
error,
|
|
507
|
+
recoverable: true,
|
|
508
|
+
attribute
|
|
509
|
+
},
|
|
510
|
+
bubbles: true,
|
|
511
|
+
composed: true
|
|
512
|
+
}));
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* 不正な accessibleLabels を回復可能なエラーとして通知する (描画は続けている)。
|
|
517
|
+
* 同じ参照では再通知しない。正しい値か指定なしになったら記憶を消す。
|
|
518
|
+
*/
|
|
519
|
+
#reportInvalidLabels(labels, error) {
|
|
520
|
+
if (error === null) {
|
|
521
|
+
this.#reportedLabels = null;
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (this.#reportedLabels !== null && Object.is(this.#reportedLabels.ref, labels)) return;
|
|
525
|
+
this.#reportedLabels = { ref: labels };
|
|
526
|
+
this.dispatchEvent(new CustomEvent("skam-error", {
|
|
527
|
+
detail: {
|
|
528
|
+
error,
|
|
529
|
+
recoverable: true,
|
|
530
|
+
attribute: "accessibleLabels"
|
|
531
|
+
},
|
|
532
|
+
bubbles: true,
|
|
533
|
+
composed: true
|
|
534
|
+
}));
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* 隠す属性が不正なとき、描いた内容を消してメッセージだけを出す。
|
|
538
|
+
* 同じ属性の同じ値では再通知しない (#reportInvalid と同じ理由)。
|
|
539
|
+
* detail には属性の生の値を入れない。値は 64 文字に切り詰めてメッセージに含まれる。
|
|
540
|
+
*/
|
|
541
|
+
#renderBlocked(blocked, attrs) {
|
|
542
|
+
const names = new Set(blocked.map((b) => b.attribute));
|
|
543
|
+
for (const name of this.#reportedBlocked.keys()) if (!names.has(name)) this.#reportedBlocked.delete(name);
|
|
544
|
+
this.#contentDiv.replaceChildren();
|
|
545
|
+
const alert = document.createElement("div");
|
|
546
|
+
alert.setAttribute("role", "alert");
|
|
547
|
+
alert.textContent = blocked.map((b) => b.error.message).join("\n");
|
|
548
|
+
this.#contentDiv.appendChild(alert);
|
|
549
|
+
for (const { attribute, error } of blocked) {
|
|
550
|
+
const value = attrs[attribute] ?? "";
|
|
551
|
+
if (this.#reportedBlocked.get(attribute) === value) continue;
|
|
552
|
+
this.#reportedBlocked.set(attribute, value);
|
|
553
|
+
this.dispatchEvent(new CustomEvent("skam-error", {
|
|
554
|
+
detail: {
|
|
555
|
+
error,
|
|
556
|
+
recoverable: false,
|
|
557
|
+
attribute
|
|
558
|
+
},
|
|
559
|
+
bubbles: true,
|
|
560
|
+
composed: true
|
|
561
|
+
}));
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* 致命的エラーを表示して通知する。XML・name・message がすべて直前の通知と同じなら通知しない。
|
|
566
|
+
* Error でない値は name を空文字、message を String(値) として比べる。
|
|
567
|
+
*/
|
|
568
|
+
#handleError(error, xml) {
|
|
569
|
+
const name = error instanceof Error ? error.name : "";
|
|
570
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
571
|
+
const last = this.#reportedFatal;
|
|
572
|
+
const alert = this.#fatalAlert;
|
|
573
|
+
if (last !== null && alert !== null && last.xml === xml && last.name === name && last.message === message) {
|
|
574
|
+
if (this.#contentDiv.childNodes.length !== 1 || this.#contentDiv.firstChild !== alert) this.#contentDiv.replaceChildren(alert);
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
const next = alert ?? document.createElement("div");
|
|
578
|
+
next.setAttribute("role", "alert");
|
|
579
|
+
next.textContent = message;
|
|
580
|
+
this.#fatalAlert = next;
|
|
581
|
+
this.#contentDiv.replaceChildren(next);
|
|
582
|
+
this.#reportedFatal = {
|
|
583
|
+
xml,
|
|
584
|
+
name,
|
|
585
|
+
message
|
|
586
|
+
};
|
|
587
|
+
this.dispatchEvent(new CustomEvent("skam-error", {
|
|
588
|
+
detail: {
|
|
589
|
+
error,
|
|
590
|
+
recoverable: false
|
|
591
|
+
},
|
|
592
|
+
bubbles: true,
|
|
593
|
+
composed: true
|
|
594
|
+
}));
|
|
595
|
+
}
|
|
596
|
+
#clearFatal() {
|
|
597
|
+
this.#reportedFatal = null;
|
|
598
|
+
this.#fatalAlert = null;
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* CSP の nonce。描画のたびに読む (observedAttributes には入れない: CSP がヘッダで与えられると
|
|
602
|
+
* ブラウザが内容属性を空にし、そのたびに再描画が走るため)。IDL 属性を優先し、無い環境
|
|
603
|
+
* (happy-dom など) では内容属性を読む。空なら undefined。
|
|
604
|
+
*/
|
|
605
|
+
#readNonce() {
|
|
606
|
+
const value = typeof this.nonce === "string" ? this.nonce : this.getAttribute("nonce");
|
|
607
|
+
return value ? value : void 0;
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* shadow の 3 つの <style> に nonce を設定する。内容属性として設定すると、ブラウザは CSP が見る
|
|
611
|
+
* nonce の値も更新する。CSP は中身が入るときに nonce を見るので、#error-css (constructor で
|
|
612
|
+
* 中身を入れている) は nonce が変わったときに空にしてから入れ直す。#main-css と #font-css は
|
|
613
|
+
* この後の描画で中身を入れ直す。
|
|
614
|
+
*/
|
|
615
|
+
#applyNonce(nonce) {
|
|
616
|
+
if (nonce === void 0) return;
|
|
617
|
+
for (const style of [
|
|
618
|
+
this.#fontStyle,
|
|
619
|
+
this.#mainStyle,
|
|
620
|
+
this.#errorStyle
|
|
621
|
+
]) style.setAttribute("nonce", nonce);
|
|
622
|
+
if (this.#appliedNonce !== nonce) {
|
|
623
|
+
this.#errorStyle.textContent = "";
|
|
624
|
+
this.#errorStyle.textContent = ERROR_CSS;
|
|
625
|
+
this.#appliedNonce = nonce;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
/** auto-font 属性があれば Google Fonts <link> を document.head に注入 */
|
|
629
|
+
#ensureFont(nonce) {
|
|
630
|
+
if (this.hasAttribute("auto-font")) injectGoogleFontsLink(nonce);
|
|
631
|
+
}
|
|
632
|
+
/** Shadow DOM 内のフォント CSS Variable を更新(auto-font 時のみ設定) */
|
|
633
|
+
#updateFontStyle(variablePrefix) {
|
|
634
|
+
if (this.hasAttribute("auto-font")) this.#fontStyle.textContent = buildFontStyle(variablePrefix);
|
|
635
|
+
else this.#fontStyle.textContent = "";
|
|
636
|
+
}
|
|
637
|
+
#getAttributeValues() {
|
|
638
|
+
return {
|
|
639
|
+
"writing-mode": this.getAttribute("writing-mode"),
|
|
640
|
+
profile: this.getAttribute("profile"),
|
|
641
|
+
inline: this.getAttribute("inline"),
|
|
642
|
+
"include-reading-layer": this.getAttribute("include-reading-layer"),
|
|
643
|
+
copyable: this.getAttribute("copyable"),
|
|
644
|
+
"class-prefix": this.getAttribute("class-prefix"),
|
|
645
|
+
"display-lang": this.getAttribute("display-lang"),
|
|
646
|
+
"reading-layer-source": this.getAttribute("reading-layer-source"),
|
|
647
|
+
"hide-quiz": this.getAttribute("hide-quiz")
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
};
|
|
651
|
+
//#endregion
|
|
652
|
+
export { SKAM_XML_MEDIA_TYPE as n, extractXmlFromScriptElement$1 as r, SkamRendererElement as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kanbun-skam/skam-web-component",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "SKAM Web Component - <skam-renderer> カスタムエレメント",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": [
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
"node": ">=18.0.0"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@kanbun-skam/skam-xml-parser": "0.
|
|
50
|
-
"@kanbun-skam/skam-html-renderer": "0.
|
|
49
|
+
"@kanbun-skam/skam-xml-parser": "0.3.0",
|
|
50
|
+
"@kanbun-skam/skam-html-renderer": "0.3.0"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
|
-
"build": "
|
|
54
|
-
"typecheck": "
|
|
53
|
+
"build": "tsdown",
|
|
54
|
+
"typecheck": "tsc --noEmit",
|
|
55
55
|
"lint": "oxlint src",
|
|
56
56
|
"test": "vitest",
|
|
57
57
|
"test:run": "vitest run"
|