@yannosay/sinth 1.8.4 → 1.11.4
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 +56 -509
- package/bin/sinth.js +1 -1
- package/package.json +1 -1
- package/src/browser/compile.ts +72 -0
- package/src/cli.ts +1 -1
- package/src/core/cli-compiler.ts +481 -0
- package/src/core/compiler.ts +134 -547
- package/src/core/expr.ts +16 -11
- package/src/core/runtime/helpers.ts +9 -8
- package/src/core/runtime/render.ts +23 -9
- package/src/core/types.ts +4 -0
- package/src/server.ts +1 -1
package/src/core/expr.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Expression, Child, AssignStmt, IfBlock, ReturnStmt, LitStr, LitNum, LitBool, CompileCtx } from "./types.ts";
|
|
2
2
|
|
|
3
|
-
export function compileExprToJS(expr: Expression, loopVars?: Set<string>): string {
|
|
3
|
+
export function compileExprToJS(expr: Expression, loopVars?: Set<string>, namespace?: string, declaredVars?: Set<string>): string {
|
|
4
4
|
switch (expr.kind) {
|
|
5
5
|
case "literal":
|
|
6
6
|
if (!expr.value) return "null";
|
|
@@ -21,32 +21,37 @@ export function compileExprToJS(expr: Expression, loopVars?: Set<string>): strin
|
|
|
21
21
|
return `_ctx.${expr.name}`;
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
+
if (namespace && declaredVars && expr.name && declaredVars.has(expr.name)) {
|
|
25
|
+
return namespace + "_" + expr.name;
|
|
26
|
+
}
|
|
24
27
|
return expr.name ?? "undefined";
|
|
25
28
|
case "binary": {
|
|
26
|
-
const l = compileExprToJS(expr.left!, loopVars);
|
|
27
|
-
const r = compileExprToJS(expr.right!, loopVars);
|
|
29
|
+
const l = compileExprToJS(expr.left!, loopVars, namespace, declaredVars);
|
|
30
|
+
const r = compileExprToJS(expr.right!, loopVars, namespace, declaredVars);
|
|
28
31
|
const o = expr.op === "and" ? "&&" : expr.op === "or" ? "||" : expr.op!;
|
|
29
32
|
return `(${l} ${o} ${r})`;
|
|
30
33
|
}
|
|
31
34
|
case "unary": {
|
|
32
35
|
const o = expr.op === "not" ? "!" : expr.op!;
|
|
33
|
-
return `${o}(${compileExprToJS(expr.operand!, loopVars)})`;
|
|
36
|
+
return `${o}(${compileExprToJS(expr.operand!, loopVars, namespace, declaredVars)})`;
|
|
34
37
|
}
|
|
35
38
|
case "assign": {
|
|
36
|
-
const v = expr.right ? compileExprToJS(expr.right, loopVars) : "null";
|
|
37
|
-
|
|
39
|
+
const v = expr.right ? compileExprToJS(expr.right, loopVars, namespace, declaredVars) : "null";
|
|
40
|
+
const t = (namespace && declaredVars && expr.target && declaredVars.has(expr.target)) ? namespace + "_" + expr.target : expr.target;
|
|
41
|
+
return `${t} ${expr.op} ${v}`;
|
|
38
42
|
}
|
|
39
43
|
case "index":
|
|
40
|
-
return `${compileExprToJS(expr.object!, loopVars)}[${compileExprToJS(expr.key!, loopVars)}]`;
|
|
44
|
+
return `${compileExprToJS(expr.object!, loopVars, namespace, declaredVars)}[${compileExprToJS(expr.key!, loopVars, namespace, declaredVars)}]`;
|
|
41
45
|
case "postfix":
|
|
42
|
-
|
|
46
|
+
const t2 = (namespace && declaredVars && expr.target && declaredVars.has(expr.target)) ? namespace + "_" + expr.target : expr.target;
|
|
47
|
+
return `${t2}${expr.op}`;
|
|
43
48
|
case "call": {
|
|
44
|
-
const callee = compileExprToJS(expr.callee!, loopVars);
|
|
49
|
+
const callee = compileExprToJS(expr.callee!, loopVars, namespace, declaredVars);
|
|
45
50
|
if (callee === "remove") {
|
|
46
|
-
const arg = expr.args && expr.args[0] ? compileExprToJS(expr.args[0], loopVars) : "''";
|
|
51
|
+
const arg = expr.args && expr.args[0] ? compileExprToJS(expr.args[0], loopVars, namespace, declaredVars) : "''";
|
|
47
52
|
return `(function(){ var _el=document.getElementById(${arg}); if(_el)_el.remove(); })()`;
|
|
48
53
|
}
|
|
49
|
-
const args = (expr.args ?? []).map(a => compileExprToJS(a, loopVars)).join(", ");
|
|
54
|
+
const args = (expr.args ?? []).map(a => compileExprToJS(a, loopVars, namespace, declaredVars)).join(", ");
|
|
50
55
|
if (expr.args && expr.args.length === 0 && /\.length$/.test(callee)) {
|
|
51
56
|
return callee;
|
|
52
57
|
}
|
|
@@ -4,15 +4,17 @@ export function generateHelpers(opts: {
|
|
|
4
4
|
needsFor: boolean;
|
|
5
5
|
needsDelay: boolean;
|
|
6
6
|
needsMixed: boolean;
|
|
7
|
+
ns?: string;
|
|
7
8
|
}): string {
|
|
8
|
-
const { needsExpr, needsIf, needsFor, needsDelay, needsMixed } = opts;
|
|
9
|
+
const { needsExpr, needsIf, needsFor, needsDelay, needsMixed, ns } = opts;
|
|
10
|
+
const X = ns ? "__X" + ns : "__X";
|
|
9
11
|
let helpers = "";
|
|
10
12
|
|
|
11
13
|
if (needsExpr || needsIf || needsFor) {
|
|
12
14
|
helpers += `
|
|
13
15
|
var sinthExpr = function(el) {
|
|
14
16
|
try {
|
|
15
|
-
let exprFn =
|
|
17
|
+
let exprFn = ${X}[el.dataset.exprId];
|
|
16
18
|
if (exprFn) el.textContent = exprFn({});
|
|
17
19
|
} catch(e) {}
|
|
18
20
|
};
|
|
@@ -99,7 +101,7 @@ function sinthDelayExpr(el, _ctx) {
|
|
|
99
101
|
el.dataset.sinthDelayDone = '1';
|
|
100
102
|
_ctx = _ctx || {};
|
|
101
103
|
try {
|
|
102
|
-
let fn =
|
|
104
|
+
let fn = ${X}[el.dataset.sinthDelayExprId];
|
|
103
105
|
let ms = fn ? parseInt(fn(_ctx)) || 0 : 0;
|
|
104
106
|
let hideEl = el.dataset.sinthDelayHide !== 'false';
|
|
105
107
|
let show = function() {
|
|
@@ -124,7 +126,7 @@ function sinthDelayExpr(el, _ctx) {
|
|
|
124
126
|
function sinthIfBlock(t) {
|
|
125
127
|
let ifId = t.dataset.sinthIfId;
|
|
126
128
|
let anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
|
|
127
|
-
let condFn =
|
|
129
|
+
let condFn = ${X}[t.dataset.sinthIfExpr];
|
|
128
130
|
let cond = condFn ? condFn() : false;
|
|
129
131
|
if (cond) {
|
|
130
132
|
let elseA = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
@@ -296,7 +298,7 @@ function sinthForBlock(t) {
|
|
|
296
298
|
if (_idx) loopCtx[_idx] = _loopIdx - 1;
|
|
297
299
|
let frag = document.createRange().createContextualFragment(tplContent);
|
|
298
300
|
frag.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
299
|
-
try { let fn =
|
|
301
|
+
try { let fn = ${X}[el.dataset.exprId]; if (fn) el.textContent = fn(loopCtx); } catch(e) {}
|
|
300
302
|
el.classList.remove('sinth-expr');
|
|
301
303
|
});
|
|
302
304
|
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
|
|
@@ -310,13 +312,12 @@ function sinthForBlock(t) {
|
|
|
310
312
|
el.setAttribute('onclick', onclick);
|
|
311
313
|
});
|
|
312
314
|
frag.querySelectorAll('template[data-sinth-if-expr]').forEach(function(ifT) {
|
|
313
|
-
let condFn =
|
|
315
|
+
let condFn = ${X}[ifT.dataset.sinthIfExpr];
|
|
314
316
|
let cond = condFn ? condFn(loopCtx) : false;
|
|
315
317
|
if (cond) {
|
|
316
318
|
let ifFrag = document.createRange().createContextualFragment(ifT.innerHTML);
|
|
317
319
|
ifFrag.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
318
|
-
try { let fn2 =
|
|
319
|
-
el2.classList.remove('sinth-expr');
|
|
320
|
+
try { let fn2 = ${X}[el2.dataset.exprId]; if (fn2) el2.textContent = fn2(loopCtx); } catch(e) {} el2.classList.remove('sinth-expr');
|
|
320
321
|
});
|
|
321
322
|
ifT.replaceWith(ifFrag);
|
|
322
323
|
} else {
|
|
@@ -11,8 +11,10 @@ export function buildRenderBody(opts: {
|
|
|
11
11
|
needsExpr: boolean;
|
|
12
12
|
needsDelay: boolean;
|
|
13
13
|
exprVarUpdates?: string;
|
|
14
|
+
ns?: string;
|
|
14
15
|
}): string {
|
|
15
|
-
const { bodyHTML, logicBlocks, mixedBlocks, needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay, exprVarUpdates } = opts;
|
|
16
|
+
const { bodyHTML, logicBlocks, mixedBlocks, needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay, exprVarUpdates, ns } = opts;
|
|
17
|
+
const X = ns ? "__X" + ns : "__X";
|
|
16
18
|
let renderBody = "";
|
|
17
19
|
renderBody += ` let _sx = window.scrollX, _sy = window.scrollY;\n`;
|
|
18
20
|
|
|
@@ -27,7 +29,7 @@ export function buildRenderBody(opts: {
|
|
|
27
29
|
renderBody += ` (function() {
|
|
28
30
|
let __el = document.getElementById(${JSON.stringify(mb.replaceId || mb.id)});
|
|
29
31
|
if (__el) {
|
|
30
|
-
let __condFn =
|
|
32
|
+
let __condFn = ${X}[${mb.conditionJS}];
|
|
31
33
|
if (__condFn ? __condFn() : false) {
|
|
32
34
|
${ifJS}
|
|
33
35
|
__el.innerHTML = ${JSON.stringify(mb.ifHTML)};
|
|
@@ -56,13 +58,13 @@ export function buildRenderBody(opts: {
|
|
|
56
58
|
renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(function(t) { var p = t.parentElement; while(p && p.tagName !== 'TEMPLATE') p = p.parentElement; if (!p || (!p.hasAttribute('data-sinth-for') && !p.hasAttribute('data-sinth-for-expr'))) sinthIfBlock(t); });\n`;
|
|
57
59
|
}
|
|
58
60
|
renderBody += ` document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
|
|
59
|
-
try { if (document.activeElement !== el) { let v=el.dataset.sinthValue; el.value =
|
|
61
|
+
try { if (document.activeElement !== el) { let v=el.dataset.sinthValue; el.value = ${X}[v] ? ${X}[v]({}) : ''; } } catch(e) {}
|
|
60
62
|
});\n`;
|
|
61
63
|
renderBody += ` document.querySelectorAll('[data-sinth-step]').forEach(function(el) {
|
|
62
|
-
try { let s=el.dataset.sinthStep; el.step =
|
|
64
|
+
try { let s=el.dataset.sinthStep; el.step = ${X}[s] ? ${X}[s]({}) : 1; } catch(e) {}
|
|
63
65
|
});\n`;
|
|
64
66
|
renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
65
|
-
try { let c=el.dataset.sinthChecked; el.checked =
|
|
67
|
+
try { let c=el.dataset.sinthChecked; el.checked = ${X}[c] ? !!${X}[c]({}) : false; } catch(e) {}
|
|
66
68
|
});\n`;
|
|
67
69
|
if (exprVarUpdates) {
|
|
68
70
|
renderBody += ` ${exprVarUpdates}\n`;
|
|
@@ -70,7 +72,7 @@ export function buildRenderBody(opts: {
|
|
|
70
72
|
if (bodyHTML.includes("data-sinth-checked-expr")) {
|
|
71
73
|
renderBody += ` document.querySelectorAll('[data-sinth-checked-expr]').forEach(function(el) {
|
|
72
74
|
try {
|
|
73
|
-
let exprFn =
|
|
75
|
+
let exprFn = ${X}[el.dataset.sinthCheckedExpr];
|
|
74
76
|
if (exprFn) el.checked = !!exprFn({});
|
|
75
77
|
} catch(e) {}
|
|
76
78
|
});\n`;
|
|
@@ -79,7 +81,7 @@ export function buildRenderBody(opts: {
|
|
|
79
81
|
renderBody += ` document.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
80
82
|
let newContent = '';
|
|
81
83
|
el.querySelectorAll('.sinth-expr').forEach(function(exprEl) {
|
|
82
|
-
let exprFn =
|
|
84
|
+
let exprFn = ${X}[exprEl.dataset.exprId];
|
|
83
85
|
if (exprFn) newContent += exprFn({});
|
|
84
86
|
});
|
|
85
87
|
if (el._sinthLastContent === undefined) {
|
|
@@ -99,11 +101,23 @@ export function buildRenderBody(opts: {
|
|
|
99
101
|
}
|
|
100
102
|
});\n`;
|
|
101
103
|
}
|
|
104
|
+
renderBody += ` document.querySelectorAll('[data-sinth-style]').forEach(function(el) {
|
|
105
|
+
try {
|
|
106
|
+
let d = el.dataset.sinthStyle.split(';');
|
|
107
|
+
for (let i = 0; i < d.length; i++) {
|
|
108
|
+
let p = d[i].split(':');
|
|
109
|
+
if (p.length === 2) {
|
|
110
|
+
let fn = ${X}[p[1]];
|
|
111
|
+
if (fn) el.style[p[0]] = fn({});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
} catch(e) {}
|
|
115
|
+
});\n`;
|
|
102
116
|
renderBody += ` document.querySelectorAll('[data-sinth-hide]').forEach(function(el) {
|
|
103
117
|
let exprId = el.dataset.sinthHide;
|
|
104
118
|
if (exprId) {
|
|
105
119
|
try {
|
|
106
|
-
let exprFn =
|
|
120
|
+
let exprFn = ${X}[exprId];
|
|
107
121
|
if (exprFn) el.style.display = exprFn({}) ? 'none' : '';
|
|
108
122
|
} catch(e) {}
|
|
109
123
|
} else {
|
|
@@ -114,7 +128,7 @@ export function buildRenderBody(opts: {
|
|
|
114
128
|
let exprId = el.dataset.sinthFullscreen;
|
|
115
129
|
if (exprId) {
|
|
116
130
|
try {
|
|
117
|
-
let exprFn =
|
|
131
|
+
let exprFn = ${X}[exprId];
|
|
118
132
|
if (exprFn) {
|
|
119
133
|
if (exprFn({})) {
|
|
120
134
|
if (document.documentElement.requestFullscreen) document.documentElement.requestFullscreen();
|
package/src/core/types.ts
CHANGED
|
@@ -149,10 +149,14 @@ export interface CompileCtx {
|
|
|
149
149
|
loopVars?: Set<string>;
|
|
150
150
|
diffingEnabled?: boolean;
|
|
151
151
|
declaredVars?: Set<string>;
|
|
152
|
+
|
|
152
153
|
// custom elements
|
|
153
154
|
scopePrefix?: string;
|
|
154
155
|
scopeVar?: string;
|
|
155
156
|
ceActionHandlers?: string[];
|
|
157
|
+
|
|
158
|
+
// web
|
|
159
|
+
namespace?: string;
|
|
156
160
|
}
|
|
157
161
|
|
|
158
162
|
export class SinthError extends Error {
|
package/src/server.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as http from "http";
|
|
2
2
|
import * as path from "path";
|
|
3
3
|
import * as fs from "fs";
|
|
4
|
-
import { compileFile, CompileOptions, findSinthPages, minifyHTML } from "./core/compiler.ts";
|
|
4
|
+
import { compileFile, CompileOptions, findSinthPages, minifyHTML } from "./core/cli-compiler.ts";
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|