chocola 1.3.7 → 1.3.8
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { JSDOM } from "jsdom";
|
|
2
2
|
import { extractContextFromElement } from "./dom-processor.js";
|
|
3
|
-
import { genRandomId, incrementAlfabet } from "./utils.js";
|
|
3
|
+
import { genRandomId, incrementAlfabet, throwError } from "./utils.js";
|
|
4
4
|
import chalk from "chalk";
|
|
5
5
|
import beautify from "js-beautify";
|
|
6
6
|
|
|
@@ -17,6 +17,20 @@ function scopeCss(cssString, cssId) {
|
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
function interpolateNode(node, ctxProxy) {
|
|
21
|
+
if (node.nodeType === 3) {
|
|
22
|
+
node.textContent = node.textContent.replace(/\{([^}]+)\}/g, (_, expr) => {
|
|
23
|
+
try {
|
|
24
|
+
return Function("ctx", `with(ctx) { return (${expr}); }`)(ctxProxy);
|
|
25
|
+
} catch {
|
|
26
|
+
return "";
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
} else {
|
|
30
|
+
node.childNodes.forEach(child => interpolateNode(child, ctxProxy));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
20
34
|
|
|
21
35
|
/**
|
|
22
36
|
* Processes a single component element and inserts it into the DOM
|
|
@@ -43,31 +57,47 @@ export function processComponentElement(
|
|
|
43
57
|
const ctx = extractContextFromElement(element);
|
|
44
58
|
const srcInnerHtml = element.innerHTML;
|
|
45
59
|
|
|
60
|
+
const ctxProxy = new Proxy(ctx, {
|
|
61
|
+
has() { return true; },
|
|
62
|
+
get(target, key) { return target[key]; }
|
|
63
|
+
});
|
|
64
|
+
|
|
46
65
|
const instance = loadedComponents.get(compName);
|
|
47
66
|
if (!instance || instance === undefined) return false;
|
|
48
67
|
|
|
49
68
|
if (instance.body) {
|
|
50
69
|
let body = instance.body;
|
|
51
|
-
|
|
52
|
-
/(?<!\b(?:if|del-if)=)\{(\w+)\}/g,
|
|
53
|
-
(_, key) => ctx[key] || ""
|
|
54
|
-
);
|
|
70
|
+
|
|
55
71
|
const fragment = JSDOM.fragment(body);
|
|
56
72
|
const children = Array.from(fragment.querySelectorAll("*"));
|
|
73
|
+
|
|
57
74
|
children.forEach(child => {
|
|
75
|
+
const reservedAttrs = ["if", "del-if"];
|
|
76
|
+
Array.from(child.attributes).forEach(attribute => {
|
|
77
|
+
if (!attribute || attribute === undefined) return;
|
|
78
|
+
if (reservedAttrs.includes(attribute.name)) return;
|
|
79
|
+
attribute.value = attribute.value.replace(
|
|
80
|
+
/\{([^}]+)\}/g,
|
|
81
|
+
(_, key) => ctx[key] ?? ""
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
|
|
58
85
|
["if", "del-if"].forEach(statement => {
|
|
59
86
|
const statAtt = child.getAttribute(statement);
|
|
60
87
|
if (!statAtt) return;
|
|
61
88
|
const expr = statAtt.slice(1, -1);
|
|
62
|
-
const
|
|
63
|
-
|
|
89
|
+
const eva = ctxProxy[expr] ? ctxProxy[expr].slice(1, -1) : null;
|
|
90
|
+
const fn = new Function("ctx", `{ return (${eva}); }`);
|
|
91
|
+
if (!fn()) {
|
|
64
92
|
if (statement === "if") child.style.display = "none";
|
|
65
93
|
if (statement === "del-if") child.remove();
|
|
66
94
|
}
|
|
67
|
-
|
|
68
95
|
child.removeAttribute(statement);
|
|
69
96
|
});
|
|
97
|
+
|
|
98
|
+
interpolateNode(child, ctxProxy)
|
|
70
99
|
});
|
|
100
|
+
|
|
71
101
|
const firstChild = fragment.firstChild;
|
|
72
102
|
|
|
73
103
|
if (firstChild && firstChild.nodeType === 1) {
|