@rohal12/spindle 0.62.1 → 0.62.2
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 +180 -59
- package/dist/pkg/story-variables.js +111 -44
- package/package.json +1 -1
- package/src/code-check.ts +24 -9
- package/src/format/source-lines.ts +4 -14
- package/src/hooks/use-modal-focus.ts +140 -46
- package/src/index.tsx +29 -10
- package/src/js-bindings.ts +29 -3
- package/src/macro-discovery.ts +7 -3
- package/src/markup/ast.ts +11 -6
- package/src/markup/line-column.ts +19 -0
- package/src/markup/pair.ts +4 -2
- package/src/markup/parse.ts +11 -5
- package/src/markup/validate.ts +30 -3
- package/src/story-variables.ts +14 -3
- package/src/tooling.ts +10 -3
|
@@ -17,18 +17,58 @@ const FOCUSABLE = [
|
|
|
17
17
|
'[tabindex]',
|
|
18
18
|
].join(',');
|
|
19
19
|
|
|
20
|
+
/** The parent in the rendered tree, including slots and shadow hosts. */
|
|
21
|
+
function composedParent(el: Element): Element | null {
|
|
22
|
+
return (
|
|
23
|
+
el.assignedSlot ??
|
|
24
|
+
el.parentElement ??
|
|
25
|
+
(el.getRootNode() as ShadowRoot).host ??
|
|
26
|
+
null
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function composedChildren(root: ParentNode): Element[] {
|
|
31
|
+
if ((root as Element).localName === 'slot') {
|
|
32
|
+
const assigned = (root as HTMLSlotElement).assignedNodes({
|
|
33
|
+
flatten: true,
|
|
34
|
+
});
|
|
35
|
+
return assigned.filter((node): node is Element => node.nodeType === 1);
|
|
36
|
+
}
|
|
37
|
+
return Array.from(root.children);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Authored elements in rendered order; unassigned light DOM is not rendered. */
|
|
41
|
+
function* composedElements(root: ParentNode): Generator<HTMLElement> {
|
|
42
|
+
for (const el of composedChildren(root)) {
|
|
43
|
+
yield el as HTMLElement;
|
|
44
|
+
yield* composedElements(el.shadowRoot ?? el);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
20
48
|
/**
|
|
21
49
|
* Whether `el` can be focused at all: shown, rendered, enabled (also by its
|
|
22
50
|
* fieldset) and not inert.
|
|
23
51
|
*/
|
|
24
52
|
function isAvailable(el: HTMLElement): boolean {
|
|
25
|
-
return
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
53
|
+
if (!isRendered(el)) return false;
|
|
54
|
+
// Visibility and inertness of a host apply to its whole shadow tree too.
|
|
55
|
+
for (let at: Element | null = el; at; at = composedParent(at)) {
|
|
56
|
+
if (at.hasAttribute('hidden') || at.hasAttribute('inert')) return false;
|
|
57
|
+
if (at.ownerDocument.defaultView?.getComputedStyle(at).display === 'none')
|
|
58
|
+
return false;
|
|
59
|
+
const parent = composedParent(at);
|
|
60
|
+
if (
|
|
61
|
+
parent?.localName === 'details' &&
|
|
62
|
+
!parent.hasAttribute('open') &&
|
|
63
|
+
at !==
|
|
64
|
+
Array.from(parent.children).find(
|
|
65
|
+
(child) => child.localName === 'summary',
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
// Also covers controls disabled by a <fieldset disabled> ancestor
|
|
71
|
+
return !el.matches(':disabled');
|
|
32
72
|
}
|
|
33
73
|
|
|
34
74
|
/** The `tabindex` of `el` as an integer, or null when absent or invalid. */
|
|
@@ -96,17 +136,40 @@ function isTabStop(el: HTMLElement): boolean {
|
|
|
96
136
|
* others in document order.
|
|
97
137
|
*/
|
|
98
138
|
function focusables(root: ParentNode): HTMLElement[] {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
139
|
+
// Each shadow host/slot owns a separate tabindex scope. Sort the owners in
|
|
140
|
+
// this scope, then expand their contents in place, as native Tab does.
|
|
141
|
+
const scopes: { el: HTMLElement; stops: HTMLElement[] }[] = [];
|
|
142
|
+
const collect = (parent: ParentNode) => {
|
|
143
|
+
for (const element of composedChildren(parent)) {
|
|
144
|
+
const el = element as HTMLElement;
|
|
145
|
+
const nested = el.shadowRoot ?? (el.localName === 'slot' ? el : null);
|
|
146
|
+
const stop = el.matches(FOCUSABLE) && isAvailable(el) && isTabStop(el);
|
|
147
|
+
if (nested) {
|
|
148
|
+
// A visibility:hidden host can contain explicitly visible controls.
|
|
149
|
+
// Each stop checks its own visibility and its ancestors' subtree gates.
|
|
150
|
+
if ((tabIndexAttr(el) ?? 0) < 0) continue;
|
|
151
|
+
scopes.push({
|
|
152
|
+
el,
|
|
153
|
+
stops: [
|
|
154
|
+
...(stop && !el.shadowRoot?.delegatesFocus ? [el] : []),
|
|
155
|
+
...focusables(nested),
|
|
156
|
+
],
|
|
157
|
+
});
|
|
158
|
+
} else {
|
|
159
|
+
if (stop) scopes.push({ el, stops: [el] });
|
|
160
|
+
collect(el);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
collect(root);
|
|
102
165
|
const positive = (el: HTMLElement) => Math.max(tabIndexAttr(el) ?? 0, 0);
|
|
103
166
|
// Array.prototype.sort is stable: equal tabindexes keep document order
|
|
104
167
|
return [
|
|
105
|
-
...
|
|
106
|
-
.filter((el) => positive(el) > 0)
|
|
107
|
-
.sort((a, b) => positive(a) - positive(b)),
|
|
108
|
-
...
|
|
109
|
-
];
|
|
168
|
+
...scopes
|
|
169
|
+
.filter(({ el }) => positive(el) > 0)
|
|
170
|
+
.sort((a, b) => positive(a.el) - positive(b.el)),
|
|
171
|
+
...scopes.filter(({ el }) => positive(el) === 0),
|
|
172
|
+
].flatMap(({ stops }) => stops);
|
|
110
173
|
}
|
|
111
174
|
|
|
112
175
|
/** An element that can take focus: HTML or SVG, of this realm or another. */
|
|
@@ -174,6 +237,23 @@ function frameDocument(frame: HTMLIFrameElement): Document | null {
|
|
|
174
237
|
}
|
|
175
238
|
}
|
|
176
239
|
|
|
240
|
+
/** Focus an element, entering accessible frames at the appropriate edge. */
|
|
241
|
+
function enter(el: HTMLElement, backwards: boolean): void {
|
|
242
|
+
if (isFrame(el)) {
|
|
243
|
+
const doc = frameDocument(el);
|
|
244
|
+
const inner = doc ? focusables(doc) : [];
|
|
245
|
+
const edge = backwards ? inner[inner.length - 1] : inner[0];
|
|
246
|
+
if (edge) return enter(edge, backwards);
|
|
247
|
+
}
|
|
248
|
+
el.focus();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** document.activeElement exposes a shadow host; follow it to the control. */
|
|
252
|
+
function activeElement(root: Document | ShadowRoot): Element | null {
|
|
253
|
+
const el = root.activeElement;
|
|
254
|
+
return el?.shadowRoot?.activeElement ? activeElement(el.shadowRoot) : el;
|
|
255
|
+
}
|
|
256
|
+
|
|
177
257
|
/** The iframe element that holds `doc`, null for the top document. */
|
|
178
258
|
function frameOf(doc: Document): HTMLElement | null {
|
|
179
259
|
try {
|
|
@@ -196,10 +276,11 @@ function nextStop(
|
|
|
196
276
|
): HTMLElement | undefined {
|
|
197
277
|
const at = from ? items.indexOf(from as HTMLElement) : -1;
|
|
198
278
|
if (at !== -1) return items[at + (backwards ? -1 : 1)];
|
|
199
|
-
|
|
279
|
+
const order = from ? [...composedElements(root)] : [];
|
|
280
|
+
const position = order.indexOf(from as HTMLElement);
|
|
281
|
+
if (position !== -1) {
|
|
200
282
|
// Inside the root but not a tab stop: continue from its position
|
|
201
|
-
const after = (el: HTMLElement) =>
|
|
202
|
-
!!(from.compareDocumentPosition(el) & Node.DOCUMENT_POSITION_FOLLOWING);
|
|
283
|
+
const after = (el: HTMLElement) => order.indexOf(el) > position;
|
|
203
284
|
return backwards
|
|
204
285
|
? items.filter((el) => !after(el)).pop()
|
|
205
286
|
: items.find(after);
|
|
@@ -217,13 +298,17 @@ function nextStop(
|
|
|
217
298
|
*/
|
|
218
299
|
function activeChain(): OpenerChain {
|
|
219
300
|
const chain: OpenerChain = [];
|
|
220
|
-
let
|
|
221
|
-
while (
|
|
222
|
-
const el: Element | null =
|
|
223
|
-
if (!el || (chain.length > 0 && el ===
|
|
301
|
+
let root: Document | ShadowRoot | null = document;
|
|
302
|
+
while (root) {
|
|
303
|
+
const el: Element | null = root.activeElement;
|
|
304
|
+
if (!el || (chain.length > 0 && el === (root as Document).body)) break;
|
|
224
305
|
if (typeof (el as HTMLElement).focus !== 'function') break;
|
|
225
306
|
chain.push(el as Focusable);
|
|
226
|
-
|
|
307
|
+
root = el.shadowRoot?.activeElement
|
|
308
|
+
? el.shadowRoot
|
|
309
|
+
: isFrame(el)
|
|
310
|
+
? frameDocument(el)
|
|
311
|
+
: null;
|
|
227
312
|
}
|
|
228
313
|
return chain;
|
|
229
314
|
}
|
|
@@ -261,12 +346,12 @@ export function useModalFocus(
|
|
|
261
346
|
// An autofocus element that can't take focus (hidden, disabled, inert)
|
|
262
347
|
// must not shadow the eligible controls (#426).
|
|
263
348
|
const initial =
|
|
264
|
-
|
|
265
|
-
isAvailable,
|
|
349
|
+
[...composedElements(panel)].find(
|
|
350
|
+
(el) => el.hasAttribute('autofocus') && isAvailable(el),
|
|
266
351
|
) ??
|
|
267
352
|
focusables(body)[0] ??
|
|
268
353
|
panel;
|
|
269
|
-
initial
|
|
354
|
+
enter(initial, false);
|
|
270
355
|
|
|
271
356
|
return () => {
|
|
272
357
|
const i = openModals.indexOf(entry);
|
|
@@ -296,19 +381,6 @@ export function useModalFocus(
|
|
|
296
381
|
|
|
297
382
|
const top = () => openModals[openModals.length - 1]?.panel === panel;
|
|
298
383
|
|
|
299
|
-
/** Focus `el`; a frame is entered at its first (or last) control (#460). */
|
|
300
|
-
const enter = (el: HTMLElement, backwards: boolean) => {
|
|
301
|
-
if (isFrame(el)) {
|
|
302
|
-
const doc = frameDocument(el);
|
|
303
|
-
const inner = doc ? focusables(doc) : [];
|
|
304
|
-
const edge = backwards ? inner[inner.length - 1] : inner[0];
|
|
305
|
-
// A frame inside the frame is entered in turn
|
|
306
|
-
if (edge) return enter(edge, backwards);
|
|
307
|
-
// Not accessible or without controls: the frame itself takes focus
|
|
308
|
-
}
|
|
309
|
-
el.focus();
|
|
310
|
-
};
|
|
311
|
-
|
|
312
384
|
/**
|
|
313
385
|
* Move focus to the next (or previous) modal control from `from`. The
|
|
314
386
|
* order is computed here, never left to the browser: its positive-tabindex
|
|
@@ -365,7 +437,7 @@ export function useModalFocus(
|
|
|
365
437
|
}
|
|
366
438
|
|
|
367
439
|
if (e.key !== 'Tab') return;
|
|
368
|
-
const active = doc
|
|
440
|
+
const active = activeElement(doc);
|
|
369
441
|
if (frame) {
|
|
370
442
|
steppingNative = null;
|
|
371
443
|
const inner = focusables(doc);
|
|
@@ -396,7 +468,7 @@ export function useModalFocus(
|
|
|
396
468
|
// Focus that lands outside the dialog anyway, e.g. leaving a frame whose
|
|
397
469
|
// document can't be reached, is brought back in.
|
|
398
470
|
const onFocusIn = (e: FocusEvent) => {
|
|
399
|
-
const target = e.target;
|
|
471
|
+
const target = e.composedPath()[0] ?? e.target;
|
|
400
472
|
if (steppingNative && target !== steppingNative.el && top()) {
|
|
401
473
|
// Focus left the media controls: on to the modal's next control
|
|
402
474
|
const { from, backwards } = steppingNative;
|
|
@@ -404,7 +476,14 @@ export function useModalFocus(
|
|
|
404
476
|
moveFocus(from, backwards);
|
|
405
477
|
return;
|
|
406
478
|
}
|
|
407
|
-
if (
|
|
479
|
+
if (
|
|
480
|
+
!top() ||
|
|
481
|
+
!(target instanceof Node) ||
|
|
482
|
+
(target instanceof Element &&
|
|
483
|
+
[...composedElements(panel)].includes(target as HTMLElement)) ||
|
|
484
|
+
panel.contains(target)
|
|
485
|
+
)
|
|
486
|
+
return;
|
|
408
487
|
if (target === document.body || target === document.documentElement) {
|
|
409
488
|
return;
|
|
410
489
|
}
|
|
@@ -416,7 +495,12 @@ export function useModalFocus(
|
|
|
416
495
|
// modal's next control takes it at once.
|
|
417
496
|
const onFocusOut = (e: FocusEvent) => {
|
|
418
497
|
const pending = steppingNative;
|
|
419
|
-
if (
|
|
498
|
+
if (
|
|
499
|
+
!pending ||
|
|
500
|
+
(e.composedPath()[0] ?? e.target) !== pending.el ||
|
|
501
|
+
e.relatedTarget ||
|
|
502
|
+
!top()
|
|
503
|
+
) {
|
|
420
504
|
return;
|
|
421
505
|
}
|
|
422
506
|
steppingNative = null;
|
|
@@ -436,6 +520,7 @@ export function useModalFocus(
|
|
|
436
520
|
};
|
|
437
521
|
const frameDocs = new Map<HTMLIFrameElement, Document>();
|
|
438
522
|
const observers: MutationObserver[] = [];
|
|
523
|
+
const watched = new WeakSet<Node>();
|
|
439
524
|
const attachFrame = (frame: HTMLIFrameElement) => {
|
|
440
525
|
const doc = frameDocument(frame);
|
|
441
526
|
// Already listening to this document
|
|
@@ -443,12 +528,21 @@ export function useModalFocus(
|
|
|
443
528
|
frameDocs.set(frame, doc);
|
|
444
529
|
listen(doc, frame);
|
|
445
530
|
watchFrames(doc);
|
|
531
|
+
// Initial focus may have reached the frame before its document loaded.
|
|
532
|
+
const chain = activeChain();
|
|
533
|
+
if (top() && chain[chain.length - 1] === frame) enter(frame, false);
|
|
446
534
|
};
|
|
447
535
|
// Attach to the frames in `root`, now and as they come and navigate.
|
|
448
536
|
// load doesn't bubble; a frame's document is replaced when it navigates
|
|
449
|
-
const watchFrames = (root: HTMLElement | Document) => {
|
|
450
|
-
|
|
451
|
-
|
|
537
|
+
const watchFrames = (root: HTMLElement | Document | ShadowRoot) => {
|
|
538
|
+
if (watched.has(root)) return;
|
|
539
|
+
watched.add(root);
|
|
540
|
+
const attachAll = () => {
|
|
541
|
+
for (const el of composedElements(root)) {
|
|
542
|
+
if (el.shadowRoot) watchFrames(el.shadowRoot);
|
|
543
|
+
if (isFrame(el)) attachFrame(el);
|
|
544
|
+
}
|
|
545
|
+
};
|
|
452
546
|
root.addEventListener(
|
|
453
547
|
'load',
|
|
454
548
|
(e) => {
|
package/src/index.tsx
CHANGED
|
@@ -18,14 +18,18 @@ import {
|
|
|
18
18
|
extractDefaults,
|
|
19
19
|
} from './story-variables';
|
|
20
20
|
import { getMacro, getMacroRegistry, isSubMacro } from './registry';
|
|
21
|
-
import { parameterLookup } from './code-check';
|
|
21
|
+
import { parameterLookup, type ParametersOf } from './code-check';
|
|
22
22
|
import { getWidget, widgetMacros } from './widgets/widget-registry';
|
|
23
23
|
import {
|
|
24
24
|
formatDiagnostic,
|
|
25
25
|
validateMarkup,
|
|
26
26
|
type MarkupPassage,
|
|
27
27
|
} from './markup/validate';
|
|
28
|
-
import {
|
|
28
|
+
import {
|
|
29
|
+
effectiveWidgets,
|
|
30
|
+
parseWidgetDef,
|
|
31
|
+
type WidgetDefinition,
|
|
32
|
+
} from './widgets/widget-def';
|
|
29
33
|
import { parseMarkup } from './markup/parse';
|
|
30
34
|
import { setBlockWidget } from './markup/ast';
|
|
31
35
|
import { registerWidgetDefinitions } from './widgets/register-widget-def';
|
|
@@ -82,8 +86,12 @@ function orStop<T>(parse: () => T, where = ''): T {
|
|
|
82
86
|
* the declared parameters of macros and what the parameters of widgets hold.
|
|
83
87
|
* (The markup and the variable references are read by the same.)
|
|
84
88
|
*/
|
|
85
|
-
function knownParameters() {
|
|
86
|
-
return parameterLookup([
|
|
89
|
+
function knownParameters(declared: readonly WidgetDefinition[] = []) {
|
|
90
|
+
return parameterLookup([
|
|
91
|
+
...getMacroRegistry(),
|
|
92
|
+
...declared.map((widget) => ({ name: widget.name, widget })),
|
|
93
|
+
...widgetMacros(),
|
|
94
|
+
]);
|
|
87
95
|
}
|
|
88
96
|
|
|
89
97
|
/** The names of the registered macros that bind a variable (the inputs). */
|
|
@@ -102,12 +110,13 @@ function variableErrors(
|
|
|
102
110
|
storyData: StoryData,
|
|
103
111
|
schema: Parameters<typeof validatePassages>[1],
|
|
104
112
|
only: (passage: MarkupPassage) => boolean = () => true,
|
|
113
|
+
parametersOf: ParametersOf = knownParameters(),
|
|
105
114
|
): string[] {
|
|
106
115
|
return validatePassages(
|
|
107
116
|
new Map([...storyData.passages].filter(([, passage]) => only(passage))),
|
|
108
117
|
schema,
|
|
109
118
|
storeVarMacroNames(),
|
|
110
|
-
|
|
119
|
+
parametersOf,
|
|
111
120
|
);
|
|
112
121
|
}
|
|
113
122
|
|
|
@@ -119,12 +128,13 @@ function variableErrors(
|
|
|
119
128
|
function markupErrors(
|
|
120
129
|
storyData: StoryData,
|
|
121
130
|
only: (passage: MarkupPassage) => boolean,
|
|
131
|
+
parametersOf: ParametersOf = knownParameters(),
|
|
122
132
|
): string[] {
|
|
123
133
|
return validateMarkup(storyData.passages.values(), {
|
|
124
134
|
isKnownMacro: (name) =>
|
|
125
135
|
!!getMacro(name) || isSubMacro(name) || !!getWidget(name),
|
|
126
136
|
macroNames: getMacroRegistry().map((m) => m.name),
|
|
127
|
-
parametersOf
|
|
137
|
+
parametersOf,
|
|
128
138
|
only,
|
|
129
139
|
}).map(formatDiagnostic);
|
|
130
140
|
}
|
|
@@ -187,20 +197,29 @@ export function boot() {
|
|
|
187
197
|
// widgets and widget bodies using other block widgets parse correctly
|
|
188
198
|
// regardless of passage or definition order. A widget defined more than
|
|
189
199
|
// once is what its latest definition is.
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
)) {
|
|
200
|
+
const widgets = orStop(() => effectiveWidgets(storyData.passages.values()));
|
|
201
|
+
for (const { name, block } of widgets) {
|
|
193
202
|
setBlockWidget(name, block);
|
|
194
203
|
}
|
|
204
|
+
// The preflight check runs before widget bodies are registered. Read their
|
|
205
|
+
// invocation contracts now too, so a do widget's body is markup here.
|
|
206
|
+
const startupParameters = knownParameters(widgets);
|
|
195
207
|
|
|
196
208
|
const errors = variableErrors(
|
|
197
209
|
storyData,
|
|
198
210
|
schema,
|
|
199
211
|
(p) => p.name === 'StoryInit',
|
|
212
|
+
startupParameters,
|
|
200
213
|
);
|
|
201
214
|
// StoryInit's markup must be valid before it runs; the other passages are
|
|
202
215
|
// validated once it has run, as it may define macros (see below).
|
|
203
|
-
errors.push(
|
|
216
|
+
errors.push(
|
|
217
|
+
...markupErrors(
|
|
218
|
+
storyData,
|
|
219
|
+
(p) => p.name === 'StoryInit',
|
|
220
|
+
startupParameters,
|
|
221
|
+
),
|
|
222
|
+
);
|
|
204
223
|
|
|
205
224
|
// Parse StoryTransients (optional — no error if missing)
|
|
206
225
|
let transientDefaults: Record<string, unknown> = {};
|
package/src/js-bindings.ts
CHANGED
|
@@ -78,6 +78,11 @@ export class Bindings {
|
|
|
78
78
|
private readonly byOpen = new Map<number, Group>();
|
|
79
79
|
private readonly byClose = new Map<number, Group>();
|
|
80
80
|
private readonly functions = new Map<Group, boolean>();
|
|
81
|
+
private readonly functionStarts = new Map<number, number>();
|
|
82
|
+
private readonly facts = new Map<
|
|
83
|
+
ReadonlySet<string>,
|
|
84
|
+
Map<string, Binding[]>
|
|
85
|
+
>();
|
|
81
86
|
private readonly top: Scope = { start: -1, end: Infinity };
|
|
82
87
|
|
|
83
88
|
constructor(private readonly code: string) {
|
|
@@ -97,6 +102,22 @@ export class Bindings {
|
|
|
97
102
|
at: number,
|
|
98
103
|
properties: ReadonlySet<string>,
|
|
99
104
|
): number | undefined {
|
|
105
|
+
let names = this.facts.get(properties);
|
|
106
|
+
if (!names) this.facts.set(properties, (names = new Map()));
|
|
107
|
+
let bindings = names.get(name);
|
|
108
|
+
if (!bindings) {
|
|
109
|
+
bindings = this.readBindings(name, properties);
|
|
110
|
+
names.set(name, bindings);
|
|
111
|
+
}
|
|
112
|
+
const found = this.innermostVisible(bindings, at);
|
|
113
|
+
return found.length === 1 ? found[0]!.object : undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Declaration and write facts are immutable for this source and property set. */
|
|
117
|
+
private readBindings(
|
|
118
|
+
name: string,
|
|
119
|
+
properties: ReadonlySet<string>,
|
|
120
|
+
): Binding[] {
|
|
100
121
|
const { code } = this;
|
|
101
122
|
const escaped = name.replace(/\$/g, '\\$');
|
|
102
123
|
const bindings: Binding[] = [];
|
|
@@ -114,8 +135,7 @@ export class Bindings {
|
|
|
114
135
|
this.innermostVisible(declarations, b.at)[0]?.scope ?? this.top;
|
|
115
136
|
}
|
|
116
137
|
}
|
|
117
|
-
|
|
118
|
-
return found.length === 1 ? found[0]!.object : undefined;
|
|
138
|
+
return bindings;
|
|
119
139
|
}
|
|
120
140
|
|
|
121
141
|
/**
|
|
@@ -213,9 +233,15 @@ export class Bindings {
|
|
|
213
233
|
|
|
214
234
|
/** The start of the function that has `index` in its body, or -1. */
|
|
215
235
|
private functionStart(index: number): number {
|
|
236
|
+
const known = this.functionStarts.get(index);
|
|
237
|
+
if (known !== undefined) return known;
|
|
216
238
|
for (let g = this.innermost(index, '{'); g; g = g.parent) {
|
|
217
|
-
if (g.kind === '{' && this.isFunctionBody(g))
|
|
239
|
+
if (g.kind === '{' && this.isFunctionBody(g)) {
|
|
240
|
+
this.functionStarts.set(index, g.open);
|
|
241
|
+
return g.open;
|
|
242
|
+
}
|
|
218
243
|
}
|
|
244
|
+
this.functionStarts.set(index, -1);
|
|
219
245
|
return -1;
|
|
220
246
|
}
|
|
221
247
|
|
package/src/macro-discovery.ts
CHANGED
|
@@ -329,6 +329,7 @@ function macroOf(config: Datum): DiscoveredMacro | undefined {
|
|
|
329
329
|
export function discoverMacros(source: string): DiscoveredMacro[] {
|
|
330
330
|
const { code, regexes } = codeOnly(source);
|
|
331
331
|
const found: DiscoveredMacro[] = [];
|
|
332
|
+
const configs = new Map<number, Datum | undefined>();
|
|
332
333
|
let bindings: Bindings | undefined;
|
|
333
334
|
for (const call of code.matchAll(CALL_RE)) {
|
|
334
335
|
let at = call.index + call[0].length;
|
|
@@ -340,9 +341,12 @@ export function discoverMacros(source: string): DiscoveredMacro[] {
|
|
|
340
341
|
if (object === undefined) continue;
|
|
341
342
|
at = object;
|
|
342
343
|
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
344
|
+
if (!configs.has(at)) {
|
|
345
|
+
const reader = new ConfigReader(source, { goal: 'statements', regexes });
|
|
346
|
+
reader.i = at;
|
|
347
|
+
configs.set(at, reader.datum());
|
|
348
|
+
}
|
|
349
|
+
const config = configs.get(at);
|
|
346
350
|
const macro = config && macroOf(config);
|
|
347
351
|
if (macro) found.push(macro);
|
|
348
352
|
}
|
package/src/markup/ast.ts
CHANGED
|
@@ -71,6 +71,8 @@ const BLOCK_MACROS = new NameSet([
|
|
|
71
71
|
* definition is, whereas the macros in BLOCK_MACROS stay blocks.
|
|
72
72
|
*/
|
|
73
73
|
const BLOCK_WIDGETS = new NameSet();
|
|
74
|
+
/** All widgets, including inline ones, override built-in parsing semantics. */
|
|
75
|
+
const WIDGETS = new NameSet();
|
|
74
76
|
|
|
75
77
|
/** Classification owned by defineMacro, independent of explicit registrations. */
|
|
76
78
|
const DEFINED_BLOCK_MACROS = new NameSet();
|
|
@@ -89,8 +91,8 @@ function setClassification(names: NameSet, name: string, block: boolean): void {
|
|
|
89
91
|
const RAW_BODY_MACROS = new Set(['do']);
|
|
90
92
|
|
|
91
93
|
/** Whether a macro's body is JavaScript, kept verbatim (`{do}`). */
|
|
92
|
-
export function isRawMacro(name: string): boolean {
|
|
93
|
-
return RAW_BODY_MACROS.has(name);
|
|
94
|
+
export function isRawMacro(name: string, widget = WIDGETS.has(name)): boolean {
|
|
95
|
+
return RAW_BODY_MACROS.has(name.toLowerCase()) && !widget;
|
|
94
96
|
}
|
|
95
97
|
|
|
96
98
|
/** Whether a macro takes a body closed by `{/name}`. */
|
|
@@ -112,12 +114,14 @@ export function registerBlockMacro(name: string): void {
|
|
|
112
114
|
* does. A block macro registered under the same name stays a block.
|
|
113
115
|
*/
|
|
114
116
|
export function setBlockWidget(name: string, isBlock: boolean): void {
|
|
117
|
+
WIDGETS.add(name);
|
|
115
118
|
setClassification(BLOCK_WIDGETS, name, isBlock);
|
|
116
119
|
}
|
|
117
120
|
|
|
118
121
|
/** Forget which widgets take a body (see clearWidgets). */
|
|
119
122
|
export function clearBlockWidgets(): void {
|
|
120
123
|
BLOCK_WIDGETS.clear();
|
|
124
|
+
WIDGETS.clear();
|
|
121
125
|
}
|
|
122
126
|
|
|
123
127
|
/** Unregister a custom block macro (for test cleanup). */
|
|
@@ -180,7 +184,8 @@ function astNode({ token, body }: PairedNode): ASTNode {
|
|
|
180
184
|
};
|
|
181
185
|
if (!body) return withSelectors(node, token);
|
|
182
186
|
const children = buildAst(body.children);
|
|
183
|
-
|
|
187
|
+
const branching = BRANCHING.has(name) && !WIDGETS.has(name);
|
|
188
|
+
if (!branching && body.branches.length === 0) {
|
|
184
189
|
node.children = children;
|
|
185
190
|
return withSelectors(node, token);
|
|
186
191
|
}
|
|
@@ -193,14 +198,14 @@ function astNode({ token, body }: PairedNode): ASTNode {
|
|
|
193
198
|
{ rawArgs: tag.rawArgs, children: branchChildren },
|
|
194
199
|
tag,
|
|
195
200
|
);
|
|
196
|
-
if (!
|
|
201
|
+
if (!branching) node.children = children;
|
|
197
202
|
node.branches = [
|
|
198
|
-
...(
|
|
203
|
+
...(branching ? [branch(token, children)] : []),
|
|
199
204
|
...body.branches.map(({ tag, children: c }: PairedBranch) =>
|
|
200
205
|
branch(tag, buildAst(c)),
|
|
201
206
|
),
|
|
202
207
|
];
|
|
203
|
-
return
|
|
208
|
+
return branching ? node : withSelectors(node, token);
|
|
204
209
|
}
|
|
205
210
|
}
|
|
206
211
|
}
|
|
@@ -12,3 +12,22 @@ export function lineColumn(
|
|
|
12
12
|
}
|
|
13
13
|
return { line, column: offset - lineStart + 1 };
|
|
14
14
|
}
|
|
15
|
+
|
|
16
|
+
/** Index one source once; offsets and columns count UTF-16 code units. */
|
|
17
|
+
export function sourcePositions(text: string) {
|
|
18
|
+
const starts = [0];
|
|
19
|
+
for (let i = 0; i < text.length; i++) {
|
|
20
|
+
if (text[i] === '\n') starts.push(i + 1);
|
|
21
|
+
}
|
|
22
|
+
return (offset: number) => {
|
|
23
|
+
let low = 0;
|
|
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);
|
|
31
|
+
return { line, column: offset - starts[line - 1]! + 1 };
|
|
32
|
+
};
|
|
33
|
+
}
|
package/src/markup/pair.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* what was open inside it stays unclosed.
|
|
12
12
|
*/
|
|
13
13
|
import { branchParents } from '../registry';
|
|
14
|
-
import {
|
|
14
|
+
import { sourcePositions } from './line-column';
|
|
15
15
|
import { isBlockMacro, isRawMacro } from './ast';
|
|
16
16
|
import type { HtmlToken, MacroToken, Span, Token } from './tokens';
|
|
17
17
|
|
|
@@ -156,9 +156,11 @@ export function pairMarkup(
|
|
|
156
156
|
};
|
|
157
157
|
const errors: PairingError[] = [];
|
|
158
158
|
const noticedAtEnd = (tokens[tokens.length - 1]?.end ?? 0) + 1;
|
|
159
|
+
let position: ReturnType<typeof sourcePositions> | undefined;
|
|
159
160
|
const where = (offset: number) => {
|
|
160
161
|
if (options.source === undefined) return `offset ${offset}`;
|
|
161
|
-
|
|
162
|
+
position ??= sourcePositions(options.source);
|
|
163
|
+
const { line, column } = position(offset);
|
|
162
164
|
return `line ${line}, column ${column}`;
|
|
163
165
|
};
|
|
164
166
|
const report = (
|
package/src/markup/parse.ts
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
import type { Selectors, Token } from './tokens';
|
|
15
15
|
import { isCodeAttribute } from './code-attributes';
|
|
16
16
|
import { defaultCodeEnd, type CodeEnd } from './code-end';
|
|
17
|
-
import { lineColumn } from './line-column';
|
|
17
|
+
import { lineColumn, sourcePositions } from './line-column';
|
|
18
18
|
|
|
19
19
|
export { lineColumn };
|
|
20
20
|
|
|
@@ -160,9 +160,10 @@ export function analyzeMarkup(
|
|
|
160
160
|
damage[i]! + 0.5,
|
|
161
161
|
e,
|
|
162
162
|
]);
|
|
163
|
+
const position = paired.errors.length ? sourcePositions(source) : undefined;
|
|
163
164
|
for (const error of paired.errors) {
|
|
164
165
|
if (!inDamage(error.start)) {
|
|
165
|
-
noticed.push([error.noticedAt, pairingError(
|
|
166
|
+
noticed.push([error.noticedAt, pairingError(position!, error)]);
|
|
166
167
|
}
|
|
167
168
|
}
|
|
168
169
|
// Array.prototype.sort is stable: equal ones stay in the order found
|
|
@@ -185,8 +186,11 @@ export function parseMarkup(
|
|
|
185
186
|
}
|
|
186
187
|
|
|
187
188
|
/** A pairing problem as the MarkupError it is for `source`. */
|
|
188
|
-
function pairingError(
|
|
189
|
-
|
|
189
|
+
function pairingError(
|
|
190
|
+
position: ReturnType<typeof sourcePositions>,
|
|
191
|
+
error: PairingError,
|
|
192
|
+
): MarkupError {
|
|
193
|
+
const { line, column } = position(error.start);
|
|
190
194
|
return new MarkupError(
|
|
191
195
|
error.message,
|
|
192
196
|
error.start,
|
|
@@ -260,6 +264,7 @@ export function scanMarkupTolerant(
|
|
|
260
264
|
const tokens: Token[] = [];
|
|
261
265
|
const errors: MarkupError[] = [];
|
|
262
266
|
const damage: number[] = [];
|
|
267
|
+
let position: ReturnType<typeof sourcePositions> | undefined;
|
|
263
268
|
// The code of a tag ends at a }: with none after it, the tag is unclosed
|
|
264
269
|
// without reading its code to the end of the source, which every
|
|
265
270
|
// unclosed tag would do again (#265).
|
|
@@ -292,7 +297,8 @@ export function scanMarkupTolerant(
|
|
|
292
297
|
break;
|
|
293
298
|
}
|
|
294
299
|
const at = base + result.offset;
|
|
295
|
-
|
|
300
|
+
position ??= sourcePositions(source);
|
|
301
|
+
const { line, column } = position(at);
|
|
296
302
|
errors.push(
|
|
297
303
|
new MarkupError(
|
|
298
304
|
result.reason,
|