@rohal12/spindle 0.64.9 → 0.64.10
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 +243 -157
- package/dist/pkg/story-variables.js +233 -143
- package/package.json +1 -1
- package/src/markup/grammar-shared.ts +2 -1
- package/src/markup/html-elements.ts +16 -0
- package/src/markup/render.tsx +3 -2
- package/src/markup/spindle.peggy +16 -8
- package/src/parser.ts +2 -1
- package/src/store.ts +6 -0
- package/src/story-api.ts +9 -3
- package/src/twine-editor/highlight.ts +7 -9
- package/src/utils/stable-key.ts +5 -6
- package/types/index.d.ts +5 -2
package/package.json
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* it holds no second copy: the generated parser imports this module's
|
|
4
4
|
* default export as `shared` (see scripts/peggy.ts).
|
|
5
5
|
*/
|
|
6
|
+
import { HTML_TEXT_ELEMENTS } from './html-elements';
|
|
6
7
|
import { SIGIL_SCOPES, withSelectors } from './tokens';
|
|
7
8
|
|
|
8
|
-
export default { SIGIL_SCOPES, withSelectors };
|
|
9
|
+
export default { SIGIL_SCOPES, withSelectors, HTML_TEXT_ELEMENTS };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** HTML raw-text bodies preserve character references as written. */
|
|
2
|
+
export const RAW_TEXT_ELEMENTS = new Set([
|
|
3
|
+
'script',
|
|
4
|
+
'style',
|
|
5
|
+
'xmp',
|
|
6
|
+
'iframe',
|
|
7
|
+
'noembed',
|
|
8
|
+
'noframes',
|
|
9
|
+
]);
|
|
10
|
+
|
|
11
|
+
/** Text-only bodies recognize their own end tag, never nested HTML tags. */
|
|
12
|
+
export const HTML_TEXT_ELEMENTS = new Set([
|
|
13
|
+
...RAW_TEXT_ELEMENTS,
|
|
14
|
+
'textarea',
|
|
15
|
+
'title',
|
|
16
|
+
]);
|
package/src/markup/render.tsx
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
type TextScope,
|
|
23
23
|
} from '../interpolation';
|
|
24
24
|
import { MacroError } from '../components/macros/MacroError';
|
|
25
|
+
import { HTML_TEXT_ELEMENTS, RAW_TEXT_ELEMENTS } from './html-elements';
|
|
25
26
|
import { isCodeAttribute } from './code-attributes';
|
|
26
27
|
import { errorMessage } from '../utils/error-message';
|
|
27
28
|
import { EMPTY_NAMESPACE } from '../utils/namespace';
|
|
@@ -113,7 +114,7 @@ const STRUCTURAL_ELEMENTS = new Set([
|
|
|
113
114
|
]);
|
|
114
115
|
|
|
115
116
|
/** Elements whose content is literal text, not markdown. */
|
|
116
|
-
const PREFORMATTED_ELEMENTS = new Set(['pre',
|
|
117
|
+
const PREFORMATTED_ELEMENTS = new Set(['pre', ...HTML_TEXT_ELEMENTS]);
|
|
117
118
|
export const WidgetChildrenContext = createContext<WidgetChildren | null>(null);
|
|
118
119
|
|
|
119
120
|
/**
|
|
@@ -778,7 +779,7 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
778
779
|
let children: preact.ComponentChildren = undefined;
|
|
779
780
|
let structural = false;
|
|
780
781
|
if (node.children.length > 0) {
|
|
781
|
-
if (tag
|
|
782
|
+
if (RAW_TEXT_ELEMENTS.has(tag)) {
|
|
782
783
|
// Raw-text elements keep script text and character-reference lookalikes
|
|
783
784
|
// (`?a=1¬ch=2`) as written (#453)
|
|
784
785
|
children = node.children.map((n) =>
|
package/src/markup/spindle.peggy
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
{{
|
|
26
26
|
const LB = String.fromCharCode(123);
|
|
27
27
|
const RB = String.fromCharCode(125);
|
|
28
|
-
const { SIGIL_SCOPES, withSelectors } = shared;
|
|
28
|
+
const { SIGIL_SCOPES, withSelectors, HTML_TEXT_ELEMENTS } = shared;
|
|
29
29
|
const VOID_TAGS = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']);
|
|
30
30
|
const BRANCH_PARENT = { elseif: 'if', else: 'if', case: 'switch', default: 'switch', next: 'timed' };
|
|
31
31
|
const LINK_SEPARATORS = [['|', false], ['->', false], ['<-', true]];
|
|
@@ -142,6 +142,8 @@ let skipEnd = -1;
|
|
|
142
142
|
let attrCode = false;
|
|
143
143
|
/** The macro whose raw body is being read. */
|
|
144
144
|
let rawName = '';
|
|
145
|
+
/** The HTML text-only body being read; its own closer ends this context. */
|
|
146
|
+
let htmlTextTag = '';
|
|
145
147
|
|
|
146
148
|
/**
|
|
147
149
|
* The index in `inner` (at `at` in the input) of the first `separator` that
|
|
@@ -209,12 +211,18 @@ Tokens
|
|
|
209
211
|
TokenItem
|
|
210
212
|
= TextRun
|
|
211
213
|
/ Escape
|
|
212
|
-
/ Comment
|
|
213
|
-
/ LinkLexeme
|
|
214
|
-
/ EmailAutolink
|
|
215
|
-
/ &{ return !textMode; } UriAutolink
|
|
216
|
-
/ &{ return !textMode; } tag:HtmlOpen {
|
|
217
|
-
|
|
214
|
+
/ &{ return !htmlTextTag; } Comment
|
|
215
|
+
/ &{ return !htmlTextTag; } LinkLexeme
|
|
216
|
+
/ &{ return !htmlTextTag; } EmailAutolink
|
|
217
|
+
/ &{ return !textMode && !htmlTextTag; } UriAutolink
|
|
218
|
+
/ &{ return !textMode && !htmlTextTag; } tag:HtmlOpen {
|
|
219
|
+
if (!tag.isSelfClose && HTML_TEXT_ELEMENTS.has(tag.lower)) htmlTextTag = tag.lower;
|
|
220
|
+
return tag;
|
|
221
|
+
}
|
|
222
|
+
/ &{ return !textMode; } tag:HtmlClose &{ return !htmlTextTag || tag.lower === htmlTextTag; } {
|
|
223
|
+
htmlTextTag = '';
|
|
224
|
+
return VOID_TAGS.has(tag.lower) ? null : tag;
|
|
225
|
+
}
|
|
218
226
|
/ Value
|
|
219
227
|
/ tag:MacroTag
|
|
220
228
|
body:( &{ rawName = tag.lower; return tag.kind === 'open' && hooks.isRaw(rawName); } b:RawTokens { return b; } )?
|
|
@@ -282,7 +290,7 @@ Escape
|
|
|
282
290
|
/** A character that starts no markup here. */
|
|
283
291
|
Literal
|
|
284
292
|
= "{" !(Selectors [$_@%(!/a-zA-Z]) { return lexeme('text', location(), { value: LB }); }
|
|
285
|
-
/ !(&{ return !textMode; } HtmlClose) "<" { return lexeme('text', location(), { value: '<' }); }
|
|
293
|
+
/ !(&{ return !textMode && !htmlTextTag; } HtmlClose) "<" { return lexeme('text', location(), { value: '<' }); }
|
|
286
294
|
/ "[" { return lexeme('text', location(), { value: '[' }); }
|
|
287
295
|
|
|
288
296
|
LinkLexeme
|
package/src/parser.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RAW_TEXT_ELEMENTS } from './markup/html-elements';
|
|
1
2
|
import { MarkupError, tokenizeMarkup } from './markup/parse';
|
|
2
3
|
import { isCodeAttribute, splitSigilTemplate } from './markup/code-attributes';
|
|
3
4
|
|
|
@@ -141,7 +142,7 @@ const escapeTextReferences = (text: string) =>
|
|
|
141
142
|
*/
|
|
142
143
|
function isLiteralTextElement(parent: ParentNode): boolean {
|
|
143
144
|
return (
|
|
144
|
-
|
|
145
|
+
RAW_TEXT_ELEMENTS.has((parent as Element).localName) &&
|
|
145
146
|
(parent as Element).namespaceURI === HTML_NAMESPACE
|
|
146
147
|
);
|
|
147
148
|
}
|
package/src/store.ts
CHANGED
|
@@ -285,6 +285,12 @@ function persistSession(get: () => StoryState): { error: unknown } | undefined {
|
|
|
285
285
|
sessionMoments.length = history.length;
|
|
286
286
|
}
|
|
287
287
|
|
|
288
|
+
// Reseeding replaces an existing moment's PRNG snapshot. Keep metadata
|
|
289
|
+
// current without reconstructing the cached variable namespaces (#702).
|
|
290
|
+
for (let i = 0; i < sessionMoments.length; i++) {
|
|
291
|
+
sessionMoments[i] = savedMoment(history[i]!, sessionMoments[i]!.variables);
|
|
292
|
+
}
|
|
293
|
+
|
|
288
294
|
// Append new entries
|
|
289
295
|
if (sessionMoments.length < history.length) {
|
|
290
296
|
const gap = history.length - sessionMoments.length;
|
package/src/story-api.ts
CHANGED
|
@@ -175,7 +175,9 @@ export interface StoryAPI {
|
|
|
175
175
|
event: E,
|
|
176
176
|
callback: StoryEventCallback<E>,
|
|
177
177
|
): () => void;
|
|
178
|
-
waitForActions(
|
|
178
|
+
waitForActions(options?: {
|
|
179
|
+
includeRunningActions?: boolean;
|
|
180
|
+
}): Promise<StoryAction[]>;
|
|
179
181
|
observe(): Observation;
|
|
180
182
|
step(id: string, value?: unknown): Promise<Observation>;
|
|
181
183
|
setObservedActions(rules: ObservedActionRules): void;
|
|
@@ -446,10 +448,14 @@ function createStoryAPI(): StoryAPI {
|
|
|
446
448
|
return trackRuntimeUnsub(unsub);
|
|
447
449
|
},
|
|
448
450
|
|
|
449
|
-
async waitForActions(
|
|
451
|
+
async waitForActions(
|
|
452
|
+
options: { includeRunningActions?: boolean } = {},
|
|
453
|
+
): Promise<StoryAction[]> {
|
|
450
454
|
// An action that finishes later (QuickLoad) has not changed what the
|
|
451
455
|
// checks below look at until it has (#504)
|
|
452
|
-
|
|
456
|
+
// A callback can request a render flush without waiting for its own
|
|
457
|
+
// completion. Keep the default external action-completion barrier.
|
|
458
|
+
if (options.includeRunningActions !== false) await settleRunningActions();
|
|
453
459
|
await new Promise<void>((resolve) => {
|
|
454
460
|
requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
|
|
455
461
|
});
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
type Token,
|
|
12
12
|
type VariableScope,
|
|
13
13
|
} from '../tooling';
|
|
14
|
+
import { HTML_TEXT_ELEMENTS } from '../markup/html-elements';
|
|
14
15
|
import { scanMarkupTolerant, tokenizeMarkupTolerant } from '../markup/parse';
|
|
15
16
|
|
|
16
17
|
type ScannedMarkup = ReturnType<typeof scanMarkupTolerant>;
|
|
@@ -234,12 +235,9 @@ function openDo(tokens: readonly Token[]): MacroToken | undefined {
|
|
|
234
235
|
return open;
|
|
235
236
|
}
|
|
236
237
|
|
|
237
|
-
/** Elements whose content is code, where `<!--` is not an HTML comment. */
|
|
238
|
-
const CODE_ELEMENTS = new Set(['script', 'style']);
|
|
239
|
-
|
|
240
238
|
/**
|
|
241
|
-
* The text tokens that are markup, each with the
|
|
242
|
-
*
|
|
239
|
+
* The text tokens that are markup, each with the HTML text-only element
|
|
240
|
+
* it is in, if any. The text of a `{do}` body is code, and is
|
|
243
241
|
* left out. (Strings and expressions in macros are not text tokens.)
|
|
244
242
|
*/
|
|
245
243
|
function* markupText(
|
|
@@ -254,7 +252,7 @@ function* markupText(
|
|
|
254
252
|
const tag = token.tag.toLowerCase();
|
|
255
253
|
if (token.isClose) {
|
|
256
254
|
if (tag === element) element = undefined;
|
|
257
|
-
} else if (!token.isSelfClose &&
|
|
255
|
+
} else if (!token.isSelfClose && HTML_TEXT_ELEMENTS.has(tag)) {
|
|
258
256
|
element ??= tag;
|
|
259
257
|
}
|
|
260
258
|
} else if (token.type === 'text' && !token.comment && !inDo) {
|
|
@@ -264,8 +262,8 @@ function* markupText(
|
|
|
264
262
|
}
|
|
265
263
|
|
|
266
264
|
/**
|
|
267
|
-
* Where the `<!--` that no `-->` closes starts, or -1. In
|
|
268
|
-
*
|
|
265
|
+
* Where the `<!--` that no `-->` closes starts, or -1. In an HTML
|
|
266
|
+
* text-only element, `<!--` is literal text, no HTML comment.
|
|
269
267
|
*/
|
|
270
268
|
function openComment(tokens: readonly Token[]): number {
|
|
271
269
|
for (const { token, element } of markupText(tokens)) {
|
|
@@ -291,7 +289,7 @@ function closesWithGt(text: string, at: number): boolean {
|
|
|
291
289
|
/**
|
|
292
290
|
* Where a tag starts that is text only for want of its `>`, or -1: `</b`,
|
|
293
291
|
* or `<b_r x=` (no error: `<b` is no tag before `_`), which a `>` on a later
|
|
294
|
-
* line makes a tag. In a
|
|
292
|
+
* line makes a tag. In a text-only element only its end tag counts.
|
|
295
293
|
*/
|
|
296
294
|
function openTag(text: string, tokens: readonly Token[]): number {
|
|
297
295
|
for (const { token, element } of markupText(tokens)) {
|
package/src/utils/stable-key.ts
CHANGED
|
@@ -86,12 +86,12 @@ function keyOf(val: unknown, seen: Map<object, number>): string {
|
|
|
86
86
|
return named(keys.length ? `${key}${fieldsOf(obj, keys, seen)}` : key);
|
|
87
87
|
};
|
|
88
88
|
|
|
89
|
-
//
|
|
90
|
-
//
|
|
89
|
+
// Every object participates in the graph, including Date/RegExp leaves
|
|
90
|
+
// whose aliases can affect mount-initialized locals (#704).
|
|
91
|
+
seen.set(obj, seen.size);
|
|
92
|
+
|
|
93
|
+
// Read with the built-in methods: a subclass may override them.
|
|
91
94
|
if (val instanceof Date || val instanceof RegExp) {
|
|
92
|
-
if (className !== undefined && extraKeys(obj).length) {
|
|
93
|
-
seen.set(obj, seen.size);
|
|
94
|
-
}
|
|
95
95
|
return withFields(
|
|
96
96
|
val instanceof Date
|
|
97
97
|
? `Date(${Date.prototype.getTime.call(val)})`
|
|
@@ -99,7 +99,6 @@ function keyOf(val: unknown, seen: Map<object, number>): string {
|
|
|
99
99
|
);
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
seen.set(obj, seen.size);
|
|
103
102
|
if (Array.isArray(val)) {
|
|
104
103
|
// A hole is no element, not an undefined one (#497), as for deepEqual
|
|
105
104
|
// and a save; a plain `map` would skip it, giving `[,]` the key of `[]`
|
package/types/index.d.ts
CHANGED
|
@@ -905,9 +905,12 @@ export interface StoryAPI {
|
|
|
905
905
|
/**
|
|
906
906
|
* Wait for the actions performed with `performAction()` that are still
|
|
907
907
|
* running, then for the next frame's actions to be registered, and return
|
|
908
|
-
* them.
|
|
908
|
+
* them. Set `includeRunningActions: false` inside an asynchronous action
|
|
909
|
+
* callback to flush rendering without waiting for the callback itself.
|
|
909
910
|
*/
|
|
910
|
-
waitForActions(
|
|
911
|
+
waitForActions(options?: {
|
|
912
|
+
includeRunningActions?: boolean;
|
|
913
|
+
}): Promise<StoryAction[]>;
|
|
911
914
|
|
|
912
915
|
/**
|
|
913
916
|
* What the story shows and offers now: the passage text, the
|