squiffy-runtime 6.0.0-beta.2 → 6.0.0-beta.4

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.
@@ -48,6 +48,43 @@ export function AnimatePlugin(): SquiffyPlugin {
48
48
  // innerHTML after animation, users can still interact with the links.
49
49
  const originalContent = el.innerHTML;
50
50
 
51
+ // For trigger="link", the links in el that run the animation when clicked.
52
+ let triggerLinks: HTMLElement[] = [];
53
+ const registerTriggerLinks = () => {
54
+ for (const link of triggerLinks) {
55
+ squiffy.animation.removeLinkAnimation(link);
56
+ }
57
+ triggerLinks = [...el.querySelectorAll<HTMLElement>("a")];
58
+ for (const link of triggerLinks) {
59
+ squiffy.animation.addLinkAnimation(link, runAnimation);
60
+ }
61
+ };
62
+
63
+ const restoreContent = () => {
64
+ if (params.trigger !== "link") {
65
+ el.innerHTML = originalContent;
66
+ return;
67
+ }
68
+
69
+ // Restoring the content replaces the links, so carry over the state
70
+ // (e.g. "disabled" from the click that triggered the animation) to the new
71
+ // links, and make sure they still trigger the animation if re-enabled.
72
+ const oldLinks = triggerLinks;
73
+ el.innerHTML = originalContent;
74
+ registerTriggerLinks();
75
+ triggerLinks.forEach((link, i) => {
76
+ const oldLink = oldLinks[i];
77
+ if (!oldLink) return;
78
+ if (oldLink.classList.contains("disabled")) {
79
+ link.classList.add("disabled");
80
+ }
81
+ const tabindex = oldLink.getAttribute("tabindex");
82
+ if (tabindex !== null) {
83
+ link.setAttribute("tabindex", tabindex);
84
+ }
85
+ });
86
+ };
87
+
51
88
  const runAnimation = () => {
52
89
  if (params.loop) {
53
90
  squiffy.animation.runAnimation(params.name, el, params, () => {}, true);
@@ -62,7 +99,7 @@ export function AnimatePlugin(): SquiffyPlugin {
62
99
 
63
100
  squiffy.animation.runAnimation(params.name, el, params, () => {
64
101
  el.classList.remove("squiffy-animate");
65
- el.innerHTML = originalContent;
102
+ restoreContent();
66
103
  resolve();
67
104
  }, false);
68
105
  });
@@ -71,10 +108,7 @@ export function AnimatePlugin(): SquiffyPlugin {
71
108
  };
72
109
 
73
110
  if (params.trigger === "link") {
74
- const links = el.querySelectorAll("a");
75
- for (const link of links) {
76
- squiffy.animation.addLinkAnimation(link, runAnimation);
77
- }
111
+ registerTriggerLinks();
78
112
  } else {
79
113
  runAnimation();
80
114
  }
@@ -86,7 +120,7 @@ export function AnimatePlugin(): SquiffyPlugin {
86
120
  init(sq: PluginHost) {
87
121
  squiffy = sq;
88
122
 
89
- squiffy.registerHelper("animate", function(name: string, options) {
123
+ squiffy.registerHelper("animate", function(this: any, name: string, options) {
90
124
  const content = options.fn(this);
91
125
  const dataset: Record<string, any> = {
92
126
  name,
@@ -7,10 +7,10 @@ export function ReplaceLabel() : SquiffyPlugin {
7
7
  return {
8
8
  name: "replaceLabel",
9
9
  init(squiffy: PluginHost) {
10
- squiffy.registerHelper("label", function(name: string, options) {
10
+ squiffy.registerHelper("label", function(this: any, name: string, options) {
11
11
  return new Handlebars.SafeString(`<span class="squiffy-label-${name}">${options.fn(this)}</span>`);
12
12
  });
13
- squiffy.registerHelper("replace", function(name: string, options) {
13
+ squiffy.registerHelper("replace", function(this: any, name: string, options) {
14
14
  const result = options.fn(this);
15
15
  const element = squiffy.outputElement.querySelector<HTMLElement>(`.squiffy-label-${name}`);
16
16
  if (element) {
@@ -25,7 +25,7 @@ export function RotateSequencePlugin() : SquiffyPlugin {
25
25
 
26
26
  const handleLink = (squiffy: PluginHost, link: HTMLElement, isRotate: boolean) => {
27
27
  const result: HandleLinkResult = {};
28
- const options = JSON.parse(link.getAttribute("data-options")) as string[] || [];
28
+ const options = JSON.parse(link.getAttribute("data-options") ?? "[]") as string[] || [];
29
29
 
30
30
  const rotateResult = rotate(options, isRotate ? link.getAttribute("data-value") : "");
31
31
 
@@ -232,6 +232,221 @@ test("Click a passage link and go back", async () => {
232
232
  expect(await squiffyApi.clickLink(linkToPassage)).toBe(true);
233
233
  });
234
234
 
235
+ test("Turn-count passages share an output block with the passage they follow", async () => {
236
+ // https://github.com/textadventures/squiffy/issues/123
237
+ const script = `
238
+ [[start]]:
239
+ Choose [a], [b] or [c]
240
+
241
+ [a]:
242
+ You chose a.
243
+
244
+ [b]:
245
+ You chose b.
246
+
247
+ [c]:
248
+ You chose c.
249
+
250
+ [@1]:
251
+ Turn one.
252
+
253
+ [@last]:
254
+ Last turn.
255
+ `;
256
+ const { squiffyApi, element } = await initScript(script);
257
+
258
+ const expectSingleBlock = (passageName: string, expectedText: string[]) => {
259
+ const passageElement = element.querySelector(`.squiffy-output-passage[data-passage="${passageName}"]`);
260
+ const blocks = passageElement!.querySelectorAll(".squiffy-output-block");
261
+ expect(blocks.length).toBe(1);
262
+ expect(Array.from(blocks[0].children).map(c => c.textContent)).toEqual(expectedText);
263
+ };
264
+
265
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
266
+ expectSingleBlock("a", ["You chose a.", "Turn one."]);
267
+
268
+ await squiffyApi.clickLink(findLink(element, "passage", "b"));
269
+ await squiffyApi.clickLink(findLink(element, "passage", "c"));
270
+ expectSingleBlock("c", ["You chose c.", "Last turn."]);
271
+ });
272
+
273
+ test("Going back to a section re-enables its inputs", async () => {
274
+ // https://github.com/textadventures/squiffy/issues/123
275
+ const script = `
276
+ [[start]]:
277
+ <input type="text" data-attribute="name">
278
+ <textarea data-attribute="bio"></textarea>
279
+ <select data-attribute="colour"><option>red</option><option>blue</option></select>
280
+ <div contenteditable="true" data-attribute="notes"></div>
281
+ <input type="text" data-attribute="fixed" disabled>
282
+ [[next]]
283
+
284
+ [[next]]:
285
+ Hello {{name}}.
286
+ `;
287
+ const { squiffyApi, element } = await initScript(script);
288
+
289
+ const input = element.querySelector("input[data-attribute='name']") as HTMLInputElement;
290
+ const textarea = element.querySelector("textarea") as HTMLTextAreaElement;
291
+ const select = element.querySelector("select") as HTMLSelectElement;
292
+ const editable = element.querySelector("[contenteditable]") as HTMLElement;
293
+ const authorDisabled = element.querySelector("input[data-attribute='fixed']") as HTMLInputElement;
294
+
295
+ input.value = "Bob";
296
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
297
+
298
+ expect(squiffyApi.get("name")).toBe("Bob");
299
+ expect(input.disabled).toBe(true);
300
+ expect(textarea.disabled).toBe(true);
301
+ expect(select.disabled).toBe(true);
302
+ expect(editable.getAttribute("contenteditable")).toBe("false");
303
+
304
+ squiffyApi.goBack();
305
+
306
+ expect(squiffyApi.get("name")).toBe(null);
307
+ expect(input.disabled).toBe(false);
308
+ expect(textarea.disabled).toBe(false);
309
+ expect(select.disabled).toBe(false);
310
+ expect(editable.getAttribute("contenteditable")).toBe("true");
311
+ expect(element.querySelectorAll("[data-squiffy-disabled]").length).toBe(0);
312
+
313
+ // An input the author disabled stays disabled
314
+ expect(authorDisabled.disabled).toBe(true);
315
+
316
+ // The edited value is picked up when moving on again
317
+ input.value = "Alice";
318
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
319
+ expect(squiffyApi.get("name")).toBe("Alice");
320
+ });
321
+
322
+ test("Going back is ignored while transitions are running", async () => {
323
+ // https://github.com/textadventures/squiffy/issues/123
324
+ let release: () => void = () => {};
325
+ (window as any).transitionGate = new Promise<void>(resolve => { release = resolve; });
326
+
327
+ // Note: the 4-space indent on the transition line is the squiffy JS syntax - don't trim it!
328
+ const script = `
329
+ [[start]]:
330
+ @set x = 1
331
+ Start. [[slow]]
332
+
333
+ [[slow]]:
334
+ @set x = 2
335
+ squiffy.ui.transition(() => window.transitionGate);
336
+ Slow section. [[after]]
337
+
338
+ [[after]]:
339
+ After.
340
+ `;
341
+ const { squiffyApi, element } = await initScript(script);
342
+
343
+ const clickPromise = squiffyApi.clickLink(findLink(element, "section", "slow"));
344
+ await new Promise(resolve => setTimeout(resolve, 0));
345
+
346
+ // Mid-transition: going back does nothing
347
+ squiffyApi.goBack();
348
+ expect(element.querySelector(".squiffy-output-section[data-section='slow']")).not.toBeNull();
349
+
350
+ release();
351
+ await clickPromise;
352
+ expect(squiffyApi.get("_section")).toBe("slow");
353
+ expect(squiffyApi.get("x")).toBe(2);
354
+
355
+ // Once the transition is done, going back fully restores the previous section
356
+ squiffyApi.goBack();
357
+ expect(element.querySelector(".squiffy-output-section[data-section='slow']")).toBeNull();
358
+ expect(squiffyApi.get("_section")).toBe("start");
359
+ expect(squiffyApi.get("x")).toBe(1);
360
+ expect(squiffyApi.get("_seen_sections")).toEqual(["start"]);
361
+ });
362
+
363
+ const visibleText = (element: HTMLElement) =>
364
+ Array.from(element.querySelector("div")!.children)
365
+ .filter(child => !child.classList.contains("squiffy-clear-stack"))
366
+ .map(child => child.textContent)
367
+ .join(" ")
368
+ .replace(/\s+/g, " ")
369
+ .trim();
370
+
371
+ test("Going back through a cleared section reached via squiffy.story.go()", async () => {
372
+ // https://github.com/textadventures/squiffy/issues/123
373
+ // Note: the 4-space indent on the squiffy.story.go line is the squiffy JS syntax - don't trim it!
374
+ const script = `
375
+ [[start]]:
376
+ @set x = 1
377
+ Start. [[jump]]
378
+
379
+ [[jump]]:
380
+ @set x = 2
381
+ squiffy.story.go("cleared");
382
+ Never shown.
383
+
384
+ [[cleared]]:
385
+ @clear
386
+ @set x = 3
387
+ Cleared. [[next]]
388
+
389
+ [[next]]:
390
+ @clear
391
+ Next.
392
+ `;
393
+ const { squiffyApi, element } = await initScript(script);
394
+
395
+ await squiffyApi.clickLink(findLink(element, "section", "jump"));
396
+ expect(visibleText(element)).toBe("Cleared. next");
397
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
398
+ expect(visibleText(element)).toBe("Next.");
399
+
400
+ squiffyApi.goBack();
401
+ expect(visibleText(element)).toBe("Cleared. next");
402
+ expect(squiffyApi.get("_section")).toBe("cleared");
403
+
404
+ squiffyApi.goBack();
405
+ expect(visibleText(element)).toBe("Start. jump");
406
+ expect(squiffyApi.get("_section")).toBe("start");
407
+ expect(squiffyApi.get("x")).toBe(1);
408
+ expect(squiffyApi.get("_seen_sections")).toEqual(["start"]);
409
+ expect(findLink(element, "section", "jump", true)).toBeDefined();
410
+ });
411
+
412
+ test("Going back through screens cleared by the master section and a passage", async () => {
413
+ // https://github.com/textadventures/squiffy/issues/123
414
+ const script = `
415
+ [[start]]:
416
+ Start. [[two]]
417
+
418
+ [[]]:
419
+ @clear
420
+
421
+ [[two]]:
422
+ Two. [a] [[three]]
423
+
424
+ [a]:
425
+ @clear
426
+ Passage a.
427
+
428
+ [[three]]:
429
+ Three.
430
+ `;
431
+ const { squiffyApi, element } = await initScript(script);
432
+
433
+ await squiffyApi.clickLink(findLink(element, "section", "two"));
434
+ expect(visibleText(element)).toBe("Two. a three");
435
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
436
+ expect(visibleText(element)).toBe("Passage a.");
437
+
438
+ squiffyApi.goBack();
439
+ expect(visibleText(element)).toBe("Two. a three");
440
+ expect(findLink(element, "passage", "a", true)).toBeDefined();
441
+
442
+ squiffyApi.goBack();
443
+ expect(visibleText(element)).toBe("Start. two");
444
+ expect(squiffyApi.get("_seen_sections")).toEqual(["start"]);
445
+
446
+ await squiffyApi.clickLink(findLink(element, "section", "two"));
447
+ expect(visibleText(element)).toBe("Two. a three");
448
+ });
449
+
235
450
  test("Run JavaScript functions", async () => {
236
451
  const script = `
237
452
  document.getElementById('test').innerText = 'Initial JavaScript';
@@ -544,9 +759,10 @@ Updated text from next section.
544
759
  squiffyApi.update(updated.story);
545
760
  output = getSectionContent(element, "section1");
546
761
 
547
- // NOTE: The embedded text does not currently get updated. We would need to track where the embedded
548
- // text has been written.
549
- expect(output).toBe("Here is an updated script with text from the next section: Text from next section.");
762
+ // section1 changed, so it is re-rendered using the new section2 text.
763
+ // NOTE: If only section2 changed, the embedded text would not currently get updated. We would need to
764
+ // track where the embedded text has been written.
765
+ expect(output).toBe("Here is an updated script with text from the next section: Updated text from next section.");
550
766
  });
551
767
 
552
768
  test("Update passage with embedded text", async () => {
@@ -575,9 +791,140 @@ Updated text from a passage.
575
791
  squiffyApi.update(updated.story);
576
792
  output = getSectionContent(element, "section1");
577
793
 
578
- // NOTE: The embedded text does not currently get updated. We would need to track where the embedded
579
- // text has been written.
580
- expect(output).toBe("Here is an updated script with text from a passage: Text from a passage.");
794
+ // section1 changed, so it is re-rendered using the new passage text.
795
+ // NOTE: If only the passage changed, the embedded text would not currently get updated. We would need to
796
+ // track where the embedded text has been written.
797
+ expect(output).toBe("Here is an updated script with text from a passage: Updated text from a passage.");
798
+ });
799
+
800
+ test("Update section to embed a newly added passage", async () => {
801
+ const script = `
802
+ [[section1]]:
803
+ Here is some text from a passage: {{embed "passage"}}
804
+ `;
805
+
806
+ const { squiffyApi, element } = await initScript(script);
807
+
808
+ let output = getSectionContent(element, "section1");
809
+ expect(output).toBe("Here is some text from a passage: ");
810
+
811
+ const script2 = `
812
+ [[section1]]:
813
+ Here is updated text from a passage: {{embed "passage"}}
814
+
815
+ [passage]:
816
+ Text from a passage.
817
+ `;
818
+
819
+ const updated = await compile(script2);
820
+ squiffyApi.update(updated.story);
821
+ output = getSectionContent(element, "section1");
822
+
823
+ expect(output).toBe("Here is updated text from a passage: Text from a passage.");
824
+ });
825
+
826
+ test("Update passage to embed a newly added passage", async () => {
827
+ const script = `
828
+ [[section1]]:
829
+ Click [a passage].
830
+
831
+ [a passage]:
832
+ Embedded: {{embed "other"}}
833
+ `;
834
+
835
+ const { squiffyApi, element } = await initScript(script);
836
+
837
+ await squiffyApi.clickLink(findLink(element, "passage", "a passage"));
838
+ let output = getPassageContent(element, "section1", "a passage");
839
+ expect(output).toBe("Embedded: ");
840
+
841
+ const script2 = `
842
+ [[section1]]:
843
+ Click [a passage].
844
+
845
+ [a passage]:
846
+ Updated embedded: {{embed "other"}}
847
+
848
+ [other]:
849
+ Other passage text.
850
+ `;
851
+
852
+ const updated = await compile(script2);
853
+ squiffyApi.update(updated.story);
854
+ output = getPassageContent(element, "section1", "a passage");
855
+
856
+ expect(output).toBe("Updated embedded: Other passage text.");
857
+ });
858
+
859
+ test("Update section to embed a newly added section", async () => {
860
+ const script = `
861
+ [[section1]]:
862
+ Here is some text from the next section: {{embed "section2"}}
863
+ `;
864
+
865
+ const { squiffyApi, element } = await initScript(script);
866
+
867
+ let output = getSectionContent(element, "section1");
868
+ expect(output).toBe("Here is some text from the next section: ");
869
+
870
+ const script2 = `
871
+ [[section1]]:
872
+ Here is updated text from the next section: {{embed "section2"}}
873
+
874
+ [[section2]]:
875
+ Text from next section.
876
+ `;
877
+
878
+ const updated = await compile(script2);
879
+ squiffyApi.update(updated.story);
880
+ output = getSectionContent(element, "section1");
881
+
882
+ expect(output).toBe("Here is updated text from the next section: Text from next section.");
883
+ });
884
+
885
+ test("Section link to a random section", async () => {
886
+ const script = `
887
+ [[start]]:
888
+ {{section (random (array "a" "b" "c"))}}
889
+
890
+ [[a]]:
891
+ A
892
+
893
+ [[b]]:
894
+ B
895
+
896
+ [[c]]:
897
+ C
898
+ `;
899
+
900
+ const { element } = await initScript(script);
901
+
902
+ expect(element.textContent).not.toContain("Template error");
903
+ const link = element.querySelector("a.squiffy-link.link-section");
904
+ expect(link).not.toBeNull();
905
+ const section = link!.getAttribute("data-section");
906
+ expect(["a", "b", "c"]).toContain(section);
907
+ expect(link!.textContent).toBe(section);
908
+ });
909
+
910
+ test("Section link to a random section with link text", async () => {
911
+ const script = `
912
+ [[start]]:
913
+ {{section (random (array "a" "b")) text="Go through the door"}}
914
+
915
+ [[a]]:
916
+ A
917
+
918
+ [[b]]:
919
+ B
920
+ `;
921
+
922
+ const { element } = await initScript(script);
923
+
924
+ expect(element.textContent).not.toContain("Template error");
925
+ const link = findLink(element, "section", "Go through the door");
926
+ expect(link).toBeDefined();
927
+ expect(["a", "b"]).toContain(link.getAttribute("data-section"));
581
928
  });
582
929
 
583
930
  test("Clear entire script, then update", async () => {
@@ -1666,7 +2013,7 @@ Hello, {{player_name}}!
1666
2013
 
1667
2014
  // Fill the input
1668
2015
  input.value = "Alice";
1669
- input.dispatchEvent(new Event("input", { bubbles: true }));
2016
+ input.dispatchEvent(new window.Event("input", { bubbles: true }));
1670
2017
 
1671
2018
  // Link should now be enabled
1672
2019
  expect(continueLink.classList.contains("validation-disabled")).toBe(false);
@@ -1695,14 +2042,14 @@ Hello, {{player_name}}!
1695
2042
 
1696
2043
  // Fill the input
1697
2044
  input.value = "Test";
1698
- input.dispatchEvent(new Event("input", { bubbles: true }));
2045
+ input.dispatchEvent(new window.Event("input", { bubbles: true }));
1699
2046
 
1700
2047
  // Link should be enabled
1701
2048
  expect(continueLink.classList.contains("validation-disabled")).toBe(false);
1702
2049
 
1703
2050
  // Clear the input
1704
2051
  input.value = "";
1705
- input.dispatchEvent(new Event("input", { bubbles: true }));
2052
+ input.dispatchEvent(new window.Event("input", { bubbles: true }));
1706
2053
 
1707
2054
  // Link should be disabled again
1708
2055
  expect(continueLink.classList.contains("validation-disabled")).toBe(true);
@@ -1743,7 +2090,7 @@ Your pockets are empty.
1743
2090
 
1744
2091
  // Fill the input
1745
2092
  input.value = "Bob";
1746
- input.dispatchEvent(new Event("input", { bubbles: true }));
2093
+ input.dispatchEvent(new window.Event("input", { bubbles: true }));
1747
2094
 
1748
2095
  // Links should now be enabled
1749
2096
  expect(lookLink.classList.contains("validation-disabled")).toBe(false);
@@ -1776,21 +2123,21 @@ Name: {{name}}, Age: {{age}}
1776
2123
 
1777
2124
  // Fill only name
1778
2125
  nameInput.value = "Charlie";
1779
- nameInput.dispatchEvent(new Event("input", { bubbles: true }));
2126
+ nameInput.dispatchEvent(new window.Event("input", { bubbles: true }));
1780
2127
 
1781
2128
  // Still disabled because age is empty
1782
2129
  expect(continueLink.classList.contains("validation-disabled")).toBe(true);
1783
2130
 
1784
2131
  // Fill age with invalid value
1785
2132
  ageInput.value = "150";
1786
- ageInput.dispatchEvent(new Event("input", { bubbles: true }));
2133
+ ageInput.dispatchEvent(new window.Event("input", { bubbles: true }));
1787
2134
 
1788
2135
  // Still disabled because age is out of range
1789
2136
  expect(continueLink.classList.contains("validation-disabled")).toBe(true);
1790
2137
 
1791
2138
  // Fill age with valid value
1792
2139
  ageInput.value = "25";
1793
- ageInput.dispatchEvent(new Event("input", { bubbles: true }));
2140
+ ageInput.dispatchEvent(new window.Event("input", { bubbles: true }));
1794
2141
 
1795
2142
  // Now should be enabled
1796
2143
  expect(continueLink.classList.contains("validation-disabled")).toBe(false);
@@ -1820,7 +2167,7 @@ Done!
1820
2167
  // Fill first input and navigate
1821
2168
  const nameInput = element.querySelector('input[data-attribute="name"]') as HTMLInputElement;
1822
2169
  nameInput.value = "Dave";
1823
- nameInput.dispatchEvent(new Event("input", { bubbles: true }));
2170
+ nameInput.dispatchEvent(new window.Event("input", { bubbles: true }));
1824
2171
 
1825
2172
  const continueLink = findLink(element, "section", "Continue", true);
1826
2173
  await squiffyApi.clickLink(continueLink);
@@ -1832,7 +2179,7 @@ Done!
1832
2179
  // Fill email and continue
1833
2180
  const emailInput = element.querySelector('input[data-attribute="email"]') as HTMLInputElement;
1834
2181
  emailInput.value = "dave@example.com";
1835
- emailInput.dispatchEvent(new Event("input", { bubbles: true }));
2182
+ emailInput.dispatchEvent(new window.Event("input", { bubbles: true }));
1836
2183
 
1837
2184
  expect(finishLink.classList.contains("validation-disabled")).toBe(false);
1838
2185
 
@@ -1858,7 +2205,7 @@ Done!
1858
2205
 
1859
2206
  // Fill only the active input (disabled one should be ignored)
1860
2207
  activeInput.value = "test";
1861
- activeInput.dispatchEvent(new Event("input", { bubbles: true }));
2208
+ activeInput.dispatchEvent(new window.Event("input", { bubbles: true }));
1862
2209
 
1863
2210
  // Link should be enabled (disabled input doesn't count)
1864
2211
  expect(continueLink.classList.contains("validation-disabled")).toBe(false);
@@ -1897,7 +2244,7 @@ You selected {{difficulty}} mode.
1897
2244
 
1898
2245
  // Select an option
1899
2246
  select.value = "normal";
1900
- select.dispatchEvent(new Event("change", { bubbles: true }));
2247
+ select.dispatchEvent(new window.Event("change", { bubbles: true }));
1901
2248
 
1902
2249
  // Link should now be enabled
1903
2250
  expect(startLink.classList.contains("validation-disabled")).toBe(false);
@@ -1931,7 +2278,7 @@ Difficulty: {{difficulty}}
1931
2278
 
1932
2279
  // Select an option
1933
2280
  select.value = "hard";
1934
- select.dispatchEvent(new Event("change", { bubbles: true }));
2281
+ select.dispatchEvent(new window.Event("change", { bubbles: true }));
1935
2282
 
1936
2283
  // Navigate
1937
2284
  await squiffyApi.clickLink(startLink);
@@ -1962,14 +2309,14 @@ Done!
1962
2309
 
1963
2310
  // Select an option
1964
2311
  select.value = "a";
1965
- select.dispatchEvent(new Event("change", { bubbles: true }));
2312
+ select.dispatchEvent(new window.Event("change", { bubbles: true }));
1966
2313
 
1967
2314
  // Link should be enabled
1968
2315
  expect(continueLink.classList.contains("validation-disabled")).toBe(false);
1969
2316
 
1970
2317
  // Change back to empty
1971
2318
  select.value = "";
1972
- select.dispatchEvent(new Event("change", { bubbles: true }));
2319
+ select.dispatchEvent(new window.Event("change", { bubbles: true }));
1973
2320
 
1974
2321
  // Link should be disabled again
1975
2322
  expect(continueLink.classList.contains("validation-disabled")).toBe(true);
@@ -2005,23 +2352,23 @@ Level: <select data-attribute="level" required>
2005
2352
 
2006
2353
  // Fill only name
2007
2354
  nameInput.value = "Player";
2008
- nameInput.dispatchEvent(new Event("input", { bubbles: true }));
2355
+ nameInput.dispatchEvent(new window.Event("input", { bubbles: true }));
2009
2356
 
2010
2357
  // Still disabled because select is empty
2011
2358
  expect(continueLink.classList.contains("validation-disabled")).toBe(true);
2012
2359
 
2013
2360
  // Select level but clear name
2014
2361
  levelSelect.value = "1";
2015
- levelSelect.dispatchEvent(new Event("change", { bubbles: true }));
2362
+ levelSelect.dispatchEvent(new window.Event("change", { bubbles: true }));
2016
2363
  nameInput.value = "";
2017
- nameInput.dispatchEvent(new Event("input", { bubbles: true }));
2364
+ nameInput.dispatchEvent(new window.Event("input", { bubbles: true }));
2018
2365
 
2019
2366
  // Still disabled because name is empty
2020
2367
  expect(continueLink.classList.contains("validation-disabled")).toBe(true);
2021
2368
 
2022
2369
  // Fill both
2023
2370
  nameInput.value = "Hero";
2024
- nameInput.dispatchEvent(new Event("input", { bubbles: true }));
2371
+ nameInput.dispatchEvent(new window.Event("input", { bubbles: true }));
2025
2372
 
2026
2373
  // Now should be enabled
2027
2374
  expect(continueLink.classList.contains("validation-disabled")).toBe(false);
@@ -2274,4 +2621,62 @@ test("Template error preserves monospace styling", async () => {
2274
2621
  expect(errorDiv).not.toBeNull();
2275
2622
  expect(errorDiv.style.fontFamily).toBe("monospace");
2276
2623
  expect(errorDiv.style.whiteSpace).toBe("pre-wrap");
2277
- });
2624
+ });
2625
+ // Infix expressions in {{#if}} / {{#unless}}
2626
+
2627
+ test("Infix comparisons in {{#if}}", async () => {
2628
+ const script = `@set gender = male
2629
+ @set score = 150
2630
+ @set has_key = true
2631
+
2632
+ {{#if gender = "male"}}Male.{{/if}}
2633
+ {{#if score > 100 and has_key}}High score with key.{{else}}No.{{/if}}
2634
+ {{#if score < 100}}Low.{{else if score >= 150}}At least 150.{{else}}Medium.{{/if}}
2635
+ {{#unless score != 150}}Exactly 150.{{/unless}}
2636
+ {{#if not gender = "female"}}Not female.{{/if}}`;
2637
+
2638
+ const { element } = await initScript(script);
2639
+ const output = getSectionContent(element, "_default");
2640
+ expect(output).toBe("Male.\nHigh score with key.\nAt least 150.\nExactly 150.\nNot female.");
2641
+ });
2642
+
2643
+ test("Infix expressions combine with existing helpers", async () => {
2644
+ const script = `
2645
+ [[start]]:
2646
+ @set count = 1
2647
+ {{#if not (seen "other") or count >= 3}}Not seen other.{{else}}Seen other.{{/if}}
2648
+ {{#if (at "start") && !(seen "other")}}At start.{{/if}}
2649
+ `;
2650
+
2651
+ const { element } = await initScript(script);
2652
+ const output = getSectionContent(element, "start");
2653
+ expect(output).toBe("Not seen other.\nAt start.");
2654
+ });
2655
+
2656
+ test("Malformed infix expression shows a template error", async () => {
2657
+ const script = `
2658
+ [[start]]:
2659
+ {{#if score >}}Yes{{/if}}
2660
+ `;
2661
+
2662
+ const { element } = await initScript(script);
2663
+ const errorDiv = element.querySelector(".squiffy-error");
2664
+ expect(errorDiv?.textContent).toContain("Invalid condition \"score >\": expected a value after '>'");
2665
+ });
2666
+
2667
+ test("Custom helpers can be registered in @ui", async () => {
2668
+ const script = `
2669
+ @ui
2670
+ squiffy.registerHelper("shout", (text) => text.toUpperCase() + "!");
2671
+ squiffy.registerHelper("bold", (text) => new squiffy.SafeString("<b>" + text + "</b>"));
2672
+ ---
2673
+
2674
+ {{shout "hello"}} {{bold "world"}} {{shout "<i>"}}
2675
+ `;
2676
+
2677
+ const { element } = await initScript(script);
2678
+ const section = element.querySelector("[data-source='[[_default]]']");
2679
+ expect(section?.textContent).toBe("HELLO! world <I>!");
2680
+ expect(section?.querySelector("b")?.textContent).toBe("world");
2681
+ expect(section?.querySelector("i")).toBeNull();
2682
+ });