@yannosay/sinth 0.7.0 → 0.8.0
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/README.md +31 -6
- package/package.json +1 -1
- package/src/sinth.ts +436 -375
package/README.md
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
# Sinth
|
|
1
|
+
# Sinth
|
|
2
2
|
|
|
3
3
|
The language that fixes HTML. Declarative. Reactive. Compiles to pure HTML.
|
|
4
4
|
|
|
5
5
|
|
|
6
|
+
[](https://www.npmjs.com/package/@yannosay/sinth)
|
|
7
|
+
[](https://www.npmjs.com/package/@yannosay/sinth)
|
|
8
|
+
[](https://github.com/Yannosay/sinth/blob/main/LICENSE)
|
|
6
9
|
|
|
7
10
|
## Why Sinth?
|
|
8
11
|
|
|
9
12
|
#### Reactive forms in one line.
|
|
10
|
-
```
|
|
13
|
+
```ts
|
|
11
14
|
Input(bind: userName, placeholder: "Enter your name")
|
|
12
15
|
Paragraph { "Hello, " + userName }
|
|
13
16
|
```
|
|
@@ -15,7 +18,7 @@ Paragraph { "Hello, " + userName }
|
|
|
15
18
|
No useState. No onChange. No e.target.value. Just bind.
|
|
16
19
|
|
|
17
20
|
#### Logic and UI live together.
|
|
18
|
-
```
|
|
21
|
+
```ts
|
|
19
22
|
if user.isAdmin {
|
|
20
23
|
Button(onClick: "deletePost()") { "Delete" }
|
|
21
24
|
}
|
|
@@ -32,11 +35,22 @@ Paragraph(delay: index * 300) { item.name }
|
|
|
32
35
|
|
|
33
36
|
Staggered animations. One expression. No useEffect. No setTimeout chains.
|
|
34
37
|
|
|
35
|
-
####
|
|
36
|
-
|
|
38
|
+
#### Multiple actions, single handler.
|
|
39
|
+
```ts
|
|
40
|
+
Button(onClick: show = not show; deleted = true) { "Toggle & Delete" }
|
|
41
|
+
Chain statements with ;. No wrapper functions. No script blocks.
|
|
42
|
+
```
|
|
37
43
|
|
|
44
|
+
```ts
|
|
45
|
+
if done == true {
|
|
46
|
+
remove "myElement"
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
Delete elements for good. No virtual DOM diffing.
|
|
38
50
|
|
|
51
|
+
Compiles to pure HTML.
|
|
39
52
|
|
|
53
|
+
#### Ready to upload wherever you want.
|
|
40
54
|
|
|
41
55
|
|
|
42
56
|
# Install
|
|
@@ -56,7 +70,7 @@ Open your browser. Edit your .sinth file. Watch it update live.
|
|
|
56
70
|
|
|
57
71
|
|
|
58
72
|
## Your First Sinth File
|
|
59
|
-
```
|
|
73
|
+
```ts
|
|
60
74
|
page
|
|
61
75
|
title = "My Sinth Project"
|
|
62
76
|
|
|
@@ -89,6 +103,16 @@ Main {
|
|
|
89
103
|
|
|
90
104
|
###### Nothing you like? Give us feedback on our [Discord Server](https://discord.gg/SUvcrafTQm)!
|
|
91
105
|
|
|
106
|
+
## Shared Runtime
|
|
107
|
+
|
|
108
|
+
For multi-page projects, extract helpers into a cached file:
|
|
109
|
+
`sinth build --shared-runtime`
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
This creates a `sinth-runtime.js` file shared across all pages. Each page shrinks to ~25 lines of render logic. The browser caches the helpers once.
|
|
113
|
+
|
|
114
|
+
Helpful when working on big projects.
|
|
115
|
+
|
|
92
116
|
## Security
|
|
93
117
|
|
|
94
118
|
Sinth's reactive runtime does not use eval(). Every expression is pre-compiled into optimized JavaScript functions at build time. No code injection possible. No runtime string evaluation. The most secure way to power reactive UI.
|
|
@@ -100,6 +124,7 @@ Sinth's reactive runtime does not use eval(). Every expression is pre-compiled i
|
|
|
100
124
|
- sinth init — scaffold a new project
|
|
101
125
|
- sinth dev [file] — start dev server with live reload
|
|
102
126
|
- sinth build — compile to static files
|
|
127
|
+
- sinth build --shared-runtime — build with shared runtime (useful when using a heavier runtime)
|
|
103
128
|
- sinth check — lint without output
|
|
104
129
|
- sinth version — print version
|
|
105
130
|
|
package/package.json
CHANGED
package/src/sinth.ts
CHANGED
|
@@ -21,22 +21,20 @@ enum TT {
|
|
|
21
21
|
KW_COMPONENT, KW_STYLE, KW_SCRIPT,
|
|
22
22
|
KW_GLOBAL, KW_PAGE, KW_LANG,
|
|
23
23
|
KW_CUSTOM_EL, KW_CUSTOM,
|
|
24
|
-
KW_IF, KW_ELSE, KW_FOR, KW_IN,
|
|
25
|
-
OP_PLUS, OP_MINUS, OP_STAR, OP_SLASH,
|
|
24
|
+
KW_IF, KW_ELSE, KW_FOR, KW_IN, KW_REMOVE,
|
|
25
|
+
OP_PLUS, OP_MINUS, OP_STAR, OP_SLASH, OP_SEMI,
|
|
26
26
|
OP_LT, OP_GT, OP_NEQ, OP_EQEQ, OP_LTEQ, OP_GTEQ,
|
|
27
27
|
EOF,
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
interface Token { type: TT; value: string; loc: Loc }
|
|
31
31
|
|
|
32
|
-
// literal types
|
|
33
32
|
type LitStr = { kind: "str"; value: string };
|
|
34
33
|
type LitNum = { kind: "num"; value: number };
|
|
35
34
|
type LitBool = { kind: "bool"; value: boolean };
|
|
36
35
|
type LitNull = { kind: "null" };
|
|
37
36
|
type Literal = LitStr | LitNum | LitBool | LitNull;
|
|
38
37
|
|
|
39
|
-
// expression AST
|
|
40
38
|
type UnaryOp = "not" | "-";
|
|
41
39
|
type BinaryOp = "+" | "-" | "*" | "/" | "==" | "!=" | "<" | ">" | "<=" | ">=" | "and" | "or";
|
|
42
40
|
type AssignOp = "=" | "+=" | "-=";
|
|
@@ -62,11 +60,11 @@ interface ExprNode { kind: "expr"; expression: Expression;
|
|
|
62
60
|
interface AssignStmt { kind: "assign_stmt"; expression: Expression; loc: Loc }
|
|
63
61
|
interface IfBlock { kind: "if"; condition: Expression; body: Child[]; elseBody?: Child[]; loc: Loc }
|
|
64
62
|
interface ForLoop { kind: "for"; keyVar?: string; itemVar: string; indexVar?: string; arrayVar: string; body: Child[]; loc: Loc }
|
|
63
|
+
interface RemoveStmt { kind: "remove"; target: string; loc: Loc }
|
|
65
64
|
|
|
66
|
-
// assignStmt is a first-class child so if bodies can be mixed
|
|
67
|
-
type Child = TextNode | CompUse | ExprNode | AssignStmt | IfBlock | ForLoop;
|
|
68
65
|
|
|
69
|
-
|
|
66
|
+
type Child = TextNode | CompUse | ExprNode | AssignStmt | IfBlock | ForLoop | RemoveStmt;
|
|
67
|
+
|
|
70
68
|
interface ParamDecl { name: string; defaultVal?: Literal; loc: Loc }
|
|
71
69
|
|
|
72
70
|
interface StyleBlock {
|
|
@@ -228,6 +226,7 @@ const KEYWORDS: Record<string, TT> = {
|
|
|
228
226
|
"else": TT.KW_ELSE,
|
|
229
227
|
"for": TT.KW_FOR,
|
|
230
228
|
"in": TT.KW_IN,
|
|
229
|
+
"remove": TT.KW_REMOVE,
|
|
231
230
|
};
|
|
232
231
|
|
|
233
232
|
export class Lexer {
|
|
@@ -266,6 +265,7 @@ export class Lexer {
|
|
|
266
265
|
if (ch === "+") { tokens.push(this.single(TT.OP_PLUS, loc)); continue; }
|
|
267
266
|
if (ch === "*") { tokens.push(this.single(TT.OP_STAR, loc)); continue; }
|
|
268
267
|
if (ch === "/") { tokens.push(this.single(TT.OP_SLASH, loc)); continue; }
|
|
268
|
+
if (ch === ";") { tokens.push(this.single(TT.OP_SEMI, loc)); continue; }
|
|
269
269
|
if (ch === "<") { tokens.push(this.single(TT.OP_LT, loc)); continue; }
|
|
270
270
|
if (ch === ">") { tokens.push(this.single(TT.OP_GT, loc)); continue; }
|
|
271
271
|
if (ch === "-" && !this.isDigit(this.src[this.pos + 1] ?? "")) {
|
|
@@ -954,6 +954,19 @@ private parseArrayLiteral(): Literal {
|
|
|
954
954
|
continue;
|
|
955
955
|
}
|
|
956
956
|
|
|
957
|
+
if (this.check(TT.KW_REMOVE)) {
|
|
958
|
+
const loc = this.consume(TT.KW_REMOVE).loc;
|
|
959
|
+
if (this.check(TT.STRING)) {
|
|
960
|
+
const target = this.consume(TT.STRING).value;
|
|
961
|
+
children.push({ kind: "remove", target, loc });
|
|
962
|
+
} else {
|
|
963
|
+
throw new SinthError(
|
|
964
|
+
`Expected a string after 'remove', got '${this.peek().value}'. Use remove "id".`,
|
|
965
|
+
this.peek().loc,
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
continue;
|
|
969
|
+
}
|
|
957
970
|
if (this.check(TT.IDENT) && this.peek().value === "var") {
|
|
958
971
|
const vd = this.parseVarDeclaration();
|
|
959
972
|
if (vd) this._varDecls.push(vd);
|
|
@@ -1141,12 +1154,23 @@ private parseArrayLiteral(): Literal {
|
|
|
1141
1154
|
value = this.parseLiteral();
|
|
1142
1155
|
} else {
|
|
1143
1156
|
const savedPos = this.pos;
|
|
1144
|
-
const expr
|
|
1145
|
-
if (expr && (this.check(TT.COMMA) || this.check(TT.RPAREN))) {
|
|
1146
|
-
|
|
1157
|
+
const expr = this.parseExpression();
|
|
1158
|
+
if (expr && (this.check(TT.COMMA) || this.check(TT.RPAREN) || this.check(TT.OP_SEMI))) {
|
|
1159
|
+
const exprs: Expression[] = [expr];
|
|
1160
|
+
while (this.check(TT.OP_SEMI)) {
|
|
1161
|
+
this.consume(TT.OP_SEMI);
|
|
1162
|
+
const next = this.parseExpression();
|
|
1163
|
+
if (next) exprs.push(next);
|
|
1164
|
+
else break;
|
|
1165
|
+
}
|
|
1166
|
+
if (exprs.length === 1) {
|
|
1167
|
+
value = { kind: "str", value: "__EXPR__" + JSON.stringify(exprs[0]) };
|
|
1168
|
+
} else {
|
|
1169
|
+
value = { kind: "str", value: "__MULTI_EXPR__" + JSON.stringify(exprs) };
|
|
1170
|
+
}
|
|
1147
1171
|
} else {
|
|
1148
1172
|
this.pos = savedPos;
|
|
1149
|
-
value
|
|
1173
|
+
value = this.parseLiteral();
|
|
1150
1174
|
}
|
|
1151
1175
|
}
|
|
1152
1176
|
} else {
|
|
@@ -2173,21 +2197,34 @@ if (name === "delay") {
|
|
|
2173
2197
|
|
|
2174
2198
|
let raw = value.value;
|
|
2175
2199
|
|
|
2200
|
+
if (raw.startsWith("__MULTI_EXPR__")) {
|
|
2201
|
+
const exprJson = raw.substring("__MULTI_EXPR__".length);
|
|
2202
|
+
try {
|
|
2203
|
+
const exprs: Expression[] = JSON.parse(exprJson);
|
|
2204
|
+
const jsExprs = exprs.map(e => compileExprToJS(e)).join("; ");
|
|
2205
|
+
const ev = eventAttrName(name);
|
|
2206
|
+
if (ev) return `${ev}="(function(){ ${jsExprs.replace(/"/g, """)}; sinthRender(); })()"`;
|
|
2207
|
+
return `${name}="${escAttr(jsExprs)}"`;
|
|
2208
|
+
} catch { }
|
|
2209
|
+
}
|
|
2176
2210
|
if (raw.startsWith("__EXPR__")) {
|
|
2177
2211
|
const exprJson = raw.substring("__EXPR__".length);
|
|
2178
2212
|
try {
|
|
2179
2213
|
const expr: Expression = JSON.parse(exprJson);
|
|
2180
2214
|
const jsExpr = compileExprToJS(expr);
|
|
2181
2215
|
const ev = eventAttrName(name);
|
|
2182
|
-
if (ev) return `${ev}="(function(){ ${jsExpr.replace(/"/g, """)}; })()"`;
|
|
2216
|
+
if (ev) return `${ev}="(function(){ ${jsExpr.replace(/"/g, """)}; sinthRender(); })()"`;
|
|
2183
2217
|
return `${name}="${escAttr(jsExpr)}"`;
|
|
2184
|
-
} catch {
|
|
2218
|
+
} catch { }
|
|
2185
2219
|
}
|
|
2186
2220
|
|
|
2187
2221
|
raw = interpolateAttr(raw, paramMap);
|
|
2188
2222
|
|
|
2189
2223
|
const ev = eventAttrName(name);
|
|
2190
|
-
if (ev)
|
|
2224
|
+
if (ev) {
|
|
2225
|
+
const call = raw.includes("(") ? raw : raw + "()";
|
|
2226
|
+
return `${ev}="${escAttr(call)};sinthRender()"`;
|
|
2227
|
+
}
|
|
2191
2228
|
return `${name}="${escAttr(raw)}"`;
|
|
2192
2229
|
}
|
|
2193
2230
|
|
|
@@ -2260,6 +2297,10 @@ function renderChild(
|
|
|
2260
2297
|
return "";
|
|
2261
2298
|
}
|
|
2262
2299
|
|
|
2300
|
+
case "remove": {
|
|
2301
|
+
return `<span data-sinth-remove="${esc((child as RemoveStmt).target)}"></span>`;
|
|
2302
|
+
}
|
|
2303
|
+
|
|
2263
2304
|
case "if":
|
|
2264
2305
|
return renderIfBlock(child, ctx, params, depth);
|
|
2265
2306
|
|
|
@@ -2311,8 +2352,18 @@ depth: number,
|
|
|
2311
2352
|
const condId = registerExpr(ctx, ifBlock.condition);
|
|
2312
2353
|
const bodyHTML = ifBlock.body.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
2313
2354
|
const elseHTML = (ifBlock.elseBody ?? []).map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
2355
|
+
let replaceAttr = "";
|
|
2356
|
+
const firstComp = ifBlock.body.find(c => c.kind === "use") as CompUse | undefined;
|
|
2357
|
+
if (firstComp) {
|
|
2358
|
+
const idAttr = firstComp.attrs.find(a => a.name === "id");
|
|
2359
|
+
const replAttr = firstComp.attrs.find(a => a.name === "replace");
|
|
2360
|
+
const wantsReplace = replAttr && (replAttr.value === null || (replAttr.value?.kind === "bool" && replAttr.value.value));
|
|
2361
|
+
if (idAttr && idAttr.value?.kind === "str" && wantsReplace) {
|
|
2362
|
+
replaceAttr = ` data-sinth-if-replace="${escAttr(idAttr.value.value)}"`;
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2314
2365
|
return (
|
|
2315
|
-
`<template data-sinth-if-id="${tplId}" data-sinth-if-expr="${condId}">${bodyHTML}</template>` +
|
|
2366
|
+
`<template data-sinth-if-id="${tplId}" data-sinth-if-expr="${condId}"${replaceAttr}>${bodyHTML}</template>` +
|
|
2316
2367
|
(elseHTML ? `<template data-sinth-else data-sinth-if-id="${tplId}">${elseHTML}</template>` : "")
|
|
2317
2368
|
);
|
|
2318
2369
|
}
|
|
@@ -2347,14 +2398,14 @@ depth: number,
|
|
|
2347
2398
|
const ifId = ifFirstComp.attrs.find(a => a.name === "id")?.value;
|
|
2348
2399
|
const ifReplace = ifFirstComp.attrs.find(a => a.name === "replace");
|
|
2349
2400
|
const ifReplaceVal = ifReplace?.value;
|
|
2350
|
-
const ifWantsReplace =
|
|
2401
|
+
const ifWantsReplace = ifReplace && (ifReplaceVal === null || (ifReplaceVal?.kind === "bool" && ifReplaceVal.value));
|
|
2351
2402
|
|
|
2352
2403
|
if (ifId && ifId.kind === "str" && ifWantsReplace) {
|
|
2353
2404
|
if (elseFirstComp) {
|
|
2354
2405
|
const elseId = elseFirstComp.attrs.find(a => a.name === "id")?.value;
|
|
2355
2406
|
const elseReplace = elseFirstComp.attrs.find(a => a.name === "replace");
|
|
2356
2407
|
const elseReplaceVal = elseReplace?.value;
|
|
2357
|
-
const elseWantsReplace =
|
|
2408
|
+
const elseWantsReplace = elseReplace && (elseReplaceVal === null || (elseReplaceVal?.kind === "bool" && elseReplaceVal.value));
|
|
2358
2409
|
|
|
2359
2410
|
if (elseId && elseId.kind === "str" && elseId.value === ifId.value && elseWantsReplace) {
|
|
2360
2411
|
replaceId = ifId.value;
|
|
@@ -2656,19 +2707,18 @@ function buildRuntime(opts: {
|
|
|
2656
2707
|
mixedBlocks: MixedBlockEntry[];
|
|
2657
2708
|
assignedVars: Set<string>;
|
|
2658
2709
|
exprRegistry: string[];
|
|
2659
|
-
|
|
2710
|
+
sharedRuntime: boolean;
|
|
2711
|
+
}): string | { page: string; shared: string } {
|
|
2660
2712
|
const { varDecls, bodyHTML, logicBlocks, mixedBlocks, assignedVars, exprRegistry } = opts;
|
|
2661
2713
|
|
|
2662
|
-
|
|
2663
|
-
const
|
|
2664
|
-
const
|
|
2665
|
-
const
|
|
2666
|
-
const
|
|
2667
|
-
const
|
|
2668
|
-
const needsLogic = logicBlocks.length > 0;
|
|
2714
|
+
const needsExpr = bodyHTML.includes("sinth-expr");
|
|
2715
|
+
const needsIf = bodyHTML.includes("data-sinth-if");
|
|
2716
|
+
const needsFor = bodyHTML.includes("data-sinth-for");
|
|
2717
|
+
const needsDelay = bodyHTML.includes("data-sinth-delay") || bodyHTML.includes("data-sinth-delay-expr-id") || mixedBlocks.some(mb => mb.ifHTML.includes("data-sinth-delay") || mb.ifHTML.includes("data-sinth-delay-expr-id") || mb.elseHTML.includes("data-sinth-delay") || mb.elseHTML.includes("data-sinth-delay-expr-id"));
|
|
2718
|
+
const needsMixed = mixedBlocks.length > 0;
|
|
2719
|
+
const needsLogic = logicBlocks.length > 0;
|
|
2669
2720
|
const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic;
|
|
2670
2721
|
|
|
2671
|
-
// var decls with type defaults
|
|
2672
2722
|
const varLines = varDecls.map(v => {
|
|
2673
2723
|
if (!v.value) {
|
|
2674
2724
|
if (!assignedVars.has(v.name)) {
|
|
@@ -2686,370 +2736,362 @@ const needsLogic = logicBlocks.length > 0;
|
|
|
2686
2736
|
if (val.startsWith("__ARR__")) return `var ${v.name} = ${val.slice(7)};`;
|
|
2687
2737
|
if (v.varType === "obj") return `var ${v.name} = ${val};`;
|
|
2688
2738
|
if (v.varType === "str") return `var ${v.name} = ${JSON.stringify(val)};`;
|
|
2689
|
-
if (v.varType === "str[]" && typeof val === 'string' && val.startsWith("__ARR__")) {
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
}
|
|
2695
|
-
return `var ${v.name} = ${val};`;
|
|
2739
|
+
if (v.varType === "str[]" && typeof val === 'string' && val.startsWith("__ARR__")) {
|
|
2740
|
+
try {
|
|
2741
|
+
const arr = JSON.parse(val.slice(7));
|
|
2742
|
+
return `var ${v.name} = ${JSON.stringify(arr)};`;
|
|
2743
|
+
} catch { return `var ${v.name} = ${val.slice(7)};`; }
|
|
2744
|
+
}
|
|
2745
|
+
return `var ${v.name} = ${val};`;
|
|
2696
2746
|
}).join("\n");
|
|
2697
2747
|
|
|
2698
2748
|
if (!needsRender && !needsDelay) {
|
|
2699
|
-
return varLines ? `// Sinth
|
|
2749
|
+
return varLines ? `// Sinth compiled runtime\n${varLines}` : "";
|
|
2700
2750
|
}
|
|
2701
2751
|
|
|
2702
|
-
let
|
|
2752
|
+
let helpers = "";
|
|
2703
2753
|
|
|
2704
|
-
|
|
2754
|
+
if (needsExpr || needsIf || needsFor) {
|
|
2755
|
+
helpers += `
|
|
2756
|
+
function sinthExpr(el) {
|
|
2757
|
+
try {
|
|
2758
|
+
var exprFn = __X[el.dataset.exprId];
|
|
2759
|
+
if (exprFn) el.textContent = exprFn({});
|
|
2760
|
+
} catch(e) {}
|
|
2761
|
+
}
|
|
2762
|
+
`;
|
|
2763
|
+
}
|
|
2705
2764
|
|
|
2706
|
-
if (
|
|
2707
|
-
|
|
2765
|
+
if (needsIf || needsMixed) {
|
|
2766
|
+
helpers += `
|
|
2767
|
+
function sinthReplaceInsert(t, anchor, ifId, replaceId) {
|
|
2768
|
+
if (anchor) {
|
|
2769
|
+
var cur = anchor.nextSibling;
|
|
2770
|
+
while (cur && cur !== t) { var nx = cur.nextSibling; cur.remove(); cur = nx; }
|
|
2771
|
+
} else {
|
|
2772
|
+
anchor = document.createElement('span');
|
|
2773
|
+
anchor.style.display = 'none';
|
|
2774
|
+
anchor.dataset.sinthIfAnchor = ifId;
|
|
2775
|
+
t.parentNode.insertBefore(anchor, t);
|
|
2776
|
+
}
|
|
2777
|
+
var _rp = null, _rpParent = null, _rpNext = null;
|
|
2778
|
+
if (replaceId) {
|
|
2779
|
+
_rp = document.getElementById(replaceId);
|
|
2780
|
+
if (_rp) {
|
|
2781
|
+
_rpParent = _rp.parentNode;
|
|
2782
|
+
_rpNext = _rp.nextSibling;
|
|
2783
|
+
_rp.parentNode.removeChild(_rp);
|
|
2784
|
+
anchor._sinthReplaced = _rp;
|
|
2785
|
+
anchor._sinthReplacedParent = _rpParent;
|
|
2786
|
+
anchor._sinthReplacedNext = _rpNext;
|
|
2787
|
+
}
|
|
2788
|
+
}
|
|
2789
|
+
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
2790
|
+
frag.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
2791
|
+
if (${needsDelay}) {
|
|
2792
|
+
frag.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
2793
|
+
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
2794
|
+
}
|
|
2795
|
+
var fragFirst = frag.firstChild;
|
|
2796
|
+
var fragLast = frag.lastChild;
|
|
2797
|
+
if (_rpParent && _rpNext) {
|
|
2798
|
+
_rpParent.insertBefore(frag, _rpNext);
|
|
2799
|
+
} else if (_rpParent) {
|
|
2800
|
+
_rpParent.appendChild(frag);
|
|
2801
|
+
} else {
|
|
2802
|
+
t.parentNode.insertBefore(frag, t);
|
|
2803
|
+
}
|
|
2804
|
+
if (replaceId && anchor) {
|
|
2805
|
+
anchor._sinthInsertedFirst = fragFirst;
|
|
2806
|
+
anchor._sinthInsertedLast = fragLast;
|
|
2807
|
+
}
|
|
2808
|
+
return anchor;
|
|
2809
|
+
}
|
|
2810
|
+
`;
|
|
2708
2811
|
}
|
|
2709
2812
|
|
|
2710
|
-
if (
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
var
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2813
|
+
if (needsDelay) {
|
|
2814
|
+
helpers += `
|
|
2815
|
+
function sinthDelay(el) {
|
|
2816
|
+
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
2817
|
+
el.dataset.sinthDelayDone = '1';
|
|
2818
|
+
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
2819
|
+
el.style.display = 'none';
|
|
2820
|
+
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2821
|
+
else el.style.display = '';
|
|
2822
|
+
}
|
|
2823
|
+
function sinthDelayExpr(el) {
|
|
2824
|
+
try {
|
|
2825
|
+
var fn = __X[el.dataset.sinthDelayExprId];
|
|
2826
|
+
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
2827
|
+
el.style.display = '';
|
|
2828
|
+
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2829
|
+
} catch(e) {}
|
|
2830
|
+
}
|
|
2831
|
+
`;
|
|
2832
|
+
}
|
|
2833
|
+
|
|
2834
|
+
if (needsIf) {
|
|
2835
|
+
helpers += `
|
|
2836
|
+
function sinthIfBlock(t) {
|
|
2837
|
+
var ifId = t.dataset.sinthIfId;
|
|
2838
|
+
var anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
|
|
2839
|
+
var condFn = __X[t.dataset.sinthIfExpr];
|
|
2840
|
+
var cond = condFn ? condFn() : false;
|
|
2841
|
+
if (cond) {
|
|
2842
|
+
anchor = sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
|
|
2843
|
+
} else {
|
|
2730
2844
|
if (anchor) {
|
|
2731
|
-
|
|
2732
|
-
|
|
2845
|
+
if (anchor._sinthReplaced) {
|
|
2846
|
+
var insFirst = anchor._sinthInsertedFirst;
|
|
2847
|
+
var insLast = anchor._sinthInsertedLast;
|
|
2848
|
+
var rpParent = anchor._sinthReplacedParent;
|
|
2849
|
+
var rpNext = anchor._sinthReplacedNext;
|
|
2850
|
+
if (insFirst && insLast) {
|
|
2851
|
+
var cur = insFirst;
|
|
2852
|
+
while (cur && cur !== insLast) {
|
|
2853
|
+
var next = cur.nextSibling;
|
|
2854
|
+
cur.remove();
|
|
2855
|
+
cur = next;
|
|
2856
|
+
}
|
|
2857
|
+
if (insLast) insLast.remove();
|
|
2858
|
+
}
|
|
2859
|
+
if (rpParent && rpNext) {
|
|
2860
|
+
rpParent.insertBefore(anchor._sinthReplaced, rpNext);
|
|
2861
|
+
} else if (rpParent) {
|
|
2862
|
+
rpParent.appendChild(anchor._sinthReplaced);
|
|
2863
|
+
}
|
|
2864
|
+
} else {
|
|
2865
|
+
var cur2 = anchor.nextSibling;
|
|
2866
|
+
while (cur2 && cur2 !== t) { var nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
|
|
2867
|
+
}
|
|
2733
2868
|
anchor.remove();
|
|
2734
2869
|
}
|
|
2735
|
-
var
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
var
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
}
|
|
2762
|
-
|
|
2763
|
-
|
|
2870
|
+
var elseT = t.nextElementSibling;
|
|
2871
|
+
if (elseT && elseT.hasAttribute('data-sinth-else')) {
|
|
2872
|
+
var elseIfId = elseT.dataset.sinthIfId;
|
|
2873
|
+
var ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + elseIfId + '"]');
|
|
2874
|
+
if (ea) {
|
|
2875
|
+
var cur3 = ea.nextSibling;
|
|
2876
|
+
while (cur3 && cur3 !== t) { var nx3 = cur3.nextSibling; cur3.remove(); cur3 = nx3; }
|
|
2877
|
+
} else {
|
|
2878
|
+
ea = document.createElement('span');
|
|
2879
|
+
ea.style.display = 'none';
|
|
2880
|
+
ea.dataset.sinthIfAnchor = '__else__' + elseIfId;
|
|
2881
|
+
t.parentNode.insertBefore(ea, t);
|
|
2882
|
+
}
|
|
2883
|
+
var ef = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
2884
|
+
ef.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
2885
|
+
if (${needsDelay}) {
|
|
2886
|
+
ef.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
2887
|
+
ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
2888
|
+
}
|
|
2889
|
+
t.parentNode.insertBefore(ef, t);
|
|
2890
|
+
} else {
|
|
2891
|
+
var ea2 = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
2892
|
+
if (ea2) {
|
|
2893
|
+
var ec = ea2.nextSibling;
|
|
2894
|
+
while (ec && ec !== t) { var en = ec.nextSibling; ec.remove(); ec = en; }
|
|
2895
|
+
ea2.remove();
|
|
2896
|
+
}
|
|
2897
|
+
}
|
|
2898
|
+
}
|
|
2899
|
+
}
|
|
2900
|
+
`;
|
|
2901
|
+
}
|
|
2902
|
+
|
|
2903
|
+
if (needsFor) {
|
|
2904
|
+
helpers += `
|
|
2905
|
+
function hashString(str) {
|
|
2906
|
+
var hash = 0, i, chr;
|
|
2907
|
+
for (i = 0; i < str.length; i++) {
|
|
2908
|
+
chr = str.charCodeAt(i);
|
|
2909
|
+
hash = ((hash << 5) - hash) + chr;
|
|
2910
|
+
hash |= 0;
|
|
2911
|
+
}
|
|
2912
|
+
return String(hash);
|
|
2913
|
+
}
|
|
2914
|
+
function sinthForBlock(t) {
|
|
2915
|
+
var source = window[t.dataset.sinthFor];
|
|
2916
|
+
if (source === undefined) source = [];
|
|
2917
|
+
var newHash = '';
|
|
2918
|
+
try { newHash = hashString(JSON.stringify(source)); } catch(e) { newHash = ''; }
|
|
2919
|
+
if (t.dataset.sinthForHash && t.dataset.sinthForHash === newHash) {
|
|
2920
|
+
return;
|
|
2921
|
+
}
|
|
2922
|
+
t.dataset.sinthForHash = newHash;
|
|
2923
|
+
var isObj = (typeof source === 'object' && source !== null && !Array.isArray(source));
|
|
2924
|
+
var entries;
|
|
2925
|
+
if (isObj) {
|
|
2926
|
+
entries = Object.entries(source);
|
|
2927
|
+
} else {
|
|
2928
|
+
if (!Array.isArray(source)) source = [];
|
|
2929
|
+
entries = source.map(function(item, index) { return [index, item]; });
|
|
2930
|
+
}
|
|
2931
|
+
var anchor = t.parentNode ? t.parentNode.querySelector('[data-sinth-for-anchor="' + t.dataset.sinthFor + '"]') : null;
|
|
2932
|
+
if (anchor) {
|
|
2933
|
+
var cur2 = anchor.nextSibling;
|
|
2934
|
+
while (cur2 && cur2 !== t) { var nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
|
|
2935
|
+
anchor.remove();
|
|
2936
|
+
}
|
|
2937
|
+
var fa = document.createElement('span');
|
|
2938
|
+
fa.style.display = 'none';
|
|
2939
|
+
fa.dataset.sinthForAnchor = t.dataset.sinthFor;
|
|
2940
|
+
t.parentNode && t.parentNode.insertBefore(fa, t);
|
|
2941
|
+
var _loopIdx = 0;
|
|
2942
|
+
entries.forEach(function(entry) {
|
|
2943
|
+
var _k = entry[0];
|
|
2944
|
+
var _v = entry[1];
|
|
2945
|
+
var _item = (t.dataset && t.dataset.sinthItem) ? t.dataset.sinthItem : '__item__';
|
|
2946
|
+
var _key = t.dataset && t.dataset.sinthKey ? t.dataset.sinthKey : null;
|
|
2947
|
+
var _idx = t.dataset && t.dataset.sinthIndex ? t.dataset.sinthIndex : null;
|
|
2948
|
+
_loopIdx++;
|
|
2949
|
+
if (t.dataset.sinthIfReplace) {
|
|
2950
|
+
var _rp = document.getElementById(t.dataset.sinthIfReplace);
|
|
2951
|
+
if (_rp) _rp.parentNode.removeChild(_rp);
|
|
2952
|
+
}
|
|
2953
|
+
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
2954
|
+
frag.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
2955
|
+
try {
|
|
2956
|
+
var exprFn = __X[el.dataset.exprId];
|
|
2957
|
+
if (!exprFn) return;
|
|
2764
2958
|
var _ctx = {};
|
|
2765
2959
|
if (_item) _ctx[_item] = _v;
|
|
2766
2960
|
if (_key) _ctx[_key] = _k;
|
|
2767
2961
|
if (_idx) _ctx[_idx] = _loopIdx - 1;
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
var ifContent = document.createRange().createContextualFragment(ifT.innerHTML);
|
|
2772
|
-
ifContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
2773
|
-
try {
|
|
2774
|
-
var exprFn2 = __X[el2.dataset.exprId];
|
|
2775
|
-
if (exprFn2) el2.textContent = exprFn2(_ctx);
|
|
2776
|
-
} catch(e) {}
|
|
2777
|
-
});
|
|
2778
|
-
ifT.parentNode.insertBefore(ifContent, ifT);
|
|
2779
|
-
} else {
|
|
2780
|
-
var elseT = ifT.nextElementSibling;
|
|
2781
|
-
if (elseT && elseT.hasAttribute('data-sinth-else')) {
|
|
2782
|
-
var elseContent = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
2783
|
-
elseContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
2784
|
-
try {
|
|
2785
|
-
var exprFn2 = __X[el2.dataset.exprId];
|
|
2786
|
-
if (exprFn2) el2.textContent = exprFn2(_ctx);
|
|
2787
|
-
} catch(e) {}
|
|
2788
|
-
});
|
|
2789
|
-
ifT.parentNode.insertBefore(elseContent, ifT);
|
|
2790
|
-
}
|
|
2791
|
-
}
|
|
2792
|
-
});
|
|
2793
|
-
frag.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2794
|
-
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
2795
|
-
el.dataset.sinthDelayDone = '1';
|
|
2796
|
-
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
2797
|
-
el.style.display = 'none';
|
|
2798
|
-
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2799
|
-
else el.style.display = '';
|
|
2800
|
-
});
|
|
2801
|
-
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2802
|
-
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
2803
|
-
el.dataset.sinthDelayDone = '1';
|
|
2804
|
-
try {
|
|
2805
|
-
var fn = __X[el.dataset.sinthDelayExprId];
|
|
2806
|
-
var _ctx = {};
|
|
2807
|
-
if (_item) _ctx[_item] = _v;
|
|
2808
|
-
if (_key) _ctx[_key] = _k;
|
|
2809
|
-
if (_idx) _ctx[_idx] = _loopIdx - 1;
|
|
2810
|
-
var ms = fn ? parseInt(fn(_ctx)) || 0 : 0;
|
|
2811
|
-
el.style.display = 'none';
|
|
2812
|
-
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2813
|
-
else el.style.display = '';
|
|
2814
|
-
} catch(e) {}
|
|
2815
|
-
});
|
|
2816
|
-
t.parentNode && t.parentNode.insertBefore(frag, t);
|
|
2962
|
+
el.textContent = exprFn(_ctx);
|
|
2963
|
+
el.classList.remove('sinth-expr');
|
|
2964
|
+
} catch(e) {}
|
|
2817
2965
|
});
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
if (anchor) {
|
|
2830
|
-
var cur = anchor.nextSibling;
|
|
2831
|
-
while (cur && cur !== t) { var nx = cur.nextSibling; cur.remove(); cur = nx; }
|
|
2832
|
-
} else {
|
|
2833
|
-
anchor = document.createElement('span');
|
|
2834
|
-
anchor.style.display = 'none';
|
|
2835
|
-
anchor.dataset.sinthIfAnchor = ifId;
|
|
2836
|
-
t.parentNode.insertBefore(anchor, t);
|
|
2837
|
-
}
|
|
2838
|
-
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
2839
|
-
frag.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
2840
|
-
try {
|
|
2841
|
-
var exprFn = __X[el.dataset.exprId];
|
|
2842
|
-
if (exprFn) el.textContent = exprFn();
|
|
2843
|
-
} catch(e) {}
|
|
2844
|
-
});
|
|
2845
|
-
frag.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2846
|
-
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
2847
|
-
el.dataset.sinthDelayDone = '1';
|
|
2848
|
-
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
2849
|
-
el.style.display = 'none';
|
|
2850
|
-
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2851
|
-
else el.style.display = '';
|
|
2852
|
-
});
|
|
2853
|
-
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2854
|
-
try {
|
|
2855
|
-
var fn = __X[el.dataset.sinthDelayExprId];
|
|
2856
|
-
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
2857
|
-
el.style.display = '';
|
|
2858
|
-
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2859
|
-
} catch(e) {}
|
|
2860
|
-
});
|
|
2861
|
-
t.parentNode.insertBefore(frag, t);
|
|
2862
|
-
} else {
|
|
2863
|
-
if (anchor) {
|
|
2864
|
-
var cur2 = anchor.nextSibling;
|
|
2865
|
-
while (cur2 && cur2 !== t) { var nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
|
|
2866
|
-
anchor.remove();
|
|
2867
|
-
}
|
|
2868
|
-
var elseT = t.nextElementSibling;
|
|
2869
|
-
if (elseT && elseT.dataset && elseT.hasAttribute('data-sinth-else')) {
|
|
2870
|
-
var ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
2871
|
-
if (ea) {
|
|
2872
|
-
var cur3 = ea.nextSibling;
|
|
2873
|
-
while (cur3 && cur3 !== t) { var nx3 = cur3.nextSibling; cur3.remove(); cur3 = nx3; }
|
|
2874
|
-
} else {
|
|
2875
|
-
ea = document.createElement('span');
|
|
2876
|
-
ea.style.display = 'none';
|
|
2877
|
-
ea.dataset.sinthIfAnchor = '__else__' + ifId;
|
|
2878
|
-
t.parentNode.insertBefore(ea, t);
|
|
2879
|
-
}
|
|
2880
|
-
var ef = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
2881
|
-
ef.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
2966
|
+
frag.querySelectorAll('template[data-sinth-if-expr]').forEach(function(ifT) {
|
|
2967
|
+
var condFn = __X[ifT.dataset.sinthIfExpr];
|
|
2968
|
+
var _ctx = {};
|
|
2969
|
+
if (_item) _ctx[_item] = _v;
|
|
2970
|
+
if (_key) _ctx[_key] = _k;
|
|
2971
|
+
if (_idx) _ctx[_idx] = _loopIdx - 1;
|
|
2972
|
+
var cond = false;
|
|
2973
|
+
try { if (condFn) cond = condFn(_ctx); } catch(e) {}
|
|
2974
|
+
if (cond) {
|
|
2975
|
+
var ifContent = document.createRange().createContextualFragment(ifT.innerHTML);
|
|
2976
|
+
ifContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
2882
2977
|
try {
|
|
2883
|
-
var
|
|
2884
|
-
if (
|
|
2978
|
+
var exprFn2 = __X[el2.dataset.exprId];
|
|
2979
|
+
if (exprFn2) el2.textContent = exprFn2(_ctx);
|
|
2885
2980
|
} catch(e) {}
|
|
2886
2981
|
});
|
|
2887
|
-
|
|
2888
|
-
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
2889
|
-
el.style.display = 'none';
|
|
2890
|
-
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2891
|
-
else el.style.display = '';
|
|
2892
|
-
});
|
|
2893
|
-
ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2894
|
-
try {
|
|
2895
|
-
var fn = __X[el.dataset.sinthDelayExprId];
|
|
2896
|
-
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
2897
|
-
el.style.display = '';
|
|
2898
|
-
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2899
|
-
} catch(e) {}
|
|
2900
|
-
});
|
|
2901
|
-
t.parentNode.insertBefore(ef, t);
|
|
2982
|
+
ifT.parentNode.insertBefore(ifContent, ifT);
|
|
2902
2983
|
} else {
|
|
2903
|
-
var
|
|
2904
|
-
if (
|
|
2905
|
-
var
|
|
2906
|
-
|
|
2907
|
-
|
|
2984
|
+
var elseT = ifT.nextElementSibling;
|
|
2985
|
+
if (elseT && elseT.hasAttribute('data-sinth-else')) {
|
|
2986
|
+
var elseContent = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
2987
|
+
elseContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
2988
|
+
try {
|
|
2989
|
+
var exprFn2 = __X[el2.dataset.exprId];
|
|
2990
|
+
if (exprFn2) el2.textContent = exprFn2(_ctx);
|
|
2991
|
+
} catch(e) {}
|
|
2992
|
+
});
|
|
2993
|
+
ifT.parentNode.insertBefore(elseContent, ifT);
|
|
2908
2994
|
}
|
|
2909
2995
|
}
|
|
2996
|
+
});
|
|
2997
|
+
if (${needsDelay}) {
|
|
2998
|
+
frag.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
2999
|
+
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
2910
3000
|
}
|
|
3001
|
+
t.parentNode && t.parentNode.insertBefore(frag, t);
|
|
2911
3002
|
});
|
|
3003
|
+
}
|
|
2912
3004
|
`;
|
|
2913
3005
|
}
|
|
2914
3006
|
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
var __el = document.getElementById(${JSON.stringify(mb.replaceId || mb.id)});
|
|
2924
|
-
if (__el) {
|
|
2925
|
-
var condFn = __X[${mb.conditionJS}];
|
|
2926
|
-
var __cond = condFn ? condFn() : false;
|
|
2927
|
-
if (__cond) {
|
|
2928
|
-
${mb.ifJS ? mb.ifJS : ""} __el.innerHTML = ${ifHTMLJS};
|
|
2929
|
-
} else {
|
|
2930
|
-
${mb.elseJS ? mb.elseJS : ""} __el.innerHTML = ${elseHTMLJS};
|
|
2931
|
-
}
|
|
2932
|
-
__el.querySelectorAll('.sinth-expr').forEach(function(e) {
|
|
2933
|
-
try {
|
|
2934
|
-
var exprFn = __X[e.dataset.exprId];
|
|
2935
|
-
if (exprFn) e.textContent = exprFn();
|
|
2936
|
-
} catch(ex) {}
|
|
2937
|
-
});
|
|
2938
|
-
__el.querySelectorAll('template[data-sinth-if-expr]').forEach(function(innerT) {
|
|
2939
|
-
var innerCondFn = __X[innerT.dataset.sinthIfExpr];
|
|
2940
|
-
var innerCond = innerCondFn ? innerCondFn() : false;
|
|
2941
|
-
if (innerCond) {
|
|
2942
|
-
var innerFrag = document.createRange().createContextualFragment(innerT.innerHTML);
|
|
2943
|
-
innerFrag.querySelectorAll('.sinth-expr').forEach(function(e2) {
|
|
2944
|
-
try {
|
|
2945
|
-
var exprFn2 = __X[e2.dataset.exprId];
|
|
2946
|
-
if (exprFn2) e2.textContent = exprFn2();
|
|
2947
|
-
} catch(ex) {}
|
|
2948
|
-
});
|
|
2949
|
-
innerT.parentNode.insertBefore(innerFrag, innerT);
|
|
3007
|
+
if (needsMixed) {
|
|
3008
|
+
helpers += `
|
|
3009
|
+
function sinthMixedBlock(el, condId, ifJS, ifHTML, elseJS, elseHTML) {
|
|
3010
|
+
var condFn = __X[condId];
|
|
3011
|
+
var cond = condFn ? condFn() : false;
|
|
3012
|
+
if (cond) {
|
|
3013
|
+
if (ifJS) eval(ifJS);
|
|
3014
|
+
el.innerHTML = ifHTML;
|
|
2950
3015
|
} else {
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
innerT.parentNode.insertBefore(innerElseFrag, innerT);
|
|
2961
|
-
}
|
|
2962
|
-
}
|
|
2963
|
-
});
|
|
2964
|
-
${hasDelay ? `
|
|
2965
|
-
__el.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2966
|
-
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
2967
|
-
el.style.display = 'none';
|
|
2968
|
-
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2969
|
-
else el.style.display = '';
|
|
2970
|
-
});
|
|
2971
|
-
__el.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2972
|
-
try {
|
|
2973
|
-
var fn = __X[el.dataset.sinthDelayExprId];
|
|
2974
|
-
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
2975
|
-
el.style.display = 'none';
|
|
2976
|
-
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2977
|
-
else el.style.display = '';
|
|
2978
|
-
} catch(e) {}
|
|
2979
|
-
});
|
|
2980
|
-
` : ""}
|
|
3016
|
+
if (elseJS) eval(elseJS);
|
|
3017
|
+
el.innerHTML = elseHTML;
|
|
3018
|
+
}
|
|
3019
|
+
el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
3020
|
+
el.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);
|
|
3021
|
+
if (${needsDelay}) {
|
|
3022
|
+
el.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
3023
|
+
el.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
3024
|
+
}
|
|
2981
3025
|
}
|
|
2982
|
-
})();
|
|
2983
3026
|
`;
|
|
2984
|
-
}
|
|
2985
|
-
}
|
|
3027
|
+
}
|
|
2986
3028
|
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
try { el.value = window[el.dataset.sinthValue] || ''; } catch(e) {}
|
|
2991
|
-
});
|
|
2992
|
-
document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
2993
|
-
try { el.checked = !!window[el.dataset.sinthChecked]; } catch(e) {}
|
|
2994
|
-
});
|
|
2995
|
-
`;
|
|
3029
|
+
const exprArrayJS = exprRegistry.length > 0
|
|
3030
|
+
? `var __X = [${exprRegistry.map((js) => `function(_ctx){ return ${js}; }`).join(",")}];\n`
|
|
3031
|
+
: "";
|
|
2996
3032
|
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
var exprFn = __X[el.dataset.exprId];
|
|
3003
|
-
if (exprFn) el.textContent = exprFn({});
|
|
3004
|
-
} catch(e) {}
|
|
3005
|
-
});
|
|
3006
|
-
`;
|
|
3033
|
+
let renderBody = "";
|
|
3034
|
+
renderBody += ` var _sx = window.scrollX, _sy = window.scrollY;\n`;
|
|
3035
|
+
|
|
3036
|
+
if (needsLogic) {
|
|
3037
|
+
renderBody += logicBlocks.map(b => b.replace(/^/gm, " ")).join("\n") + "\n";
|
|
3007
3038
|
}
|
|
3008
3039
|
|
|
3040
|
+
if (needsMixed) {
|
|
3041
|
+
for (const mb of mixedBlocks) {
|
|
3042
|
+
renderBody += ` (function() {
|
|
3043
|
+
var __el = document.getElementById(${JSON.stringify(mb.replaceId || mb.id)});
|
|
3044
|
+
if (__el) sinthMixedBlock(__el, ${mb.conditionJS}, ${JSON.stringify(mb.ifJS)}, ${JSON.stringify(mb.ifHTML)}, ${JSON.stringify(mb.elseJS)}, ${JSON.stringify(mb.elseHTML)});
|
|
3045
|
+
})();\n`;
|
|
3046
|
+
}
|
|
3047
|
+
}
|
|
3048
|
+
|
|
3049
|
+
renderBody += ` document.querySelectorAll('[data-sinth-remove]').forEach(function(el) {
|
|
3050
|
+
var target = document.getElementById(el.dataset.sinthRemove);
|
|
3051
|
+
if (target) target.remove();
|
|
3052
|
+
});\n`;
|
|
3009
3053
|
|
|
3054
|
+
if (needsIf) {
|
|
3055
|
+
renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);\n`;
|
|
3056
|
+
}
|
|
3057
|
+
if (needsFor) {
|
|
3058
|
+
renderBody += ` document.querySelectorAll('template[data-sinth-for]').forEach(sinthForBlock);\n`;
|
|
3059
|
+
}
|
|
3060
|
+
renderBody += ` document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
|
|
3061
|
+
try { el.value = window[el.dataset.sinthValue] || ''; } catch(e) {}
|
|
3062
|
+
});\n`;
|
|
3063
|
+
renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
3064
|
+
try { el.checked = !!window[el.dataset.sinthChecked]; } catch(e) {}
|
|
3065
|
+
});\n`;
|
|
3066
|
+
if (needsExpr) {
|
|
3067
|
+
renderBody += ` document.querySelectorAll('.sinth-expr').forEach(sinthExpr);\n`;
|
|
3068
|
+
}
|
|
3069
|
+
if (needsDelay) {
|
|
3070
|
+
renderBody += ` setTimeout(function() {
|
|
3071
|
+
document.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
3072
|
+
document.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
3073
|
+
}, 0);\n`;
|
|
3074
|
+
}
|
|
3010
3075
|
renderBody += ` window.scrollTo(_sx, _sy);\n`;
|
|
3011
3076
|
|
|
3077
|
+
const renderFunc = needsRender ? `function sinthRender() {\n${renderBody}}\nsinthRender();` : "";
|
|
3012
3078
|
|
|
3013
|
-
const
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
3024
|
-
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
3025
|
-
else el.style.display = '';
|
|
3026
|
-
} catch(e) {}
|
|
3027
|
-
});
|
|
3028
|
-
}, 0);
|
|
3029
|
-
` : "";
|
|
3030
|
-
|
|
3031
|
-
// Build the expression registry as a JS array
|
|
3032
|
-
const exprArrayJS = opts.exprRegistry && opts.exprRegistry.length > 0
|
|
3033
|
-
? `var __X = [${opts.exprRegistry.map((js) => {
|
|
3034
|
-
return `function(_ctx){ return ${js}; }`;
|
|
3035
|
-
}).join(",")}];\n`
|
|
3036
|
-
: "";
|
|
3079
|
+
const pageCode = `// Sinth page runtime
|
|
3080
|
+
${varLines}
|
|
3081
|
+
${exprArrayJS}
|
|
3082
|
+
${renderFunc}`;
|
|
3083
|
+
|
|
3084
|
+
if (opts.sharedRuntime && helpers.trim()) {
|
|
3085
|
+
const sharedCode = `// Sinth shared runtime
|
|
3086
|
+
${helpers}`;
|
|
3087
|
+
return { page: pageCode, shared: sharedCode };
|
|
3088
|
+
}
|
|
3037
3089
|
|
|
3038
3090
|
return `// Sinth compiled runtime
|
|
3039
3091
|
${varLines}
|
|
3040
|
-
${
|
|
3041
|
-
var hash = 0, i, chr;
|
|
3042
|
-
for (i = 0; i < str.length; i++) {
|
|
3043
|
-
chr = str.charCodeAt(i);
|
|
3044
|
-
hash = ((hash << 5) - hash) + chr;
|
|
3045
|
-
hash |= 0;
|
|
3046
|
-
}
|
|
3047
|
-
return String(hash);
|
|
3048
|
-
}
|
|
3049
|
-
` : ""}
|
|
3092
|
+
${helpers}
|
|
3050
3093
|
${exprArrayJS}
|
|
3051
|
-
${
|
|
3052
|
-
${delayBlock}`;
|
|
3094
|
+
${renderFunc}`;
|
|
3053
3095
|
}
|
|
3054
3096
|
|
|
3055
3097
|
// main compilation pipeline
|
|
@@ -3060,9 +3102,10 @@ interface CompileOptions {
|
|
|
3060
3102
|
libraryPaths: string[];
|
|
3061
3103
|
minify: boolean;
|
|
3062
3104
|
checkOnly: boolean;
|
|
3105
|
+
sharedRuntime: boolean;
|
|
3063
3106
|
}
|
|
3064
3107
|
|
|
3065
|
-
function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
3108
|
+
function compileFile(filePath: string, opts: CompileOptions): { html: string; shared?: string } | null {
|
|
3066
3109
|
const absPath = path.resolve(filePath);
|
|
3067
3110
|
const file = parseFile(absPath);
|
|
3068
3111
|
|
|
@@ -3089,7 +3132,7 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
3089
3132
|
const body = file.uses.map(u => renderCompUse(u, ctx, new Map(), 0)).join("\n");
|
|
3090
3133
|
const pageCSS = file.styles.map(s => processStyleBlock(s, hash)).join("\n");
|
|
3091
3134
|
const allCSS = [pageCSS, ...ctx.extraCSS].join("\n");
|
|
3092
|
-
return `${body}\n<style>\n${allCSS}\n</style
|
|
3135
|
+
return { html: `${body}\n<style>\n${allCSS}\n</style>` };
|
|
3093
3136
|
}
|
|
3094
3137
|
|
|
3095
3138
|
const headData = buildHeadData(file.meta);
|
|
@@ -3123,18 +3166,23 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
3123
3166
|
})(),
|
|
3124
3167
|
}));
|
|
3125
3168
|
|
|
3126
|
-
const
|
|
3127
|
-
|
|
3128
|
-
// build tree-shaken runtime
|
|
3129
|
-
const runtimeJS = buildRuntime({
|
|
3169
|
+
const runtimeResult = buildRuntime({
|
|
3130
3170
|
varDecls: allVarDecls,
|
|
3131
3171
|
bodyHTML,
|
|
3132
3172
|
logicBlocks: ctx.logicBlocks,
|
|
3133
3173
|
mixedBlocks: ctx.mixedBlocks,
|
|
3134
3174
|
assignedVars,
|
|
3135
3175
|
exprRegistry: ctx.exprRegistry,
|
|
3176
|
+
sharedRuntime: opts.sharedRuntime,
|
|
3136
3177
|
});
|
|
3178
|
+
const runtimeJS = typeof runtimeResult === 'string' ? runtimeResult : runtimeResult.page;
|
|
3179
|
+
const sharedJS = typeof runtimeResult === 'string' ? null : runtimeResult.shared;
|
|
3180
|
+
|
|
3181
|
+
const sharedRuntimeTag = opts.sharedRuntime && sharedJS
|
|
3182
|
+
? `<script src="./sinth-runtime.js"></script>`
|
|
3183
|
+
: "";
|
|
3137
3184
|
|
|
3185
|
+
const head = renderHead(headData, relativeCssLinks, relativeJsLinks, scopedCSS, companionJS);
|
|
3138
3186
|
const scriptTags: string[] = [];
|
|
3139
3187
|
if (componentScripts.length > 0) {
|
|
3140
3188
|
scriptTags.push(`<script>\n${componentScripts.join("\n\n")}\n</script>`);
|
|
@@ -3154,13 +3202,15 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
3154
3202
|
head,
|
|
3155
3203
|
`<body data-s="${hash}">`,
|
|
3156
3204
|
bodyHTML,
|
|
3205
|
+
sharedRuntimeTag,
|
|
3157
3206
|
runtimeJS.trim() ? `<script>\n${runtimeJS}\n</script>` : "",
|
|
3158
3207
|
scriptTags.join("\n"),
|
|
3159
3208
|
"</body>",
|
|
3160
3209
|
"</html>",
|
|
3161
3210
|
].filter(Boolean).join("\n");
|
|
3162
3211
|
|
|
3163
|
-
|
|
3212
|
+
const finalHTML = opts.minify ? minifyHTML(html) : html;
|
|
3213
|
+
return sharedJS ? { html: finalHTML, shared: sharedJS } : { html: finalHTML };
|
|
3164
3214
|
}
|
|
3165
3215
|
|
|
3166
3216
|
function minifyHTML(html: string): string {
|
|
@@ -3330,6 +3380,7 @@ async function main(): Promise<void> {
|
|
|
3330
3380
|
const portIdx = args.indexOf("--port");
|
|
3331
3381
|
const port = portIdx !== -1 ? parseInt(args[portIdx + 1], 10) : (cfg.port as number | undefined) ?? 3000;
|
|
3332
3382
|
const minify = args.includes("--prod") || Boolean(cfg.minify);
|
|
3383
|
+
const sharedRuntime = args.includes("--shared-runtime") || Boolean(cfg.sharedRuntime);
|
|
3333
3384
|
const libraryPaths = (cfg.libraryPaths as string[] | undefined) ?? [path.join(cwd, "libraries")];
|
|
3334
3385
|
|
|
3335
3386
|
const flagValues = new Set<string>();
|
|
@@ -3337,7 +3388,7 @@ async function main(): Promise<void> {
|
|
|
3337
3388
|
if (portIdx !== -1) flagValues.add(args[portIdx + 1]);
|
|
3338
3389
|
const cleanArgs = args.filter(a => !a.startsWith("--") && !flagValues.has(a));
|
|
3339
3390
|
|
|
3340
|
-
const opts: CompileOptions = { projectRoot: cwd, outDir, libraryPaths, minify, checkOnly: false };
|
|
3391
|
+
const opts: CompileOptions = { projectRoot: cwd, outDir, libraryPaths, minify, checkOnly: false, sharedRuntime };
|
|
3341
3392
|
|
|
3342
3393
|
switch (command) {
|
|
3343
3394
|
case "build": {
|
|
@@ -3353,10 +3404,15 @@ async function main(): Promise<void> {
|
|
|
3353
3404
|
|
|
3354
3405
|
let hadError = false, built = 0;
|
|
3355
3406
|
|
|
3407
|
+
const sharedRuntimes: string[] = [];
|
|
3356
3408
|
for (const p of pages) {
|
|
3357
3409
|
try {
|
|
3358
|
-
const
|
|
3359
|
-
if (!
|
|
3410
|
+
const result = compileFile(p, opts);
|
|
3411
|
+
if (!result) continue;
|
|
3412
|
+
const html = result.html;
|
|
3413
|
+
if (result.shared) {
|
|
3414
|
+
sharedRuntimes.push(result.shared);
|
|
3415
|
+
}
|
|
3360
3416
|
const rel = path.relative(cwd, p).replace(/\.sinth$/, ".html");
|
|
3361
3417
|
const out = path.join(outDir, rel);
|
|
3362
3418
|
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
@@ -3369,6 +3425,12 @@ async function main(): Promise<void> {
|
|
|
3369
3425
|
}
|
|
3370
3426
|
}
|
|
3371
3427
|
|
|
3428
|
+
if (sharedRuntimes.length > 0) {
|
|
3429
|
+
const combined = sharedRuntimes.join("\n");
|
|
3430
|
+
fs.writeFileSync(path.join(outDir, "sinth-runtime.js"), combined);
|
|
3431
|
+
process.stdout.write(` \x1b[32m✓\x1b[0m sinth-runtime.js (shared)\n`);
|
|
3432
|
+
}
|
|
3433
|
+
|
|
3372
3434
|
// copies to output
|
|
3373
3435
|
const assetsIn = path.join(cwd, "assets"), assetsOut = path.join(outDir, "assets");
|
|
3374
3436
|
if (fs.existsSync(assetsIn)) {
|
|
@@ -3439,8 +3501,24 @@ async function main(): Promise<void> {
|
|
|
3439
3501
|
}
|
|
3440
3502
|
|
|
3441
3503
|
default: {
|
|
3504
|
+
let version = "1.0.0";
|
|
3505
|
+
const paths = [
|
|
3506
|
+
path.resolve(__dirname, "..", "package.json"),
|
|
3507
|
+
path.resolve(__dirname, "package.json"),
|
|
3508
|
+
path.resolve(process.cwd(), "package.json"),
|
|
3509
|
+
path.resolve(process.cwd(), "..", "package.json"),
|
|
3510
|
+
];
|
|
3511
|
+
for (const p of paths) {
|
|
3512
|
+
if (fs.existsSync(p)) {
|
|
3513
|
+
try {
|
|
3514
|
+
const pkg = JSON.parse(fs.readFileSync(p, "utf-8"));
|
|
3515
|
+
version = pkg.version;
|
|
3516
|
+
break;
|
|
3517
|
+
} catch {}
|
|
3518
|
+
}
|
|
3519
|
+
}
|
|
3442
3520
|
process.stdout.write(`
|
|
3443
|
-
\x1b[1mSinth Compiler
|
|
3521
|
+
\x1b[1mSinth Compiler v${version}\x1b[0m
|
|
3444
3522
|
|
|
3445
3523
|
\x1b[1mCommands:\x1b[0m
|
|
3446
3524
|
sinth build [files] [--out ./dist] [--prod] Compile .sinth pages
|
|
@@ -3448,23 +3526,6 @@ async function main(): Promise<void> {
|
|
|
3448
3526
|
sinth check Lint without emitting
|
|
3449
3527
|
sinth init [name] Scaffold a new project
|
|
3450
3528
|
sinth version Print version
|
|
3451
|
-
|
|
3452
|
-
\x1b[1mConfig:\x1b[0m sinth.config.json
|
|
3453
|
-
{ "outDir": "./dist", "libraryPaths": ["./libraries"], "minify": false }
|
|
3454
|
-
|
|
3455
|
-
\x1b[1mSinth Style v1.0.0:\x1b[0m
|
|
3456
|
-
style Paragraph { color: "red"; onHover { color: "blue" } }
|
|
3457
|
-
style { Hero { padding: "4rem"; media(maxWidth: "600px") { padding: "1rem" } } }
|
|
3458
|
-
Paragraph(color: "red", fontSize: "1.2rem") { "Inline shorthand" }
|
|
3459
|
-
|
|
3460
|
-
\x1b[1mMixed if/else:\x1b[0m
|
|
3461
|
-
if guess == target {
|
|
3462
|
-
result = "Correct!"
|
|
3463
|
-
Paragraph(id: "msg") { result }
|
|
3464
|
-
} else {
|
|
3465
|
-
result = "Wrong!"
|
|
3466
|
-
Paragraph(id: "msg") { "Nope!" }
|
|
3467
|
-
}
|
|
3468
3529
|
`);
|
|
3469
3530
|
break;
|
|
3470
3531
|
}
|