@yannosay/sinth 0.6.1 → 0.7.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/LICENSE +659 -659
- package/README.md +5 -1
- package/bin/sinth.js +4 -4
- package/package.json +3 -2
- package/src/sinth.ts +204 -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.1",
|
|
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,21 @@ 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("");
|
|
2314
|
+
let replaceAttr = "";
|
|
2315
|
+
const firstComp = ifBlock.body.find(c => c.kind === "use") as CompUse | undefined;
|
|
2316
|
+
if (firstComp) {
|
|
2317
|
+
const idAttr = firstComp.attrs.find(a => a.name === "id");
|
|
2318
|
+
const replAttr = firstComp.attrs.find(a => a.name === "replace");
|
|
2319
|
+
const wantsReplace = !replAttr || replAttr.value === null || (replAttr.value?.kind === "bool" && replAttr.value.value === true);
|
|
2320
|
+
if (idAttr && idAttr.value?.kind === "str" && wantsReplace) {
|
|
2321
|
+
replaceAttr = ` data-sinth-if-replace="${escAttr(idAttr.value.value)}"`;
|
|
2322
|
+
}
|
|
2323
|
+
}
|
|
2248
2324
|
return (
|
|
2249
|
-
`<template data-sinth-if="${
|
|
2325
|
+
`<template data-sinth-if-id="${tplId}" data-sinth-if-expr="${condId}"${replaceAttr}>${bodyHTML}</template>` +
|
|
2250
2326
|
(elseHTML ? `<template data-sinth-else data-sinth-if-id="${tplId}">${elseHTML}</template>` : "")
|
|
2251
2327
|
);
|
|
2252
2328
|
}
|
|
@@ -2297,7 +2373,8 @@ depth: number,
|
|
|
2297
2373
|
}
|
|
2298
2374
|
}
|
|
2299
2375
|
|
|
2300
|
-
|
|
2376
|
+
const condId = registerExpr(ctx, ifBlock.condition);
|
|
2377
|
+
ctx.mixedBlocks.push({ id, conditionJS: String(condId), ifJS: ifAssignJS, ifHTML, elseJS: elseAssignJS, elseHTML, replaceId });
|
|
2301
2378
|
|
|
2302
2379
|
return `<span id="${replaceId || id}" data-sinth-mixed></span>`;
|
|
2303
2380
|
}
|
|
@@ -2329,7 +2406,7 @@ function renderCompUse(
|
|
|
2329
2406
|
if (customEl) {
|
|
2330
2407
|
const attrParts = [`data-s="${ctx.scopeHash}"`];
|
|
2331
2408
|
for (const attr of use.attrs) {
|
|
2332
|
-
const r = renderAttr(attr, params);
|
|
2409
|
+
const r = renderAttr(attr, params, ctx);
|
|
2333
2410
|
if (r) attrParts.push(r);
|
|
2334
2411
|
}
|
|
2335
2412
|
const inner = use.children.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
@@ -2359,7 +2436,7 @@ function renderCompUse(
|
|
|
2359
2436
|
if (attr.value?.kind === "str") userClass = interpolateAttr(attr.value.value, params);
|
|
2360
2437
|
continue;
|
|
2361
2438
|
}
|
|
2362
|
-
const rendered = renderAttr(attr, params);
|
|
2439
|
+
const rendered = renderAttr(attr, params, ctx);
|
|
2363
2440
|
if (rendered) attrParts.push(rendered);
|
|
2364
2441
|
}
|
|
2365
2442
|
|
|
@@ -2588,14 +2665,15 @@ function buildRuntime(opts: {
|
|
|
2588
2665
|
logicBlocks: string[];
|
|
2589
2666
|
mixedBlocks: MixedBlockEntry[];
|
|
2590
2667
|
assignedVars: Set<string>;
|
|
2668
|
+
exprRegistry: string[];
|
|
2591
2669
|
}): string {
|
|
2592
|
-
const { varDecls, bodyHTML, logicBlocks, mixedBlocks, assignedVars } = opts;
|
|
2670
|
+
const { varDecls, bodyHTML, logicBlocks, mixedBlocks, assignedVars, exprRegistry } = opts;
|
|
2593
2671
|
|
|
2594
2672
|
|
|
2595
2673
|
const needsExpr = bodyHTML.includes("sinth-expr");
|
|
2596
2674
|
const needsIf = bodyHTML.includes("data-sinth-if");
|
|
2597
2675
|
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"));
|
|
2676
|
+
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
2677
|
const needsMixed = mixedBlocks.length > 0;
|
|
2600
2678
|
const needsLogic = logicBlocks.length > 0;
|
|
2601
2679
|
const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic;
|
|
@@ -2642,8 +2720,8 @@ return `var ${v.name} = ${val};`;
|
|
|
2642
2720
|
if (needsFor) {
|
|
2643
2721
|
renderBody += `
|
|
2644
2722
|
document.querySelectorAll('template[data-sinth-for]').forEach(function(t) {
|
|
2645
|
-
var source;
|
|
2646
|
-
|
|
2723
|
+
var source = window[t.dataset.sinthFor];
|
|
2724
|
+
if (source === undefined) source = [];
|
|
2647
2725
|
var newHash = '';
|
|
2648
2726
|
try { newHash = hashString(JSON.stringify(source)); } catch(e) { newHash = ''; }
|
|
2649
2727
|
if (t.dataset.sinthForHash && t.dataset.sinthForHash === newHash) {
|
|
@@ -2678,29 +2756,38 @@ return `var ${v.name} = ${val};`;
|
|
|
2678
2756
|
|
|
2679
2757
|
_loopIdx++;
|
|
2680
2758
|
var _user = _v;
|
|
2759
|
+
if (t.dataset.sinthIfReplace) {
|
|
2760
|
+
var _rp = document.getElementById(t.dataset.sinthIfReplace);
|
|
2761
|
+
if (_rp) _rp.parentNode.removeChild(_rp);
|
|
2762
|
+
}
|
|
2681
2763
|
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
2682
2764
|
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
|
-
}
|
|
2765
|
+
try {
|
|
2766
|
+
var exprFn = __X[el.dataset.exprId];
|
|
2767
|
+
if (!exprFn) return;
|
|
2768
|
+
var _ctx = {};
|
|
2769
|
+
if (_item) _ctx[_item] = _v;
|
|
2770
|
+
if (_key) _ctx[_key] = _k;
|
|
2771
|
+
if (_idx) _ctx[_idx] = _loopIdx - 1;
|
|
2772
|
+
el.textContent = exprFn(_ctx);
|
|
2773
|
+
el.classList.remove('sinth-expr');
|
|
2695
2774
|
} catch(e) {}
|
|
2696
2775
|
});
|
|
2697
|
-
frag.querySelectorAll('template[data-sinth-if]').forEach(function(ifT) {
|
|
2776
|
+
frag.querySelectorAll('template[data-sinth-if-expr]').forEach(function(ifT) {
|
|
2777
|
+
var condFn = __X[ifT.dataset.sinthIfExpr];
|
|
2778
|
+
var _ctx = {};
|
|
2779
|
+
if (_item) _ctx[_item] = _v;
|
|
2780
|
+
if (_key) _ctx[_key] = _k;
|
|
2781
|
+
if (_idx) _ctx[_idx] = _loopIdx - 1;
|
|
2698
2782
|
var cond = false;
|
|
2699
|
-
try {
|
|
2783
|
+
try { if (condFn) cond = condFn(_ctx); } catch(e) {}
|
|
2700
2784
|
if (cond) {
|
|
2701
2785
|
var ifContent = document.createRange().createContextualFragment(ifT.innerHTML);
|
|
2702
2786
|
ifContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
2703
|
-
try {
|
|
2787
|
+
try {
|
|
2788
|
+
var exprFn2 = __X[el2.dataset.exprId];
|
|
2789
|
+
if (exprFn2) el2.textContent = exprFn2(_ctx);
|
|
2790
|
+
} catch(e) {}
|
|
2704
2791
|
});
|
|
2705
2792
|
ifT.parentNode.insertBefore(ifContent, ifT);
|
|
2706
2793
|
} else {
|
|
@@ -2708,7 +2795,10 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2708
2795
|
if (elseT && elseT.hasAttribute('data-sinth-else')) {
|
|
2709
2796
|
var elseContent = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
2710
2797
|
elseContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
2711
|
-
try {
|
|
2798
|
+
try {
|
|
2799
|
+
var exprFn2 = __X[el2.dataset.exprId];
|
|
2800
|
+
if (exprFn2) el2.textContent = exprFn2(_ctx);
|
|
2801
|
+
} catch(e) {}
|
|
2712
2802
|
});
|
|
2713
2803
|
ifT.parentNode.insertBefore(elseContent, ifT);
|
|
2714
2804
|
}
|
|
@@ -2722,14 +2812,16 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2722
2812
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2723
2813
|
else el.style.display = '';
|
|
2724
2814
|
});
|
|
2725
|
-
frag.querySelectorAll('[data-sinth-delay-expr]').forEach(function(el) {
|
|
2815
|
+
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2726
2816
|
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
2727
2817
|
el.dataset.sinthDelayDone = '1';
|
|
2728
2818
|
try {
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
if (
|
|
2732
|
-
|
|
2819
|
+
var fn = __X[el.dataset.sinthDelayExprId];
|
|
2820
|
+
var _ctx = {};
|
|
2821
|
+
if (_item) _ctx[_item] = _v;
|
|
2822
|
+
if (_key) _ctx[_key] = _k;
|
|
2823
|
+
if (_idx) _ctx[_idx] = _loopIdx - 1;
|
|
2824
|
+
var ms = fn ? parseInt(fn(_ctx)) || 0 : 0;
|
|
2733
2825
|
el.style.display = 'none';
|
|
2734
2826
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2735
2827
|
else el.style.display = '';
|
|
@@ -2742,12 +2834,16 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2742
2834
|
}
|
|
2743
2835
|
if (needsIf) {
|
|
2744
2836
|
renderBody += `
|
|
2745
|
-
document.querySelectorAll('template[data-sinth-if]').forEach(function(t) {
|
|
2837
|
+
document.querySelectorAll('template[data-sinth-if-expr]').forEach(function(t) {
|
|
2746
2838
|
var ifId = t.dataset.sinthIfId;
|
|
2747
2839
|
var anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
|
|
2748
|
-
var
|
|
2749
|
-
|
|
2840
|
+
var condFn = __X[t.dataset.sinthIfExpr];
|
|
2841
|
+
var cond = condFn ? condFn() : false;
|
|
2750
2842
|
if (cond) {
|
|
2843
|
+
if (t.dataset.sinthIfReplace) {
|
|
2844
|
+
var _rp = document.getElementById(t.dataset.sinthIfReplace);
|
|
2845
|
+
if (_rp) _rp.parentNode.removeChild(_rp);
|
|
2846
|
+
}
|
|
2751
2847
|
if (anchor) {
|
|
2752
2848
|
var cur = anchor.nextSibling;
|
|
2753
2849
|
while (cur && cur !== t) { var nx = cur.nextSibling; cur.remove(); cur = nx; }
|
|
@@ -2759,7 +2855,10 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2759
2855
|
}
|
|
2760
2856
|
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
2761
2857
|
frag.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
2762
|
-
try {
|
|
2858
|
+
try {
|
|
2859
|
+
var exprFn = __X[el.dataset.exprId];
|
|
2860
|
+
if (exprFn) el.textContent = exprFn();
|
|
2861
|
+
} catch(e) {}
|
|
2763
2862
|
});
|
|
2764
2863
|
frag.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2765
2864
|
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
@@ -2769,9 +2868,10 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2769
2868
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2770
2869
|
else el.style.display = '';
|
|
2771
2870
|
});
|
|
2772
|
-
frag.querySelectorAll('[data-sinth-delay-expr]').forEach(function(el) {
|
|
2871
|
+
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2773
2872
|
try {
|
|
2774
|
-
var
|
|
2873
|
+
var fn = __X[el.dataset.sinthDelayExprId];
|
|
2874
|
+
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
2775
2875
|
el.style.display = '';
|
|
2776
2876
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2777
2877
|
} catch(e) {}
|
|
@@ -2797,7 +2897,10 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2797
2897
|
}
|
|
2798
2898
|
var ef = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
2799
2899
|
ef.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
2800
|
-
try {
|
|
2900
|
+
try {
|
|
2901
|
+
var exprFn = __X[el.dataset.exprId];
|
|
2902
|
+
if (exprFn) el.textContent = exprFn();
|
|
2903
|
+
} catch(e) {}
|
|
2801
2904
|
});
|
|
2802
2905
|
ef.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2803
2906
|
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
@@ -2805,9 +2908,10 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2805
2908
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2806
2909
|
else el.style.display = '';
|
|
2807
2910
|
});
|
|
2808
|
-
ef.querySelectorAll('[data-sinth-delay-expr]').forEach(function(el) {
|
|
2911
|
+
ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2809
2912
|
try {
|
|
2810
|
-
var
|
|
2913
|
+
var fn = __X[el.dataset.sinthDelayExprId];
|
|
2914
|
+
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
2811
2915
|
el.style.display = '';
|
|
2812
2916
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2813
2917
|
} catch(e) {}
|
|
@@ -2831,28 +2935,34 @@ if (needsMixed) {
|
|
|
2831
2935
|
for (const mb of mixedBlocks) {
|
|
2832
2936
|
const ifHTMLJS = JSON.stringify(mb.ifHTML);
|
|
2833
2937
|
const elseHTMLJS = JSON.stringify(mb.elseHTML);
|
|
2834
|
-
const hasDelay = mb.ifHTML.includes("data-sinth-delay") || mb.elseHTML.includes("data-sinth-delay");
|
|
2938
|
+
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
2939
|
renderBody += `
|
|
2836
2940
|
(function() {
|
|
2837
2941
|
var __el = document.getElementById(${JSON.stringify(mb.replaceId || mb.id)});
|
|
2838
2942
|
if (__el) {
|
|
2839
|
-
var
|
|
2840
|
-
|
|
2943
|
+
var condFn = __X[${mb.conditionJS}];
|
|
2944
|
+
var __cond = condFn ? condFn() : false;
|
|
2841
2945
|
if (__cond) {
|
|
2842
2946
|
${mb.ifJS ? mb.ifJS : ""} __el.innerHTML = ${ifHTMLJS};
|
|
2843
2947
|
} else {
|
|
2844
2948
|
${mb.elseJS ? mb.elseJS : ""} __el.innerHTML = ${elseHTMLJS};
|
|
2845
2949
|
}
|
|
2846
2950
|
__el.querySelectorAll('.sinth-expr').forEach(function(e) {
|
|
2847
|
-
try {
|
|
2951
|
+
try {
|
|
2952
|
+
var exprFn = __X[e.dataset.exprId];
|
|
2953
|
+
if (exprFn) e.textContent = exprFn();
|
|
2954
|
+
} catch(ex) {}
|
|
2848
2955
|
});
|
|
2849
|
-
__el.querySelectorAll('template[data-sinth-if]').forEach(function(innerT) {
|
|
2850
|
-
var
|
|
2851
|
-
|
|
2956
|
+
__el.querySelectorAll('template[data-sinth-if-expr]').forEach(function(innerT) {
|
|
2957
|
+
var innerCondFn = __X[innerT.dataset.sinthIfExpr];
|
|
2958
|
+
var innerCond = innerCondFn ? innerCondFn() : false;
|
|
2852
2959
|
if (innerCond) {
|
|
2853
2960
|
var innerFrag = document.createRange().createContextualFragment(innerT.innerHTML);
|
|
2854
2961
|
innerFrag.querySelectorAll('.sinth-expr').forEach(function(e2) {
|
|
2855
|
-
try {
|
|
2962
|
+
try {
|
|
2963
|
+
var exprFn2 = __X[e2.dataset.exprId];
|
|
2964
|
+
if (exprFn2) e2.textContent = exprFn2();
|
|
2965
|
+
} catch(ex) {}
|
|
2856
2966
|
});
|
|
2857
2967
|
innerT.parentNode.insertBefore(innerFrag, innerT);
|
|
2858
2968
|
} else {
|
|
@@ -2860,23 +2970,26 @@ __el.querySelectorAll('template[data-sinth-if]').forEach(function(innerT) {
|
|
|
2860
2970
|
if (innerElse && innerElse.hasAttribute('data-sinth-else')) {
|
|
2861
2971
|
var innerElseFrag = document.createRange().createContextualFragment(innerElse.innerHTML);
|
|
2862
2972
|
innerElseFrag.querySelectorAll('.sinth-expr').forEach(function(e2) {
|
|
2863
|
-
try {
|
|
2973
|
+
try {
|
|
2974
|
+
var exprFn2 = __X[e2.dataset.exprId];
|
|
2975
|
+
if (exprFn2) e2.textContent = exprFn2();
|
|
2976
|
+
} catch(ex) {}
|
|
2864
2977
|
});
|
|
2865
2978
|
innerT.parentNode.insertBefore(innerElseFrag, innerT);
|
|
2866
2979
|
}
|
|
2867
2980
|
}
|
|
2868
2981
|
});
|
|
2869
2982
|
${hasDelay ? `
|
|
2870
|
-
// Handle delays for this mixed block
|
|
2871
2983
|
__el.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2872
2984
|
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
2873
|
-
el.style.display = 'none';
|
|
2985
|
+
el.style.display = 'none';
|
|
2874
2986
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2875
2987
|
else el.style.display = '';
|
|
2876
2988
|
});
|
|
2877
|
-
__el.querySelectorAll('[data-sinth-delay-expr]').forEach(function(el) {
|
|
2989
|
+
__el.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2878
2990
|
try {
|
|
2879
|
-
var
|
|
2991
|
+
var fn = __X[el.dataset.sinthDelayExprId];
|
|
2992
|
+
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
2880
2993
|
el.style.display = 'none';
|
|
2881
2994
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2882
2995
|
else el.style.display = '';
|
|
@@ -2892,10 +3005,10 @@ else el.style.display = '';
|
|
|
2892
3005
|
// set input vals from bound vars
|
|
2893
3006
|
renderBody += `
|
|
2894
3007
|
document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
|
|
2895
|
-
try { el.value =
|
|
3008
|
+
try { el.value = window[el.dataset.sinthValue] || ''; } catch(e) {}
|
|
2896
3009
|
});
|
|
2897
3010
|
document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
2898
|
-
try { el.checked = !!
|
|
3011
|
+
try { el.checked = !!window[el.dataset.sinthChecked]; } catch(e) {}
|
|
2899
3012
|
});
|
|
2900
3013
|
`;
|
|
2901
3014
|
|
|
@@ -2903,7 +3016,10 @@ else el.style.display = '';
|
|
|
2903
3016
|
if (needsExpr) {
|
|
2904
3017
|
renderBody += `
|
|
2905
3018
|
document.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
2906
|
-
try {
|
|
3019
|
+
try {
|
|
3020
|
+
var exprFn = __X[el.dataset.exprId];
|
|
3021
|
+
if (exprFn) el.textContent = exprFn({});
|
|
3022
|
+
} catch(e) {}
|
|
2907
3023
|
});
|
|
2908
3024
|
`;
|
|
2909
3025
|
}
|
|
@@ -2919,9 +3035,10 @@ setTimeout(function() {
|
|
|
2919
3035
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2920
3036
|
else el.style.display = '';
|
|
2921
3037
|
});
|
|
2922
|
-
document.querySelectorAll('[data-sinth-delay-expr]').forEach(function(el) {
|
|
3038
|
+
document.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
2923
3039
|
try {
|
|
2924
|
-
var
|
|
3040
|
+
var fn = __X[el.dataset.sinthDelayExprId];
|
|
3041
|
+
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
2925
3042
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2926
3043
|
else el.style.display = '';
|
|
2927
3044
|
} catch(e) {}
|
|
@@ -2929,6 +3046,13 @@ setTimeout(function() {
|
|
|
2929
3046
|
}, 0);
|
|
2930
3047
|
` : "";
|
|
2931
3048
|
|
|
3049
|
+
// Build the expression registry as a JS array
|
|
3050
|
+
const exprArrayJS = opts.exprRegistry && opts.exprRegistry.length > 0
|
|
3051
|
+
? `var __X = [${opts.exprRegistry.map((js) => {
|
|
3052
|
+
return `function(_ctx){ return ${js}; }`;
|
|
3053
|
+
}).join(",")}];\n`
|
|
3054
|
+
: "";
|
|
3055
|
+
|
|
2932
3056
|
return `// Sinth compiled runtime
|
|
2933
3057
|
${varLines}
|
|
2934
3058
|
${needsFor ? `function hashString(str) {
|
|
@@ -2941,6 +3065,7 @@ ${needsFor ? `function hashString(str) {
|
|
|
2941
3065
|
return String(hash);
|
|
2942
3066
|
}
|
|
2943
3067
|
` : ""}
|
|
3068
|
+
${exprArrayJS}
|
|
2944
3069
|
${needsRender ? `function sinthRender() {\n${renderBody}}\nsinthRender();` : ""}
|
|
2945
3070
|
${delayBlock}`;
|
|
2946
3071
|
}
|
|
@@ -2974,6 +3099,8 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2974
3099
|
mixedCounter: 0,
|
|
2975
3100
|
logicBlocks: [],
|
|
2976
3101
|
ifIdCounter: 0,
|
|
3102
|
+
exprRegistry: [],
|
|
3103
|
+
exprMap: new Map(),
|
|
2977
3104
|
};
|
|
2978
3105
|
|
|
2979
3106
|
if (!file.isPage) {
|
|
@@ -3023,6 +3150,7 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
3023
3150
|
logicBlocks: ctx.logicBlocks,
|
|
3024
3151
|
mixedBlocks: ctx.mixedBlocks,
|
|
3025
3152
|
assignedVars,
|
|
3153
|
+
exprRegistry: ctx.exprRegistry,
|
|
3026
3154
|
});
|
|
3027
3155
|
|
|
3028
3156
|
const scriptTags: string[] = [];
|