@torpor/view 0.0.5 → 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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @torpor/view
2
2
 
3
+ ## 0.0.6
4
+
5
+ ### Patch Changes
6
+
7
+ - 808da4c: Fix: hydrating a node with empty text
8
+
3
9
  ## 0.0.5
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torpor/view",
3
- "version": "0.0.5",
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",
@@ -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
@@ -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
+ }