@torpor/view 0.0.4 → 0.0.6

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.
Files changed (25) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/compile.cjs +21 -20
  3. package/dist/compile.cjs.map +1 -1
  4. package/dist/compile.js +21 -20
  5. package/dist/compile.js.map +1 -1
  6. package/dist/metafile-cjs.json +1 -1
  7. package/dist/metafile-esm.json +1 -1
  8. package/package.json +1 -1
  9. package/src/compile/build/client/replaceForVarNames.ts +1 -1
  10. package/src/render/nodeChild.ts +6 -1
  11. package/test/for/components/ForEscape.torp +1 -0
  12. package/test/for/components/output/ForEscape-client.ts +11 -2
  13. package/test/for/components/output/ForEscape-server.ts +1 -1
  14. package/test/for/components/temp/{ForEscape-client-2c881b16a6382edf074e1f151a209d60ca9b2b66.ts → ForEscape-client-80d3078cf15001f2ab37b0ec63cfce7267a8a166.ts} +11 -2
  15. package/test/for/components/temp/{ForEscape-server-94a1bceabed006e15326a7f635724d88c3daa8fe.ts → ForEscape-server-375744ca1e10e5c8ed7dd6420beef72107e55568.ts} +1 -1
  16. package/test/for/for-escape.test.ts +13 -4
  17. package/test/text/components/Text.d.ts +21 -0
  18. package/test/text/components/Text.torp +17 -0
  19. package/test/text/components/output/Text-client.ts +44 -0
  20. package/test/text/components/output/Text-server.ts +23 -0
  21. package/test/text/components/temp/Text-client-9840092be1b4552ddce6e5efc2f1b395637a0a3d.ts +44 -0
  22. package/test/text/components/temp/Text-server-6774aa9d4ccce941224ba0a8fb1d3e42eb31807e.ts +23 -0
  23. package/test/text/text.test.ts +51 -0
  24. /package/test/for/components/temp/{ForIn-client-c8a1ec4473d05475e11928d06d081ab671fd9f28.ts → ForIn-client-da39a3ee5e6b4b0d3255bfef95601890afd80709.ts} +0 -0
  25. /package/test/party/components/temp/{DoubleCount-server-40fc88156e52281b80fd6b12dd73d2469201fa29.ts → DoubleCount-server-da39a3ee5e6b4b0d3255bfef95601890afd80709.ts} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torpor/view",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Torpor's view library, for writing and mounting components",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -8,7 +8,7 @@ export default function replaceForVarNames(value: string, status: BuildStatus) {
8
8
  // get it from the loop data to trigger an update when it is changed
9
9
  for (let varName of status.forVarNames) {
10
10
  value = value.replaceAll(
11
- new RegExp(`([\\s\\(\\[\\{])${varName}([\\s\\.\\(\\)\\[\\]\\};])`, "g"),
11
+ new RegExp(`(^|\\s|\\(|\\[|\\{)${varName}($|\\s|\\.|\\(|\\)|\\[|\\]|\\}|;)`, "g"),
12
12
  `$1t_item.data.${varName}$2`,
13
13
  );
14
14
  }
@@ -4,8 +4,13 @@ import context from "./context";
4
4
  * Gets the first child of a node
5
5
  */
6
6
  export default function nodeChild(parent: Node): Node {
7
- const child = parent.firstChild;
7
+ let child = parent.firstChild;
8
8
  if (context.hydrationNode) {
9
+ // The child may be null when hydrating the sole text child of an
10
+ // element with no text
11
+ if (!child) {
12
+ child = parent.appendChild(parent.ownerDocument!.createTextNode(""));
13
+ }
9
14
  context.hydrationNode = child;
10
15
  }
11
16
  // NOTE: We know this is not null as it is being called from generated code
@@ -7,6 +7,7 @@ export default function ForEscape() {
7
7
  <div data-testid="input1-{i}" name={i} />
8
8
  <div data-testid="input2-{i}" name="{i}" />
9
9
  <div data-testid="input3-{i}" name={things[i]} />
10
+ <input :value={i} name="{i}" />
10
11
  }
11
12
  </section>
12
13
  }
@@ -5,6 +5,7 @@ import t_add_fragment from "../../../../src/render/addFragment";
5
5
  import t_anchor from "../../../../src/render/nodeAnchor";
6
6
  import t_attribute from "../../../../src/render/setAttribute";
7
7
  import t_child from "../../../../src/render/nodeChild";
8
+ import t_event from "../../../../src/render/addEvent";
8
9
  import t_fmt from "../../../../src/render/formatText";
9
10
  import t_fragment from "../../../../src/render/getFragment";
10
11
  import t_list_item from "../../../../src/render/newListItem";
@@ -54,15 +55,16 @@ export default function ForEscape(
54
55
  },
55
56
  function createListItem(t_item, t_before) {
56
57
  let t_old_range_1 = t_push_range(t_item, true);
57
- const t_fragment_1 = t_fragment($parent.ownerDocument!, t_fragments, 1, ` <p>#</p> <div data-testid=""></div> <div data-testid=""></div> <div data-testid=""></div> `);
58
+ const t_fragment_1 = t_fragment($parent.ownerDocument!, t_fragments, 1, ` <p>#</p> <div data-testid=""></div> <div data-testid=""></div> <div data-testid=""></div> <input></input> `);
58
59
  // @ts-ignore
59
60
  const t_root_1 = t_root(t_fragment_1);
60
61
  const t_text_1 = t_child(t_next(t_root_1));
61
62
  const t_div_1 = t_next(t_next(t_next(t_root_1))) as HTMLDivElement;
62
63
  const t_div_2 = t_next(t_next(t_div_1)) as HTMLDivElement;
63
64
  const t_div_3 = t_next(t_next(t_div_2)) as HTMLDivElement;
65
+ const t_input_1 = t_next(t_next(t_div_3)) as HTMLInputElement;
64
66
  // @ts-ignore
65
- const t_text_2 = t_next(t_div_3);
67
+ const t_text_2 = t_next(t_input_1);
66
68
  $run(function setTextContent() {
67
69
  t_text_1.textContent = t_fmt(t_item.data.i);
68
70
  });
@@ -84,6 +86,13 @@ export default function ForEscape(
84
86
  $run(function setAttribute() {
85
87
  t_attribute(t_div_3, "name", things[t_item.data.i]);
86
88
  });
89
+ $run(function setBinding() {
90
+ t_input_1.value = t_item.data.i || "";
91
+ });
92
+ t_event(t_input_1, "input", (e) => t_item.data.i = e.target.value);
93
+ $run(function setAttribute() {
94
+ t_attribute(t_input_1, "name", `${t_item.data.i}`);
95
+ });
87
96
  t_add_fragment(t_fragment_1, t_section_1, t_before);
88
97
  t_next(t_text_2);
89
98
  t_pop_range(t_old_range_1);
@@ -16,7 +16,7 @@ export default function ForEscape(
16
16
  let $output = "";
17
17
  $output += `<section> <![>`;
18
18
  for (let i = 0; i < 5; i++) {
19
- $output += `<!^> <p>${t_fmt(i)}</p> <div data-testid="input1-${t_attr(i)}" ${i ? `name="${t_attr(i)}"` : ''}></div> <div data-testid="input2-${t_attr(i)}" name="${t_attr(i)}"></div> <div data-testid="input3-${t_attr(i)}" ${things[i] ? `name="${t_attr(things[i])}"` : ''}></div> `;
19
+ $output += `<!^> <p>${t_fmt(i)}</p> <div data-testid="input1-${t_attr(i)}" ${i ? `name="${t_attr(i)}"` : ''}></div> <div data-testid="input2-${t_attr(i)}" name="${t_attr(i)}"></div> <div data-testid="input3-${t_attr(i)}" ${things[i] ? `name="${t_attr(things[i])}"` : ''}></div> <input value="${t_attr(i) || ""}" name="${t_attr(i)}"> `;
20
20
  }
21
21
  $output += `<!]><!> </section>`;
22
22
 
@@ -5,6 +5,7 @@ import t_add_fragment from "../../../../src/render/addFragment";
5
5
  import t_anchor from "../../../../src/render/nodeAnchor";
6
6
  import t_attribute from "../../../../src/render/setAttribute";
7
7
  import t_child from "../../../../src/render/nodeChild";
8
+ import t_event from "../../../../src/render/addEvent";
8
9
  import t_fmt from "../../../../src/render/formatText";
9
10
  import t_fragment from "../../../../src/render/getFragment";
10
11
  import t_list_item from "../../../../src/render/newListItem";
@@ -54,15 +55,16 @@ export default function ForEscape(
54
55
  },
55
56
  function createListItem(t_item, t_before) {
56
57
  let t_old_range_1 = t_push_range(t_item, true);
57
- const t_fragment_1 = t_fragment($parent.ownerDocument!, t_fragments, 1, ` <p>#</p> <div data-testid=""></div> <div data-testid=""></div> <div data-testid=""></div> `);
58
+ const t_fragment_1 = t_fragment($parent.ownerDocument!, t_fragments, 1, ` <p>#</p> <div data-testid=""></div> <div data-testid=""></div> <div data-testid=""></div> <input></input> `);
58
59
  // @ts-ignore
59
60
  const t_root_1 = t_root(t_fragment_1);
60
61
  const t_text_1 = t_child(t_next(t_root_1));
61
62
  const t_div_1 = t_next(t_next(t_next(t_root_1))) as HTMLDivElement;
62
63
  const t_div_2 = t_next(t_next(t_div_1)) as HTMLDivElement;
63
64
  const t_div_3 = t_next(t_next(t_div_2)) as HTMLDivElement;
65
+ const t_input_1 = t_next(t_next(t_div_3)) as HTMLInputElement;
64
66
  // @ts-ignore
65
- const t_text_2 = t_next(t_div_3);
67
+ const t_text_2 = t_next(t_input_1);
66
68
  $run(function setTextContent() {
67
69
  t_text_1.textContent = t_fmt(t_item.data.i);
68
70
  });
@@ -84,6 +86,13 @@ export default function ForEscape(
84
86
  $run(function setAttribute() {
85
87
  t_attribute(t_div_3, "name", things[t_item.data.i]);
86
88
  });
89
+ $run(function setBinding() {
90
+ t_input_1.value = t_item.data.i || "";
91
+ });
92
+ t_event(t_input_1, "input", (e) => t_item.data.i = e.target.value);
93
+ $run(function setAttribute() {
94
+ t_attribute(t_input_1, "name", `${t_item.data.i}`);
95
+ });
87
96
  t_add_fragment(t_fragment_1, t_section_1, t_before);
88
97
  t_next(t_text_2);
89
98
  t_pop_range(t_old_range_1);
@@ -16,7 +16,7 @@ export default function ForEscape(
16
16
  let $output = "";
17
17
  $output += `<section> <![>`;
18
18
  for (let i = 0; i < 5; i++) {
19
- $output += `<!^> <p>${t_fmt(i)}</p> <div data-testid="input1-${t_attr(i)}" ${i ? `name="${t_attr(i)}"` : ''}></div> <div data-testid="input2-${t_attr(i)}" name="${t_attr(i)}"></div> <div data-testid="input3-${t_attr(i)}" ${things[i] ? `name="${t_attr(things[i])}"` : ''}></div> `;
19
+ $output += `<!^> <p>${t_fmt(i)}</p> <div data-testid="input1-${t_attr(i)}" ${i ? `name="${t_attr(i)}"` : ''}></div> <div data-testid="input2-${t_attr(i)}" name="${t_attr(i)}"></div> <div data-testid="input3-${t_attr(i)}" ${things[i] ? `name="${t_attr(things[i])}"` : ''}></div> <input value="${t_attr(i) || ""}" name="${t_attr(i)}"> `;
20
20
  }
21
21
  $output += `<!]><!> </section>`;
22
22
 
@@ -1,5 +1,6 @@
1
- import { queryByTestId } from "@testing-library/dom";
1
+ import { queryByTestId, queryByText } from "@testing-library/dom";
2
2
  import "@testing-library/jest-dom/vitest";
3
+ import userEvent from "@testing-library/user-event";
3
4
  import { beforeAll, expect, test } from "vitest";
4
5
  import buildOutputFiles from "../buildOutputFiles";
5
6
  import hydrateComponent from "../hydrateComponent";
@@ -17,7 +18,7 @@ test("for escape -- mounted", async () => {
17
18
  const component = await importComponent(componentPath, "client");
18
19
  mountComponent(container, component);
19
20
 
20
- check(container);
21
+ await check(container);
21
22
  });
22
23
 
23
24
  test("for escape -- hydrated", async () => {
@@ -26,10 +27,10 @@ test("for escape -- hydrated", async () => {
26
27
  const serverComponent = await importComponent(componentPath, "server");
27
28
  hydrateComponent(container, clientComponent, serverComponent);
28
29
 
29
- check(container);
30
+ await check(container);
30
31
  });
31
32
 
32
- function check(container: HTMLElement) {
33
+ async function check(container: HTMLElement) {
33
34
  expect(queryByTestId(container, "input1-2")).not.toBeNull();
34
35
  expect(queryByTestId(container, "input1-2")).toHaveAttribute("name", "2");
35
36
 
@@ -38,4 +39,12 @@ function check(container: HTMLElement) {
38
39
 
39
40
  expect(queryByTestId(container, "input3-2")).not.toBeNull();
40
41
  expect(queryByTestId(container, "input3-2")).toHaveAttribute("name", "c");
42
+
43
+ const user = userEvent.setup();
44
+ const input = container.getElementsByTagName("input")[0];
45
+
46
+ await user.clear(input);
47
+ await user.type(input, "Hello");
48
+
49
+ expect(queryByText(container, "Hello")).not.toBeNull();
41
50
  }
@@ -0,0 +1,21 @@
1
+ import { type SlotRender } from "@torpor/view";
2
+
3
+ /**
4
+ * Mounts or hydrates the component into the supplied parent node.
5
+ * @param $parent -- The parent node.
6
+ * @param $anchor -- The node to mount the component before.
7
+ * @param $props -- The values that have been passed into the component as properties.
8
+ * @param $context -- Values that have been passed into the component from its ancestors.
9
+ * @param $slots -- Functions for rendering children into slot nodes within the component.
10
+ */
11
+ declare function Text(
12
+ $parent: ParentNode,
13
+ $anchor: Node | null,
14
+ $props: {
15
+ value: string;
16
+ empty: string;
17
+ },
18
+ $context?: Record<PropertyKey, any>,
19
+ $slots?: Record<string, SlotRender>,
20
+ ): void;
21
+ export default Text;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * A component with some text in it.
3
+ */
4
+ export default function Text($props: {
5
+ value: string;
6
+ empty: string;
7
+ }) {
8
+ @render {
9
+ <div>
10
+ <p>
11
+ {$props.value}
12
+ </p>
13
+ @// Make sure the #text element to be set is not null if the value is empty
14
+ <p>{$props.empty}</p>
15
+ </div>
16
+ }
17
+ }
@@ -0,0 +1,44 @@
1
+ import $run from "../../../../src/render/$run";
2
+ import { type SlotRender } from "../../../../src/types/SlotRender";
3
+ import t_add_fragment from "../../../../src/render/addFragment";
4
+ import t_child from "../../../../src/render/nodeChild";
5
+ import t_fmt from "../../../../src/render/formatText";
6
+ import t_fragment from "../../../../src/render/getFragment";
7
+ import t_next from "../../../../src/render/nodeNext";
8
+ import t_root from "../../../../src/render/nodeRoot";
9
+
10
+ /**
11
+ * A component with some text in it.
12
+ */
13
+ export default function Text(
14
+ $parent: ParentNode,
15
+ $anchor: Node | null,
16
+ $props: {
17
+ value: string;
18
+ empty: string;
19
+ },
20
+ // @ts-ignore
21
+ $context?: Record<PropertyKey, any>,
22
+ // @ts-ignore
23
+ $slots?: Record<string, SlotRender>
24
+ ) {
25
+ $props ??= {};
26
+
27
+ /* User interface */
28
+ const t_fragments: DocumentFragment[] = [];
29
+
30
+ const t_fragment_0 = t_fragment($parent.ownerDocument!, t_fragments, 0, `<div> <p>#</p> <p>#</p> </div>`);
31
+ // @ts-ignore
32
+ const t_div_1 = t_root(t_fragment_0) as HTMLDivElement;
33
+ const t_text_1 = t_child(t_next(t_child(t_div_1)));
34
+ const t_text_2 = t_child(t_next(t_next(t_next(t_child(t_div_1)))));
35
+ $run(function setTextContent() {
36
+ t_text_1.textContent = ` ${t_fmt($props.value)} `;
37
+ });
38
+ $run(function setTextContent() {
39
+ t_text_2.textContent = t_fmt($props.empty);
40
+ });
41
+ t_add_fragment(t_fragment_0, $parent, $anchor);
42
+ t_next(t_div_1);
43
+
44
+ }
@@ -0,0 +1,23 @@
1
+ import { type ServerSlotRender } from "../../../../src/types/ServerSlotRender";
2
+ import t_fmt from "../../../../src/render/formatText";
3
+
4
+ /**
5
+ * A component with some text in it.
6
+ */
7
+ export default function Text(
8
+ $props: {
9
+ value: string;
10
+ empty: string;
11
+ },
12
+ // @ts-ignore
13
+ $context?: Record<PropertyKey, any>,
14
+ // @ts-ignore
15
+ $slots?: Record<string, ServerSlotRender>
16
+ ) {
17
+
18
+ /* User interface */
19
+ let $output = "";
20
+ $output += `<div> <p> ${t_fmt($props.value)} </p> <p>${t_fmt($props.empty)}</p> </div>`;
21
+
22
+ return $output;
23
+ }
@@ -0,0 +1,44 @@
1
+ import $run from "../../../../src/render/$run";
2
+ import { type SlotRender } from "../../../../src/types/SlotRender";
3
+ import t_add_fragment from "../../../../src/render/addFragment";
4
+ import t_child from "../../../../src/render/nodeChild";
5
+ import t_fmt from "../../../../src/render/formatText";
6
+ import t_fragment from "../../../../src/render/getFragment";
7
+ import t_next from "../../../../src/render/nodeNext";
8
+ import t_root from "../../../../src/render/nodeRoot";
9
+
10
+ /**
11
+ * A component with some text in it.
12
+ */
13
+ export default function Text(
14
+ $parent: ParentNode,
15
+ $anchor: Node | null,
16
+ $props: {
17
+ value: string;
18
+ empty: string;
19
+ },
20
+ // @ts-ignore
21
+ $context?: Record<PropertyKey, any>,
22
+ // @ts-ignore
23
+ $slots?: Record<string, SlotRender>
24
+ ) {
25
+ $props ??= {};
26
+
27
+ /* User interface */
28
+ const t_fragments: DocumentFragment[] = [];
29
+
30
+ const t_fragment_0 = t_fragment($parent.ownerDocument!, t_fragments, 0, `<div> <p>#</p> <p>#</p> </div>`);
31
+ // @ts-ignore
32
+ const t_div_1 = t_root(t_fragment_0) as HTMLDivElement;
33
+ const t_text_1 = t_child(t_next(t_child(t_div_1)));
34
+ const t_text_2 = t_child(t_next(t_next(t_next(t_child(t_div_1)))));
35
+ $run(function setTextContent() {
36
+ t_text_1.textContent = ` ${t_fmt($props.value)} `;
37
+ });
38
+ $run(function setTextContent() {
39
+ t_text_2.textContent = t_fmt($props.empty);
40
+ });
41
+ t_add_fragment(t_fragment_0, $parent, $anchor);
42
+ t_next(t_div_1);
43
+
44
+ }
@@ -0,0 +1,23 @@
1
+ import { type ServerSlotRender } from "../../../../src/types/ServerSlotRender";
2
+ import t_fmt from "../../../../src/render/formatText";
3
+
4
+ /**
5
+ * A component with some text in it.
6
+ */
7
+ export default function Text(
8
+ $props: {
9
+ value: string;
10
+ empty: string;
11
+ },
12
+ // @ts-ignore
13
+ $context?: Record<PropertyKey, any>,
14
+ // @ts-ignore
15
+ $slots?: Record<string, ServerSlotRender>
16
+ ) {
17
+
18
+ /* User interface */
19
+ let $output = "";
20
+ $output += `<div> <p> ${t_fmt($props.value)} </p> <p>${t_fmt($props.empty)}</p> </div>`;
21
+
22
+ return $output;
23
+ }
@@ -0,0 +1,51 @@
1
+ import { queryByText } from "@testing-library/dom";
2
+ import "@testing-library/jest-dom/vitest";
3
+ import { beforeAll, expect, test } from "vitest";
4
+ import $watch from "../../src/render/$watch";
5
+ import buildOutputFiles from "../buildOutputFiles";
6
+ import hydrateComponent from "../hydrateComponent";
7
+ import importComponent from "../importComponent";
8
+ import mountComponent from "../mountComponent";
9
+
10
+ const componentPath = "./test/text/components/Text";
11
+
12
+ beforeAll(() => {
13
+ buildOutputFiles(componentPath);
14
+ });
15
+
16
+ interface State {
17
+ value: string;
18
+ empty: string;
19
+ }
20
+
21
+ test("text -- mounted", async () => {
22
+ const $state = $watch({ value: "hi", empty: "" });
23
+
24
+ const container = document.createElement("div");
25
+ const component = await importComponent(componentPath, "client");
26
+ mountComponent(container, component, $state);
27
+
28
+ check(container, $state);
29
+ });
30
+
31
+ test("switch values -- hydrated", async () => {
32
+ const $state = $watch({ value: "hi", empty: "" });
33
+
34
+ const container = document.createElement("div");
35
+ const clientComponent = await importComponent(componentPath, "client");
36
+ const serverComponent = await importComponent(componentPath, "server");
37
+ hydrateComponent(container, clientComponent, serverComponent, $state);
38
+
39
+ check(container, $state);
40
+ });
41
+
42
+ function check(container: HTMLElement, state: State) {
43
+ expect(queryByText(container, "hi")).not.toBeNull();
44
+
45
+ state.value = "hello";
46
+ state.empty = "world";
47
+
48
+ expect(queryByText(container, "hi")).toBeNull();
49
+ expect(queryByText(container, "hello")).not.toBeNull();
50
+ expect(queryByText(container, "world")).not.toBeNull();
51
+ }