@rohal12/spindle 0.59.27 → 0.59.28

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.
@@ -13012,11 +13012,25 @@ function resolveAttributeValue(name, value, scope, errors) {
13012
13012
  function isBlankText(n) {
13013
13013
  return n.type === "text" && (n.comment === true || n.value.trim() === "");
13014
13014
  }
13015
- /** The index of the summary element opening `nodes`, or -1 when none does. */
13016
- function leadingSummaryIndex(nodes) {
13015
+ /**
13016
+ * The element a container must open with, to stay its direct child: the
13017
+ * summary of a details element (#382), the legend of a fieldset (#475), whose
13018
+ * first legend exempts the controls in it from the fieldset's `disabled`.
13019
+ */
13020
+ var LEADING_PARTS = {
13021
+ details: "summary",
13022
+ fieldset: "legend"
13023
+ };
13024
+ /**
13025
+ * The index of the node opening `nodes` as the leading part of the `tag`
13026
+ * container, or -1 when none does: the element itself, or a macro, which may
13027
+ * render it ({if}, {include}, a widget: #474).
13028
+ */
13029
+ function leadingPartIndex(tag, nodes) {
13030
+ const part = LEADING_PARTS[tag];
13017
13031
  const at = nodes.findIndex((n) => !isBlankText(n));
13018
13032
  const first = nodes[at];
13019
- return first?.type === "html" && first.tag.toLowerCase() === "summary" ? at : -1;
13033
+ return part && (first?.type === "macro" || first?.type === "html" && first.tag.toLowerCase() === part) ? at : -1;
13020
13034
  }
13021
13035
  function HtmlNodeRenderer({ node }) {
13022
13036
  const scope = useTextScope();
@@ -13024,6 +13038,8 @@ function HtmlNodeRenderer({ node }) {
13024
13038
  const inSvg = useContext(SvgContext);
13025
13039
  const tag = node.tag.toLowerCase();
13026
13040
  const isSvgRoot = tag === "svg";
13041
+ const isHtmlRoot = inSvg && tag === "foreignobject";
13042
+ const rawContent = inRaw && !isHtmlRoot;
13027
13043
  const isRawRoot = !inRaw && (isSvgRoot || PREFORMATTED_ELEMENTS.has(tag));
13028
13044
  const errors = [];
13029
13045
  const { props, direct } = splitAttributes(Object.entries(node.attributes).map(([k, v]) => [
@@ -13038,19 +13054,19 @@ function HtmlNodeRenderer({ node }) {
13038
13054
  let structural = false;
13039
13055
  if (node.children.length > 0) {
13040
13056
  if (tag === "style") children = node.children.map((n) => n.type === "text" ? n.value : renderSingleNode(n));
13041
- else if (inRaw || isRawRoot) children = renderInlineNodes(node.children);
13057
+ else if (rawContent || isRawRoot) children = renderInlineNodes(node.children);
13042
13058
  else {
13043
- const summaryAt = tag === "details" ? leadingSummaryIndex(node.children) : -1;
13059
+ const leadAt = leadingPartIndex(tag, node.children);
13044
13060
  const body = (nodes) => renderNodes(nodes, {
13045
13061
  nobr,
13046
13062
  locals,
13047
13063
  inline: isInline
13048
13064
  });
13049
13065
  structural = STRUCTURAL_ELEMENTS.has(tag) && node.children.every((n) => n.type !== "text" || isBlankText(n));
13050
- children = structural ? node.children.filter((n) => n.type !== "text").map((n) => renderSingleNode(n)) : summaryAt === -1 ? body(node.children) : /* @__PURE__ */ jsxs(Fragment$1, { children: [
13051
- body(node.children.slice(0, summaryAt)),
13052
- renderSingleNode(node.children[summaryAt]),
13053
- body(node.children.slice(summaryAt + 1))
13066
+ children = structural ? node.children.filter((n) => n.type !== "text").map((n) => renderSingleNode(n)) : leadAt === -1 ? body(node.children) : /* @__PURE__ */ jsxs(Fragment$1, { children: [
13067
+ body(node.children.slice(0, leadAt)),
13068
+ renderSingleNode(node.children[leadAt]),
13069
+ body(node.children.slice(leadAt + 1))
13054
13070
  ] });
13055
13071
  if (isInline !== parentInline) children = /* @__PURE__ */ jsx(InlineContext.Provider, {
13056
13072
  value: isInline,
@@ -13062,9 +13078,12 @@ function HtmlNodeRenderer({ node }) {
13062
13078
  });
13063
13079
  }
13064
13080
  }
13065
- if (inSvg && tag === "foreignobject" && children) children = /* @__PURE__ */ jsx(SvgContext.Provider, {
13081
+ if (isHtmlRoot && children) children = /* @__PURE__ */ jsx(SvgContext.Provider, {
13066
13082
  value: false,
13067
- children
13083
+ children: /* @__PURE__ */ jsx(RawTextContext.Provider, {
13084
+ value: false,
13085
+ children
13086
+ })
13068
13087
  });
13069
13088
  let element = h(node.tag, props, children);
13070
13089
  if (isSvgRoot && !inSvg) element = /* @__PURE__ */ jsx(SvgContext.Provider, {
@@ -16754,11 +16773,22 @@ var clonersFor = (paths) => {
16754
16773
  function frozenCopy(value) {
16755
16774
  return freeze(cloneValue(value), true);
16756
16775
  }
16776
+ /**
16777
+ * Whether a built-in's mutators can still change a frozen `value` (#473):
16778
+ * Object.freeze() locks properties, not internal slots. Immer locks a Map or
16779
+ * Set it freezes by overriding its mutators with own properties; a Date, URL
16780
+ * or URLSearchParams has no such lock, and one frozen by hand has none either.
16781
+ */
16782
+ function hasUnlockedSlots(value) {
16783
+ if (value instanceof Map) return !hasOwn(value, "set");
16784
+ if (value instanceof Set) return !hasOwn(value, "add");
16785
+ return value instanceof Date || value instanceof URL || value instanceof URLSearchParams;
16786
+ }
16757
16787
  /** Whether `value` and everything it holds is frozen. */
16758
16788
  function deeplyFrozen(value, seen = /* @__PURE__ */ new Set()) {
16759
16789
  if (typeof value !== "object" || value === null || seen.has(value)) return true;
16760
16790
  if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) return false;
16761
- if (!Object.isFrozen(value)) return false;
16791
+ if (!Object.isFrozen(value) || hasUnlockedSlots(value)) return false;
16762
16792
  seen.add(value);
16763
16793
  if (value instanceof Map) {
16764
16794
  for (const [key, entry] of mapEntries(value)) if (!deeplyFrozen(key, seen) || !deeplyFrozen(entry, seen)) return false;
@@ -23453,6 +23483,18 @@ function stopWithErrors(errors) {
23453
23483
  throw new Error(`spindle: ${errors.length} validation error(s):\n${errors.join("\n")}`);
23454
23484
  }
23455
23485
  /**
23486
+ * The result of `parse`, or, when it throws, the error shown on the page
23487
+ * instead of the story (a declaration that does not evaluate, a widget passage
23488
+ * that does not parse), prefixed with `where` when that is not in the message.
23489
+ */
23490
+ function orStop(parse, where = "") {
23491
+ try {
23492
+ return parse();
23493
+ } catch (err) {
23494
+ return stopWithErrors([where + errorMessage(err)]);
23495
+ }
23496
+ }
23497
+ /**
23456
23498
  * The undeclared variables the passages refer to, with the macros known now:
23457
23499
  * the input macros that bind a variable (registered by author JS or
23458
23500
  * StoryInit) and the roles of their parameters decide what is a reference.
@@ -23509,14 +23551,14 @@ function boot() {
23509
23551
  if (root) renderErrors(root, [msg]);
23510
23552
  throw new Error(`spindle: ${msg}`);
23511
23553
  }
23512
- const schema = parseStoryVariables(storyVarsPassage.content);
23513
- for (const name of blockWidgetNames(storyData.passages.values())) registerBlockMacro(name);
23554
+ const schema = orStop(() => parseStoryVariables(storyVarsPassage.content));
23555
+ for (const name of orStop(() => blockWidgetNames(storyData.passages.values()))) registerBlockMacro(name);
23514
23556
  const errors = variableErrors(storyData, schema);
23515
23557
  errors.push(...markupErrors(storyData, (p) => p.name === "StoryInit"));
23516
23558
  let transientDefaults = {};
23517
23559
  const storyTransientsPassage = storyData.passages.get("StoryTransients");
23518
23560
  if (storyTransientsPassage) {
23519
- const transientSchema = parseStoryVariables(storyTransientsPassage.content, "%");
23561
+ const transientSchema = orStop(() => parseStoryVariables(storyTransientsPassage.content, "%"));
23520
23562
  for (const name of transientSchema.keys()) if (schema.has(name)) errors.push(`StoryTransients: Variable "${name}" is already declared in StoryVariables. Names must be unique across scopes.`);
23521
23563
  transientDefaults = extractDefaults(transientSchema);
23522
23564
  }
@@ -23525,7 +23567,7 @@ function boot() {
23525
23567
  setDeclaredVariables(Object.keys(defaults), Object.keys(transientDefaults));
23526
23568
  useStoryStore.getState().init(storyData, defaults, transientDefaults);
23527
23569
  enterRuntimePhase();
23528
- for (const [, passage] of storyData.passages) if (passage.tags.includes("widget")) registerWidgetDefinitions(parseMarkup(passage.content), passage.name);
23570
+ for (const [, passage] of storyData.passages) if (passage.tags.includes("widget")) orStop(() => registerWidgetDefinitions(parseMarkup(passage.content), passage.name), `Passage "${passage.name}": `);
23529
23571
  initializeStory(loadSession(storyData.ifid));
23530
23572
  const markup = [...variableErrors(storyData, schema), ...markupErrors(storyData, (p) => p.name !== "StoryInit")];
23531
23573
  if (markup.length > 0) stopWithErrors(markup);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.59.27",
3
+ "version": "0.59.28",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
@@ -139,6 +139,22 @@ export function frozenCopy<T>(value: T): T {
139
139
  return freeze(cloneValue(value), true);
140
140
  }
141
141
 
142
+ /**
143
+ * Whether a built-in's mutators can still change a frozen `value` (#473):
144
+ * Object.freeze() locks properties, not internal slots. Immer locks a Map or
145
+ * Set it freezes by overriding its mutators with own properties; a Date, URL
146
+ * or URLSearchParams has no such lock, and one frozen by hand has none either.
147
+ */
148
+ function hasUnlockedSlots(value: object): boolean {
149
+ if (value instanceof Map) return !hasOwn(value, 'set');
150
+ if (value instanceof Set) return !hasOwn(value, 'add');
151
+ return (
152
+ value instanceof Date ||
153
+ value instanceof URL ||
154
+ value instanceof URLSearchParams
155
+ );
156
+ }
157
+
142
158
  /** Whether `value` and everything it holds is frozen. */
143
159
  function deeplyFrozen(value: unknown, seen = new Set<object>()): boolean {
144
160
  if (typeof value !== 'object' || value === null || seen.has(value)) {
@@ -148,7 +164,7 @@ function deeplyFrozen(value: unknown, seen = new Set<object>()): boolean {
148
164
  // a write to them would change the store's recorded history (#429): a value
149
165
  // holding one is handed out as a copy, which keeps the buffers it shares
150
166
  if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) return false;
151
- if (!Object.isFrozen(value)) return false;
167
+ if (!Object.isFrozen(value) || hasUnlockedSlots(value)) return false;
152
168
  seen.add(value);
153
169
  // What a Map or Set holds is not in its properties (#388)
154
170
  if (value instanceof Map) {
package/src/index.tsx CHANGED
@@ -63,6 +63,19 @@ function stopWithErrors(errors: string[]): never {
63
63
  );
64
64
  }
65
65
 
66
+ /**
67
+ * The result of `parse`, or, when it throws, the error shown on the page
68
+ * instead of the story (a declaration that does not evaluate, a widget passage
69
+ * that does not parse), prefixed with `where` when that is not in the message.
70
+ */
71
+ function orStop<T>(parse: () => T, where = ''): T {
72
+ try {
73
+ return parse();
74
+ } catch (err) {
75
+ return stopWithErrors([where + errorMessage(err)]);
76
+ }
77
+ }
78
+
66
79
  /**
67
80
  * The undeclared variables the passages refer to, with the macros known now:
68
81
  * the input macros that bind a variable (registered by author JS or
@@ -149,12 +162,14 @@ export function boot() {
149
162
  throw new Error(`spindle: ${msg}`);
150
163
  }
151
164
 
152
- const schema = parseStoryVariables(storyVarsPassage.content);
165
+ const schema = orStop(() => parseStoryVariables(storyVarsPassage.content));
153
166
  // Pass 1: Register the block widgets as block macros BEFORE any passage
154
167
  // is parsed (validation and StoryInit included), so that passages
155
168
  // invoking block widgets and widget bodies using other block widgets
156
169
  // parse correctly regardless of passage or definition order.
157
- for (const name of blockWidgetNames(storyData.passages.values())) {
170
+ for (const name of orStop(() =>
171
+ blockWidgetNames(storyData.passages.values()),
172
+ )) {
158
173
  registerBlockMacro(name);
159
174
  }
160
175
 
@@ -167,9 +182,8 @@ export function boot() {
167
182
  let transientDefaults: Record<string, unknown> = {};
168
183
  const storyTransientsPassage = storyData.passages.get('StoryTransients');
169
184
  if (storyTransientsPassage) {
170
- const transientSchema = parseStoryVariables(
171
- storyTransientsPassage.content,
172
- '%',
185
+ const transientSchema = orStop(() =>
186
+ parseStoryVariables(storyTransientsPassage.content, '%'),
173
187
  );
174
188
 
175
189
  // Check for cross-scope name collisions
@@ -198,7 +212,11 @@ export function boot() {
198
212
  // before StoryInit runs so that it can invoke them
199
213
  for (const [, passage] of storyData.passages) {
200
214
  if (passage.tags.includes('widget')) {
201
- registerWidgetDefinitions(parseMarkup(passage.content), passage.name);
215
+ orStop(
216
+ () =>
217
+ registerWidgetDefinitions(parseMarkup(passage.content), passage.name),
218
+ `Passage "${passage.name}": `,
219
+ );
202
220
  }
203
221
  }
204
222
 
@@ -684,11 +684,28 @@ function isBlankText(n: ASTNode): boolean {
684
684
  return n.type === 'text' && (n.comment === true || n.value.trim() === '');
685
685
  }
686
686
 
687
- /** The index of the summary element opening `nodes`, or -1 when none does. */
688
- function leadingSummaryIndex(nodes: ASTNode[]): number {
687
+ /**
688
+ * The element a container must open with, to stay its direct child: the
689
+ * summary of a details element (#382), the legend of a fieldset (#475), whose
690
+ * first legend exempts the controls in it from the fieldset's `disabled`.
691
+ */
692
+ const LEADING_PARTS: Record<string, string> = {
693
+ details: 'summary',
694
+ fieldset: 'legend',
695
+ };
696
+
697
+ /**
698
+ * The index of the node opening `nodes` as the leading part of the `tag`
699
+ * container, or -1 when none does: the element itself, or a macro, which may
700
+ * render it ({if}, {include}, a widget: #474).
701
+ */
702
+ function leadingPartIndex(tag: string, nodes: ASTNode[]): number {
703
+ const part = LEADING_PARTS[tag];
689
704
  const at = nodes.findIndex((n) => !isBlankText(n));
690
705
  const first = nodes[at];
691
- return first?.type === 'html' && first.tag.toLowerCase() === 'summary'
706
+ return part &&
707
+ (first?.type === 'macro' ||
708
+ (first?.type === 'html' && first.tag.toLowerCase() === part))
692
709
  ? at
693
710
  : -1;
694
711
  }
@@ -705,6 +722,9 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
705
722
  const inSvg = useContext(SvgContext);
706
723
  const tag = node.tag.toLowerCase();
707
724
  const isSvgRoot = tag === 'svg';
725
+ // The content of a foreignObject is HTML again (see below)
726
+ const isHtmlRoot = inSvg && tag === 'foreignobject';
727
+ const rawContent = inRaw && !isHtmlRoot;
708
728
  const isRawRoot = !inRaw && (isSvgRoot || PREFORMATTED_ELEMENTS.has(tag));
709
729
  const errors: AttributeError[] = [];
710
730
  const resolved = Object.entries(node.attributes).map(([k, v]): Attribute => [
@@ -731,13 +751,13 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
731
751
  children = node.children.map((n) =>
732
752
  n.type === 'text' ? n.value : renderSingleNode(n),
733
753
  );
734
- } else if (inRaw || isRawRoot) {
754
+ } else if (rawContent || isRawRoot) {
735
755
  children = renderInlineNodes(node.children);
736
756
  } else {
737
- // A details element's summary stays its direct child: it is rendered
738
- // apart from the markdown of the rest of the content.
739
- const summaryAt =
740
- tag === 'details' ? leadingSummaryIndex(node.children) : -1;
757
+ // The summary of a details element and the legend of a fieldset stay
758
+ // its direct children: rendered apart from the markdown of the rest of
759
+ // the content.
760
+ const leadAt = leadingPartIndex(tag, node.children);
741
761
  const body = (nodes: ASTNode[]) =>
742
762
  renderNodes(nodes, { nobr, locals, inline: isInline });
743
763
  structural =
@@ -747,13 +767,13 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
747
767
  node.children
748
768
  .filter((n) => n.type !== 'text')
749
769
  .map((n) => renderSingleNode(n))
750
- ) : summaryAt === -1 ? (
770
+ ) : leadAt === -1 ? (
751
771
  body(node.children)
752
772
  ) : (
753
773
  <>
754
- {body(node.children.slice(0, summaryAt))}
755
- {renderSingleNode(node.children[summaryAt]!)}
756
- {body(node.children.slice(summaryAt + 1))}
774
+ {body(node.children.slice(0, leadAt))}
775
+ {renderSingleNode(node.children[leadAt]!)}
776
+ {body(node.children.slice(leadAt + 1))}
757
777
  </>
758
778
  );
759
779
  if (isInline !== parentInline) {
@@ -772,10 +792,15 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
772
792
  }
773
793
  }
774
794
  }
775
- // The content of a foreignObject is HTML again, with HTML attributes
776
- if (inSvg && tag === 'foreignobject' && children) {
795
+ // The content of a foreignObject is HTML again: HTML attributes, and
796
+ // markdown, for the content of macros and widgets in it too (#477)
797
+ if (isHtmlRoot && children) {
777
798
  children = (
778
- <SvgContext.Provider value={false}>{children}</SvgContext.Provider>
799
+ <SvgContext.Provider value={false}>
800
+ <RawTextContext.Provider value={false}>
801
+ {children}
802
+ </RawTextContext.Provider>
803
+ </SvgContext.Provider>
779
804
  );
780
805
  }
781
806
  let element = h(node.tag, props, children);