@rohal12/spindle 0.62.7 → 0.62.9
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 +15345 -15292
- package/dist/pkg/macro-registry.js +3 -1
- package/dist/pkg/macro-registry.json +3 -1
- package/dist/pkg/story-variables.js +206 -139
- package/package.json +1 -1
- package/src/action-registry.ts +9 -6
- package/src/components/macros/Computed.tsx +5 -3
- package/src/components/macros/Cycle.tsx +1 -0
- package/src/format/format-passage.ts +37 -1
- package/src/headless.ts +4 -3
- package/src/hooks/use-modal-focus.ts +6 -19
- package/src/js-lexer.ts +26 -0
- package/src/markup/ast.ts +5 -1
- package/src/markup/code-attributes.ts +11 -3
- package/src/markup/line-column.ts +3 -8
- package/src/markup/pair.ts +20 -1
- package/src/markup/parse.ts +13 -2
- package/src/markup/render.tsx +5 -5
- package/src/observe/observable-text.ts +20 -8
- package/src/parser.ts +2 -2
- package/src/registry.ts +9 -4
- package/src/story-api.ts +1 -5
- package/src/story-variables.ts +25 -42
- package/src/tooling.ts +16 -5
- package/src/utils/element-visibility.ts +35 -0
- package/src/utils/macro-names.ts +9 -0
- package/src/utils/sorted.ts +11 -0
- package/types/headless.d.ts +2 -2
- package/types/tooling.d.ts +5 -1
package/src/headless.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* same boot sequence directly.
|
|
9
9
|
*/
|
|
10
10
|
import { boot } from './index';
|
|
11
|
+
import { readOnlyValue } from './execute-mutation';
|
|
11
12
|
import { isSpecialPassage, useStoryStore } from './store';
|
|
12
13
|
import { noPassageError, specialPassageError } from './runtime-errors';
|
|
13
14
|
import type { StoryAPI } from './story-api';
|
|
@@ -87,11 +88,11 @@ export function bootStory(options: BootStoryOptions): Promise<StoryAPI> {
|
|
|
87
88
|
}
|
|
88
89
|
|
|
89
90
|
/**
|
|
90
|
-
* The story's `$` variables
|
|
91
|
-
*
|
|
91
|
+
* The story's `$` variables, keyed by name without the `$`. Mutable values
|
|
92
|
+
* are copied together, preserving aliases without exposing live state.
|
|
92
93
|
*/
|
|
93
94
|
export function storyVariables(): Record<string, unknown> {
|
|
94
|
-
return { ...useStoryStore.getState().variables };
|
|
95
|
+
return { ...readOnlyValue(useStoryStore.getState().variables) };
|
|
95
96
|
}
|
|
96
97
|
|
|
97
98
|
/**
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
composedChildren,
|
|
3
|
+
composedElements,
|
|
4
|
+
composedParent,
|
|
5
|
+
isRendered,
|
|
6
|
+
} from '../utils/element-visibility';
|
|
2
7
|
import { useLayoutEffect } from 'preact/hooks';
|
|
3
8
|
import type { RefObject } from 'preact';
|
|
4
9
|
|
|
@@ -17,24 +22,6 @@ const FOCUSABLE = [
|
|
|
17
22
|
'[tabindex]',
|
|
18
23
|
].join(',');
|
|
19
24
|
|
|
20
|
-
function composedChildren(root: ParentNode): Element[] {
|
|
21
|
-
if ((root as Element).localName === 'slot') {
|
|
22
|
-
const assigned = (root as HTMLSlotElement).assignedNodes({
|
|
23
|
-
flatten: true,
|
|
24
|
-
});
|
|
25
|
-
return assigned.filter((node): node is Element => node.nodeType === 1);
|
|
26
|
-
}
|
|
27
|
-
return Array.from(root.children);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Authored elements in rendered order; unassigned light DOM is not rendered. */
|
|
31
|
-
function* composedElements(root: ParentNode): Generator<HTMLElement> {
|
|
32
|
-
for (const el of composedChildren(root)) {
|
|
33
|
-
yield el as HTMLElement;
|
|
34
|
-
yield* composedElements(el.shadowRoot ?? el);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
25
|
/**
|
|
39
26
|
* Whether `el` can be focused at all: shown, rendered, enabled (also by its
|
|
40
27
|
* fieldset) and not inert.
|
package/src/js-lexer.ts
CHANGED
|
@@ -607,6 +607,32 @@ export function parseMemberIdentifier(src: string): string | null {
|
|
|
607
607
|
return property.name as string;
|
|
608
608
|
}
|
|
609
609
|
|
|
610
|
+
/** Read the contiguous dot members after a variable, keeping decoded names. */
|
|
611
|
+
export function readMemberPath(
|
|
612
|
+
src: string,
|
|
613
|
+
end: number,
|
|
614
|
+
): { path: string[]; end: number } {
|
|
615
|
+
const path: string[] = [];
|
|
616
|
+
const p = tokenizerAt(src, end, 'expression', { afterOperand: true });
|
|
617
|
+
try {
|
|
618
|
+
for (;;) {
|
|
619
|
+
p.nextToken();
|
|
620
|
+
if (p.type !== tt.dot || p.start !== end) break;
|
|
621
|
+
const dotEnd = p.end;
|
|
622
|
+
p.nextToken();
|
|
623
|
+
if (p.start !== dotEnd || (p.type !== tt.name && !p.type.keyword)) break;
|
|
624
|
+
// Field schemas cover the runtime's word-character path names.
|
|
625
|
+
if (!/^\w+$/.test(String(p.value))) break;
|
|
626
|
+
path.push(String(p.value));
|
|
627
|
+
end = p.end;
|
|
628
|
+
}
|
|
629
|
+
} catch (error) {
|
|
630
|
+
// Reference collection also reads incomplete authoring input.
|
|
631
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
632
|
+
}
|
|
633
|
+
return { path, end };
|
|
634
|
+
}
|
|
635
|
+
|
|
610
636
|
/**
|
|
611
637
|
* Parse `src` as `goal`, reading `%` as `percentAt` says where it says, and
|
|
612
638
|
* noting where it read a transient in `transientsRead`.
|
package/src/markup/ast.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { withSelectors, type Selectors, type VariableScope } from './tokens';
|
|
6
6
|
import type { PairedBranch, PairedNode } from './pair';
|
|
7
7
|
import { NameSet } from '../utils/macro-names';
|
|
8
|
+
import { getMacroParameters } from '../registry';
|
|
8
9
|
|
|
9
10
|
export interface TextNode {
|
|
10
11
|
type: 'text';
|
|
@@ -184,7 +185,10 @@ function astNode({ token, body }: PairedNode): ASTNode {
|
|
|
184
185
|
};
|
|
185
186
|
if (!body) return withSelectors(node, token);
|
|
186
187
|
const children = buildAst(body.children);
|
|
187
|
-
const branching =
|
|
188
|
+
const branching =
|
|
189
|
+
BRANCHING.has(name) &&
|
|
190
|
+
!WIDGETS.has(name) &&
|
|
191
|
+
getMacroParameters.source(name) !== 'user';
|
|
188
192
|
if (!branching && body.branches.length === 0) {
|
|
189
193
|
node.children = children;
|
|
190
194
|
return withSelectors(node, token);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isSigil } from './tokens';
|
|
2
2
|
import { indexCodeEnds } from './code-end';
|
|
3
|
+
import { lexJs } from '../js-lexer';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Attributes whose value is code with braces of its own, not text: event
|
|
@@ -23,7 +24,7 @@ export type SigilPart =
|
|
|
23
24
|
* Split a code attribute value into literal text and its `{…}` blocks that
|
|
24
25
|
* start with a sigil. Braces inside strings don't close a block; an
|
|
25
26
|
* unclosed block, any other brace and backslashes are text. A block whose
|
|
26
|
-
* sigil is not followed by
|
|
27
|
+
* sigil is not followed by an identifier (`{$(…)}`) is code, kept
|
|
27
28
|
* verbatim: not resolved, and not decoded like text.
|
|
28
29
|
*/
|
|
29
30
|
export function splitSigilTemplate(
|
|
@@ -52,9 +53,16 @@ export function splitSigilTemplate(
|
|
|
52
53
|
if (text) parts.push({ text });
|
|
53
54
|
text = '';
|
|
54
55
|
const block = template.slice(i, end);
|
|
56
|
+
let reference = /^.\w/.test(block);
|
|
57
|
+
if (!reference && block[1] === '\\') {
|
|
58
|
+
lexJs(block, {
|
|
59
|
+
variable(_sigil, _name, start) {
|
|
60
|
+
if (start === 0) reference = true;
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
|
55
64
|
parts.push(
|
|
56
|
-
|
|
57
|
-
(incomplete && block.length === 1 && end === template.length)
|
|
65
|
+
reference || (incomplete && block.length === 1 && end === template.length)
|
|
58
66
|
? { expr: block }
|
|
59
67
|
: { verbatim: `{${block}}` },
|
|
60
68
|
);
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { upperBound } from '../utils/sorted';
|
|
2
|
+
|
|
1
3
|
/** 1-based line and column of `offset` in `text`. */
|
|
2
4
|
export function lineColumn(
|
|
3
5
|
text: string,
|
|
@@ -20,14 +22,7 @@ export function sourcePositions(text: string) {
|
|
|
20
22
|
if (text[i] === '\n') starts.push(i + 1);
|
|
21
23
|
}
|
|
22
24
|
return (offset: number) => {
|
|
23
|
-
|
|
24
|
-
let high = starts.length;
|
|
25
|
-
while (low < high) {
|
|
26
|
-
const mid = (low + high) >>> 1;
|
|
27
|
-
if (starts[mid]! <= offset) low = mid + 1;
|
|
28
|
-
else high = mid;
|
|
29
|
-
}
|
|
30
|
-
const line = Math.max(1, low);
|
|
25
|
+
const line = Math.max(1, upperBound(starts, offset));
|
|
31
26
|
return { line, column: offset - starts[line - 1]! + 1 };
|
|
32
27
|
};
|
|
33
28
|
}
|
package/src/markup/pair.ts
CHANGED
|
@@ -17,7 +17,14 @@ import type { HtmlToken, MacroToken, Span, Token } from './tokens';
|
|
|
17
17
|
|
|
18
18
|
/** What is wrong with the pairing, by kind. */
|
|
19
19
|
export type PairingErrorCode =
|
|
20
|
-
'unclosed-block'
|
|
20
|
+
| 'unclosed-block'
|
|
21
|
+
| 'mismatched-closer'
|
|
22
|
+
| 'stray-closer'
|
|
23
|
+
| 'misplaced-branch'
|
|
24
|
+
| 'nesting-limit';
|
|
25
|
+
|
|
26
|
+
/** Keep AST construction and recursive rendering within a safe call depth. */
|
|
27
|
+
const MAX_MARKUP_DEPTH = 256;
|
|
21
28
|
|
|
22
29
|
/**
|
|
23
30
|
* A problem in the pairing of the tokens, at the token from `start` up to
|
|
@@ -322,6 +329,18 @@ export function pairMarkup(
|
|
|
322
329
|
add(node);
|
|
323
330
|
continue;
|
|
324
331
|
}
|
|
332
|
+
if (stack.length === MAX_MARKUP_DEPTH) {
|
|
333
|
+
report(
|
|
334
|
+
'nesting-limit',
|
|
335
|
+
token,
|
|
336
|
+
`Markup nesting exceeds the limit of ${MAX_MARKUP_DEPTH} elements`,
|
|
337
|
+
token.start,
|
|
338
|
+
{
|
|
339
|
+
name: nameOf(token as MacroToken | HtmlToken),
|
|
340
|
+
limit: String(MAX_MARKUP_DEPTH),
|
|
341
|
+
},
|
|
342
|
+
);
|
|
343
|
+
}
|
|
325
344
|
const body: PairedBody = { children: [], branches: [] };
|
|
326
345
|
node.body = body;
|
|
327
346
|
const key = tagKey(token as MacroToken | HtmlToken);
|
package/src/markup/parse.ts
CHANGED
|
@@ -15,6 +15,7 @@ import type { Selectors, Token } from './tokens';
|
|
|
15
15
|
import { isCodeAttribute } from './code-attributes';
|
|
16
16
|
import { defaultCodeEnd, indexCodeEnds, type CodeEnd } from './code-end';
|
|
17
17
|
import { lineColumn, sourcePositions } from './line-column';
|
|
18
|
+
import { upperBound } from '../utils/sorted';
|
|
18
19
|
|
|
19
20
|
export { lineColumn };
|
|
20
21
|
|
|
@@ -163,8 +164,18 @@ export function analyzeMarkup(
|
|
|
163
164
|
// A malformed tag is noticed where it starts, an unpaired tag where its
|
|
164
165
|
// problem shows (see PairingError.noticedAt). Within a malformed tag the
|
|
165
166
|
// markup is no markup: what the recovery pairs there is dropped.
|
|
166
|
-
|
|
167
|
-
|
|
167
|
+
// Damage starts increase during recovery, but intervals can overlap. A
|
|
168
|
+
// prefix maximum keeps even nested damage searchable in logarithmic time.
|
|
169
|
+
const damageEnds: number[] = [];
|
|
170
|
+
for (const e of errors) {
|
|
171
|
+
damageEnds.push(
|
|
172
|
+
Math.max(damageEnds[damageEnds.length - 1] ?? -1, e.offset),
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
const inDamage = (start: number) => {
|
|
176
|
+
const index = upperBound(damage, start) - 1;
|
|
177
|
+
return index >= 0 && start <= damageEnds[index]!;
|
|
178
|
+
};
|
|
168
179
|
const noticed: [number, MarkupError][] = errors.map((e, i) => [
|
|
169
180
|
damage[i]! + 0.5,
|
|
170
181
|
e,
|
package/src/markup/render.tsx
CHANGED
|
@@ -62,7 +62,7 @@ export const StructuralContext = createContext(false);
|
|
|
62
62
|
/**
|
|
63
63
|
* True while rendering inside an element whose content is not markdown: SVG
|
|
64
64
|
* (whose namespace `<p>` wrappers would break) and the preformatted `<pre>`
|
|
65
|
-
* `<textarea>` and `<
|
|
65
|
+
* `<textarea>`, `<style>` and `<script>`, whose text is literal.
|
|
66
66
|
* Macro and widget bodies read it so their content stays literal too.
|
|
67
67
|
*/
|
|
68
68
|
export const RawTextContext = createContext(false);
|
|
@@ -113,7 +113,7 @@ const STRUCTURAL_ELEMENTS = new Set([
|
|
|
113
113
|
]);
|
|
114
114
|
|
|
115
115
|
/** Elements whose content is literal text, not markdown. */
|
|
116
|
-
const PREFORMATTED_ELEMENTS = new Set(['pre', 'textarea', 'style']);
|
|
116
|
+
const PREFORMATTED_ELEMENTS = new Set(['pre', 'textarea', 'style', 'script']);
|
|
117
117
|
export const WidgetChildrenContext = createContext<WidgetChildren | null>(null);
|
|
118
118
|
|
|
119
119
|
/**
|
|
@@ -651,7 +651,7 @@ function decodeText(text: string): string {
|
|
|
651
651
|
}
|
|
652
652
|
|
|
653
653
|
/** A `{` and a sigil starting a reference, in a code attribute. */
|
|
654
|
-
const SIGIL_REFERENCE = /\{[$_@%]\w/;
|
|
654
|
+
const SIGIL_REFERENCE = /\{[$_@%][\w\\]/;
|
|
655
655
|
|
|
656
656
|
const attributeNodes = new Map<string, ParsedText>();
|
|
657
657
|
const ATTRIBUTE_CACHE_LIMIT = 2000;
|
|
@@ -774,8 +774,8 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
774
774
|
let children: preact.ComponentChildren = undefined;
|
|
775
775
|
let structural = false;
|
|
776
776
|
if (node.children.length > 0) {
|
|
777
|
-
if (tag === 'style') {
|
|
778
|
-
//
|
|
777
|
+
if (tag === 'style' || tag === 'script') {
|
|
778
|
+
// Raw-text elements keep script text and character-reference lookalikes
|
|
779
779
|
// (`?a=1¬ch=2`) as written (#453)
|
|
780
780
|
children = node.children.map((n) =>
|
|
781
781
|
n.type === 'text' ? n.value : renderSingleNode(n),
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
composedChildNodes,
|
|
3
|
+
composedElements,
|
|
4
|
+
composedParent,
|
|
5
|
+
isHidden,
|
|
6
|
+
isRendered,
|
|
7
|
+
} from '../utils/element-visibility';
|
|
2
8
|
import { getAction } from '../action-registry';
|
|
3
9
|
|
|
4
10
|
/** Elements that start and end a line. */
|
|
@@ -81,8 +87,11 @@ export function observableText(
|
|
|
81
87
|
|
|
82
88
|
const walk = (node: Node, inPre: boolean) => {
|
|
83
89
|
if (node.nodeType === 3) {
|
|
84
|
-
|
|
85
|
-
|
|
90
|
+
const parent =
|
|
91
|
+
(node as Text).assignedSlot ??
|
|
92
|
+
node.parentElement ??
|
|
93
|
+
(node.getRootNode() as ShadowRoot).host;
|
|
94
|
+
if (parent && !isRendered(parent, visibilityRoot)) return;
|
|
86
95
|
const data = node.textContent ?? '';
|
|
87
96
|
if (inPre) out += data;
|
|
88
97
|
else {
|
|
@@ -114,9 +123,10 @@ export function observableText(
|
|
|
114
123
|
if (block) endLine();
|
|
115
124
|
const closedDetails =
|
|
116
125
|
child.tagName === 'DETAILS' && !child.hasAttribute('open');
|
|
126
|
+
const rendered = composedChildNodes(child);
|
|
117
127
|
const children = closedDetails
|
|
118
|
-
?
|
|
119
|
-
:
|
|
128
|
+
? rendered.filter((c) => c.nodeType === 1 && !isHidden(c as Element))
|
|
129
|
+
: rendered;
|
|
120
130
|
for (const c of children) {
|
|
121
131
|
walk(c, inPre || child.tagName === 'PRE');
|
|
122
132
|
}
|
|
@@ -148,14 +158,16 @@ export function observableErrors(
|
|
|
148
158
|
visibilityRoot?: Element,
|
|
149
159
|
): string[] {
|
|
150
160
|
const left = (e: Element) => {
|
|
151
|
-
for (let at: Element | null = e; at; at = at
|
|
161
|
+
for (let at: Element | null = e; at; at = composedParent(at)) {
|
|
152
162
|
if (isHidden(at)) return true;
|
|
153
163
|
if (at === el) return false;
|
|
154
164
|
if (skip && at.matches(skip)) return true;
|
|
155
165
|
}
|
|
156
166
|
return false;
|
|
157
167
|
};
|
|
158
|
-
return
|
|
159
|
-
.filter(
|
|
168
|
+
return [...composedElements(el)]
|
|
169
|
+
.filter(
|
|
170
|
+
(e) => e.matches('.error') && !left(e) && isRendered(e, visibilityRoot),
|
|
171
|
+
)
|
|
160
172
|
.map((e) => observableText(e, skip, visibilityRoot).trim());
|
|
161
173
|
}
|
package/src/parser.ts
CHANGED
|
@@ -136,12 +136,12 @@ const escapeTextReferences = (text: string) =>
|
|
|
136
136
|
|
|
137
137
|
/**
|
|
138
138
|
* Elements whose text the renderer shows as written, with no character
|
|
139
|
-
* references decoded:
|
|
139
|
+
* references decoded: those it treats as raw text (`style`, `script`, see
|
|
140
140
|
* HtmlNodeRenderer), for which the browser did not decode any either.
|
|
141
141
|
*/
|
|
142
142
|
function isLiteralTextElement(parent: ParentNode): boolean {
|
|
143
143
|
return (
|
|
144
|
-
(parent as Element).localName
|
|
144
|
+
['style', 'script'].includes((parent as Element).localName) &&
|
|
145
145
|
(parent as Element).namespaceURI === HTML_NAMESPACE
|
|
146
146
|
);
|
|
147
147
|
}
|
package/src/registry.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { ComponentType } from 'preact';
|
|
2
2
|
import type { ASTNode, Branch } from './markup/ast';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
effectiveNamedDefinitions,
|
|
5
|
+
NameMap,
|
|
6
|
+
NameSet,
|
|
7
|
+
} from './utils/macro-names';
|
|
4
8
|
|
|
5
9
|
export interface MacroProps {
|
|
6
10
|
rawArgs: string;
|
|
@@ -250,20 +254,21 @@ export function registerMacroMetadata(
|
|
|
250
254
|
}
|
|
251
255
|
}
|
|
252
256
|
|
|
253
|
-
/** The parents declaring a branching companion (
|
|
257
|
+
/** The parents declaring a branching companion (input options are values). */
|
|
254
258
|
export function branchParents(
|
|
255
259
|
name: string,
|
|
256
260
|
macros?: readonly Pick<MacroMetadata, 'name' | 'subMacros'>[],
|
|
257
261
|
): string[] {
|
|
258
262
|
if (!macros) {
|
|
259
263
|
return [...(companionParents.get(name) ?? [])].filter(
|
|
260
|
-
(parent) => parent !== 'listbox',
|
|
264
|
+
(parent) => parent !== 'listbox' && parent !== 'cycle',
|
|
261
265
|
);
|
|
262
266
|
}
|
|
263
|
-
return macros
|
|
267
|
+
return effectiveNamedDefinitions(macros)
|
|
264
268
|
.filter(
|
|
265
269
|
(macro) =>
|
|
266
270
|
macro.name.toLowerCase() !== 'listbox' &&
|
|
271
|
+
macro.name.toLowerCase() !== 'cycle' &&
|
|
267
272
|
macro.subMacros.some((sub) => sub.toLowerCase() === name.toLowerCase()),
|
|
268
273
|
)
|
|
269
274
|
.map((macro) => macro.name.toLowerCase());
|
package/src/story-api.ts
CHANGED
|
@@ -275,11 +275,7 @@ function setOne(draft: VariableNamespaces, name: string, value: unknown): void {
|
|
|
275
275
|
const { isTransient, key } = parseName(name);
|
|
276
276
|
const namespace = isTransient ? draft.transient : draft.variables;
|
|
277
277
|
|
|
278
|
-
|
|
279
|
-
setByPath(namespace, key.split('.'), value);
|
|
280
|
-
} else {
|
|
281
|
-
namespace[key] = value;
|
|
282
|
-
}
|
|
278
|
+
setByPath(namespace, key.split('.'), value);
|
|
283
279
|
}
|
|
284
280
|
|
|
285
281
|
/** A method calling the store action `name`, as the store holds it then. */
|
package/src/story-variables.ts
CHANGED
|
@@ -11,8 +11,10 @@ import { errorMessage } from './utils/error-message';
|
|
|
11
11
|
import {
|
|
12
12
|
CodeSyntaxError,
|
|
13
13
|
lexJs,
|
|
14
|
+
readMemberPath,
|
|
14
15
|
scanStringLiteral,
|
|
15
16
|
type JsGoal,
|
|
17
|
+
type VariableRef,
|
|
16
18
|
} from './js-lexer';
|
|
17
19
|
import {
|
|
18
20
|
argPieces,
|
|
@@ -88,9 +90,6 @@ function declarationRegex(sigil: string): RegExp {
|
|
|
88
90
|
const escaped = sigil.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
89
91
|
return new RegExp(`^${escaped}(\\w+)\\s*=\\s*(.+)$`);
|
|
90
92
|
}
|
|
91
|
-
/** A `$name` or `%name` reference with its dot path, at a sigil the lexer found. */
|
|
92
|
-
const VAR_PATH_RE = /([$%])(\w+(?:\.\w+)*)/y;
|
|
93
|
-
|
|
94
93
|
const VALID_VAR_TYPES = new Set<string>(['number', 'string', 'boolean']);
|
|
95
94
|
|
|
96
95
|
/** Boxed sample values whose members a primitive of each type can access. */
|
|
@@ -524,9 +523,6 @@ type Emit = (ref: VariableReference) => void;
|
|
|
524
523
|
|
|
525
524
|
const NO_STORE_VAR_MACROS: ReadonlySet<string> = new Set();
|
|
526
525
|
|
|
527
|
-
/** A `.field` path written right after a reference, which may be empty. */
|
|
528
|
-
const FIELD_PATH_RE = /(?:\.\w+)*/y;
|
|
529
|
-
|
|
530
526
|
/**
|
|
531
527
|
* The reference to variable `name`, which JavaScript reads at `[start, end)`
|
|
532
528
|
* of `code` (the sigil and the name as written, which may be spelled with
|
|
@@ -534,39 +530,21 @@ const FIELD_PATH_RE = /(?:\.\w+)*/y;
|
|
|
534
530
|
*/
|
|
535
531
|
function referenceAt(
|
|
536
532
|
code: string,
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
533
|
+
{
|
|
534
|
+
sigil,
|
|
535
|
+
name,
|
|
536
|
+
start,
|
|
537
|
+
end,
|
|
538
|
+
}: Pick<VariableRef, 'sigil' | 'name' | 'start' | 'end'>,
|
|
541
539
|
at: At,
|
|
542
540
|
): VariableReference {
|
|
543
|
-
|
|
544
|
-
const fields = FIELD_PATH_RE.exec(code)![0];
|
|
541
|
+
const fields = readMemberPath(code, end);
|
|
545
542
|
return {
|
|
546
543
|
sigil: sigil as '$' | '%',
|
|
547
544
|
name,
|
|
548
|
-
path: fields
|
|
545
|
+
path: fields.path,
|
|
549
546
|
start: at(start),
|
|
550
|
-
end: at(
|
|
551
|
-
};
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
/** A reference written at `index` of `term`, a `variable` parameter as written. */
|
|
555
|
-
function termReferenceAt(
|
|
556
|
-
term: string,
|
|
557
|
-
index: number,
|
|
558
|
-
at: At,
|
|
559
|
-
): VariableReference | undefined {
|
|
560
|
-
VAR_PATH_RE.lastIndex = index;
|
|
561
|
-
const match = VAR_PATH_RE.exec(term);
|
|
562
|
-
if (!match) return undefined;
|
|
563
|
-
const [name, ...path] = match[2]!.split('.');
|
|
564
|
-
return {
|
|
565
|
-
sigil: match[1] as '$' | '%',
|
|
566
|
-
name: name!,
|
|
567
|
-
path,
|
|
568
|
-
start: at(index),
|
|
569
|
-
end: at(index + match[0].length),
|
|
547
|
+
end: at(fields.end),
|
|
570
548
|
};
|
|
571
549
|
}
|
|
572
550
|
|
|
@@ -618,7 +596,7 @@ function scanCode(
|
|
|
618
596
|
}
|
|
619
597
|
for (const ref of parsed.refs) {
|
|
620
598
|
if (ref.sigil !== '$' && ref.sigil !== '%') continue;
|
|
621
|
-
emit(referenceAt(code, ref
|
|
599
|
+
emit(referenceAt(code, ref, at));
|
|
622
600
|
}
|
|
623
601
|
}
|
|
624
602
|
|
|
@@ -634,7 +612,7 @@ function scanCodeLeniently(
|
|
|
634
612
|
{
|
|
635
613
|
variable(sigil, name, index, _nesting, end) {
|
|
636
614
|
if (sigil !== '$' && sigil !== '%') return;
|
|
637
|
-
emit(referenceAt(code, sigil, name, index, end, at));
|
|
615
|
+
emit(referenceAt(code, { sigil, name, start: index, end }, at));
|
|
638
616
|
},
|
|
639
617
|
// Strings, regex literals and comments hold no references
|
|
640
618
|
literal() {},
|
|
@@ -682,8 +660,8 @@ function scanArgs(
|
|
|
682
660
|
const params = parametersOf(token.name);
|
|
683
661
|
let byParameters = false;
|
|
684
662
|
if (params) {
|
|
685
|
-
if (
|
|
686
|
-
scanVariableParameters(token, params, at, emit);
|
|
663
|
+
if (!options.storeVarMacros.has(token.name.toLowerCase())) {
|
|
664
|
+
scanVariableParameters(token, params, at, emit, options.all);
|
|
687
665
|
}
|
|
688
666
|
const pieces = [
|
|
689
667
|
...argPieces(token.rawArgs, token.argsStart, params, token.name, ''),
|
|
@@ -709,14 +687,15 @@ function scanArgs(
|
|
|
709
687
|
|
|
710
688
|
/**
|
|
711
689
|
* Report the variables the `variable` parameters of a macro name
|
|
712
|
-
* (`{unset $x}`, `{computed $x = …}`)
|
|
713
|
-
*
|
|
690
|
+
* (`{unset $x}`, `{computed $x = …}`). Computed members read variables even
|
|
691
|
+
* when the receiver itself is only included in the complete editor list.
|
|
714
692
|
*/
|
|
715
693
|
function scanVariableParameters(
|
|
716
694
|
token: Extract<Token, { type: 'macro' }>,
|
|
717
695
|
params: readonly ParameterDef[],
|
|
718
696
|
at: At,
|
|
719
697
|
emit: Emit,
|
|
698
|
+
all: boolean | undefined,
|
|
720
699
|
): void {
|
|
721
700
|
const spans: ArgSpans = new Map();
|
|
722
701
|
try {
|
|
@@ -728,10 +707,14 @@ function scanVariableParameters(
|
|
|
728
707
|
for (const param of params) {
|
|
729
708
|
const span = spans.get(param);
|
|
730
709
|
if (param.type !== 'variable' || !span) continue;
|
|
731
|
-
const
|
|
732
|
-
|
|
710
|
+
const start = token.argsStart + span[0];
|
|
711
|
+
scanCode(
|
|
712
|
+
token.rawArgs.slice(span[0], span[1]),
|
|
713
|
+
(i) => at(start + i),
|
|
714
|
+
(ref) => {
|
|
715
|
+
if (all || ref.start !== at(start)) emit(ref);
|
|
716
|
+
},
|
|
733
717
|
);
|
|
734
|
-
if (ref) emit(ref);
|
|
735
718
|
}
|
|
736
719
|
}
|
|
737
720
|
|
package/src/tooling.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NameMap } from './utils/macro-names';
|
|
1
|
+
import { effectiveNamedDefinitions, NameMap } from './utils/macro-names';
|
|
2
2
|
export {
|
|
3
3
|
completionContext,
|
|
4
4
|
type CompletionContext,
|
|
@@ -208,11 +208,22 @@ export interface ParseMarkupOptions extends RuntimeParseMarkupOptions {
|
|
|
208
208
|
macros?: Iterable<ToolingMacro>;
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
+
/** Widgets take precedence over macros regardless of metadata input order. */
|
|
212
|
+
function effectiveToolingMacros(
|
|
213
|
+
macros: Iterable<ToolingMacro>,
|
|
214
|
+
): ToolingMacro[] {
|
|
215
|
+
const list = [...macros];
|
|
216
|
+
return effectiveNamedDefinitions([
|
|
217
|
+
...list.filter((m) => !m.widget),
|
|
218
|
+
...list.filter((m) => m.widget),
|
|
219
|
+
]);
|
|
220
|
+
}
|
|
221
|
+
|
|
211
222
|
function tokenizationOptions(
|
|
212
223
|
options: ParseMarkupOptions,
|
|
213
224
|
): RuntimeParseMarkupOptions {
|
|
214
225
|
if (!options.macros) return options;
|
|
215
|
-
const macros =
|
|
226
|
+
const macros = effectiveToolingMacros(options.macros);
|
|
216
227
|
const parametersOf = parameterLookup(macros);
|
|
217
228
|
return {
|
|
218
229
|
...options,
|
|
@@ -254,7 +265,7 @@ export function validateStoryMarkup(
|
|
|
254
265
|
options: ValidateMarkupOptions = {},
|
|
255
266
|
): MarkupDiagnostic[] {
|
|
256
267
|
const list = [...passages];
|
|
257
|
-
const all =
|
|
268
|
+
const all = effectiveToolingMacros(macros);
|
|
258
269
|
const known = new Set<string>();
|
|
259
270
|
// The calls of widgets that declare what their arguments hold are checked
|
|
260
271
|
const widgets = effectiveWidgetDefinitions(
|
|
@@ -313,7 +324,7 @@ export function passagePieces(
|
|
|
313
324
|
source: string,
|
|
314
325
|
macros: Iterable<ToolingMacro> = [],
|
|
315
326
|
): Piece[] {
|
|
316
|
-
const all =
|
|
327
|
+
const all = effectiveToolingMacros(macros);
|
|
317
328
|
const parametersOf = parameterLookup(all);
|
|
318
329
|
return passagePiecesOfTokens(
|
|
319
330
|
source,
|
|
@@ -337,7 +348,7 @@ export function passagePiecesOfTokens(
|
|
|
337
348
|
macros: Iterable<ToolingMacro>,
|
|
338
349
|
tokens: readonly Token[],
|
|
339
350
|
): Piece[] {
|
|
340
|
-
const all =
|
|
351
|
+
const all = effectiveToolingMacros(macros);
|
|
341
352
|
return [
|
|
342
353
|
...passagePiecesOf(source, tokens, parameterLookup(all), {
|
|
343
354
|
isBlock: blockLookup(all),
|
|
@@ -8,6 +8,31 @@ export function composedParent(el: Element): Element | null {
|
|
|
8
8
|
);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
/** Rendered child nodes: shadow content, slot assignments, or fallback. */
|
|
12
|
+
export function composedChildNodes(root: ParentNode): Node[] {
|
|
13
|
+
if ((root as Element).localName === 'slot') {
|
|
14
|
+
// Visit nested slots themselves so their visibility and filters apply.
|
|
15
|
+
const assigned = (root as HTMLSlotElement).assignedNodes();
|
|
16
|
+
if (assigned.length) return assigned;
|
|
17
|
+
}
|
|
18
|
+
return Array.from(((root as Element).shadowRoot ?? root).childNodes);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The elements among a container's rendered child nodes. */
|
|
22
|
+
export function composedChildren(root: ParentNode): Element[] {
|
|
23
|
+
return composedChildNodes(root).filter(
|
|
24
|
+
(node): node is Element => node.nodeType === 1,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Authored elements in rendered order; unassigned light DOM is omitted. */
|
|
29
|
+
export function* composedElements(root: ParentNode): Generator<HTMLElement> {
|
|
30
|
+
for (const el of composedChildren(root)) {
|
|
31
|
+
yield el as HTMLElement;
|
|
32
|
+
yield* composedElements(el);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
11
36
|
/** Elements whose content is never text the reader reads. */
|
|
12
37
|
const NOT_TEXT = new Set(['SCRIPT', 'STYLE', 'TEMPLATE', 'NOSCRIPT']);
|
|
13
38
|
|
|
@@ -65,6 +90,16 @@ export function isRendered(el: Element, root?: Element): boolean {
|
|
|
65
90
|
for (let e: Element | null = el; e; e = composedParent(e)) {
|
|
66
91
|
if (style(e)?.display === 'none') return false;
|
|
67
92
|
if (e === root) break;
|
|
93
|
+
const parent = e.parentElement;
|
|
94
|
+
// Connected light DOM may have no place in its host's rendered tree.
|
|
95
|
+
if (parent?.shadowRoot && !e.assignedSlot) return false;
|
|
96
|
+
// Slot fallback disappears whenever any light-DOM nodes are assigned.
|
|
97
|
+
if (
|
|
98
|
+
parent?.localName === 'slot' &&
|
|
99
|
+
(parent as HTMLSlotElement).assignedNodes().length > 0 &&
|
|
100
|
+
e.assignedSlot !== parent
|
|
101
|
+
)
|
|
102
|
+
return false;
|
|
68
103
|
}
|
|
69
104
|
return !inClosedDetails(el, root);
|
|
70
105
|
}
|
package/src/utils/macro-names.ts
CHANGED
|
@@ -18,6 +18,15 @@ export class NameMap<V> extends Map<string, V> {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
/** The latest definition of each name, using runtime replacement precedence. */
|
|
22
|
+
export function effectiveNamedDefinitions<T extends { name: string }>(
|
|
23
|
+
definitions: Iterable<T>,
|
|
24
|
+
): T[] {
|
|
25
|
+
const latest = new NameMap<T>();
|
|
26
|
+
for (const definition of definitions) latest.set(definition.name, definition);
|
|
27
|
+
return [...latest.values()];
|
|
28
|
+
}
|
|
29
|
+
|
|
21
30
|
/** A set of macro or widget names (see NameMap). */
|
|
22
31
|
export class NameSet {
|
|
23
32
|
private readonly names = new NameMap<true>();
|