@yannosay/sinth 0.6.1 → 0.7.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/LICENSE +659 -659
- package/README.md +5 -1
- package/bin/sinth.js +4 -4
- package/package.json +3 -2
- package/src/sinth.ts +186 -76
- package/tsconfig.json +22 -22
package/README.md
CHANGED
|
@@ -87,9 +87,13 @@ Main {
|
|
|
87
87
|
- Self-contained HTML
|
|
88
88
|
- \+ more!
|
|
89
89
|
|
|
90
|
-
###### Nothing you like? Give
|
|
90
|
+
###### Nothing you like? Give us feedback on our [Discord Server](https://discord.gg/SUvcrafTQm)!
|
|
91
91
|
|
|
92
|
+
## Security
|
|
92
93
|
|
|
94
|
+
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.
|
|
95
|
+
|
|
96
|
+
###### Don't trust? -> Ctrl+F "eval(" returns nothing!
|
|
93
97
|
|
|
94
98
|
## Commands
|
|
95
99
|
|
package/bin/sinth.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const tsSource = path.join(__dirname, '..', 'src', 'sinth.ts');
|
|
4
|
-
require('ts-node').register({ project: path.join(__dirname, '..', 'tsconfig.json') });
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const tsSource = path.join(__dirname, '..', 'src', 'sinth.ts');
|
|
4
|
+
require('ts-node').register({ project: path.join(__dirname, '..', 'tsconfig.json') });
|
|
5
5
|
require(tsSource);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yannosay/sinth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "A declarative language that compiles to pure HTML, CSS, and JavaScript.",
|
|
5
5
|
"author": "Yannosay Productions",
|
|
6
6
|
"license": "AGPL-3.0",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"declarative",
|
|
18
18
|
"web",
|
|
19
19
|
"components",
|
|
20
|
-
"scss"
|
|
20
|
+
"scss",
|
|
21
|
+
"reactive"
|
|
21
22
|
],
|
|
22
23
|
"bin": {
|
|
23
24
|
"sinth": "bin/sinth.js"
|
package/src/sinth.ts
CHANGED
|
@@ -128,6 +128,9 @@ interface CompileCtx {
|
|
|
128
128
|
mixedCounter: number;
|
|
129
129
|
logicBlocks: string[];
|
|
130
130
|
ifIdCounter: number;
|
|
131
|
+
exprRegistry: string[];
|
|
132
|
+
exprMap: Map<string, number>;
|
|
133
|
+
loopVars?: Set<string>;
|
|
131
134
|
}
|
|
132
135
|
|
|
133
136
|
class SinthError extends Error {
|
|
@@ -167,7 +170,7 @@ class SinthWarning {
|
|
|
167
170
|
}
|
|
168
171
|
}
|
|
169
172
|
|
|
170
|
-
|
|
173
|
+
// FNV-1a 32-bit hash → 8-char hex scope id
|
|
171
174
|
function fnv1a(s: string): string {
|
|
172
175
|
let h = 0x811c9dc5;
|
|
173
176
|
for (let i = 0; i < s.length; i++) {
|
|
@@ -183,7 +186,7 @@ function camelToKebab(s: string): string {
|
|
|
183
186
|
return s.replace(/([A-Z])/g, m => `-${m.toLowerCase()}`);
|
|
184
187
|
}
|
|
185
188
|
|
|
186
|
-
|
|
189
|
+
// "w-counter" → "WCounter"
|
|
187
190
|
function tagNameToPascal(tag: string): string {
|
|
188
191
|
return tag.split("-").map(p => p.charAt(0).toUpperCase() + p.slice(1)).join("");
|
|
189
192
|
}
|
|
@@ -937,6 +940,14 @@ private parseArrayLiteral(): Literal {
|
|
|
937
940
|
while (!this.check(TT.RBRACE) && !this.check(TT.EOF)) {
|
|
938
941
|
if (this.check(TT.KW_IF)) { children.push(this.parseIfBlock()); continue; }
|
|
939
942
|
if (this.check(TT.KW_FOR)) { children.push(this.parseForLoop()); continue; }
|
|
943
|
+
if (this.check(TT.LPAREN)) {
|
|
944
|
+
const loc = this.peek().loc;
|
|
945
|
+
const expr = this.parseExpression();
|
|
946
|
+
if (expr) {
|
|
947
|
+
children.push({ kind: "expr", expression: expr, loc });
|
|
948
|
+
}
|
|
949
|
+
continue;
|
|
950
|
+
}
|
|
940
951
|
if (this.check(TT.STRING)) {
|
|
941
952
|
const loc = this.peek().loc;
|
|
942
953
|
children.push({ kind: "text", value: this.consume(TT.STRING).value, loc });
|
|
@@ -1070,23 +1081,36 @@ private parseArrayLiteral(): Literal {
|
|
|
1070
1081
|
let indexVar: string | undefined;
|
|
1071
1082
|
if (this.check(TT.COMMA)) {
|
|
1072
1083
|
this.consume(TT.COMMA);
|
|
1073
|
-
|
|
1074
|
-
itemVar = this.consume(TT.IDENT).value;
|
|
1084
|
+
const secondVar = this.consume(TT.IDENT).value;
|
|
1075
1085
|
if (this.check(TT.COMMA)) {
|
|
1086
|
+
// three variables: key, value, index
|
|
1076
1087
|
this.consume(TT.COMMA);
|
|
1088
|
+
keyVar = firstVar;
|
|
1089
|
+
itemVar = secondVar;
|
|
1077
1090
|
indexVar = this.consume(TT.IDENT).value;
|
|
1091
|
+
} else {
|
|
1092
|
+
// two variables – tentatively value, index; fix after seeing source type
|
|
1093
|
+
itemVar = firstVar;
|
|
1094
|
+
indexVar = secondVar;
|
|
1078
1095
|
}
|
|
1079
1096
|
} else {
|
|
1097
|
+
// one variable: item only
|
|
1080
1098
|
itemVar = firstVar;
|
|
1081
|
-
if (this.check(TT.COMMA)) {
|
|
1082
|
-
this.consume(TT.COMMA);
|
|
1083
|
-
indexVar = this.consume(TT.IDENT).value;
|
|
1084
|
-
}
|
|
1085
1099
|
}
|
|
1086
1100
|
if (!this.check(TT.KW_IN)) throw new SinthError("Expected 'in' after loop variable", this.peek().loc);
|
|
1087
1101
|
this.consume(TT.KW_IN);
|
|
1088
1102
|
const arrayVar = this.consume(TT.IDENT).value;
|
|
1089
1103
|
|
|
1104
|
+
// If two variables and the source is an object, swap to key, value
|
|
1105
|
+
if (keyVar === undefined && indexVar !== undefined) {
|
|
1106
|
+
const srcDecl = this._varDecls.find(d => d.name === arrayVar);
|
|
1107
|
+
if (srcDecl && srcDecl.varType === "obj") {
|
|
1108
|
+
keyVar = itemVar; // first → key
|
|
1109
|
+
itemVar = indexVar; // second → value
|
|
1110
|
+
indexVar = undefined;
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1090
1114
|
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after for..in expression", this.peek().loc);
|
|
1091
1115
|
this.consume(TT.LBRACE);
|
|
1092
1116
|
this.loopVar = itemVar;
|
|
@@ -1177,6 +1201,14 @@ private parseArrayLiteral(): Literal {
|
|
|
1177
1201
|
private parseChildList(): Child[] {
|
|
1178
1202
|
const children: Child[] = [];
|
|
1179
1203
|
while (!this.check(TT.EOF) && !this.check(TT.RBRACE)) {
|
|
1204
|
+
if (this.check(TT.LPAREN)) {
|
|
1205
|
+
const loc = this.peek().loc;
|
|
1206
|
+
const expr = this.parseExpression();
|
|
1207
|
+
if (expr) {
|
|
1208
|
+
children.push({ kind: "expr", expression: expr, loc });
|
|
1209
|
+
}
|
|
1210
|
+
continue;
|
|
1211
|
+
}
|
|
1180
1212
|
if (this.check(TT.STRING)) {
|
|
1181
1213
|
const loc = this.peek().loc;
|
|
1182
1214
|
let leftExpr: Expression = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
|
|
@@ -1990,7 +2022,7 @@ function extractCSSBlock(css: string, openBrace: number): { content: string; end
|
|
|
1990
2022
|
|
|
1991
2023
|
// expression -> js compiler
|
|
1992
2024
|
|
|
1993
|
-
function compileExprToJS(expr: Expression): string {
|
|
2025
|
+
function compileExprToJS(expr: Expression, loopVars?: Set<string>): string {
|
|
1994
2026
|
switch (expr.kind) {
|
|
1995
2027
|
case "literal":
|
|
1996
2028
|
if (!expr.value) return "null";
|
|
@@ -1999,23 +2031,26 @@ function compileExprToJS(expr: Expression): string {
|
|
|
1999
2031
|
if (expr.value.kind === "bool") return String((expr.value as LitBool).value);
|
|
2000
2032
|
return "null";
|
|
2001
2033
|
case "variable":
|
|
2034
|
+
if (loopVars && expr.name && loopVars.has(expr.name)) {
|
|
2035
|
+
return `_ctx.${expr.name}`;
|
|
2036
|
+
}
|
|
2002
2037
|
return expr.name ?? "undefined";
|
|
2003
2038
|
case "binary": {
|
|
2004
|
-
const l = compileExprToJS(expr.left
|
|
2005
|
-
const r = compileExprToJS(expr.right
|
|
2039
|
+
const l = compileExprToJS(expr.left!, loopVars);
|
|
2040
|
+
const r = compileExprToJS(expr.right!, loopVars);
|
|
2006
2041
|
const o = expr.op === "and" ? "&&" : expr.op === "or" ? "||" : expr.op!;
|
|
2007
2042
|
return `(${l} ${o} ${r})`;
|
|
2008
2043
|
}
|
|
2009
2044
|
case "unary": {
|
|
2010
2045
|
const o = expr.op === "not" ? "!" : expr.op!;
|
|
2011
|
-
return `${o}(${compileExprToJS(expr.operand
|
|
2046
|
+
return `${o}(${compileExprToJS(expr.operand!, loopVars)})`;
|
|
2012
2047
|
}
|
|
2013
2048
|
case "assign": {
|
|
2014
|
-
const v = expr.right ? compileExprToJS(expr.right) : "null";
|
|
2049
|
+
const v = expr.right ? compileExprToJS(expr.right, loopVars) : "null";
|
|
2015
2050
|
return `${expr.target} ${expr.op} ${v}`;
|
|
2016
2051
|
}
|
|
2017
2052
|
case "index":
|
|
2018
|
-
return `${compileExprToJS(expr.object
|
|
2053
|
+
return `${compileExprToJS(expr.object!, loopVars)}[${compileExprToJS(expr.key!, loopVars)}]`;
|
|
2019
2054
|
case "postfix":
|
|
2020
2055
|
return `${expr.target}${expr.op}`;
|
|
2021
2056
|
default:
|
|
@@ -2088,7 +2123,18 @@ function resolveBuiltinTag(
|
|
|
2088
2123
|
return { tag: name.toLowerCase(), voidEl: false };
|
|
2089
2124
|
}
|
|
2090
2125
|
|
|
2091
|
-
function
|
|
2126
|
+
function registerExpr(ctx: CompileCtx, expr: Expression): number {
|
|
2127
|
+
const jsExpr = compileExprToJS(expr, ctx.loopVars);
|
|
2128
|
+
const existing = ctx.exprMap.get(jsExpr);
|
|
2129
|
+
if (existing !== undefined) return existing;
|
|
2130
|
+
const id = ctx.exprRegistry.length;
|
|
2131
|
+
ctx.exprRegistry.push(jsExpr);
|
|
2132
|
+
ctx.exprMap.set(jsExpr, id);
|
|
2133
|
+
return id;
|
|
2134
|
+
}
|
|
2135
|
+
|
|
2136
|
+
|
|
2137
|
+
function renderAttr(attr: Attr, paramMap: Map<string, string>, ctx?: CompileCtx): string {
|
|
2092
2138
|
const { name, value } = attr;
|
|
2093
2139
|
if (value === null) return name;
|
|
2094
2140
|
if (value.kind === "null") return "";
|
|
@@ -2110,11 +2156,13 @@ if (name === "delay") {
|
|
|
2110
2156
|
if (v.startsWith("__EXPR__")) {
|
|
2111
2157
|
try {
|
|
2112
2158
|
const expr: Expression = JSON.parse(v.substring(8));
|
|
2113
|
-
const
|
|
2114
|
-
return `data-sinth-delay-expr="${
|
|
2159
|
+
const id = registerExpr(ctx, expr);
|
|
2160
|
+
return `data-sinth-delay-expr-id="${id}" style="display:none"`;
|
|
2115
2161
|
} catch {}
|
|
2116
2162
|
}
|
|
2117
|
-
|
|
2163
|
+
// plain variable name – register as expression
|
|
2164
|
+
const id = registerExpr(ctx, { kind: "variable", name: v });
|
|
2165
|
+
return `data-sinth-delay-expr-id="${id}" style="display:none"`;
|
|
2118
2166
|
}
|
|
2119
2167
|
|
|
2120
2168
|
if (name === "checked") {
|
|
@@ -2182,6 +2230,7 @@ function interpolateAttr(text: string, params: Map<string, string>): string {
|
|
|
2182
2230
|
return s.replace(/\x00LB\x00/g, "{");
|
|
2183
2231
|
}
|
|
2184
2232
|
|
|
2233
|
+
|
|
2185
2234
|
function renderChild(
|
|
2186
2235
|
child: Child,
|
|
2187
2236
|
ctx: CompileCtx,
|
|
@@ -2195,8 +2244,15 @@ function renderChild(
|
|
|
2195
2244
|
return renderText(child.value, params);
|
|
2196
2245
|
|
|
2197
2246
|
case "expr": {
|
|
2198
|
-
|
|
2199
|
-
|
|
2247
|
+
if (child.expression.kind === "literal" && child.expression.value) {
|
|
2248
|
+
return esc(litToString(child.expression.value));
|
|
2249
|
+
}
|
|
2250
|
+
if (child.expression.kind === "variable" && child.expression.name && !ctx.loopVars?.has(child.expression.name)) {
|
|
2251
|
+
const exprId = registerExpr(ctx, child.expression);
|
|
2252
|
+
return `<span class="sinth-expr" data-expr-id="${exprId}"></span>`;
|
|
2253
|
+
}
|
|
2254
|
+
const exprId = registerExpr(ctx, child.expression);
|
|
2255
|
+
return `<span class="sinth-expr" data-expr-id="${exprId}"></span>`;
|
|
2200
2256
|
}
|
|
2201
2257
|
|
|
2202
2258
|
case "assign_stmt": {
|
|
@@ -2208,7 +2264,16 @@ function renderChild(
|
|
|
2208
2264
|
return renderIfBlock(child, ctx, params, depth);
|
|
2209
2265
|
|
|
2210
2266
|
case "for": {
|
|
2267
|
+
// collect loop‑variable names
|
|
2268
|
+
const loopVars = new Set<string>();
|
|
2269
|
+
loopVars.add(child.itemVar);
|
|
2270
|
+
if (child.keyVar) loopVars.add(child.keyVar);
|
|
2271
|
+
if (child.indexVar) loopVars.add(child.indexVar);
|
|
2272
|
+
// set them on ctx for compilation of the body
|
|
2273
|
+
const prev = ctx.loopVars;
|
|
2274
|
+
ctx.loopVars = loopVars;
|
|
2211
2275
|
const bodyHTML = child.body.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
2276
|
+
ctx.loopVars = prev; // restore
|
|
2212
2277
|
const keyAttr = child.keyVar ? ` data-sinth-key="${escAttr(child.keyVar)}"` : "";
|
|
2213
2278
|
const idxAttr = child.indexVar ? ` data-sinth-index="${escAttr(child.indexVar)}"` : "";
|
|
2214
2279
|
return (
|
|
@@ -2243,10 +2308,11 @@ depth: number,
|
|
|
2243
2308
|
// pure DOM
|
|
2244
2309
|
if (!hasAssign && hasComp) {
|
|
2245
2310
|
const tplId = ctx.ifIdCounter++;
|
|
2311
|
+
const condId = registerExpr(ctx, ifBlock.condition);
|
|
2246
2312
|
const bodyHTML = ifBlock.body.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
2247
2313
|
const elseHTML = (ifBlock.elseBody ?? []).map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
2248
2314
|
return (
|
|
2249
|
-
`<template data-sinth-if="${
|
|
2315
|
+
`<template data-sinth-if-id="${tplId}" data-sinth-if-expr="${condId}">${bodyHTML}</template>` +
|
|
2250
2316
|
(elseHTML ? `<template data-sinth-else data-sinth-if-id="${tplId}">${elseHTML}</template>` : "")
|
|
2251
2317
|
);
|
|
2252
2318
|
}
|
|
@@ -2297,7 +2363,8 @@ depth: number,
|
|
|
2297
2363
|
}
|
|
2298
2364
|
}
|
|
2299
2365
|
|
|
2300
|
-
|
|
2366
|
+
const condId = registerExpr(ctx, ifBlock.condition);
|
|
2367
|
+
ctx.mixedBlocks.push({ id, conditionJS: String(condId), ifJS: ifAssignJS, ifHTML, elseJS: elseAssignJS, elseHTML, replaceId });
|
|
2301
2368
|
|
|
2302
2369
|
return `<span id="${replaceId || id}" data-sinth-mixed></span>`;
|
|
2303
2370
|
}
|
|
@@ -2329,7 +2396,7 @@ function renderCompUse(
|
|
|
2329
2396
|
if (customEl) {
|
|
2330
2397
|
const attrParts = [`data-s="${ctx.scopeHash}"`];
|
|
2331
2398
|
for (const attr of use.attrs) {
|
|
2332
|
-
const r = renderAttr(attr, params);
|
|
2399
|
+
const r = renderAttr(attr, params, ctx);
|
|
2333
2400
|
if (r) attrParts.push(r);
|
|
2334
2401
|
}
|
|
2335
2402
|
const inner = use.children.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
@@ -2359,7 +2426,7 @@ function renderCompUse(
|
|
|
2359
2426
|
if (attr.value?.kind === "str") userClass = interpolateAttr(attr.value.value, params);
|
|
2360
2427
|
continue;
|
|
2361
2428
|
}
|
|
2362
|
-
const rendered = renderAttr(attr, params);
|
|
2429
|
+
const rendered = renderAttr(attr, params, ctx);
|
|
2363
2430
|
if (rendered) attrParts.push(rendered);
|
|
2364
2431
|
}
|
|
2365
2432
|
|
|
@@ -2588,14 +2655,15 @@ function buildRuntime(opts: {
|
|
|
2588
2655
|
logicBlocks: string[];
|
|
2589
2656
|
mixedBlocks: MixedBlockEntry[];
|
|
2590
2657
|
assignedVars: Set<string>;
|
|
2658
|
+
exprRegistry: string[];
|
|
2591
2659
|
}): string {
|
|
2592
|
-
const { varDecls, bodyHTML, logicBlocks, mixedBlocks, assignedVars } = opts;
|
|
2660
|
+
const { varDecls, bodyHTML, logicBlocks, mixedBlocks, assignedVars, exprRegistry } = opts;
|
|
2593
2661
|
|
|
2594
2662
|
|
|
2595
2663
|
const needsExpr = bodyHTML.includes("sinth-expr");
|
|
2596
2664
|
const needsIf = bodyHTML.includes("data-sinth-if");
|
|
2597
2665
|
const needsFor = bodyHTML.includes("data-sinth-for");
|
|
2598
|
-
const needsDelay = bodyHTML.includes("data-sinth-delay") || bodyHTML.includes("data-sinth-delay-expr") || mixedBlocks.some(mb => mb.ifHTML.includes("data-sinth-delay") || mb.ifHTML.includes("data-sinth-delay-expr") || mb.elseHTML.includes("data-sinth-delay") || mb.elseHTML.includes("data-sinth-delay-expr"));
|
|
2666
|
+
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"));
|
|
2599
2667
|
const needsMixed = mixedBlocks.length > 0;
|
|
2600
2668
|
const needsLogic = logicBlocks.length > 0;
|
|
2601
2669
|
const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic;
|
|
@@ -2642,8 +2710,8 @@ return `var ${v.name} = ${val};`;
|
|
|
2642
2710
|
if (needsFor) {
|
|
2643
2711
|
renderBody += `
|
|
2644
2712
|
document.querySelectorAll('template[data-sinth-for]').forEach(function(t) {
|
|
2645
|
-
var source;
|
|
2646
|
-
|
|
2713
|
+
var source = window[t.dataset.sinthFor];
|
|
2714
|
+
if (source === undefined) source = [];
|
|
2647
2715
|
var newHash = '';
|
|
2648
2716
|
try { newHash = hashString(JSON.stringify(source)); } catch(e) { newHash = ''; }
|
|
2649
2717
|
if (t.dataset.sinthForHash && t.dataset.sinthForHash === newHash) {
|
|
@@ -2680,27 +2748,32 @@ return `var ${v.name} = ${val};`;
|
|
|
2680
2748
|
var _user = _v;
|
|
2681
2749
|
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
2682
2750
|
frag.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
2683
|
-
try {
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
el.textContent = eval(el.dataset.expr);
|
|
2693
|
-
el.classList.remove('sinth-expr'); // Mark as processed
|
|
2694
|
-
}
|
|
2751
|
+
try {
|
|
2752
|
+
var exprFn = __X[el.dataset.exprId];
|
|
2753
|
+
if (!exprFn) return;
|
|
2754
|
+
var _ctx = {};
|
|
2755
|
+
if (_item) _ctx[_item] = _v;
|
|
2756
|
+
if (_key) _ctx[_key] = _k;
|
|
2757
|
+
if (_idx) _ctx[_idx] = _loopIdx - 1;
|
|
2758
|
+
el.textContent = exprFn(_ctx);
|
|
2759
|
+
el.classList.remove('sinth-expr');
|
|
2695
2760
|
} catch(e) {}
|
|
2696
2761
|
});
|
|
2697
|
-
frag.querySelectorAll('template[data-sinth-if]').forEach(function(ifT) {
|
|
2762
|
+
frag.querySelectorAll('template[data-sinth-if-expr]').forEach(function(ifT) {
|
|
2763
|
+
var condFn = __X[ifT.dataset.sinthIfExpr];
|
|
2764
|
+
var _ctx = {};
|
|
2765
|
+
if (_item) _ctx[_item] = _v;
|
|
2766
|
+
if (_key) _ctx[_key] = _k;
|
|
2767
|
+
if (_idx) _ctx[_idx] = _loopIdx - 1;
|
|
2698
2768
|
var cond = false;
|
|
2699
|
-
try {
|
|
2769
|
+
try { if (condFn) cond = condFn(_ctx); } catch(e) {}
|
|
2700
2770
|
if (cond) {
|
|
2701
2771
|
var ifContent = document.createRange().createContextualFragment(ifT.innerHTML);
|
|
2702
2772
|
ifContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
2703
|
-
try {
|
|
2773
|
+
try {
|
|
2774
|
+
var exprFn2 = __X[el2.dataset.exprId];
|
|
2775
|
+
if (exprFn2) el2.textContent = exprFn2(_ctx);
|
|
2776
|
+
} catch(e) {}
|
|
2704
2777
|
});
|
|
2705
2778
|
ifT.parentNode.insertBefore(ifContent, ifT);
|
|
2706
2779
|
} else {
|
|
@@ -2708,7 +2781,10 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2708
2781
|
if (elseT && elseT.hasAttribute('data-sinth-else')) {
|
|
2709
2782
|
var elseContent = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
2710
2783
|
elseContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
2711
|
-
try {
|
|
2784
|
+
try {
|
|
2785
|
+
var exprFn2 = __X[el2.dataset.exprId];
|
|
2786
|
+
if (exprFn2) el2.textContent = exprFn2(_ctx);
|
|
2787
|
+
} catch(e) {}
|
|
2712
2788
|
});
|
|
2713
2789
|
ifT.parentNode.insertBefore(elseContent, ifT);
|
|
2714
2790
|
}
|
|
@@ -2722,14 +2798,16 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2722
2798
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2723
2799
|
else el.style.display = '';
|
|
2724
2800
|
});
|
|
2725
|
-
frag.querySelectorAll('[data-sinth-delay-expr]').forEach(function(el) {
|
|
2801
|
+
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2726
2802
|
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
2727
2803
|
el.dataset.sinthDelayDone = '1';
|
|
2728
2804
|
try {
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
if (
|
|
2732
|
-
|
|
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;
|
|
2733
2811
|
el.style.display = 'none';
|
|
2734
2812
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2735
2813
|
else el.style.display = '';
|
|
@@ -2742,11 +2820,11 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2742
2820
|
}
|
|
2743
2821
|
if (needsIf) {
|
|
2744
2822
|
renderBody += `
|
|
2745
|
-
document.querySelectorAll('template[data-sinth-if]').forEach(function(t) {
|
|
2823
|
+
document.querySelectorAll('template[data-sinth-if-expr]').forEach(function(t) {
|
|
2746
2824
|
var ifId = t.dataset.sinthIfId;
|
|
2747
2825
|
var anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
|
|
2748
|
-
var
|
|
2749
|
-
|
|
2826
|
+
var condFn = __X[t.dataset.sinthIfExpr];
|
|
2827
|
+
var cond = condFn ? condFn() : false;
|
|
2750
2828
|
if (cond) {
|
|
2751
2829
|
if (anchor) {
|
|
2752
2830
|
var cur = anchor.nextSibling;
|
|
@@ -2759,7 +2837,10 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2759
2837
|
}
|
|
2760
2838
|
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
2761
2839
|
frag.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
2762
|
-
try {
|
|
2840
|
+
try {
|
|
2841
|
+
var exprFn = __X[el.dataset.exprId];
|
|
2842
|
+
if (exprFn) el.textContent = exprFn();
|
|
2843
|
+
} catch(e) {}
|
|
2763
2844
|
});
|
|
2764
2845
|
frag.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2765
2846
|
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
@@ -2769,9 +2850,10 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2769
2850
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2770
2851
|
else el.style.display = '';
|
|
2771
2852
|
});
|
|
2772
|
-
frag.querySelectorAll('[data-sinth-delay-expr]').forEach(function(el) {
|
|
2853
|
+
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2773
2854
|
try {
|
|
2774
|
-
var
|
|
2855
|
+
var fn = __X[el.dataset.sinthDelayExprId];
|
|
2856
|
+
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
2775
2857
|
el.style.display = '';
|
|
2776
2858
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2777
2859
|
} catch(e) {}
|
|
@@ -2797,7 +2879,10 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2797
2879
|
}
|
|
2798
2880
|
var ef = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
2799
2881
|
ef.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
2800
|
-
try {
|
|
2882
|
+
try {
|
|
2883
|
+
var exprFn = __X[el.dataset.exprId];
|
|
2884
|
+
if (exprFn) el.textContent = exprFn();
|
|
2885
|
+
} catch(e) {}
|
|
2801
2886
|
});
|
|
2802
2887
|
ef.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2803
2888
|
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
@@ -2805,9 +2890,10 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2805
2890
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2806
2891
|
else el.style.display = '';
|
|
2807
2892
|
});
|
|
2808
|
-
ef.querySelectorAll('[data-sinth-delay-expr]').forEach(function(el) {
|
|
2893
|
+
ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2809
2894
|
try {
|
|
2810
|
-
var
|
|
2895
|
+
var fn = __X[el.dataset.sinthDelayExprId];
|
|
2896
|
+
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
2811
2897
|
el.style.display = '';
|
|
2812
2898
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2813
2899
|
} catch(e) {}
|
|
@@ -2831,28 +2917,34 @@ if (needsMixed) {
|
|
|
2831
2917
|
for (const mb of mixedBlocks) {
|
|
2832
2918
|
const ifHTMLJS = JSON.stringify(mb.ifHTML);
|
|
2833
2919
|
const elseHTMLJS = JSON.stringify(mb.elseHTML);
|
|
2834
|
-
const hasDelay = mb.ifHTML.includes("data-sinth-delay") || mb.elseHTML.includes("data-sinth-delay");
|
|
2920
|
+
const hasDelay = mb.ifHTML.includes("data-sinth-delay") || mb.elseHTML.includes("data-sinth-delay") || mb.ifHTML.includes("data-sinth-delay-expr-id") || mb.elseHTML.includes("data-sinth-delay-expr-id");
|
|
2835
2921
|
renderBody += `
|
|
2836
2922
|
(function() {
|
|
2837
2923
|
var __el = document.getElementById(${JSON.stringify(mb.replaceId || mb.id)});
|
|
2838
2924
|
if (__el) {
|
|
2839
|
-
var
|
|
2840
|
-
|
|
2925
|
+
var condFn = __X[${mb.conditionJS}];
|
|
2926
|
+
var __cond = condFn ? condFn() : false;
|
|
2841
2927
|
if (__cond) {
|
|
2842
2928
|
${mb.ifJS ? mb.ifJS : ""} __el.innerHTML = ${ifHTMLJS};
|
|
2843
2929
|
} else {
|
|
2844
2930
|
${mb.elseJS ? mb.elseJS : ""} __el.innerHTML = ${elseHTMLJS};
|
|
2845
2931
|
}
|
|
2846
2932
|
__el.querySelectorAll('.sinth-expr').forEach(function(e) {
|
|
2847
|
-
try {
|
|
2933
|
+
try {
|
|
2934
|
+
var exprFn = __X[e.dataset.exprId];
|
|
2935
|
+
if (exprFn) e.textContent = exprFn();
|
|
2936
|
+
} catch(ex) {}
|
|
2848
2937
|
});
|
|
2849
|
-
__el.querySelectorAll('template[data-sinth-if]').forEach(function(innerT) {
|
|
2850
|
-
var
|
|
2851
|
-
|
|
2938
|
+
__el.querySelectorAll('template[data-sinth-if-expr]').forEach(function(innerT) {
|
|
2939
|
+
var innerCondFn = __X[innerT.dataset.sinthIfExpr];
|
|
2940
|
+
var innerCond = innerCondFn ? innerCondFn() : false;
|
|
2852
2941
|
if (innerCond) {
|
|
2853
2942
|
var innerFrag = document.createRange().createContextualFragment(innerT.innerHTML);
|
|
2854
2943
|
innerFrag.querySelectorAll('.sinth-expr').forEach(function(e2) {
|
|
2855
|
-
try {
|
|
2944
|
+
try {
|
|
2945
|
+
var exprFn2 = __X[e2.dataset.exprId];
|
|
2946
|
+
if (exprFn2) e2.textContent = exprFn2();
|
|
2947
|
+
} catch(ex) {}
|
|
2856
2948
|
});
|
|
2857
2949
|
innerT.parentNode.insertBefore(innerFrag, innerT);
|
|
2858
2950
|
} else {
|
|
@@ -2860,23 +2952,26 @@ __el.querySelectorAll('template[data-sinth-if]').forEach(function(innerT) {
|
|
|
2860
2952
|
if (innerElse && innerElse.hasAttribute('data-sinth-else')) {
|
|
2861
2953
|
var innerElseFrag = document.createRange().createContextualFragment(innerElse.innerHTML);
|
|
2862
2954
|
innerElseFrag.querySelectorAll('.sinth-expr').forEach(function(e2) {
|
|
2863
|
-
try {
|
|
2955
|
+
try {
|
|
2956
|
+
var exprFn2 = __X[e2.dataset.exprId];
|
|
2957
|
+
if (exprFn2) e2.textContent = exprFn2();
|
|
2958
|
+
} catch(ex) {}
|
|
2864
2959
|
});
|
|
2865
2960
|
innerT.parentNode.insertBefore(innerElseFrag, innerT);
|
|
2866
2961
|
}
|
|
2867
2962
|
}
|
|
2868
2963
|
});
|
|
2869
2964
|
${hasDelay ? `
|
|
2870
|
-
// Handle delays for this mixed block
|
|
2871
2965
|
__el.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2872
2966
|
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
2873
|
-
el.style.display = 'none';
|
|
2967
|
+
el.style.display = 'none';
|
|
2874
2968
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2875
2969
|
else el.style.display = '';
|
|
2876
2970
|
});
|
|
2877
|
-
__el.querySelectorAll('[data-sinth-delay-expr]').forEach(function(el) {
|
|
2971
|
+
__el.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2878
2972
|
try {
|
|
2879
|
-
var
|
|
2973
|
+
var fn = __X[el.dataset.sinthDelayExprId];
|
|
2974
|
+
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
2880
2975
|
el.style.display = 'none';
|
|
2881
2976
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2882
2977
|
else el.style.display = '';
|
|
@@ -2892,10 +2987,10 @@ else el.style.display = '';
|
|
|
2892
2987
|
// set input vals from bound vars
|
|
2893
2988
|
renderBody += `
|
|
2894
2989
|
document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
|
|
2895
|
-
try { el.value =
|
|
2990
|
+
try { el.value = window[el.dataset.sinthValue] || ''; } catch(e) {}
|
|
2896
2991
|
});
|
|
2897
2992
|
document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
2898
|
-
try { el.checked = !!
|
|
2993
|
+
try { el.checked = !!window[el.dataset.sinthChecked]; } catch(e) {}
|
|
2899
2994
|
});
|
|
2900
2995
|
`;
|
|
2901
2996
|
|
|
@@ -2903,7 +2998,10 @@ else el.style.display = '';
|
|
|
2903
2998
|
if (needsExpr) {
|
|
2904
2999
|
renderBody += `
|
|
2905
3000
|
document.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
2906
|
-
try {
|
|
3001
|
+
try {
|
|
3002
|
+
var exprFn = __X[el.dataset.exprId];
|
|
3003
|
+
if (exprFn) el.textContent = exprFn({});
|
|
3004
|
+
} catch(e) {}
|
|
2907
3005
|
});
|
|
2908
3006
|
`;
|
|
2909
3007
|
}
|
|
@@ -2919,9 +3017,10 @@ setTimeout(function() {
|
|
|
2919
3017
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2920
3018
|
else el.style.display = '';
|
|
2921
3019
|
});
|
|
2922
|
-
document.querySelectorAll('[data-sinth-delay-expr]').forEach(function(el) {
|
|
3020
|
+
document.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2923
3021
|
try {
|
|
2924
|
-
var
|
|
3022
|
+
var fn = __X[el.dataset.sinthDelayExprId];
|
|
3023
|
+
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
2925
3024
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2926
3025
|
else el.style.display = '';
|
|
2927
3026
|
} catch(e) {}
|
|
@@ -2929,6 +3028,13 @@ setTimeout(function() {
|
|
|
2929
3028
|
}, 0);
|
|
2930
3029
|
` : "";
|
|
2931
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
|
+
: "";
|
|
3037
|
+
|
|
2932
3038
|
return `// Sinth compiled runtime
|
|
2933
3039
|
${varLines}
|
|
2934
3040
|
${needsFor ? `function hashString(str) {
|
|
@@ -2941,6 +3047,7 @@ ${needsFor ? `function hashString(str) {
|
|
|
2941
3047
|
return String(hash);
|
|
2942
3048
|
}
|
|
2943
3049
|
` : ""}
|
|
3050
|
+
${exprArrayJS}
|
|
2944
3051
|
${needsRender ? `function sinthRender() {\n${renderBody}}\nsinthRender();` : ""}
|
|
2945
3052
|
${delayBlock}`;
|
|
2946
3053
|
}
|
|
@@ -2974,6 +3081,8 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2974
3081
|
mixedCounter: 0,
|
|
2975
3082
|
logicBlocks: [],
|
|
2976
3083
|
ifIdCounter: 0,
|
|
3084
|
+
exprRegistry: [],
|
|
3085
|
+
exprMap: new Map(),
|
|
2977
3086
|
};
|
|
2978
3087
|
|
|
2979
3088
|
if (!file.isPage) {
|
|
@@ -3023,6 +3132,7 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
3023
3132
|
logicBlocks: ctx.logicBlocks,
|
|
3024
3133
|
mixedBlocks: ctx.mixedBlocks,
|
|
3025
3134
|
assignedVars,
|
|
3135
|
+
exprRegistry: ctx.exprRegistry,
|
|
3026
3136
|
});
|
|
3027
3137
|
|
|
3028
3138
|
const scriptTags: string[] = [];
|