@rohal12/spindle 0.64.9 → 0.64.11
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 +412 -241
- package/dist/pkg/play.js +2 -1
- package/dist/pkg/story-variables.js +489 -271
- package/dist/pkg/tooling.js +2 -0
- package/package.json +1 -1
- package/src/completion-context.ts +111 -23
- package/src/js-lexer.ts +178 -63
- package/src/markup/grammar-shared.ts +2 -1
- package/src/markup/html-elements.ts +16 -0
- package/src/markup/html-integration.tsx +101 -0
- package/src/markup/render.tsx +70 -32
- package/src/markup/spindle.peggy +16 -8
- package/src/parser.ts +2 -1
- package/src/play/session.ts +9 -1
- package/src/store.ts +6 -0
- package/src/story-api.ts +9 -3
- package/src/tooling.ts +63 -19
- package/src/twine-editor/highlight.ts +17 -20
- package/src/utils/stable-key.ts +5 -6
- package/types/index.d.ts +5 -2
- package/types/tooling.d.ts +42 -0
|
@@ -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
|
+
]);
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createPortal,
|
|
3
|
+
h,
|
|
4
|
+
type ComponentChildren,
|
|
5
|
+
type RefObject,
|
|
6
|
+
} from 'preact';
|
|
7
|
+
import { useLayoutEffect, useRef, useState } from 'preact/hooks';
|
|
8
|
+
|
|
9
|
+
export function isHtmlEncoding(value: unknown): boolean {
|
|
10
|
+
return (
|
|
11
|
+
typeof value === 'string' &&
|
|
12
|
+
/^(?:text\/html|application\/xhtml\+xml)$/i.test(value)
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function authorElement(
|
|
17
|
+
tag: string,
|
|
18
|
+
attributes: Record<string, unknown>,
|
|
19
|
+
children: ComponentChildren,
|
|
20
|
+
math: boolean,
|
|
21
|
+
) {
|
|
22
|
+
return math && tag === 'annotation-xml' ? (
|
|
23
|
+
<HtmlAnnotation
|
|
24
|
+
key={attributes.key as string | number | undefined}
|
|
25
|
+
attributes={attributes}
|
|
26
|
+
>
|
|
27
|
+
{children}
|
|
28
|
+
</HtmlAnnotation>
|
|
29
|
+
) : (
|
|
30
|
+
h(tag, attributes, children)
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Preact handles MathML text integration points, but does not yet switch the
|
|
36
|
+
* namespace for HTML-encoded annotation-xml. A portal's container can supply
|
|
37
|
+
* that namespace without adding a wrapper or changing the MathML element.
|
|
38
|
+
*/
|
|
39
|
+
function HtmlAnnotation({
|
|
40
|
+
attributes,
|
|
41
|
+
children,
|
|
42
|
+
}: {
|
|
43
|
+
attributes: Record<string, unknown>;
|
|
44
|
+
children: ComponentChildren;
|
|
45
|
+
}) {
|
|
46
|
+
const elementRef = useRef<Element>(null);
|
|
47
|
+
const [container, setContainer] = useState<
|
|
48
|
+
| (Pick<
|
|
49
|
+
Element,
|
|
50
|
+
| 'nodeType'
|
|
51
|
+
| 'parentNode'
|
|
52
|
+
| 'firstChild'
|
|
53
|
+
| 'childNodes'
|
|
54
|
+
| 'ownerDocument'
|
|
55
|
+
| 'insertBefore'
|
|
56
|
+
| 'appendChild'
|
|
57
|
+
| 'removeChild'
|
|
58
|
+
> & { namespaceURI: string })
|
|
59
|
+
| null
|
|
60
|
+
>(null);
|
|
61
|
+
useLayoutEffect(() => {
|
|
62
|
+
const element = elementRef.current!;
|
|
63
|
+
setContainer({
|
|
64
|
+
nodeType: 1,
|
|
65
|
+
namespaceURI: 'http://www.w3.org/1999/xhtml',
|
|
66
|
+
ownerDocument: element.ownerDocument,
|
|
67
|
+
get parentNode() {
|
|
68
|
+
return element.parentNode;
|
|
69
|
+
},
|
|
70
|
+
get firstChild() {
|
|
71
|
+
return element.firstChild;
|
|
72
|
+
},
|
|
73
|
+
get childNodes() {
|
|
74
|
+
return element.childNodes;
|
|
75
|
+
},
|
|
76
|
+
insertBefore: element.insertBefore.bind(element),
|
|
77
|
+
appendChild: element.appendChild.bind(element),
|
|
78
|
+
removeChild: element.removeChild.bind(element),
|
|
79
|
+
});
|
|
80
|
+
}, []);
|
|
81
|
+
return (
|
|
82
|
+
<>
|
|
83
|
+
{h(
|
|
84
|
+
'annotation-xml',
|
|
85
|
+
{
|
|
86
|
+
...attributes,
|
|
87
|
+
ref: (element: Element | null) => {
|
|
88
|
+
elementRef.current = element;
|
|
89
|
+
const forwarded = attributes.ref as
|
|
90
|
+
RefObject<Element | null> | undefined;
|
|
91
|
+
if (forwarded) forwarded.current = element;
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
isHtmlEncoding(attributes.encoding) ? undefined : children,
|
|
95
|
+
)}
|
|
96
|
+
{container &&
|
|
97
|
+
isHtmlEncoding(attributes.encoding) &&
|
|
98
|
+
createPortal(children, container)}
|
|
99
|
+
</>
|
|
100
|
+
);
|
|
101
|
+
}
|
package/src/markup/render.tsx
CHANGED
|
@@ -22,9 +22,11 @@ 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';
|
|
29
|
+
import { authorElement, isHtmlEncoding } from './html-integration';
|
|
28
30
|
|
|
29
31
|
export interface LocalsUpdater {
|
|
30
32
|
update: (key: string, value: unknown) => void;
|
|
@@ -60,7 +62,7 @@ export const InlineContext = createContext(false);
|
|
|
60
62
|
*/
|
|
61
63
|
export const StructuralContext = createContext(false);
|
|
62
64
|
/**
|
|
63
|
-
* True while rendering inside an element whose content is not markdown: SVG
|
|
65
|
+
* True while rendering inside an element whose content is not markdown: SVG/MathML
|
|
64
66
|
* (whose namespace `<p>` wrappers would break) and the preformatted `<pre>`
|
|
65
67
|
* `<textarea>`, `<style>` and `<script>`, whose text is literal.
|
|
66
68
|
* Macro and widget bodies read it so their content stays literal too.
|
|
@@ -84,12 +86,14 @@ export function useViewScope(): string {
|
|
|
84
86
|
}
|
|
85
87
|
|
|
86
88
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
+
* The foreign namespace containing author elements. SVG and MathML attributes
|
|
90
|
+
* are case-sensitive and have no live HTML form properties.
|
|
89
91
|
*/
|
|
90
|
-
const
|
|
92
|
+
const ForeignContext = createContext<'svg' | 'math' | undefined>(undefined);
|
|
91
93
|
|
|
92
94
|
const SVG_NAMESPACE = 'http://www.w3.org/2000/svg';
|
|
95
|
+
const MATH_NAMESPACE = 'http://www.w3.org/1998/Math/MathML';
|
|
96
|
+
const MATH_TEXT_ELEMENTS = new Set(['mi', 'mo', 'mn', 'ms', 'mtext']);
|
|
93
97
|
|
|
94
98
|
/**
|
|
95
99
|
* Elements whose children must be elements of particular types: markdown
|
|
@@ -113,7 +117,7 @@ const STRUCTURAL_ELEMENTS = new Set([
|
|
|
113
117
|
]);
|
|
114
118
|
|
|
115
119
|
/** Elements whose content is literal text, not markdown. */
|
|
116
|
-
const PREFORMATTED_ELEMENTS = new Set(['pre',
|
|
120
|
+
const PREFORMATTED_ELEMENTS = new Set(['pre', ...HTML_TEXT_ELEMENTS]);
|
|
117
121
|
export const WidgetChildrenContext = createContext<WidgetChildren | null>(null);
|
|
118
122
|
|
|
119
123
|
/**
|
|
@@ -204,13 +208,15 @@ function PlaceholderAttributes({
|
|
|
204
208
|
tag,
|
|
205
209
|
attributes,
|
|
206
210
|
placeholders,
|
|
207
|
-
|
|
211
|
+
foreign,
|
|
212
|
+
math,
|
|
208
213
|
children,
|
|
209
214
|
}: {
|
|
210
215
|
tag: string;
|
|
211
216
|
attributes: Attribute[];
|
|
212
217
|
placeholders: Record<string, ASTNode[]>;
|
|
213
|
-
|
|
218
|
+
foreign: boolean;
|
|
219
|
+
math: boolean;
|
|
214
220
|
children: preact.ComponentChildren[];
|
|
215
221
|
}) {
|
|
216
222
|
const scope = useTextScope();
|
|
@@ -222,11 +228,11 @@ function PlaceholderAttributes({
|
|
|
222
228
|
for (const error of result.errors) errors.push([name, error]);
|
|
223
229
|
return [name, result.text, written, true];
|
|
224
230
|
});
|
|
225
|
-
const { props, direct } = splitAttributes(resolved,
|
|
231
|
+
const { props, direct } = splitAttributes(resolved, foreign, tag);
|
|
226
232
|
const ref = useDirectAttributes(direct);
|
|
227
233
|
return withAttributeErrors(
|
|
228
234
|
errors,
|
|
229
|
-
|
|
235
|
+
authorElement(tag, ref ? { ...props, ref } : props, children, math),
|
|
230
236
|
);
|
|
231
237
|
}
|
|
232
238
|
|
|
@@ -235,15 +241,17 @@ function DirectAttributes({
|
|
|
235
241
|
tag,
|
|
236
242
|
props,
|
|
237
243
|
direct,
|
|
244
|
+
math,
|
|
238
245
|
children,
|
|
239
246
|
}: {
|
|
240
247
|
tag: string;
|
|
241
248
|
props: Record<string, unknown>;
|
|
242
249
|
direct: [string, string][];
|
|
250
|
+
math: boolean;
|
|
243
251
|
children: preact.ComponentChildren[];
|
|
244
252
|
}) {
|
|
245
253
|
const ref = useDirectAttributes(direct);
|
|
246
|
-
return
|
|
254
|
+
return authorElement(tag, { ...props, ref }, children, math);
|
|
247
255
|
}
|
|
248
256
|
|
|
249
257
|
/** An error met while evaluating the named attribute. */
|
|
@@ -352,7 +360,8 @@ function convertDomNode(
|
|
|
352
360
|
|
|
353
361
|
// Convert attributes, as author HTML's (see splitAttributes): markdown
|
|
354
362
|
// output holds raw HTML from text nodes (a custom macro's, or comments)
|
|
355
|
-
const
|
|
363
|
+
const foreign =
|
|
364
|
+
el.namespaceURI === SVG_NAMESPACE || el.namespaceURI === MATH_NAMESPACE;
|
|
356
365
|
const attributes: Attribute[] = [];
|
|
357
366
|
let placeholders: Record<string, ASTNode[]> | undefined;
|
|
358
367
|
for (const { name, value } of Array.from(el.attributes)) {
|
|
@@ -379,12 +388,13 @@ function convertDomNode(
|
|
|
379
388
|
tag={tag}
|
|
380
389
|
attributes={attributes}
|
|
381
390
|
placeholders={placeholders}
|
|
382
|
-
|
|
391
|
+
foreign={foreign}
|
|
392
|
+
math={el.namespaceURI === MATH_NAMESPACE}
|
|
383
393
|
children={children}
|
|
384
394
|
/>
|
|
385
395
|
);
|
|
386
396
|
}
|
|
387
|
-
const { props, direct } = splitAttributes(attributes,
|
|
397
|
+
const { props, direct } = splitAttributes(attributes, foreign, tag);
|
|
388
398
|
if (direct.length > 0) {
|
|
389
399
|
return (
|
|
390
400
|
<DirectAttributes
|
|
@@ -392,11 +402,17 @@ function convertDomNode(
|
|
|
392
402
|
tag={tag}
|
|
393
403
|
props={props}
|
|
394
404
|
direct={direct}
|
|
405
|
+
math={el.namespaceURI === MATH_NAMESPACE}
|
|
395
406
|
children={children}
|
|
396
407
|
/>
|
|
397
408
|
);
|
|
398
409
|
}
|
|
399
|
-
return
|
|
410
|
+
return authorElement(
|
|
411
|
+
tag,
|
|
412
|
+
{ ...props, key },
|
|
413
|
+
children,
|
|
414
|
+
el.namespaceURI === MATH_NAMESPACE,
|
|
415
|
+
);
|
|
400
416
|
}
|
|
401
417
|
return null;
|
|
402
418
|
}
|
|
@@ -512,7 +528,7 @@ const PREACT_RESERVED = new Set([
|
|
|
512
528
|
]);
|
|
513
529
|
|
|
514
530
|
/** Names set directly on SVG elements (see splitAttributes). */
|
|
515
|
-
const
|
|
531
|
+
const FOREIGN_DIRECT = new Set([...PREACT_RESERVED, 'class', 'className']);
|
|
516
532
|
|
|
517
533
|
/** Attribute names setAttribute accepts (`@click` is parsed, not settable). */
|
|
518
534
|
const SETTABLE_NAME = /^[A-Za-z_:][\w:.-]*$/;
|
|
@@ -563,7 +579,7 @@ function isNativeDefault(tag: string, name: string): boolean {
|
|
|
563
579
|
*/
|
|
564
580
|
function splitAttributes(
|
|
565
581
|
attributes: Attribute[],
|
|
566
|
-
|
|
582
|
+
foreign: boolean,
|
|
567
583
|
tag: string,
|
|
568
584
|
): { props: Record<string, unknown>; direct: [string, string][] } {
|
|
569
585
|
const props: Record<string, unknown> = {};
|
|
@@ -575,7 +591,7 @@ function splitAttributes(
|
|
|
575
591
|
const lower = name.toLowerCase();
|
|
576
592
|
if (BOOLEAN_ATTRIBUTES.has(lower) && written !== '' && value === '') {
|
|
577
593
|
continue;
|
|
578
|
-
} else if (!
|
|
594
|
+
} else if (!foreign && LIVE_PROPERTIES.has(lower)) {
|
|
579
595
|
if (!bound && isNativeDefault(tag, lower)) {
|
|
580
596
|
defaults.push([name.toUpperCase(), lower === 'value' ? value : '']);
|
|
581
597
|
} else {
|
|
@@ -585,11 +601,11 @@ function splitAttributes(
|
|
|
585
601
|
!SETTABLE_NAME.test(name) ||
|
|
586
602
|
lower.startsWith('on') ||
|
|
587
603
|
lower === 'translate' ||
|
|
588
|
-
(
|
|
604
|
+
(foreign && (FOREIGN_DIRECT.has(name) || name === '__proto__'))
|
|
589
605
|
) {
|
|
590
606
|
direct.push([name, value]);
|
|
591
607
|
} else {
|
|
592
|
-
props[
|
|
608
|
+
props[foreign ? name : name.toUpperCase()] = value;
|
|
593
609
|
}
|
|
594
610
|
}
|
|
595
611
|
for (const [name, value] of defaults) props[name] = value;
|
|
@@ -751,13 +767,9 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
751
767
|
inline: parentInline,
|
|
752
768
|
structural: parentStructural,
|
|
753
769
|
} = useRenderOptions();
|
|
754
|
-
const
|
|
770
|
+
const parentNamespace = useContext(ForeignContext);
|
|
755
771
|
const tag = node.tag.toLowerCase();
|
|
756
|
-
const
|
|
757
|
-
// The content of a foreignObject is HTML again (see below)
|
|
758
|
-
const isHtmlRoot = inSvg && tag === 'foreignobject';
|
|
759
|
-
const rawContent = inRaw && !isHtmlRoot;
|
|
760
|
-
const isRawRoot = !inRaw && (isSvgRoot || PREFORMATTED_ELEMENTS.has(tag));
|
|
772
|
+
const namespace = tag === 'svg' || tag === 'math' ? tag : parentNamespace;
|
|
761
773
|
const errors: AttributeError[] = [];
|
|
762
774
|
const resolved = Object.entries(node.attributes).map(([k, v]): Attribute => [
|
|
763
775
|
k,
|
|
@@ -765,7 +777,24 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
765
777
|
v,
|
|
766
778
|
hasInterpolation(v),
|
|
767
779
|
]);
|
|
768
|
-
const
|
|
780
|
+
const htmlAnnotation =
|
|
781
|
+
namespace === 'math' &&
|
|
782
|
+
tag === 'annotation-xml' &&
|
|
783
|
+
isHtmlEncoding(resolved.find(([name]) => name === 'encoding')?.[1]);
|
|
784
|
+
// MathML token elements keep literal text, but their element children use
|
|
785
|
+
// HTML attributes (Preact also switches their DOM namespace to HTML).
|
|
786
|
+
const mathText = namespace === 'math' && MATH_TEXT_ELEMENTS.has(tag);
|
|
787
|
+
const isHtmlRoot =
|
|
788
|
+
(namespace === 'svg' && tag === 'foreignobject') || htmlAnnotation;
|
|
789
|
+
const childNamespace = isHtmlRoot || mathText ? undefined : namespace;
|
|
790
|
+
const rawContent = inRaw && !isHtmlRoot;
|
|
791
|
+
const isRawRoot =
|
|
792
|
+
!inRaw && (namespace !== undefined || PREFORMATTED_ELEMENTS.has(tag));
|
|
793
|
+
const { props, direct } = splitAttributes(
|
|
794
|
+
resolved,
|
|
795
|
+
namespace !== undefined,
|
|
796
|
+
tag,
|
|
797
|
+
);
|
|
769
798
|
const ref = useDirectAttributes(direct);
|
|
770
799
|
if (ref) props.ref = ref;
|
|
771
800
|
const isInline = INLINE_ELEMENTS.has(tag);
|
|
@@ -778,7 +807,7 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
778
807
|
let children: preact.ComponentChildren = undefined;
|
|
779
808
|
let structural = false;
|
|
780
809
|
if (node.children.length > 0) {
|
|
781
|
-
if (tag
|
|
810
|
+
if (RAW_TEXT_ELEMENTS.has(tag)) {
|
|
782
811
|
// Raw-text elements keep script text and character-reference lookalikes
|
|
783
812
|
// (`?a=1¬ch=2`) as written (#453)
|
|
784
813
|
children = node.children.map((n) =>
|
|
@@ -829,17 +858,26 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
829
858
|
// markdown, for the content of macros and widgets in it too (#477)
|
|
830
859
|
if (isHtmlRoot && children) {
|
|
831
860
|
children = (
|
|
832
|
-
<
|
|
861
|
+
<ForeignContext.Provider value={undefined}>
|
|
833
862
|
<RawTextContext.Provider value={false}>
|
|
834
863
|
{children}
|
|
835
864
|
</RawTextContext.Provider>
|
|
836
|
-
</
|
|
865
|
+
</ForeignContext.Provider>
|
|
837
866
|
);
|
|
838
867
|
}
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
868
|
+
if (childNamespace !== parentNamespace) {
|
|
869
|
+
children = (
|
|
870
|
+
<ForeignContext.Provider value={childNamespace}>
|
|
871
|
+
{children}
|
|
872
|
+
</ForeignContext.Provider>
|
|
873
|
+
);
|
|
842
874
|
}
|
|
875
|
+
const element = authorElement(
|
|
876
|
+
node.tag,
|
|
877
|
+
props,
|
|
878
|
+
children,
|
|
879
|
+
namespace === 'math',
|
|
880
|
+
);
|
|
843
881
|
return withAttributeErrors(
|
|
844
882
|
errors,
|
|
845
883
|
isRawRoot ? (
|
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/play/session.ts
CHANGED
|
@@ -188,7 +188,15 @@ export function openPlaySessionWith(
|
|
|
188
188
|
reject(new Error(CLOSED_MESSAGE));
|
|
189
189
|
return;
|
|
190
190
|
}
|
|
191
|
-
|
|
191
|
+
// Match postMessage's call-time snapshot even while another request
|
|
192
|
+
// is active. A clone error rejects just this promise before enqueueing.
|
|
193
|
+
const snapshot: unknown[] = structuredClone(args);
|
|
194
|
+
queue.push({
|
|
195
|
+
op,
|
|
196
|
+
args: snapshot,
|
|
197
|
+
resolve: resolve as Job['resolve'],
|
|
198
|
+
reject,
|
|
199
|
+
});
|
|
192
200
|
pump();
|
|
193
201
|
});
|
|
194
202
|
|
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
|
});
|
package/src/tooling.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { completionScopes } from './completion-context';
|
|
1
2
|
import { effectiveNamedDefinitions, NameMap } from './utils/macro-names';
|
|
2
3
|
export {
|
|
3
4
|
completionContext,
|
|
5
|
+
completionTarget,
|
|
6
|
+
type CompletionTarget,
|
|
4
7
|
type CompletionContext,
|
|
5
8
|
type CompletionContainer,
|
|
6
9
|
} from './completion-context';
|
|
@@ -32,6 +35,7 @@ import {
|
|
|
32
35
|
tokenizeMarkupTolerant as tokenizeRawMarkupTolerant,
|
|
33
36
|
type ParseMarkupOptions as RuntimeParseMarkupOptions,
|
|
34
37
|
type TolerantTokens,
|
|
38
|
+
mapOffsets,
|
|
35
39
|
} from './markup/parse';
|
|
36
40
|
import {
|
|
37
41
|
parameterLookup,
|
|
@@ -97,6 +101,7 @@ export type {
|
|
|
97
101
|
FindCodeEndOptions,
|
|
98
102
|
JsGoal,
|
|
99
103
|
JsLexHandlers,
|
|
104
|
+
CompletionIdentifier,
|
|
100
105
|
Sigil,
|
|
101
106
|
} from './js-lexer';
|
|
102
107
|
export { MarkupError, parseSelectors } from './markup/parse';
|
|
@@ -347,7 +352,6 @@ export function macroCalls(
|
|
|
347
352
|
macros: Iterable<ToolingMacro> = [],
|
|
348
353
|
): MacroCall[] {
|
|
349
354
|
const all = effectiveToolingMacros(macros);
|
|
350
|
-
const definitions = new NameMap(all.map((macro) => [macro.name, macro]));
|
|
351
355
|
const parametersOf = parameterLookup(all);
|
|
352
356
|
const options = tokenizationOptions({
|
|
353
357
|
macros: all,
|
|
@@ -356,7 +360,39 @@ export function macroCalls(
|
|
|
356
360
|
const hooks = options.hooks!;
|
|
357
361
|
const tokens = tokenizeRawMarkupTolerant(source, options).tokens;
|
|
358
362
|
const calls: MacroCall[] = [];
|
|
363
|
+
const classifier = macroClassifier(all);
|
|
359
364
|
const classify = (tokens: readonly Token[]) => {
|
|
365
|
+
for (const call of classifier(tokens)) calls.push(call);
|
|
366
|
+
};
|
|
367
|
+
classify(tokens);
|
|
368
|
+
for (const piece of passagePiecesOf(source, tokens, parametersOf, {
|
|
369
|
+
isBlock: hooks.isBlock,
|
|
370
|
+
})) {
|
|
371
|
+
if (piece.kind !== 'text' || !piece.tokens.length) continue;
|
|
372
|
+
classify(piece.tokens);
|
|
373
|
+
}
|
|
374
|
+
// Consumed arguments are literal even if a same-named widget would
|
|
375
|
+
// normally evaluate them as markup. Drop heads inside those arguments.
|
|
376
|
+
let literalEnd = -1;
|
|
377
|
+
return calls
|
|
378
|
+
.sort((a, b) => a.token.start - b.token.start)
|
|
379
|
+
.filter((call) => {
|
|
380
|
+
if (call.token.start < literalEnd) return false;
|
|
381
|
+
if (call.kind === 'value' || call.kind === 'branch')
|
|
382
|
+
literalEnd = call.token.argsEnd;
|
|
383
|
+
return true;
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Shared contextual dispatch for complete and unfinished grammar tokens. */
|
|
388
|
+
function macroClassifier(all: readonly ToolingMacro[]) {
|
|
389
|
+
const definitions = new NameMap(all.map((macro) => [macro.name, macro]));
|
|
390
|
+
const hooks = tokenizationOptions({
|
|
391
|
+
macros: all,
|
|
392
|
+
hooks: { isBlock: blockLookup(all, false) },
|
|
393
|
+
}).hooks!;
|
|
394
|
+
return (tokens: readonly Token[]) => {
|
|
395
|
+
const calls: MacroCall[] = [];
|
|
360
396
|
const found = new Map<MacroToken, MacroCall>();
|
|
361
397
|
const pending: { node: PairedNode; parent?: MacroToken }[] = pairMarkup(
|
|
362
398
|
tokens,
|
|
@@ -392,25 +428,33 @@ export function macroCalls(
|
|
|
392
428
|
const call = found.get(token) ?? { token, kind: 'branch' as const };
|
|
393
429
|
calls.push(call);
|
|
394
430
|
}
|
|
431
|
+
return calls;
|
|
395
432
|
};
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** The opening macro head containing a cursor, including unfinished heads. */
|
|
436
|
+
export function macroCallAt(
|
|
437
|
+
source: string,
|
|
438
|
+
cursor: number,
|
|
439
|
+
macros: Iterable<ToolingMacro> = [],
|
|
440
|
+
): MacroCall | undefined {
|
|
441
|
+
const all = effectiveToolingMacros(macros);
|
|
442
|
+
const classify = macroClassifier(all);
|
|
443
|
+
let selected: MacroCall | undefined;
|
|
444
|
+
completionScopes(source, cursor, all, (tokens, map) => {
|
|
445
|
+
const call = classify(tokens).find(
|
|
446
|
+
({ token }) => map(token.start) < cursor && cursor <= map(token.end),
|
|
447
|
+
);
|
|
448
|
+
if (call)
|
|
449
|
+
selected = {
|
|
450
|
+
...call,
|
|
451
|
+
token: mapOffsets(call.token, map),
|
|
452
|
+
...(call.owner && { owner: mapOffsets(call.owner, map) }),
|
|
453
|
+
};
|
|
454
|
+
// A control-owned argument is literal, even for a markup-holding widget.
|
|
455
|
+
return !call || call.kind === 'invocation';
|
|
456
|
+
});
|
|
457
|
+
return selected;
|
|
414
458
|
}
|
|
415
459
|
|
|
416
460
|
/**
|