neobiz 4.7.0 → 5.0.1
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/{browser.js → browser_v4.js} +1 -1
- package/example/async_components.v.js +88 -0
- package/example/basic_syntax.v.js +86 -22
- package/example/control_flow.v.js +89 -54
- package/example/fromString.v.js +63 -20
- package/example/interact_withDOM.v.js +79 -10
- package/example/loop_and_Array.v.js +94 -46
- package/example/style.v.js +118 -24
- package/index.js +1 -20
- package/index2.js +859 -0
- package/indexw.js +21 -0
- package/package.json +1 -1
- package/readme.md +502 -534
- package/readme_v4.md +735 -0
- package/example/boolean_attributes.js +0 -17
- package/example/explicit-tag.v..js +0 -16
- package/example/innerText&html.v.js +0 -15
- package/example/multi_tag.v.js +0 -13
- package/example/orderChild.v.js +0 -15
package/index2.js
ADDED
|
@@ -0,0 +1,859 @@
|
|
|
1
|
+
/** @format */
|
|
2
|
+
|
|
3
|
+
const componentRegistry = new Map();
|
|
4
|
+
const styleRegistry = new Map();
|
|
5
|
+
const directiveRegistry = new Map();
|
|
6
|
+
const Null = Symbol("@null");
|
|
7
|
+
const SCOPE_KEY = Symbol("@scope");
|
|
8
|
+
|
|
9
|
+
const VOID_ELEMENTS = new Set([
|
|
10
|
+
"area", "base", "br", "col", "embed", "hr", "img",
|
|
11
|
+
"input", "link", "meta", "param", "source", "track", "wbr",
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
const RESERVED_KEYS = new Set([
|
|
15
|
+
"text", "html", "noprefix", "nopAttr", "tag", "bind", "when", "$head", "each",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
const defaultOptions = {
|
|
19
|
+
camelCase: false,
|
|
20
|
+
once: false,
|
|
21
|
+
nodoctype: false,
|
|
22
|
+
ignoreTag: false,
|
|
23
|
+
prefixTag: false,
|
|
24
|
+
prefixAttr: false,
|
|
25
|
+
pretty: false,
|
|
26
|
+
indentSize: 2,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
let currentOption = { ...defaultOptions };
|
|
30
|
+
|
|
31
|
+
const toKebabCase = (str) =>
|
|
32
|
+
str.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
|
|
33
|
+
|
|
34
|
+
const escapeHtml = (str) =>
|
|
35
|
+
String(str)
|
|
36
|
+
.replaceAll("&", "&")
|
|
37
|
+
.replaceAll("<", "<")
|
|
38
|
+
.replaceAll(">", ">")
|
|
39
|
+
.replaceAll("'", "'");
|
|
40
|
+
|
|
41
|
+
const serializeFunctionBody = (fn) => {
|
|
42
|
+
const fnStr = fn.toString().trim();
|
|
43
|
+
const bodyMatch = fnStr.match(/^[^{]*\{([\s\S]*)\}$/);
|
|
44
|
+
if (bodyMatch) return bodyMatch[1].trim();
|
|
45
|
+
const arrowMatch = fnStr.match(/^[^=]*=>\s*([\s\S]*)$/);
|
|
46
|
+
return arrowMatch ? arrowMatch[1].trim() : fnStr;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Mengurai sintaks Shorthand Emmet pada key (contoh: "div#app.box.shadow")
|
|
51
|
+
*/
|
|
52
|
+
function parseEmmetKey(rawKey) {
|
|
53
|
+
const trimmed = rawKey.trim();
|
|
54
|
+
if (!trimmed || trimmed === "$" || trimmed === "$comment") {
|
|
55
|
+
return { tag: trimmed, id: null, classes: [] };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const match = trimmed.match(/^([a-zA-Z0-9-]+)?((?:[#.][a-zA-Z0-9_-]+)*)$/);
|
|
59
|
+
if (!match || (!match[1] && !match[2])) {
|
|
60
|
+
return { tag: trimmed, id: null, classes: [] };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const tag = match[1] || "div";
|
|
64
|
+
const modifiers = match[2] || "";
|
|
65
|
+
let id = null;
|
|
66
|
+
const classes = [];
|
|
67
|
+
|
|
68
|
+
const tokens = modifiers.match(/[#.][a-zA-Z0-9_-]+/g) || [];
|
|
69
|
+
for (const token of tokens) {
|
|
70
|
+
if (token.startsWith("#")) {
|
|
71
|
+
id = token.slice(1);
|
|
72
|
+
} else if (token.startsWith(".")) {
|
|
73
|
+
classes.push(token.slice(1));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return { tag, id, classes };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Menyuntikkan variabel `bind` ke awal tubuh fungsi event handler
|
|
82
|
+
*/
|
|
83
|
+
function buildBoundEventCode(fn, bindObj) {
|
|
84
|
+
let fnBody = serializeFunctionBody(fn);
|
|
85
|
+
|
|
86
|
+
if (bindObj && typeof bindObj === "object" && !Array.isArray(bindObj)) {
|
|
87
|
+
const validEntries = Object.entries(bindObj).filter(
|
|
88
|
+
([k, v]) => /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(k) && v !== undefined
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
if (validEntries.length > 0) {
|
|
92
|
+
const keys = validEntries.map(([k]) => k).join(", ");
|
|
93
|
+
const safeObj = Object.fromEntries(validEntries);
|
|
94
|
+
const serializedData = JSON.stringify(safeObj)
|
|
95
|
+
.replaceAll("</script>", "<\\/script>")
|
|
96
|
+
.replaceAll("'", "\\'");
|
|
97
|
+
|
|
98
|
+
fnBody = `const { ${keys} } = ${serializedData}; ${fnBody}`;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return fnBody.replaceAll("'", "'");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Mengompilasi CSS khusus untuk `.scope()`
|
|
107
|
+
*/
|
|
108
|
+
function compileScopedCss(styleObj, scopeName) {
|
|
109
|
+
const attrSelector = `[data-nb-s="${scopeName}"]`;
|
|
110
|
+
|
|
111
|
+
const transformSelector = (sel) => {
|
|
112
|
+
return sel
|
|
113
|
+
.split(",")
|
|
114
|
+
.map((part) => {
|
|
115
|
+
const s = part.trim();
|
|
116
|
+
if (s === "self" || s === ":host") return attrSelector;
|
|
117
|
+
const pseudoIdx = s.indexOf(":");
|
|
118
|
+
if (pseudoIdx > 0) {
|
|
119
|
+
return `${s.slice(0, pseudoIdx)}${attrSelector}${s.slice(pseudoIdx)}`;
|
|
120
|
+
}
|
|
121
|
+
return `${s}${attrSelector}`;
|
|
122
|
+
})
|
|
123
|
+
.join(", ");
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const buildRules = (obj) => {
|
|
127
|
+
return Object.entries(obj)
|
|
128
|
+
.map(([key, val]) => {
|
|
129
|
+
if (val && typeof val === "object") {
|
|
130
|
+
if (key.startsWith("@")) {
|
|
131
|
+
return `${key}{${buildRules(val)}}`;
|
|
132
|
+
}
|
|
133
|
+
return `${transformSelector(key)}{${buildRules(val)}}`;
|
|
134
|
+
}
|
|
135
|
+
return `${toKebabCase(key)}:${val};`;
|
|
136
|
+
})
|
|
137
|
+
.join("");
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
return buildRules(styleObj);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Helper Pengumpul & Pengurut Aset `$head`
|
|
145
|
+
*/
|
|
146
|
+
function collectHeadAssets(headInput, headAssetsMap) {
|
|
147
|
+
const items = Array.isArray(headInput) ? headInput : [headInput];
|
|
148
|
+
|
|
149
|
+
for (const item of items) {
|
|
150
|
+
if (!item || typeof item !== "object") continue;
|
|
151
|
+
|
|
152
|
+
for (const [rawTag, rawConfig] of Object.entries(item)) {
|
|
153
|
+
const configs = Array.isArray(rawConfig) ? rawConfig : [rawConfig];
|
|
154
|
+
const emmet = parseEmmetKey(rawTag);
|
|
155
|
+
const tag = (rawConfig?.tag || emmet.tag).toLowerCase();
|
|
156
|
+
|
|
157
|
+
for (const cfg of configs) {
|
|
158
|
+
if (!cfg || typeof cfg !== "object") continue;
|
|
159
|
+
|
|
160
|
+
let dedupKey;
|
|
161
|
+
if (cfg.src) dedupKey = `${tag}:src:${cfg.src}`;
|
|
162
|
+
else if (cfg.href) dedupKey = `${tag}:href:${cfg.href}`;
|
|
163
|
+
else if (cfg.name) dedupKey = `${tag}:name:${cfg.name}`;
|
|
164
|
+
else if (cfg.property) dedupKey = `${tag}:prop:${cfg.property}`;
|
|
165
|
+
else if (cfg.charset) dedupKey = `${tag}:charset`;
|
|
166
|
+
else dedupKey = `${tag}:inline:${JSON.stringify(cfg)}`;
|
|
167
|
+
|
|
168
|
+
if (headAssetsMap.has(dedupKey)) continue;
|
|
169
|
+
|
|
170
|
+
let priority = 2;
|
|
171
|
+
if (
|
|
172
|
+
tag === "meta" ||
|
|
173
|
+
(tag === "link" && /preconnect|preload|dns-prefetch/.test(cfg.rel || ""))
|
|
174
|
+
) {
|
|
175
|
+
priority = 1;
|
|
176
|
+
} else if (tag === "link" || tag === "style") {
|
|
177
|
+
priority = 2;
|
|
178
|
+
} else if (tag === "script") {
|
|
179
|
+
priority = cfg.defer || cfg.async || cfg.type === "module" ? 4 : 3;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
headAssetsMap.set(dedupKey, {
|
|
183
|
+
priority,
|
|
184
|
+
node: { [rawTag]: cfg },
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* FITUR LOOP DECLARATIVE (`each`):
|
|
193
|
+
* Mengevaluasi properti dinamis `(item, index) => ...` dan placeholder `$item` / `$index`
|
|
194
|
+
*/
|
|
195
|
+
function interpolateLoopTokens(str, item, index) {
|
|
196
|
+
if (!str.includes("$item") && !str.includes("$index")) return str;
|
|
197
|
+
|
|
198
|
+
return str
|
|
199
|
+
.replaceAll("$index", String(index))
|
|
200
|
+
.replace(/\$item(?:\.([a-zA-Z0-9_$.]+))?/g, (_, path) => {
|
|
201
|
+
if (!path) return item != null ? String(item) : "";
|
|
202
|
+
const resolved = path.split(".").reduce((acc, k) => acc?.[k], item);
|
|
203
|
+
return resolved != null ? String(resolved) : "";
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function resolveLoopNode(node, item, index) {
|
|
208
|
+
if (typeof node === "function") {
|
|
209
|
+
return node(item, index);
|
|
210
|
+
}
|
|
211
|
+
if (typeof node === "string") {
|
|
212
|
+
return interpolateLoopTokens(node, item, index);
|
|
213
|
+
}
|
|
214
|
+
if (Array.isArray(node)) {
|
|
215
|
+
return node.map((child) => resolveLoopNode(child, item, index));
|
|
216
|
+
}
|
|
217
|
+
if (node !== null && typeof node === "object" && node !== Null) {
|
|
218
|
+
const cloned = {};
|
|
219
|
+
if (node[SCOPE_KEY]) cloned[SCOPE_KEY] = node[SCOPE_KEY];
|
|
220
|
+
|
|
221
|
+
for (const [k, v] of Object.entries(node)) {
|
|
222
|
+
if (k === "each") continue;
|
|
223
|
+
|
|
224
|
+
// Jangan eksekusi fungsi event handler (`on*`) saat compile time; biarkan menjadi atribut event HTML
|
|
225
|
+
const isEventHandler =
|
|
226
|
+
typeof v === "function" && k.toLowerCase().startsWith("on");
|
|
227
|
+
|
|
228
|
+
cloned[k] = isEventHandler ? v : resolveLoopNode(v, item, index);
|
|
229
|
+
}
|
|
230
|
+
return cloned;
|
|
231
|
+
}
|
|
232
|
+
return node;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function expandEachItems(rawVal) {
|
|
236
|
+
const source = rawVal.each;
|
|
237
|
+
const items =
|
|
238
|
+
typeof source === "number" && source >= 0
|
|
239
|
+
? Array.from({ length: Math.floor(source) }, (_, i) => i)
|
|
240
|
+
: Array.isArray(source)
|
|
241
|
+
? source
|
|
242
|
+
: [];
|
|
243
|
+
|
|
244
|
+
return items.map((item, index) => resolveLoopNode(rawVal, item, index));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Selector Query Engine (`.query()`) untuk Pohon Objek Neobiz
|
|
249
|
+
*/
|
|
250
|
+
function matchNodeSelector(rawKey, nodeObj, parsedSelector) {
|
|
251
|
+
const emmet = parseEmmetKey(rawKey);
|
|
252
|
+
const actualTag = (nodeObj.tag || emmet.tag).toLowerCase();
|
|
253
|
+
|
|
254
|
+
if (parsedSelector.tag && actualTag !== parsedSelector.tag.toLowerCase()) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const actualId = nodeObj.id ?? emmet.id;
|
|
259
|
+
if (parsedSelector.id && actualId !== parsedSelector.id) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (parsedSelector.classes.length > 0) {
|
|
264
|
+
const classStr = typeof nodeObj.class === "string" ? nodeObj.class : "";
|
|
265
|
+
const actualClasses = new Set([
|
|
266
|
+
...emmet.classes,
|
|
267
|
+
...classStr.split(/\s+/).filter(Boolean),
|
|
268
|
+
]);
|
|
269
|
+
for (const reqClass of parsedSelector.classes) {
|
|
270
|
+
if (!actualClasses.has(reqClass)) return false;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function queryTree(target, selector, callback) {
|
|
278
|
+
const parsedSelector = parseEmmetKey(selector);
|
|
279
|
+
if (/^[#.]/.test(selector.trim())) {
|
|
280
|
+
parsedSelector.tag = null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const traverse = (current) => {
|
|
284
|
+
if (!current || typeof current !== "object") return;
|
|
285
|
+
|
|
286
|
+
if (Array.isArray(current)) {
|
|
287
|
+
current.forEach(traverse);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
for (const [key, val] of Object.entries(current)) {
|
|
292
|
+
if (key === "$comment" || key === "$head" || RESERVED_KEYS.has(key)) continue;
|
|
293
|
+
|
|
294
|
+
if (Array.isArray(val)) {
|
|
295
|
+
val.forEach((item) => {
|
|
296
|
+
if (item && typeof item === "object") {
|
|
297
|
+
if (key.trim() !== "$" && matchNodeSelector(key, item, parsedSelector)) {
|
|
298
|
+
callback(item);
|
|
299
|
+
}
|
|
300
|
+
traverse(item);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
} else if (val && typeof val === "object" && val !== Null) {
|
|
304
|
+
if (key.trim() !== "$" && matchNodeSelector(key, val, parsedSelector)) {
|
|
305
|
+
callback(val);
|
|
306
|
+
}
|
|
307
|
+
traverse(val);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
traverse(target);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* ASYNC RESOLVER: Menyelesaikan semua Promise secara paralel
|
|
317
|
+
*/
|
|
318
|
+
async function resolveAsyncTree(node) {
|
|
319
|
+
if (node instanceof Promise) {
|
|
320
|
+
return resolveAsyncTree(await node);
|
|
321
|
+
}
|
|
322
|
+
if (typeof node === "function") {
|
|
323
|
+
return node;
|
|
324
|
+
}
|
|
325
|
+
if (Array.isArray(node)) {
|
|
326
|
+
const resolvedArr = await Promise.all(
|
|
327
|
+
node.map((item) => resolveAsyncTree(item))
|
|
328
|
+
);
|
|
329
|
+
if (node[SCOPE_KEY]) resolvedArr[SCOPE_KEY] = node[SCOPE_KEY];
|
|
330
|
+
return resolvedArr;
|
|
331
|
+
}
|
|
332
|
+
if (node !== null && typeof node === "object" && node !== Null) {
|
|
333
|
+
const entries = await Promise.all(
|
|
334
|
+
Object.entries(node).map(async ([k, v]) => [k, await resolveAsyncTree(v)])
|
|
335
|
+
);
|
|
336
|
+
const result = Object.fromEntries(entries);
|
|
337
|
+
if (node[SCOPE_KEY]) result[SCOPE_KEY] = node[SCOPE_KEY];
|
|
338
|
+
return result;
|
|
339
|
+
}
|
|
340
|
+
return node;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* REPULSE: Mengubah HTML String menjadi Objek Neobiz
|
|
345
|
+
*/
|
|
346
|
+
function htmlToNeobiz(htmlString) {
|
|
347
|
+
const cleanHtml = String(htmlString).trim();
|
|
348
|
+
if (!cleanHtml) return {};
|
|
349
|
+
|
|
350
|
+
const tokens = cleanHtml.match(/<!--[\s\S]*?-->|<\/?[^>]+>|[^<]+/g) || [];
|
|
351
|
+
const root = { children: [] };
|
|
352
|
+
const stack = [root];
|
|
353
|
+
|
|
354
|
+
for (const token of tokens) {
|
|
355
|
+
const trimmed = token.trim();
|
|
356
|
+
if (!trimmed) continue;
|
|
357
|
+
|
|
358
|
+
const currentParent = stack[stack.length - 1];
|
|
359
|
+
|
|
360
|
+
if (trimmed.startsWith("<!--") && trimmed.endsWith("-->")) {
|
|
361
|
+
const commentText = trimmed.slice(4, -3).trim();
|
|
362
|
+
currentParent.children.push({ isComment: true, content: commentText });
|
|
363
|
+
} else if (trimmed.startsWith("</")) {
|
|
364
|
+
if (stack.length > 1) stack.pop();
|
|
365
|
+
} else if (trimmed.startsWith("<")) {
|
|
366
|
+
const isSelfClosing = trimmed.endsWith("/>");
|
|
367
|
+
const tagMatch = trimmed.match(/^<([a-zA-Z0-9-]+)/);
|
|
368
|
+
if (!tagMatch) continue;
|
|
369
|
+
|
|
370
|
+
const tagName = tagMatch[1].toLowerCase();
|
|
371
|
+
const attrString = trimmed
|
|
372
|
+
.slice(tagMatch[0].length, isSelfClosing ? -2 : -1)
|
|
373
|
+
.trim();
|
|
374
|
+
|
|
375
|
+
const nodeObj = {};
|
|
376
|
+
const attrRegex =
|
|
377
|
+
/([a-zA-Z0-9-:@]+)(?:=(?:"([^"]*)"|'([^']*)'|([^\s"'>]+)))?/g;
|
|
378
|
+
let match;
|
|
379
|
+
|
|
380
|
+
while ((match = attrRegex.exec(attrString)) !== null) {
|
|
381
|
+
const [, key, vDouble, vSingle, vUnquoted] = match;
|
|
382
|
+
const val = vDouble ?? vSingle ?? vUnquoted;
|
|
383
|
+
nodeObj[key] = val === undefined ? Null : val;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const element = { tag: tagName, obj: nodeObj, children: [] };
|
|
387
|
+
currentParent.children.push(element);
|
|
388
|
+
|
|
389
|
+
if (!isSelfClosing && !VOID_ELEMENTS.has(tagName)) {
|
|
390
|
+
stack.push(element);
|
|
391
|
+
}
|
|
392
|
+
} else {
|
|
393
|
+
currentParent.children.push({ isText: true, content: trimmed });
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function buildNeobizTree(node) {
|
|
398
|
+
const result = { ...node.obj };
|
|
399
|
+
const tagCounts = new Map();
|
|
400
|
+
|
|
401
|
+
for (const child of node.children) {
|
|
402
|
+
if (child.isComment) {
|
|
403
|
+
result.$comment = child.content;
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
if (child.isText) {
|
|
407
|
+
result.text = result.text
|
|
408
|
+
? `${result.text} ${child.content}`
|
|
409
|
+
: child.content;
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const childValue = buildNeobizTree(child);
|
|
414
|
+
const count = tagCounts.get(child.tag) || 0;
|
|
415
|
+
tagCounts.set(child.tag, count + 1);
|
|
416
|
+
|
|
417
|
+
const keyName =
|
|
418
|
+
count === 0 ? child.tag : `${child.tag}${" ".repeat(count)}`;
|
|
419
|
+
result[keyName] = childValue;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return result;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const finalOutput = {};
|
|
426
|
+
const rootCounts = new Map();
|
|
427
|
+
|
|
428
|
+
for (const topNode of root.children) {
|
|
429
|
+
if (!topNode.isText && !topNode.isComment) {
|
|
430
|
+
const count = rootCounts.get(topNode.tag) || 0;
|
|
431
|
+
rootCounts.set(topNode.tag, count + 1);
|
|
432
|
+
const key =
|
|
433
|
+
count === 0 ? topNode.tag : `${topNode.tag}${" ".repeat(count)}`;
|
|
434
|
+
finalOutput[key] = buildNeobizTree(topNode);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return finalOutput;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* COMPILER INTI
|
|
443
|
+
*/
|
|
444
|
+
function compileNode(node, activeOpt, ctx, depth = 0, activeScope = null) {
|
|
445
|
+
if (node == null || typeof node !== "object") return "";
|
|
446
|
+
|
|
447
|
+
const currentScope = node[SCOPE_KEY] ?? activeScope;
|
|
448
|
+
if (currentScope) {
|
|
449
|
+
ctx.usedScopes.add(currentScope);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const pad = activeOpt.pretty ? " ".repeat(depth * activeOpt.indentSize) : "";
|
|
453
|
+
const nl = activeOpt.pretty ? "\n" : "";
|
|
454
|
+
const chunks = [];
|
|
455
|
+
|
|
456
|
+
for (const [rawKey, rawVal] of Object.entries(node)) {
|
|
457
|
+
if (rawVal == null || rawVal === false) continue;
|
|
458
|
+
|
|
459
|
+
// HTML Comment Node (`$comment`)
|
|
460
|
+
if (rawKey.trim() === "$comment") {
|
|
461
|
+
const commentStr = String(rawVal).replaceAll("--", "- -");
|
|
462
|
+
const isImportant = commentStr.startsWith("!");
|
|
463
|
+
if (activeOpt.pretty || isImportant) {
|
|
464
|
+
chunks.push(`${pad}<!-- ${commentStr} -->`);
|
|
465
|
+
}
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// Hoisting aset `$head`
|
|
470
|
+
if (rawKey.trim() === "$head") {
|
|
471
|
+
collectHeadAssets(rawVal, ctx.headAssets);
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// Fragment ($): Mendukung array, objek tunggal, `when`, dan `each`
|
|
476
|
+
if (rawKey.trim() === "$") {
|
|
477
|
+
if (!Array.isArray(rawVal) && typeof rawVal === "object") {
|
|
478
|
+
if ("each" in rawVal) {
|
|
479
|
+
const expanded = expandEachItems(rawVal);
|
|
480
|
+
for (const itemNode of expanded) {
|
|
481
|
+
chunks.push(compileNode(itemNode, activeOpt, ctx, depth, currentScope));
|
|
482
|
+
}
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
if ("when" in rawVal && !rawVal.when) {
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const items = Array.isArray(rawVal) ? rawVal : [rawVal];
|
|
491
|
+
for (const item of items) {
|
|
492
|
+
chunks.push(compileNode(item, activeOpt, ctx, depth, currentScope));
|
|
493
|
+
}
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
if (Array.isArray(rawVal)) {
|
|
498
|
+
for (const item of rawVal) {
|
|
499
|
+
chunks.push(
|
|
500
|
+
compileNode({ [rawKey]: item }, activeOpt, ctx, depth, currentScope)
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
if (typeof rawVal !== "object") continue;
|
|
507
|
+
|
|
508
|
+
// Declarative Loop (`each`) pada elemen tag langsung
|
|
509
|
+
if ("each" in rawVal) {
|
|
510
|
+
const expandedItems = expandEachItems(rawVal);
|
|
511
|
+
for (const itemNode of expandedItems) {
|
|
512
|
+
chunks.push(
|
|
513
|
+
compileNode({ [rawKey]: itemNode }, activeOpt, ctx, depth, currentScope)
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// Conditional Rendering (`when`)
|
|
520
|
+
if ("when" in rawVal && !rawVal.when) {
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const nodeScope = rawVal[SCOPE_KEY] ?? currentScope;
|
|
525
|
+
if (nodeScope) {
|
|
526
|
+
ctx.usedScopes.add(nodeScope);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const localOpt = {
|
|
530
|
+
...activeOpt,
|
|
531
|
+
prefixTag: rawVal.noprefix ? false : activeOpt.prefixTag,
|
|
532
|
+
prefixAttr: rawVal.nopAttr ? false : activeOpt.prefixAttr,
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
const emmet = parseEmmetKey(rawKey);
|
|
536
|
+
let tagName = emmet.tag;
|
|
537
|
+
|
|
538
|
+
if (!localOpt.ignoreTag && rawVal.tag) {
|
|
539
|
+
tagName = rawVal.tag;
|
|
540
|
+
}
|
|
541
|
+
if (localOpt.camelCase) {
|
|
542
|
+
tagName = toKebabCase(tagName);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
const fullTag = `${localOpt.prefixTag || ""}${tagName}`;
|
|
546
|
+
const attrs = [];
|
|
547
|
+
const children = [];
|
|
548
|
+
let hasElementChild = false;
|
|
549
|
+
|
|
550
|
+
if (nodeScope && tagName !== "style" && tagName !== "script") {
|
|
551
|
+
attrs.push(` data-nb-s='${nodeScope}'`);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
const finalId = rawVal.id ?? emmet.id;
|
|
555
|
+
const mergedClasses = [
|
|
556
|
+
...emmet.classes,
|
|
557
|
+
...(rawVal.class ? [String(rawVal.class)] : []),
|
|
558
|
+
];
|
|
559
|
+
|
|
560
|
+
if (finalId) {
|
|
561
|
+
const idAttr = `${localOpt.prefixAttr || ""}id`;
|
|
562
|
+
attrs.push(` ${idAttr}='${String(finalId)}'`);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
if (mergedClasses.length > 0) {
|
|
566
|
+
const classAttr = `${localOpt.prefixAttr || ""}class`;
|
|
567
|
+
attrs.push(` ${classAttr}='${mergedClasses.join(" ")}'`);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
for (const [propKey, propVal] of Object.entries(rawVal)) {
|
|
571
|
+
if (propVal === undefined || propVal === false || propVal === null) continue;
|
|
572
|
+
if (propKey === "id" || propKey === "class") continue;
|
|
573
|
+
|
|
574
|
+
if (propKey === "$head") {
|
|
575
|
+
collectHeadAssets(propVal, ctx.headAssets);
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
if (directiveRegistry.has(propKey)) {
|
|
580
|
+
const directiveHandler = directiveRegistry.get(propKey);
|
|
581
|
+
directiveHandler(propVal, {
|
|
582
|
+
attrs,
|
|
583
|
+
children,
|
|
584
|
+
tagName: fullTag,
|
|
585
|
+
node: rawVal,
|
|
586
|
+
escapeHtml,
|
|
587
|
+
});
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
if (propKey === "text") {
|
|
592
|
+
children.push(escapeHtml(propVal));
|
|
593
|
+
continue;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
if (propKey === "html") {
|
|
597
|
+
const rawHtml =
|
|
598
|
+
typeof propVal === "function"
|
|
599
|
+
? serializeFunctionBody(propVal)
|
|
600
|
+
: String(propVal);
|
|
601
|
+
children.push(rawHtml);
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
if (RESERVED_KEYS.has(propKey)) continue;
|
|
606
|
+
|
|
607
|
+
if (propVal === Null || propVal === true) {
|
|
608
|
+
const formattedKey = localOpt.camelCase ? toKebabCase(propKey) : propKey;
|
|
609
|
+
const fullAttr = `${localOpt.prefixAttr || ""}${formattedKey}`;
|
|
610
|
+
attrs.push(` ${fullAttr}`);
|
|
611
|
+
continue;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
if (typeof propVal === "function") {
|
|
615
|
+
const attrName = localOpt.camelCase ? toKebabCase(propKey) : propKey;
|
|
616
|
+
const boundCode = buildBoundEventCode(propVal, rawVal.bind);
|
|
617
|
+
attrs.push(` ${attrName}='${boundCode}'`);
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
if (typeof propVal === "object") {
|
|
622
|
+
hasElementChild = true;
|
|
623
|
+
children.push(
|
|
624
|
+
compileNode({ [propKey]: propVal }, localOpt, ctx, depth + 1, nodeScope)
|
|
625
|
+
);
|
|
626
|
+
continue;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
const formattedKey = localOpt.camelCase ? toKebabCase(propKey) : propKey;
|
|
630
|
+
const fullAttr = `${localOpt.prefixAttr || ""}${formattedKey}`;
|
|
631
|
+
attrs.push(` ${fullAttr}='${String(propVal)}'`);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
const isMainHead = tagName.toLowerCase() === "head";
|
|
635
|
+
const headMarker = isMainHead ? "__NB_HEAD_SLOT__" : "";
|
|
636
|
+
|
|
637
|
+
if (VOID_ELEMENTS.has(tagName.toLowerCase())) {
|
|
638
|
+
chunks.push(`${pad}<${fullTag}${attrs.join("")}/>`);
|
|
639
|
+
} else if ((hasElementChild || isMainHead) && activeOpt.pretty) {
|
|
640
|
+
const innerStr = children.length > 0 ? `${nl}${children.join(nl)}` : "";
|
|
641
|
+
chunks.push(
|
|
642
|
+
`${pad}<${fullTag}${attrs.join("")}>${innerStr}${headMarker}${nl}${pad}</${fullTag}>`
|
|
643
|
+
);
|
|
644
|
+
} else {
|
|
645
|
+
chunks.push(
|
|
646
|
+
`${pad}<${fullTag}${attrs.join("")}>${children.join("")}${headMarker}</${fullTag}>`
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
return chunks.join(nl);
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
function renderEngine(target) {
|
|
655
|
+
const activeOpt = { ...currentOption };
|
|
656
|
+
if (currentOption.once) {
|
|
657
|
+
currentOption = { ...defaultOptions };
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
const ctx = {
|
|
661
|
+
usedScopes: new Set(),
|
|
662
|
+
headAssets: new Map(),
|
|
663
|
+
};
|
|
664
|
+
|
|
665
|
+
const nl = activeOpt.pretty ? "\n" : "";
|
|
666
|
+
const doctype = activeOpt.nodoctype ? "" : `<!DOCTYPE html>${nl}`;
|
|
667
|
+
|
|
668
|
+
let body = Array.isArray(target)
|
|
669
|
+
? target.map((item) => compileNode(item, activeOpt, ctx, 0, null)).join(nl)
|
|
670
|
+
: compileNode(target, activeOpt, ctx, 0, null);
|
|
671
|
+
|
|
672
|
+
const sortedHeadNodes = [...ctx.headAssets.values()]
|
|
673
|
+
.sort((a, b) => a.priority - b.priority)
|
|
674
|
+
.map((entry) =>
|
|
675
|
+
compileNode(
|
|
676
|
+
entry.node,
|
|
677
|
+
activeOpt,
|
|
678
|
+
ctx,
|
|
679
|
+
body.includes("__NB_HEAD_SLOT__") ? 2 : 0,
|
|
680
|
+
null
|
|
681
|
+
)
|
|
682
|
+
);
|
|
683
|
+
|
|
684
|
+
const hoistedHeadHtml =
|
|
685
|
+
sortedHeadNodes.length > 0 ? `${nl}${sortedHeadNodes.join(nl)}` : "";
|
|
686
|
+
|
|
687
|
+
if (body.includes("__NB_HEAD_SLOT__")) {
|
|
688
|
+
body = body.replace("__NB_HEAD_SLOT__", hoistedHeadHtml);
|
|
689
|
+
} else if (hoistedHeadHtml) {
|
|
690
|
+
body = `${sortedHeadNodes.join(nl)}${nl}${body}`;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
const scopedCssBlocks = [];
|
|
694
|
+
for (const scopeName of ctx.usedScopes) {
|
|
695
|
+
if (styleRegistry.has(scopeName)) {
|
|
696
|
+
scopedCssBlocks.push(compileScopedCss(styleRegistry.get(scopeName), scopeName));
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
const styleTag =
|
|
701
|
+
scopedCssBlocks.length > 0
|
|
702
|
+
? `${nl}<style>${scopedCssBlocks.join(nl)}</style>`
|
|
703
|
+
: "";
|
|
704
|
+
|
|
705
|
+
return `${doctype}${body}${styleTag}`;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
function compileCss(obj, isInline = false) {
|
|
709
|
+
return Object.entries(obj)
|
|
710
|
+
.map(([key, val]) => {
|
|
711
|
+
if (val && typeof val === "object") {
|
|
712
|
+
return `${key}{${compileCss(val, true)}}`;
|
|
713
|
+
}
|
|
714
|
+
const prop = toKebabCase(key);
|
|
715
|
+
return `${prop}:${val};`;
|
|
716
|
+
})
|
|
717
|
+
.join(isInline ? "" : "\n");
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
Object.defineProperties(Object.prototype, {
|
|
721
|
+
Null: { value: Null },
|
|
722
|
+
renderOption: {
|
|
723
|
+
value(opt) {
|
|
724
|
+
currentOption = { ...defaultOptions, ...opt };
|
|
725
|
+
},
|
|
726
|
+
},
|
|
727
|
+
addDirective: {
|
|
728
|
+
value(name, handler) {
|
|
729
|
+
if (typeof handler === "function") {
|
|
730
|
+
directiveRegistry.set(name, handler);
|
|
731
|
+
}
|
|
732
|
+
},
|
|
733
|
+
},
|
|
734
|
+
removeDirective: {
|
|
735
|
+
value(name) {
|
|
736
|
+
directiveRegistry.delete(name);
|
|
737
|
+
},
|
|
738
|
+
},
|
|
739
|
+
regStyle: {
|
|
740
|
+
value(name) {
|
|
741
|
+
styleRegistry.set(name, this);
|
|
742
|
+
return this;
|
|
743
|
+
},
|
|
744
|
+
},
|
|
745
|
+
scope: {
|
|
746
|
+
value(name) {
|
|
747
|
+
this[SCOPE_KEY] = name;
|
|
748
|
+
return this;
|
|
749
|
+
},
|
|
750
|
+
},
|
|
751
|
+
query: {
|
|
752
|
+
value(selector, callback) {
|
|
753
|
+
if (typeof callback === "function") {
|
|
754
|
+
queryTree(this, selector, callback);
|
|
755
|
+
}
|
|
756
|
+
return this;
|
|
757
|
+
},
|
|
758
|
+
},
|
|
759
|
+
_render: {
|
|
760
|
+
get() {
|
|
761
|
+
return renderEngine(this);
|
|
762
|
+
},
|
|
763
|
+
},
|
|
764
|
+
_renderAsync: {
|
|
765
|
+
get() {
|
|
766
|
+
return (async () => {
|
|
767
|
+
const resolvedTarget = await resolveAsyncTree(this);
|
|
768
|
+
return renderEngine(resolvedTarget);
|
|
769
|
+
})();
|
|
770
|
+
},
|
|
771
|
+
},
|
|
772
|
+
reg: {
|
|
773
|
+
value(name) {
|
|
774
|
+
if (!componentRegistry.has(name)) componentRegistry.set(name, this);
|
|
775
|
+
return this;
|
|
776
|
+
},
|
|
777
|
+
},
|
|
778
|
+
regm: {
|
|
779
|
+
value(name) {
|
|
780
|
+
if (componentRegistry.has(name)) componentRegistry.set(name, this);
|
|
781
|
+
return this;
|
|
782
|
+
},
|
|
783
|
+
},
|
|
784
|
+
unreg: {
|
|
785
|
+
value(name) {
|
|
786
|
+
componentRegistry.delete(name);
|
|
787
|
+
},
|
|
788
|
+
},
|
|
789
|
+
load: {
|
|
790
|
+
value(name) {
|
|
791
|
+
return componentRegistry.get(name) ?? null;
|
|
792
|
+
},
|
|
793
|
+
},
|
|
794
|
+
css: {
|
|
795
|
+
get() {
|
|
796
|
+
return { text: compileCss(this, false) };
|
|
797
|
+
},
|
|
798
|
+
},
|
|
799
|
+
cssText: {
|
|
800
|
+
get() {
|
|
801
|
+
return compileCss(this, true);
|
|
802
|
+
},
|
|
803
|
+
},
|
|
804
|
+
alter: {
|
|
805
|
+
value(fn) {
|
|
806
|
+
fn(this);
|
|
807
|
+
return this;
|
|
808
|
+
},
|
|
809
|
+
},
|
|
810
|
+
});
|
|
811
|
+
|
|
812
|
+
Object.defineProperty(String.prototype, "repulse", {
|
|
813
|
+
get() {
|
|
814
|
+
return htmlToNeobiz(this);
|
|
815
|
+
},
|
|
816
|
+
});
|
|
817
|
+
|
|
818
|
+
Object.defineProperty(Array.prototype, "range", {
|
|
819
|
+
value(length, start = 0) {
|
|
820
|
+
return Array.from({ length }, (_, i) => start + i);
|
|
821
|
+
},
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* ISOMORPHIC BROWSER DOM SUPPORT (Document & Element .query, .htmlObj, .appendObj)
|
|
826
|
+
*/
|
|
827
|
+
if (typeof Element !== "undefined" && typeof Document !== "undefined") {
|
|
828
|
+
const domQueryMethod = {
|
|
829
|
+
value(selector, callback) {
|
|
830
|
+
if (typeof callback === "function") {
|
|
831
|
+
this.querySelectorAll(selector).forEach((el) => callback(el));
|
|
832
|
+
}
|
|
833
|
+
return this;
|
|
834
|
+
},
|
|
835
|
+
};
|
|
836
|
+
|
|
837
|
+
Object.defineProperty(Document.prototype, "query", domQueryMethod);
|
|
838
|
+
Object.defineProperty(Element.prototype, "query", domQueryMethod);
|
|
839
|
+
|
|
840
|
+
Object.defineProperties(Element.prototype, {
|
|
841
|
+
htmlObj: {
|
|
842
|
+
set(neobizObj) {
|
|
843
|
+
const prevOpt = { ...currentOption };
|
|
844
|
+
currentOption.nodoctype = true;
|
|
845
|
+
this.innerHTML = renderEngine(neobizObj);
|
|
846
|
+
currentOption = prevOpt;
|
|
847
|
+
},
|
|
848
|
+
},
|
|
849
|
+
appendObj: {
|
|
850
|
+
value(neobizObj) {
|
|
851
|
+
const prevOpt = { ...currentOption };
|
|
852
|
+
currentOption.nodoctype = true;
|
|
853
|
+
this.insertAdjacentHTML("beforeend", renderEngine(neobizObj));
|
|
854
|
+
currentOption = prevOpt;
|
|
855
|
+
return this;
|
|
856
|
+
},
|
|
857
|
+
},
|
|
858
|
+
});
|
|
859
|
+
}
|