@rohal12/spindle 0.64.26 → 0.64.27
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/pkg/format.js +1 -1
- package/dist/pkg/headless.js +283 -34
- package/package.json +1 -1
- package/src/components/macros/PassageDisplay.tsx +3 -3
- package/src/index.tsx +2 -2
- package/src/markup/render.tsx +3 -2
- package/src/markup/svg-names.ts +69 -0
- package/src/observe/observable-text.ts +61 -7
- package/src/observe/svg-instances.ts +210 -0
- package/src/saves/save-manager.ts +25 -9
- package/src/store.ts +16 -2
- package/src/story-init.ts +6 -1
- package/src/utils/element-visibility.ts +27 -10
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
isRendered,
|
|
7
7
|
} from '../utils/element-visibility';
|
|
8
8
|
import { getAction } from '../action-registry';
|
|
9
|
+
import { SvgInstances } from './svg-instances';
|
|
9
10
|
|
|
10
11
|
/** Elements that start and end a line. */
|
|
11
12
|
const BLOCKS = new Set([
|
|
@@ -102,13 +103,38 @@ export function observableText(
|
|
|
102
103
|
if (out !== '' && !out.endsWith('\n')) out += '\n';
|
|
103
104
|
};
|
|
104
105
|
|
|
105
|
-
|
|
106
|
+
// A use element has no DOM children for its rendered instance tree. Follow
|
|
107
|
+
// local references at that position, never unused definitions or external
|
|
108
|
+
// documents. Bound both depth and total work for cyclic or expanding graphs.
|
|
109
|
+
const references = new Set<Element>();
|
|
110
|
+
const instances = new SvgInstances();
|
|
111
|
+
let instanceNodes = 10_000;
|
|
112
|
+
const localTarget = (use: Element): Element | undefined => {
|
|
113
|
+
use = instances.source(use);
|
|
114
|
+
const href =
|
|
115
|
+
use.getAttribute('href') ??
|
|
116
|
+
use.getAttributeNS('http://www.w3.org/1999/xlink', 'href');
|
|
117
|
+
if (!href?.startsWith('#') || href.length === 1) return;
|
|
118
|
+
let id: string;
|
|
119
|
+
try {
|
|
120
|
+
id = decodeURIComponent(href.slice(1));
|
|
121
|
+
} catch {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const root = use.getRootNode() as Document | ShadowRoot | Element;
|
|
125
|
+
return 'getElementById' in root
|
|
126
|
+
? (root.getElementById(id) ?? undefined)
|
|
127
|
+
: [root, ...root.querySelectorAll('[id]')].find((node) => node.id === id);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const walk = (node: Node, inPre: boolean, instanceRoot?: Element) => {
|
|
131
|
+
if (instanceRoot && --instanceNodes < 0) return;
|
|
106
132
|
if (node.nodeType === 3) {
|
|
107
133
|
const parent =
|
|
108
134
|
(node as Text).assignedSlot ??
|
|
109
135
|
node.parentElement ??
|
|
110
136
|
(node.getRootNode() as ShadowRoot).host;
|
|
111
|
-
if (parent && !isRendered(parent, visibilityRoot)) return;
|
|
137
|
+
if (parent && !isRendered(parent, instanceRoot ?? visibilityRoot)) return;
|
|
112
138
|
const data = node.textContent ?? '';
|
|
113
139
|
if (inPre) out += data;
|
|
114
140
|
else {
|
|
@@ -121,20 +147,44 @@ export function observableText(
|
|
|
121
147
|
}
|
|
122
148
|
if (node.nodeType !== 1) return;
|
|
123
149
|
const child = node as Element;
|
|
124
|
-
if (
|
|
150
|
+
if (
|
|
151
|
+
isHidden(child, child === instanceRoot) ||
|
|
152
|
+
(skip && child !== el && child.matches(skip))
|
|
153
|
+
)
|
|
125
154
|
return;
|
|
155
|
+
if (
|
|
156
|
+
child.namespaceURI === 'http://www.w3.org/2000/svg' &&
|
|
157
|
+
child.localName === 'use'
|
|
158
|
+
) {
|
|
159
|
+
// Visibility is inherited by the instance and can be overridden inside
|
|
160
|
+
// it; unlike display:none, it does not hide the entire subtree.
|
|
161
|
+
if (!isRendered(child, instanceRoot ?? visibilityRoot, false)) return;
|
|
162
|
+
const target = localTarget(child);
|
|
163
|
+
if (
|
|
164
|
+
!target ||
|
|
165
|
+
target.namespaceURI !== child.namespaceURI ||
|
|
166
|
+
references.has(target) ||
|
|
167
|
+
references.size >= 32
|
|
168
|
+
)
|
|
169
|
+
return;
|
|
170
|
+
references.add(target);
|
|
171
|
+
const instance = instances.expand(child, target);
|
|
172
|
+
if (instance) walk(instance, inPre, instance);
|
|
173
|
+
references.delete(target);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
126
176
|
if (
|
|
127
177
|
child.matches('input, textarea, select') ||
|
|
128
178
|
(child.hasAttribute('data-action') &&
|
|
129
179
|
getAction(child.getAttribute('data-action')!)?.type === 'cycle')
|
|
130
180
|
) {
|
|
131
181
|
if (child.matches('input[type="hidden" i]')) return;
|
|
132
|
-
if (!isRendered(child, visibilityRoot)) return;
|
|
182
|
+
if (!isRendered(child, instanceRoot ?? visibilityRoot)) return;
|
|
133
183
|
out += controlMarker(child);
|
|
134
184
|
return;
|
|
135
185
|
}
|
|
136
186
|
if (child.tagName === 'BR') {
|
|
137
|
-
if (isRendered(child, visibilityRoot)) out += '\n';
|
|
187
|
+
if (isRendered(child, instanceRoot ?? visibilityRoot)) out += '\n';
|
|
138
188
|
return;
|
|
139
189
|
}
|
|
140
190
|
const block = BLOCKS.has(child.tagName);
|
|
@@ -146,13 +196,17 @@ export function observableText(
|
|
|
146
196
|
? rendered.filter((c) => c.nodeType === 1 && !isHidden(c as Element))
|
|
147
197
|
: rendered;
|
|
148
198
|
for (const c of children) {
|
|
149
|
-
walk(c, inPre || child.tagName === 'PRE');
|
|
199
|
+
walk(c, inPre || child.tagName === 'PRE', instanceRoot);
|
|
150
200
|
}
|
|
151
201
|
if (child.tagName === 'TD' || child.tagName === 'TH') out += '\t';
|
|
152
202
|
if (block) endLine();
|
|
153
203
|
};
|
|
154
204
|
|
|
155
|
-
|
|
205
|
+
try {
|
|
206
|
+
walk(el, false);
|
|
207
|
+
} finally {
|
|
208
|
+
instances.dispose();
|
|
209
|
+
}
|
|
156
210
|
return (
|
|
157
211
|
out
|
|
158
212
|
.split('\n')
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// Keep the isolated browsing context across observations. Repeatedly inserting
|
|
2
|
+
// and removing it would retrigger author MutationObservers that call observe().
|
|
3
|
+
const frames = new WeakMap<Document, HTMLIFrameElement>();
|
|
4
|
+
const detachedDocuments = new WeakMap<Document, Document>();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Resolve a use instance's styles in an isolated tree. Source computed styles
|
|
8
|
+
* inherit from the definition's ancestors, whereas SVG instances inherit from
|
|
9
|
+
* their use host and match selectors within their own shadow tree.
|
|
10
|
+
*/
|
|
11
|
+
export class SvgInstances {
|
|
12
|
+
private document?: Document;
|
|
13
|
+
private hosts: Element[] = [];
|
|
14
|
+
private sources = new WeakMap<Element, Element>();
|
|
15
|
+
private stylesheets = new WeakMap<Node, string>();
|
|
16
|
+
private remaining = 10_000;
|
|
17
|
+
|
|
18
|
+
source(element: Element): Element {
|
|
19
|
+
return this.sources.get(element) ?? element;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private stylesheet(use: Element): string {
|
|
23
|
+
const root = this.source(use).getRootNode() as Document | ShadowRoot;
|
|
24
|
+
const cached = this.stylesheets.get(root);
|
|
25
|
+
if (cached !== undefined) return cached;
|
|
26
|
+
const rules = (sheet: CSSStyleSheet): string => {
|
|
27
|
+
try {
|
|
28
|
+
return [...sheet.cssRules]
|
|
29
|
+
.map((rule) => {
|
|
30
|
+
if (rule.type === 3) {
|
|
31
|
+
const imported = rule as CSSImportRule;
|
|
32
|
+
if (!imported.styleSheet) return '';
|
|
33
|
+
const text = rules(imported.styleSheet);
|
|
34
|
+
let wrapped = imported.media.mediaText
|
|
35
|
+
? `@media ${imported.media.mediaText}{${text}}`
|
|
36
|
+
: text;
|
|
37
|
+
if (imported.supportsText) {
|
|
38
|
+
wrapped = `@supports (${imported.supportsText}){${wrapped}}`;
|
|
39
|
+
}
|
|
40
|
+
if (
|
|
41
|
+
imported.layerName !== null &&
|
|
42
|
+
imported.layerName !== undefined
|
|
43
|
+
) {
|
|
44
|
+
wrapped = `@layer ${imported.layerName}{${wrapped}}`;
|
|
45
|
+
}
|
|
46
|
+
return wrapped;
|
|
47
|
+
}
|
|
48
|
+
return rule.cssText;
|
|
49
|
+
})
|
|
50
|
+
.join('\n');
|
|
51
|
+
} catch {
|
|
52
|
+
// As elsewhere in headless observations, inaccessible external CSS
|
|
53
|
+
// cannot be inspected. Inline styles and presentation attributes work.
|
|
54
|
+
return '';
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const text = [
|
|
58
|
+
...(root.styleSheets ?? []),
|
|
59
|
+
...(root.adoptedStyleSheets ?? []),
|
|
60
|
+
]
|
|
61
|
+
.filter((sheet) => !sheet.disabled)
|
|
62
|
+
.map((sheet) => {
|
|
63
|
+
const text = rules(sheet);
|
|
64
|
+
return sheet.media.mediaText
|
|
65
|
+
? `@media ${sheet.media.mediaText}{${text}}`
|
|
66
|
+
: text;
|
|
67
|
+
})
|
|
68
|
+
.join('\n');
|
|
69
|
+
this.stylesheets.set(root, text);
|
|
70
|
+
return text;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
expand(use: Element, target: Element): Element | undefined {
|
|
74
|
+
if (this.remaining <= 0) return;
|
|
75
|
+
if (!this.document) {
|
|
76
|
+
const doc = use.ownerDocument;
|
|
77
|
+
const detached = doc.implementation.createHTMLDocument('');
|
|
78
|
+
const probe = detached.createElement('div');
|
|
79
|
+
probe.style.visibility = 'hidden';
|
|
80
|
+
detached.body.appendChild(probe);
|
|
81
|
+
// DOMs without layout can resolve styles in a detached document. Avoid
|
|
82
|
+
// creating a browsing context there (headless hosts disable iframes).
|
|
83
|
+
if (
|
|
84
|
+
(doc.defaultView ?? globalThis.window)?.getComputedStyle(probe)
|
|
85
|
+
.visibility === 'hidden'
|
|
86
|
+
) {
|
|
87
|
+
probe.remove();
|
|
88
|
+
let isolated = detachedDocuments.get(doc);
|
|
89
|
+
if (!isolated) {
|
|
90
|
+
// Headless DOM constructors provide an independent custom-element
|
|
91
|
+
// registry. createHTMLDocument alone still shares the author's one.
|
|
92
|
+
const view = doc.defaultView ?? globalThis.window;
|
|
93
|
+
isolated = detached;
|
|
94
|
+
try {
|
|
95
|
+
const context = Reflect.construct((view as typeof window).Window, [
|
|
96
|
+
{
|
|
97
|
+
settings: {
|
|
98
|
+
disableJavaScriptEvaluation: true,
|
|
99
|
+
disableCSSFileLoading: true,
|
|
100
|
+
disableIframePageLoading: true,
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
]) as Window;
|
|
104
|
+
isolated = context.document ?? detached;
|
|
105
|
+
} catch {
|
|
106
|
+
// Other DOMs (including jsdom) expose a nonconstructible Window;
|
|
107
|
+
// their inert documents already have an isolated element registry.
|
|
108
|
+
}
|
|
109
|
+
detachedDocuments.set(doc, isolated);
|
|
110
|
+
}
|
|
111
|
+
this.document = isolated;
|
|
112
|
+
} else {
|
|
113
|
+
let frame = frames.get(doc);
|
|
114
|
+
if (!frame?.isConnected) {
|
|
115
|
+
frame = doc.createElement('iframe');
|
|
116
|
+
// A separate registry avoids running author custom-element constructors;
|
|
117
|
+
// sandboxing and CSP also prevent cloned content from executing scripts
|
|
118
|
+
// or fetching resources. Nothing in the author's source tree is changed.
|
|
119
|
+
frame.setAttribute('sandbox', 'allow-same-origin');
|
|
120
|
+
frame.setAttribute('aria-hidden', 'true');
|
|
121
|
+
frame.tabIndex = -1;
|
|
122
|
+
frame.style.cssText =
|
|
123
|
+
'all:initial!important;display:block!important;position:fixed!important;' +
|
|
124
|
+
'left:-100000px!important;top:0!important;' +
|
|
125
|
+
'visibility:hidden!important;pointer-events:none!important;border:0!important;' +
|
|
126
|
+
`width:${doc.defaultView?.innerWidth ?? 1024}px!important;` +
|
|
127
|
+
`height:${doc.defaultView?.innerHeight ?? 768}px!important;`;
|
|
128
|
+
doc.documentElement.insertBefore(frame, doc.body);
|
|
129
|
+
frames.set(doc, frame);
|
|
130
|
+
const isolated = frame.contentDocument;
|
|
131
|
+
if (isolated) {
|
|
132
|
+
const meta = isolated.createElement('meta');
|
|
133
|
+
meta.httpEquiv = 'Content-Security-Policy';
|
|
134
|
+
meta.content = "default-src 'none'; style-src 'unsafe-inline'";
|
|
135
|
+
isolated.head.appendChild(meta);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
this.document = frame.contentDocument ?? undefined;
|
|
139
|
+
if (!this.document) return;
|
|
140
|
+
// Only write changed dimensions, so observing from a MutationObserver
|
|
141
|
+
// does not create another mutation on every call.
|
|
142
|
+
const width = `${doc.defaultView?.innerWidth ?? 1024}px`;
|
|
143
|
+
const height = `${doc.defaultView?.innerHeight ?? 768}px`;
|
|
144
|
+
if (frame.style.width !== width)
|
|
145
|
+
frame.style.setProperty('width', width, 'important');
|
|
146
|
+
if (frame.style.height !== height)
|
|
147
|
+
frame.style.setProperty('height', height, 'important');
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const doc = this.document;
|
|
151
|
+
const host = doc.createElement('div');
|
|
152
|
+
const computed = (
|
|
153
|
+
use.ownerDocument.defaultView ?? globalThis.window
|
|
154
|
+
)?.getComputedStyle(use);
|
|
155
|
+
// Custom properties can drive visibility/display in the referenced tree.
|
|
156
|
+
if (computed) {
|
|
157
|
+
host.style.visibility =
|
|
158
|
+
computed.visibility ||
|
|
159
|
+
(use as SVGElement).style.visibility ||
|
|
160
|
+
use.getAttribute('visibility') ||
|
|
161
|
+
'';
|
|
162
|
+
host.style.display =
|
|
163
|
+
computed.display || (use as SVGElement).style.display;
|
|
164
|
+
for (const name of Array.from(computed)) {
|
|
165
|
+
if (name.startsWith('--')) {
|
|
166
|
+
host.style.setProperty(name, computed.getPropertyValue(name));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
doc.body.appendChild(host);
|
|
171
|
+
this.hosts.push(host);
|
|
172
|
+
const shadow = host.attachShadow({ mode: 'open' });
|
|
173
|
+
const style = doc.createElement('style');
|
|
174
|
+
style.textContent = this.stylesheet(use);
|
|
175
|
+
const copy = (node: Node): Node | undefined => {
|
|
176
|
+
if (--this.remaining < 0) return;
|
|
177
|
+
let clone: Node;
|
|
178
|
+
if (node.nodeType === 1) {
|
|
179
|
+
const element = node as Element;
|
|
180
|
+
clone = doc.createElementNS(element.namespaceURI, element.localName);
|
|
181
|
+
for (const attribute of element.attributes) {
|
|
182
|
+
(clone as Element).setAttributeNS(
|
|
183
|
+
attribute.namespaceURI,
|
|
184
|
+
attribute.name,
|
|
185
|
+
attribute.value,
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
this.sources.set(clone as Element, this.source(node as Element));
|
|
189
|
+
} else if (node.nodeType === 3) {
|
|
190
|
+
clone = doc.createTextNode(node.textContent ?? '');
|
|
191
|
+
} else {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
for (const child of node.childNodes) {
|
|
195
|
+
if (this.remaining <= 0) break;
|
|
196
|
+
const copied = copy(child);
|
|
197
|
+
if (copied) clone.appendChild(copied);
|
|
198
|
+
}
|
|
199
|
+
return clone;
|
|
200
|
+
};
|
|
201
|
+
const clone = copy(target) as Element | undefined;
|
|
202
|
+
if (clone) shadow.appendChild(clone);
|
|
203
|
+
shadow.appendChild(style);
|
|
204
|
+
return clone;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
dispose(): void {
|
|
208
|
+
for (const host of this.hosts) host.remove();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -953,17 +953,33 @@ export function saveSession(
|
|
|
953
953
|
* warning, and the story starts fresh.
|
|
954
954
|
*/
|
|
955
955
|
export function loadSession(ifid: string): SavePayload | undefined {
|
|
956
|
+
return prepareSessionRestore(ifid)();
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
/** Capture storage before StoryInit can navigate and write a new session,
|
|
960
|
+
* but defer revival until its class registrations have run.
|
|
961
|
+
*/
|
|
962
|
+
export function prepareSessionRestore(
|
|
963
|
+
ifid: string,
|
|
964
|
+
): () => SavePayload | undefined {
|
|
965
|
+
let raw: string | null;
|
|
956
966
|
try {
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
return
|
|
960
|
-
} catch (err) {
|
|
961
|
-
if (err instanceof IncompatibleSaveError) {
|
|
962
|
-
console.warn(`spindle: ${err.message}; the session was not restored.`);
|
|
963
|
-
clearSession(ifid);
|
|
964
|
-
}
|
|
965
|
-
return undefined;
|
|
967
|
+
raw = sessionStorage.getItem(`${SESSION_KEY_PREFIX}${ifid}`);
|
|
968
|
+
} catch {
|
|
969
|
+
return () => undefined;
|
|
966
970
|
}
|
|
971
|
+
return () => {
|
|
972
|
+
try {
|
|
973
|
+
if (!raw) return undefined;
|
|
974
|
+
return decodePayload(JSON.parse(raw));
|
|
975
|
+
} catch (err) {
|
|
976
|
+
if (err instanceof IncompatibleSaveError) {
|
|
977
|
+
console.warn(`spindle: ${err.message}; the session was not restored.`);
|
|
978
|
+
clearSession(ifid);
|
|
979
|
+
}
|
|
980
|
+
return undefined;
|
|
981
|
+
}
|
|
982
|
+
};
|
|
967
983
|
}
|
|
968
984
|
|
|
969
985
|
export function clearSession(ifid: string): void {
|
package/src/store.ts
CHANGED
|
@@ -1197,7 +1197,7 @@ export interface StoryState {
|
|
|
1197
1197
|
getHistoryVariables: (index: number) => Record<string, unknown>;
|
|
1198
1198
|
setTransition: (config: TransitionConfig | null) => void;
|
|
1199
1199
|
setNextTransition: (config: TransitionConfig | null) => void;
|
|
1200
|
-
consumeNextTransition: () => TransitionConfig | null;
|
|
1200
|
+
consumeNextTransition: (navigationId?: number) => TransitionConfig | null;
|
|
1201
1201
|
deferRender: () => void;
|
|
1202
1202
|
clearDeferredRender: () => void;
|
|
1203
1203
|
}
|
|
@@ -1398,6 +1398,10 @@ function clearAndRestart(
|
|
|
1398
1398
|
return reported(cleared, message);
|
|
1399
1399
|
}
|
|
1400
1400
|
|
|
1401
|
+
// All mounted passage displays share the override consumed for a navigation.
|
|
1402
|
+
let consumedTransition:
|
|
1403
|
+
{ navigationId: number; config: TransitionConfig | null } | undefined;
|
|
1404
|
+
|
|
1401
1405
|
export const useStoryStore = create<StoryState>()(
|
|
1402
1406
|
storyStore((set, get) => ({
|
|
1403
1407
|
storyData: null,
|
|
@@ -1452,6 +1456,7 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1452
1456
|
variableDefaults: Record<string, unknown> = {},
|
|
1453
1457
|
transientDefaults: Record<string, unknown> = {},
|
|
1454
1458
|
) => {
|
|
1459
|
+
consumedTransition = undefined;
|
|
1455
1460
|
const startPassage = storyData.passagesById.get(storyData.startNode);
|
|
1456
1461
|
if (!startPassage) {
|
|
1457
1462
|
throw new Error(
|
|
@@ -2109,8 +2114,17 @@ export const useStoryStore = create<StoryState>()(
|
|
|
2109
2114
|
});
|
|
2110
2115
|
},
|
|
2111
2116
|
|
|
2112
|
-
consumeNextTransition: (): TransitionConfig | null => {
|
|
2117
|
+
consumeNextTransition: (navigationId): TransitionConfig | null => {
|
|
2118
|
+
if (
|
|
2119
|
+
navigationId !== undefined &&
|
|
2120
|
+
consumedTransition?.navigationId === navigationId
|
|
2121
|
+
) {
|
|
2122
|
+
return consumedTransition.config;
|
|
2123
|
+
}
|
|
2113
2124
|
const current = get().nextTransition;
|
|
2125
|
+
if (navigationId !== undefined) {
|
|
2126
|
+
consumedTransition = { navigationId, config: current };
|
|
2127
|
+
}
|
|
2114
2128
|
if (current !== null) {
|
|
2115
2129
|
set((state) => {
|
|
2116
2130
|
state.nextTransition = null;
|
package/src/story-init.ts
CHANGED
|
@@ -84,8 +84,13 @@ export function executeStoryInit() {
|
|
|
84
84
|
* handlers set synchronously belongs to the start moment, like StoryInit's
|
|
85
85
|
* changes; a restored session keeps the history it recorded.
|
|
86
86
|
*/
|
|
87
|
-
export function initializeStory(
|
|
87
|
+
export function initializeStory(
|
|
88
|
+
savedSession?: SavePayload | (() => SavePayload | undefined),
|
|
89
|
+
): void {
|
|
88
90
|
executeStoryInit();
|
|
91
|
+
// StoryInit can register the classes needed to revive saved variables.
|
|
92
|
+
let session =
|
|
93
|
+
typeof savedSession === 'function' ? savedSession() : savedSession;
|
|
89
94
|
|
|
90
95
|
// A session that refers to a passage the story no longer has (it was
|
|
91
96
|
// updated since) is dropped: the game starts from the beginning
|
|
@@ -47,19 +47,33 @@ const SVG_NOT_TEXT = new Set([
|
|
|
47
47
|
'marker',
|
|
48
48
|
]);
|
|
49
49
|
|
|
50
|
+
/** Detached/headless DOMs may have no computed value; CSSOM still resolves
|
|
51
|
+
* duplicate inline declarations and priorities correctly.
|
|
52
|
+
*/
|
|
53
|
+
function styleValue(el: Element, name: 'display' | 'visibility'): string {
|
|
54
|
+
return (
|
|
55
|
+
(el.ownerDocument.defaultView ?? globalThis.window)?.getComputedStyle?.(el)[
|
|
56
|
+
name
|
|
57
|
+
] ||
|
|
58
|
+
(el as HTMLElement).style?.[name] ||
|
|
59
|
+
''
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
50
63
|
/**
|
|
51
64
|
* Whether the element hides its whole subtree, including stylesheet rules.
|
|
52
65
|
* This needs computed styles but no layout, so it also works headlessly.
|
|
66
|
+
* A symbol referenced by SVG use is rendered only as an instance root.
|
|
53
67
|
*/
|
|
54
|
-
export function isHidden(el: Element): boolean {
|
|
68
|
+
export function isHidden(el: Element, svgInstanceRoot = false): boolean {
|
|
55
69
|
return (
|
|
56
70
|
el.hasAttribute('hidden') ||
|
|
57
71
|
el.getAttribute('aria-hidden') === 'true' ||
|
|
58
|
-
|
|
59
|
-
el.ownerDocument.defaultView?.getComputedStyle(el).display === 'none' ||
|
|
72
|
+
styleValue(el, 'display') === 'none' ||
|
|
60
73
|
NOT_TEXT.has(el.localName.toLowerCase()) ||
|
|
61
74
|
(el.namespaceURI === 'http://www.w3.org/2000/svg' &&
|
|
62
|
-
SVG_NOT_TEXT.has(el.localName.toLowerCase())
|
|
75
|
+
SVG_NOT_TEXT.has(el.localName.toLowerCase()) &&
|
|
76
|
+
!(svgInstanceRoot && el.localName.toLowerCase() === 'symbol')) ||
|
|
63
77
|
(el.parentElement?.tagName === 'DETAILS' &&
|
|
64
78
|
!el.parentElement.hasAttribute('open') &&
|
|
65
79
|
el !==
|
|
@@ -95,13 +109,16 @@ function inClosedDetails(el: Element, root?: Element): boolean {
|
|
|
95
109
|
* `<details>`. An optional root bounds ancestor checks, so observations can
|
|
96
110
|
* read StoryContext inside its deliberately hidden host.
|
|
97
111
|
*/
|
|
98
|
-
export function isRendered(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
112
|
+
export function isRendered(
|
|
113
|
+
el: Element,
|
|
114
|
+
root?: Element,
|
|
115
|
+
checkVisibility = true,
|
|
116
|
+
): boolean {
|
|
117
|
+
const visibility = styleValue(el, 'visibility');
|
|
118
|
+
if (checkVisibility && (visibility === 'hidden' || visibility === 'collapse'))
|
|
119
|
+
return false;
|
|
103
120
|
for (let e: Element | null = el; e; e = composedParent(e)) {
|
|
104
|
-
if (
|
|
121
|
+
if (styleValue(e, 'display') === 'none') return false;
|
|
105
122
|
if (e === root) break;
|
|
106
123
|
const parent = e.parentElement;
|
|
107
124
|
// Connected light DOM may have no place in its host's rendered tree.
|