@torpor/view 0.0.3 → 0.0.5
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 +13 -0
- package/dist/compile.cjs +21 -20
- package/dist/compile.cjs.map +1 -1
- package/dist/compile.js +21 -20
- package/dist/compile.js.map +1 -1
- package/dist/metafile-cjs.json +1 -1
- package/dist/metafile-esm.json +1 -1
- package/package.json +1 -1
- package/src/compile/build/client/buildElementNode.ts +8 -8
- package/src/compile/build/client/buildMount.ts +4 -9
- package/src/compile/build/client/buildRun.ts +3 -8
- package/src/compile/build/client/replaceForVarNames.ts +16 -0
- package/src/compile/parse/parseStyles.ts +2 -0
- package/test/for/components/ForEscape.torp +1 -0
- package/test/for/components/output/ForEscape-client.ts +11 -2
- package/test/for/components/output/ForEscape-server.ts +1 -1
- package/test/for/components/temp/{ForEscape-client-2c881b16a6382edf074e1f151a209d60ca9b2b66.ts → ForEscape-client-80d3078cf15001f2ab37b0ec63cfce7267a8a166.ts} +11 -2
- package/test/for/components/temp/{ForEscape-server-94a1bceabed006e15326a7f635724d88c3daa8fe.ts → ForEscape-server-375744ca1e10e5c8ed7dd6420beef72107e55568.ts} +1 -1
- package/test/for/for-escape.test.ts +13 -4
package/package.json
CHANGED
|
@@ -13,6 +13,7 @@ import buildFragment from "./buildFragment";
|
|
|
13
13
|
import buildMount from "./buildMount";
|
|
14
14
|
import buildNode from "./buildNode";
|
|
15
15
|
import buildRun from "./buildRun";
|
|
16
|
+
import replaceForVarNames from "./replaceForVarNames";
|
|
16
17
|
|
|
17
18
|
export default function buildElementNode(
|
|
18
19
|
node: ElementNode,
|
|
@@ -230,6 +231,8 @@ function buildBindGroupAttribute(
|
|
|
230
231
|
status: BuildStatus,
|
|
231
232
|
b: Builder,
|
|
232
233
|
) {
|
|
234
|
+
value = replaceForVarNames(value, status);
|
|
235
|
+
|
|
233
236
|
// Automatically add an event to bind the value
|
|
234
237
|
// TODO: Only tested this with radio buttons
|
|
235
238
|
let eventName = "change";
|
|
@@ -253,6 +256,8 @@ function buildBindAttribute(
|
|
|
253
256
|
status: BuildStatus,
|
|
254
257
|
b: Builder,
|
|
255
258
|
) {
|
|
259
|
+
value = replaceForVarNames(value, status);
|
|
260
|
+
|
|
256
261
|
// Automatically add an event to bind the value
|
|
257
262
|
// TODO: Need to check the element to find out what type of event to add
|
|
258
263
|
let eventName = "input";
|
|
@@ -298,14 +303,7 @@ function buildEventAttribute(
|
|
|
298
303
|
status: BuildStatus,
|
|
299
304
|
b: Builder,
|
|
300
305
|
) {
|
|
301
|
-
|
|
302
|
-
// get it from the loop data to trigger an update when it is changed
|
|
303
|
-
for (let varName of status.forVarNames) {
|
|
304
|
-
value = value.replaceAll(
|
|
305
|
-
new RegExp(`([\\s\\(\\[])${varName}([\\s\\.\\(\\)\\[\\];])`, "g"),
|
|
306
|
-
`$1t_item.data.${varName}$2`,
|
|
307
|
-
);
|
|
308
|
-
}
|
|
306
|
+
value = replaceForVarNames(value, status);
|
|
309
307
|
|
|
310
308
|
// Add an event listener, after the fragment has been added
|
|
311
309
|
const eventName = name.substring(2);
|
|
@@ -321,6 +319,8 @@ function buildTransitionAttribute(
|
|
|
321
319
|
status: BuildStatus,
|
|
322
320
|
b: Builder,
|
|
323
321
|
) {
|
|
322
|
+
value = replaceForVarNames(value, status);
|
|
323
|
+
|
|
324
324
|
status.imports.add("t_animate");
|
|
325
325
|
|
|
326
326
|
// Add an in and out transition, after the fragment has been added
|
|
@@ -1,22 +1,17 @@
|
|
|
1
1
|
import Builder from "../../utils/Builder";
|
|
2
2
|
import { type BuildStatus } from "./BuildStatus";
|
|
3
|
+
import replaceForVarNames from "./replaceForVarNames";
|
|
3
4
|
|
|
4
|
-
export default function
|
|
5
|
+
export default function buildMount(
|
|
5
6
|
functionName: string,
|
|
6
7
|
functionBody: string,
|
|
7
8
|
status: BuildStatus,
|
|
8
9
|
b: Builder,
|
|
9
10
|
) {
|
|
11
|
+
functionBody = replaceForVarNames(functionBody, status);
|
|
12
|
+
|
|
10
13
|
status.imports.add("$mount");
|
|
11
14
|
b.append(`$mount(function ${functionName}() {`);
|
|
12
|
-
// HACK: If a value from a for loop is used in the function body,
|
|
13
|
-
// get it from the loop data to trigger an update when it is changed
|
|
14
|
-
for (let varName of status.forVarNames) {
|
|
15
|
-
functionBody = functionBody.replaceAll(
|
|
16
|
-
new RegExp(`([\\s\\(\\[])${varName}([\\s\\.\\(\\)\\[\\];])`, "g"),
|
|
17
|
-
`$1t_item.data.${varName}$2`,
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
15
|
b.append(functionBody);
|
|
21
16
|
b.append("});");
|
|
22
17
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Builder from "../../utils/Builder";
|
|
2
2
|
import { type BuildStatus } from "./BuildStatus";
|
|
3
|
+
import replaceForVarNames from "./replaceForVarNames";
|
|
3
4
|
|
|
4
5
|
export default function buildRun(
|
|
5
6
|
functionName: string,
|
|
@@ -7,16 +8,10 @@ export default function buildRun(
|
|
|
7
8
|
status: BuildStatus,
|
|
8
9
|
b: Builder,
|
|
9
10
|
) {
|
|
11
|
+
functionBody = replaceForVarNames(functionBody, status);
|
|
12
|
+
|
|
10
13
|
status.imports.add("$run");
|
|
11
14
|
b.append(`$run(function ${functionName}() {`);
|
|
12
|
-
// HACK: If a value from a for loop is used in the function body,
|
|
13
|
-
// get it from the loop data to trigger an update when it is changed
|
|
14
|
-
for (let varName of status.forVarNames) {
|
|
15
|
-
functionBody = functionBody.replaceAll(
|
|
16
|
-
new RegExp(`([\\s\\(\\[\\{])${varName}([\\s\\.\\(\\)\\[\\]\\};])`, "g"),
|
|
17
|
-
`$1t_item.data.${varName}$2`,
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
15
|
b.append(functionBody);
|
|
21
16
|
b.append("});");
|
|
22
17
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { BuildStatus } from "./BuildStatus";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Variables used within a for loop need to be retrieved from the loop data.
|
|
5
|
+
*/
|
|
6
|
+
export default function replaceForVarNames(value: string, status: BuildStatus) {
|
|
7
|
+
// HACK: If a value from a for loop is used in the function body,
|
|
8
|
+
// get it from the loop data to trigger an update when it is changed
|
|
9
|
+
for (let varName of status.forVarNames) {
|
|
10
|
+
value = value.replaceAll(
|
|
11
|
+
new RegExp(`(^|\\s|\\(|\\[|\\{)${varName}($|\\s|\\.|\\(|\\)|\\[|\\]|\\}|;)`, "g"),
|
|
12
|
+
`$1t_item.data.${varName}$2`,
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
@@ -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(
|
|
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(
|
|
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
|
}
|