@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.
@@ -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 code element (`<script>`
242
- * or `<style>`) it is in, if any. The text of a `{do}` body is code, and is
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 && CODE_ELEMENTS.has(tag)) {
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 a `<script>` or
268
- * `<style>`, `<!--` is code, no comment.
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)) {
@@ -278,27 +276,26 @@ function openComment(tokens: readonly Token[]): number {
278
276
  /** `<name` or `</name`, where a tag may start. */
279
277
  const TAG_START = /<(\/?)([a-zA-Z][a-zA-Z0-9-]*)/g;
280
278
 
281
- /**
282
- * Whether the text from `at` is a tag that a `>` on the next line would
283
- * close (on the same line, `<a@b>` would be an email autolink).
284
- */
285
- function closesWithGt(text: string, at: number): boolean {
286
- const rest = `${text.slice(at)}\n>`;
287
- const [first] = tokenizeMarkupTolerant(rest).tokens;
288
- return first?.type === 'html' && first.end === rest.length;
289
- }
290
-
291
279
  /**
292
280
  * Where a tag starts that is text only for want of its `>`, or -1: `</b`,
293
281
  * or `<b_r x=` (no error: `<b` is no tag before `_`), which a `>` on a later
294
- * line makes a tag. In a code element only its end tag counts.
282
+ * line makes a tag. In a text-only element only its end tag counts.
295
283
  */
296
284
  function openTag(text: string, tokens: readonly Token[]): number {
285
+ let closed: number | undefined;
297
286
  for (const { token, element } of markupText(tokens)) {
298
287
  for (const m of text.slice(token.start, token.end).matchAll(TAG_START)) {
299
288
  const at = token.start + m.index;
300
289
  if (element && !(m[1] && m[2]!.toLowerCase() === element)) continue;
301
- if (closesWithGt(text, at)) return at;
290
+ // Only a tag ending at the appended > can have been left open.
291
+ // Probe once for all candidates: re-tokenizing every suffix makes
292
+ // plain text such as repeated `</x ` quadratic (#709). The newline
293
+ // also keeps `<a@b` from becoming an email autolink.
294
+ closed ??=
295
+ tokenizeMarkupTolerant(`${text}\n>`).tokens.find(
296
+ (t) => t.type === 'html' && t.end === text.length + 2,
297
+ )?.start ?? -1;
298
+ if (closed === at) return at;
302
299
  }
303
300
  }
304
301
  return -1;
@@ -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
- // Read with the built-in methods: a subclass may override them. A Date or
90
- // RegExp holds no other value, so it needs no number unless it has fields.
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(): Promise<StoryAction[]>;
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
@@ -1002,6 +1002,17 @@ export function macroCalls(
1002
1002
  macros?: Iterable<ToolingMacro>,
1003
1003
  ): MacroCall[];
1004
1004
 
1005
+ /** Opening macro head containing cursor, including unfinished heads. Uses
1006
+ * the same contextual ownership as macroCalls, including independently
1007
+ * evaluated labels/attributes. Returns undefined outside a head. UTF-16
1008
+ * offsets are in source; invalid cursors throw RangeError.
1009
+ */
1010
+ export declare function macroCallAt(
1011
+ source: string,
1012
+ cursor: number,
1013
+ macros?: Iterable<ToolingMacro>,
1014
+ ): MacroCall | undefined;
1015
+
1005
1016
  /** A branch (`{else}`, `{case}`, …) of a macro, with what follows it. */
1006
1017
  export interface PairedBranch {
1007
1018
  tag: MacroToken;
@@ -1340,3 +1351,34 @@ export declare function completionContext(
1340
1351
  cursor: number,
1341
1352
  macros?: Iterable<ToolingMacro>,
1342
1353
  ): CompletionContext;
1354
+
1355
+ /** Identifier replacement boundaries in decoded code (UTF-16). */
1356
+ export interface CompletionIdentifier {
1357
+ kind: 'root' | 'member';
1358
+ /** Decoded name, excluding a root's sigil. */
1359
+ name: string;
1360
+ /** Includes the sigil for roots and the complete escaped identifier suffix. */
1361
+ start: number;
1362
+ end: number;
1363
+ /** Start of the preceding dot, for bracket-access edits. */
1364
+ dotStart?: number;
1365
+ }
1366
+
1367
+ /** Full code and identifier boundaries for an edit at the cursor. */
1368
+ export type CompletionTarget =
1369
+ | { kind: 'literal' }
1370
+ | (Extract<CompletionContext, { kind: 'code' }> & {
1371
+ identifier?: CompletionIdentifier;
1372
+ });
1373
+
1374
+ /** Like completionContext, with full code including the suffix after cursor.
1375
+ * identifier supplies root/member replacement boundaries in code; use
1376
+ * pieceOffset(target, boundary) for original source positions and encode
1377
+ * inserted text through containers from innermost to outermost. Accepts
1378
+ * unfinished enclosing markup; invalid cursors throw RangeError.
1379
+ */
1380
+ export declare function completionTarget(
1381
+ source: string,
1382
+ cursor: number,
1383
+ macros?: Iterable<ToolingMacro>,
1384
+ ): CompletionTarget;