@yannosay/sinth 0.13.1 → 0.13.3
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 +30 -1
- package/package.json +1 -1
- package/src/core/builtins.ts +117 -0
- package/src/core/compiler.ts +64 -927
- package/src/core/expr.ts +6 -2
- package/src/core/head-builder.ts +86 -0
- package/src/core/parser.ts +8 -10
- package/src/core/runtime/functions.ts +33 -0
- package/src/core/runtime/helpers.ts +296 -0
- package/src/core/runtime/render.ts +82 -0
- package/src/core/style-processor.ts +299 -0
- package/src/resolver.ts +1 -1
- package/src/utils.ts +40 -1
package/src/core/expr.ts
CHANGED
|
@@ -10,8 +10,12 @@ export function compileExprToJS(expr: Expression, loopVars?: Set<string>): strin
|
|
|
10
10
|
if (expr.value.kind === "bool") return String((expr.value as LitBool).value);
|
|
11
11
|
return "null";
|
|
12
12
|
case "variable":
|
|
13
|
-
if (loopVars && expr.name
|
|
14
|
-
|
|
13
|
+
if (loopVars && expr.name) {
|
|
14
|
+
const dotIdx = expr.name.indexOf('.');
|
|
15
|
+
const root = dotIdx !== -1 ? expr.name.substring(0, dotIdx) : expr.name;
|
|
16
|
+
if (loopVars.has(root)) {
|
|
17
|
+
return `_ctx.${expr.name}`;
|
|
18
|
+
}
|
|
15
19
|
}
|
|
16
20
|
return expr.name ?? "undefined";
|
|
17
21
|
case "binary": {
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import { MetaEntry, SinthWarning } from "./types.ts";
|
|
3
|
+
import { esc, escAttr, litToString, camelToKebab } from "../utils.ts";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export interface HeadData {
|
|
8
|
+
title?: string; fav?: string;
|
|
9
|
+
lang: string; charset: string; viewport: string;
|
|
10
|
+
metaTags: { name: string; content: string }[];
|
|
11
|
+
metaProps: { property: string; content: string }[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function buildHeadData(meta: MetaEntry[]): HeadData {
|
|
15
|
+
let title: string | undefined, fav: string | undefined;
|
|
16
|
+
let lang = "en", charset = "UTF-8", viewport = "width=device-width, initial-scale=1.0";
|
|
17
|
+
const metaTags: { name: string; content: string }[] = [];
|
|
18
|
+
const metaProps: { property: string; content: string }[] = [];
|
|
19
|
+
|
|
20
|
+
for (const m of meta) {
|
|
21
|
+
const val = litToString(m.value);
|
|
22
|
+
switch (m.key) {
|
|
23
|
+
case "title": title = val; break;
|
|
24
|
+
case "fav": fav = val; break;
|
|
25
|
+
case "lang": lang = val; break;
|
|
26
|
+
case "charset": charset = val; break;
|
|
27
|
+
case "viewport": viewport = val; break;
|
|
28
|
+
case "descr": metaTags.push({ name: "description", content: val }); break;
|
|
29
|
+
case "author": metaTags.push({ name: "author", content: val }); break;
|
|
30
|
+
case "keywords": metaTags.push({ name: "keywords", content: val }); break;
|
|
31
|
+
case "robots": metaTags.push({ name: "robots", content: val }); break;
|
|
32
|
+
default:
|
|
33
|
+
if (m.key.startsWith("og") || m.key.startsWith("twitter")) {
|
|
34
|
+
const prop = m.key
|
|
35
|
+
.replace(/^og([A-Z])/, (_: string, c: string) => `og:${c.toLowerCase()}`)
|
|
36
|
+
.replace(/^twitter([A-Z])/, (_: string, c: string) => `twitter:${c.toLowerCase()}`);
|
|
37
|
+
metaProps.push({ property: prop, content: val });
|
|
38
|
+
} else {
|
|
39
|
+
metaTags.push({ name: camelToKebab(m.key), content: val });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return { title, fav, lang, charset, viewport, metaTags, metaProps };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function faviconType(p: string): string {
|
|
48
|
+
switch (path.extname(p).toLowerCase()) {
|
|
49
|
+
case ".ico": return "image/x-icon";
|
|
50
|
+
case ".png": return "image/png";
|
|
51
|
+
case ".svg": return "image/svg+xml";
|
|
52
|
+
case ".gif": return "image/gif";
|
|
53
|
+
case ".webp": return "image/webp";
|
|
54
|
+
case ".jpg":
|
|
55
|
+
case ".jpeg": return "image/jpeg";
|
|
56
|
+
default:
|
|
57
|
+
SinthWarning.emit(`Unknown favicon extension '${path.extname(p)}', defaulting to image/x-icon`);
|
|
58
|
+
return "image/x-icon";
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function renderHead(
|
|
63
|
+
hd: HeadData,
|
|
64
|
+
cssLinks: string[],
|
|
65
|
+
jsLinks: { src: string; attrs: Record<string, string> }[],
|
|
66
|
+
scopedCSS: string,
|
|
67
|
+
companionJS: string | undefined,
|
|
68
|
+
): string {
|
|
69
|
+
const lines: string[] = [];
|
|
70
|
+
lines.push(` <meta charset="${escAttr(hd.charset)}">`);
|
|
71
|
+
lines.push(` <meta name="viewport" content="${escAttr(hd.viewport)}">`);
|
|
72
|
+
if (hd.title) lines.push(` <title>${esc(hd.title)}</title>`);
|
|
73
|
+
if (hd.fav) lines.push(` <link rel="icon" href="${escAttr(hd.fav)}" type="${faviconType(hd.fav)}">`);
|
|
74
|
+
for (const m of hd.metaTags) lines.push(` <meta name="${escAttr(m.name)}" content="${escAttr(m.content)}">`);
|
|
75
|
+
for (const m of hd.metaProps) lines.push(` <meta property="${escAttr(m.property)}" content="${escAttr(m.content)}">`);
|
|
76
|
+
for (const css of cssLinks) lines.push(` <link rel="stylesheet" href="${escAttr(css)}">`);
|
|
77
|
+
if (scopedCSS.trim()) lines.push(` <style>\n${scopedCSS}\n </style>`);
|
|
78
|
+
for (const js of jsLinks) {
|
|
79
|
+
const extra = Object.entries(js.attrs)
|
|
80
|
+
.map(([k, v]) => v === "true" ? k : `${k}="${escAttr(v)}"`)
|
|
81
|
+
.join(" ");
|
|
82
|
+
lines.push(` <script src="${escAttr(js.src)}"${extra ? " " + extra : ""}></script>`);
|
|
83
|
+
}
|
|
84
|
+
if (companionJS) lines.push(` <script src="${escAttr(companionJS)}"></script>`);
|
|
85
|
+
return `<head>\n${lines.join("\n")}\n</head>`;
|
|
86
|
+
}
|
package/src/core/parser.ts
CHANGED
|
@@ -698,7 +698,6 @@ private parseArrayLiteral(): Literal {
|
|
|
698
698
|
return children;
|
|
699
699
|
}
|
|
700
700
|
|
|
701
|
-
// for loops
|
|
702
701
|
|
|
703
702
|
private parseForLoop(): ForLoop {
|
|
704
703
|
const loc = this.consume(TT.KW_FOR).loc;
|
|
@@ -710,31 +709,31 @@ private parseArrayLiteral(): Literal {
|
|
|
710
709
|
this.consume(TT.COMMA);
|
|
711
710
|
const secondVar = this.consume(TT.IDENT).value;
|
|
712
711
|
if (this.check(TT.COMMA)) {
|
|
713
|
-
// three variables: key, value, index
|
|
714
712
|
this.consume(TT.COMMA);
|
|
715
713
|
keyVar = firstVar;
|
|
716
714
|
itemVar = secondVar;
|
|
717
715
|
indexVar = this.consume(TT.IDENT).value;
|
|
718
716
|
} else {
|
|
719
|
-
// two variables – tentatively value, index; fix after seeing source type
|
|
720
717
|
itemVar = firstVar;
|
|
721
718
|
indexVar = secondVar;
|
|
722
719
|
}
|
|
723
720
|
} else {
|
|
724
|
-
// one variable: item only
|
|
725
721
|
itemVar = firstVar;
|
|
726
722
|
}
|
|
727
723
|
if (!this.check(TT.KW_IN)) throw new SinthError("Expected 'in' after loop variable", this.peek().loc);
|
|
728
724
|
this.consume(TT.KW_IN);
|
|
729
725
|
const arrayVar = this.consume(TT.IDENT).value;
|
|
730
726
|
|
|
731
|
-
// If two variables and the source is an object, swap to key, value
|
|
732
727
|
if (keyVar === undefined && indexVar !== undefined) {
|
|
733
728
|
const srcDecl = this._varDecls.find(d => d.name === arrayVar);
|
|
734
729
|
if (srcDecl && srcDecl.varType === "obj") {
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
730
|
+
const val = srcDecl.value;
|
|
731
|
+
const isArray = val && val.kind === "str" && val.value.startsWith("__ARR__");
|
|
732
|
+
if (!isArray) {
|
|
733
|
+
keyVar = itemVar;
|
|
734
|
+
itemVar = indexVar;
|
|
735
|
+
indexVar = undefined;
|
|
736
|
+
}
|
|
738
737
|
}
|
|
739
738
|
}
|
|
740
739
|
|
|
@@ -748,7 +747,7 @@ private parseArrayLiteral(): Literal {
|
|
|
748
747
|
return { kind: "for", keyVar, itemVar, indexVar, arrayVar, body, loc };
|
|
749
748
|
}
|
|
750
749
|
|
|
751
|
-
|
|
750
|
+
|
|
752
751
|
|
|
753
752
|
private parseCompUse(): CompUse {
|
|
754
753
|
const loc = this.peek().loc;
|
|
@@ -994,7 +993,6 @@ private parseArrayLiteral(): Literal {
|
|
|
994
993
|
const name = this.consume(TT.IDENT).value;
|
|
995
994
|
const varExpr: Expression = { kind: "variable", name };
|
|
996
995
|
let indexedExpr = this.parsePostfix(varExpr);
|
|
997
|
-
// support concatenation: data[prop] + "..."
|
|
998
996
|
if (this.check(TT.OP_PLUS)) {
|
|
999
997
|
let leftExpr: Expression = indexedExpr;
|
|
1000
998
|
while (this.check(TT.OP_PLUS)) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { CompileCtx, FunctionDef } from "../types.ts";
|
|
2
|
+
import { compileExprToJS, compileIfToJS } from "../expr.ts";
|
|
3
|
+
|
|
4
|
+
export function compileFunctionDef(fn: FunctionDef, ctx: CompileCtx): string {
|
|
5
|
+
const paramsJS = fn.params.map(p => p.name).join(", ");
|
|
6
|
+
|
|
7
|
+
if (fn.returnType === "ui") {
|
|
8
|
+
return "";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const bodyStatements: string[] = [];
|
|
12
|
+
for (const child of fn.body) {
|
|
13
|
+
switch (child.kind) {
|
|
14
|
+
case "text":
|
|
15
|
+
break;
|
|
16
|
+
case "expr":
|
|
17
|
+
bodyStatements.push(`return ${compileExprToJS(child.expression)};`);
|
|
18
|
+
break;
|
|
19
|
+
case "assign_stmt":
|
|
20
|
+
bodyStatements.push(`${compileExprToJS(child.expression)};`);
|
|
21
|
+
break;
|
|
22
|
+
case "if":
|
|
23
|
+
bodyStatements.push(compileIfToJS(child as IfBlock));
|
|
24
|
+
break;
|
|
25
|
+
case "for":
|
|
26
|
+
break;
|
|
27
|
+
case "return":
|
|
28
|
+
bodyStatements.push(`return ${child.expression ? compileExprToJS(child.expression) : ""};`);
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return `function ${fn.name}(${paramsJS}) {\n${bodyStatements.map(s => ` ${s}`).join("\n")}\n}`;
|
|
33
|
+
}
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
export function generateHelpers(opts: {
|
|
2
|
+
needsExpr: boolean;
|
|
3
|
+
needsIf: boolean;
|
|
4
|
+
needsFor: boolean;
|
|
5
|
+
needsDelay: boolean;
|
|
6
|
+
needsMixed: boolean;
|
|
7
|
+
}): string {
|
|
8
|
+
const { needsExpr, needsIf, needsFor, needsDelay, needsMixed } = opts;
|
|
9
|
+
let helpers = "";
|
|
10
|
+
|
|
11
|
+
if (needsExpr || needsIf || needsFor) {
|
|
12
|
+
helpers += `
|
|
13
|
+
function sinthExpr(el) {
|
|
14
|
+
try {
|
|
15
|
+
var exprFn = __X[el.dataset.exprId];
|
|
16
|
+
if (exprFn) el.textContent = exprFn({});
|
|
17
|
+
} catch(e) {}
|
|
18
|
+
}
|
|
19
|
+
`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (needsIf || needsMixed) {
|
|
23
|
+
helpers += `
|
|
24
|
+
function sinthReplaceInsert(t, anchor, ifId, replaceId) {
|
|
25
|
+
if (anchor) {
|
|
26
|
+
var cur = anchor.nextSibling;
|
|
27
|
+
while (cur && cur !== t) { var nx = cur.nextSibling; cur.remove(); cur = nx; }
|
|
28
|
+
} else {
|
|
29
|
+
anchor = document.createElement('span');
|
|
30
|
+
anchor.style.display = 'none';
|
|
31
|
+
anchor.dataset.sinthIfAnchor = ifId;
|
|
32
|
+
t.parentNode.insertBefore(anchor, t);
|
|
33
|
+
}
|
|
34
|
+
var _rp = null, _rpParent = null, _rpNext = null;
|
|
35
|
+
if (replaceId) {
|
|
36
|
+
_rp = document.getElementById(replaceId);
|
|
37
|
+
if (_rp) {
|
|
38
|
+
_rpParent = _rp.parentNode;
|
|
39
|
+
_rpNext = _rp.nextSibling;
|
|
40
|
+
_rp.parentNode.removeChild(_rp);
|
|
41
|
+
anchor._sinthReplaced = _rp;
|
|
42
|
+
anchor._sinthReplacedParent = _rpParent;
|
|
43
|
+
anchor._sinthReplacedNext = _rpNext;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
47
|
+
frag.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
48
|
+
if (${needsDelay}) {
|
|
49
|
+
frag.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
50
|
+
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
|
|
51
|
+
}
|
|
52
|
+
var fragFirst = frag.firstChild;
|
|
53
|
+
var fragLast = frag.lastChild;
|
|
54
|
+
if (_rpParent && _rpNext) {
|
|
55
|
+
_rpParent.insertBefore(frag, _rpNext);
|
|
56
|
+
} else if (_rpParent) {
|
|
57
|
+
_rpParent.appendChild(frag);
|
|
58
|
+
} else {
|
|
59
|
+
t.parentNode.insertBefore(frag, t);
|
|
60
|
+
}
|
|
61
|
+
if (replaceId && anchor) {
|
|
62
|
+
anchor._sinthInsertedFirst = fragFirst;
|
|
63
|
+
anchor._sinthInsertedLast = fragLast;
|
|
64
|
+
}
|
|
65
|
+
return anchor;
|
|
66
|
+
}
|
|
67
|
+
`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (needsDelay) {
|
|
71
|
+
helpers += `
|
|
72
|
+
function sinthDelay(el) {
|
|
73
|
+
if (el.dataset.sinthDelayDone) { return; }
|
|
74
|
+
el.dataset.sinthDelayDone = '1';
|
|
75
|
+
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
76
|
+
if (el.dataset.sinthDelayHide !== 'false') {
|
|
77
|
+
el.style.display = 'none';
|
|
78
|
+
}
|
|
79
|
+
var show = function() {
|
|
80
|
+
el.style.display = '';
|
|
81
|
+
el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
82
|
+
};
|
|
83
|
+
if (ms > 0) setTimeout(show, ms);
|
|
84
|
+
else show();
|
|
85
|
+
}
|
|
86
|
+
function sinthDelayExpr(el, _ctx) {
|
|
87
|
+
_ctx = _ctx || {};
|
|
88
|
+
try {
|
|
89
|
+
var fn = __X[el.dataset.sinthDelayExprId];
|
|
90
|
+
var ms = fn ? parseInt(fn(_ctx)) || 0 : 0;
|
|
91
|
+
var show = function() {
|
|
92
|
+
el.style.display = '';
|
|
93
|
+
el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
94
|
+
};
|
|
95
|
+
if (ms > 0) setTimeout(show, ms);
|
|
96
|
+
else show();
|
|
97
|
+
} catch(e) {}
|
|
98
|
+
}
|
|
99
|
+
`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (needsIf) {
|
|
103
|
+
helpers += `
|
|
104
|
+
function sinthIfBlock(t) {
|
|
105
|
+
var ifId = t.dataset.sinthIfId;
|
|
106
|
+
var anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
|
|
107
|
+
var condFn = __X[t.dataset.sinthIfExpr];
|
|
108
|
+
var cond = condFn ? condFn() : false;
|
|
109
|
+
if (cond) {
|
|
110
|
+
var _hasContent = anchor || t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
111
|
+
if (t.dataset.sinthIfDelayHide === 'false' && t.dataset.sinthIfDelay && _hasContent) {
|
|
112
|
+
var dms = parseInt(t.dataset.sinthIfDelay) || 0;
|
|
113
|
+
setTimeout(function() {
|
|
114
|
+
sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
|
|
115
|
+
}, dms);
|
|
116
|
+
} else {
|
|
117
|
+
anchor = sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
|
|
118
|
+
}
|
|
119
|
+
} else {
|
|
120
|
+
var runElse = function() {
|
|
121
|
+
if (anchor) {
|
|
122
|
+
if (anchor._sinthReplaced) {
|
|
123
|
+
var insFirst = anchor._sinthInsertedFirst;
|
|
124
|
+
var insLast = anchor._sinthInsertedLast;
|
|
125
|
+
var rpParent = anchor._sinthReplacedParent;
|
|
126
|
+
var rpNext = anchor._sinthReplacedNext;
|
|
127
|
+
if (insFirst && insLast) {
|
|
128
|
+
var cur = insFirst;
|
|
129
|
+
while (cur && cur !== insLast) {
|
|
130
|
+
var next = cur.nextSibling;
|
|
131
|
+
cur.remove();
|
|
132
|
+
cur = next;
|
|
133
|
+
}
|
|
134
|
+
if (insLast) insLast.remove();
|
|
135
|
+
}
|
|
136
|
+
if (rpParent && rpNext) {
|
|
137
|
+
rpParent.insertBefore(anchor._sinthReplaced, rpNext);
|
|
138
|
+
} else if (rpParent) {
|
|
139
|
+
rpParent.appendChild(anchor._sinthReplaced);
|
|
140
|
+
}
|
|
141
|
+
if (anchor._sinthReplaced && anchor._sinthReplaced.dataset.sinthDelay) {
|
|
142
|
+
delete anchor._sinthReplaced.dataset.sinthDelayDone;
|
|
143
|
+
sinthDelay(anchor._sinthReplaced);
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
var cur2 = anchor.nextSibling;
|
|
147
|
+
while (cur2 && cur2 !== t) { var nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
|
|
148
|
+
}
|
|
149
|
+
anchor.remove();
|
|
150
|
+
}
|
|
151
|
+
var elseT = t.nextElementSibling;
|
|
152
|
+
if (elseT && elseT.hasAttribute('data-sinth-else')) {
|
|
153
|
+
var elseIfId = elseT.dataset.sinthIfId;
|
|
154
|
+
var ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + elseIfId + '"]');
|
|
155
|
+
if (ea) {
|
|
156
|
+
var cur3 = ea.nextSibling;
|
|
157
|
+
while (cur3 && cur3 !== t) { var nx3 = cur3.nextSibling; cur3.remove(); cur3 = nx3; }
|
|
158
|
+
} else {
|
|
159
|
+
ea = document.createElement('span');
|
|
160
|
+
ea.style.display = 'none';
|
|
161
|
+
ea.dataset.sinthIfAnchor = '__else__' + elseIfId;
|
|
162
|
+
t.parentNode.insertBefore(ea, t);
|
|
163
|
+
}
|
|
164
|
+
var ef = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
165
|
+
ef.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
166
|
+
if (${needsDelay}) {
|
|
167
|
+
ef.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
168
|
+
ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
|
|
169
|
+
}
|
|
170
|
+
t.parentNode.insertBefore(ef, t);
|
|
171
|
+
ef.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
172
|
+
delete el.dataset.sinthDelayDone;
|
|
173
|
+
sinthDelay(el);
|
|
174
|
+
});
|
|
175
|
+
} else {
|
|
176
|
+
var ea2 = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
177
|
+
if (ea2) {
|
|
178
|
+
var ec = ea2.nextSibling;
|
|
179
|
+
while (ec && ec !== t) { var en = ec.nextSibling; ec.remove(); ec = en; }
|
|
180
|
+
ea2.remove();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
var elseT = t.nextElementSibling;
|
|
185
|
+
if (elseT && elseT.hasAttribute('data-sinth-else') && elseT.dataset.sinthIfDelayHide === 'false' && elseT.dataset.sinthIfDelay && anchor) {
|
|
186
|
+
var edms = parseInt(elseT.dataset.sinthIfDelay) || 0;
|
|
187
|
+
setTimeout(runElse, edms);
|
|
188
|
+
} else {
|
|
189
|
+
runElse();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
`;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (needsFor) {
|
|
197
|
+
helpers += `
|
|
198
|
+
function hashString(str) {
|
|
199
|
+
var hash = 0, i, chr;
|
|
200
|
+
for (i = 0; i < str.length; i++) {
|
|
201
|
+
chr = str.charCodeAt(i);
|
|
202
|
+
hash = ((hash << 5) - hash) + chr;
|
|
203
|
+
hash |= 0;
|
|
204
|
+
}
|
|
205
|
+
return String(hash);
|
|
206
|
+
}
|
|
207
|
+
function sinthForBlock(t) {
|
|
208
|
+
var source = window[t.dataset.sinthFor];
|
|
209
|
+
if (source === undefined) source = [];
|
|
210
|
+
var newHash = '';
|
|
211
|
+
try { newHash = hashString(JSON.stringify(source)); } catch(e) { newHash = ''; }
|
|
212
|
+
if (t.dataset.sinthForHash && t.dataset.sinthForHash === newHash) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
t.dataset.sinthForHash = newHash;
|
|
216
|
+
var isObj = (typeof source === 'object' && source !== null && !Array.isArray(source));
|
|
217
|
+
var entries;
|
|
218
|
+
if (isObj) {
|
|
219
|
+
entries = Object.entries(source);
|
|
220
|
+
} else {
|
|
221
|
+
if (!Array.isArray(source)) source = [];
|
|
222
|
+
entries = source.map(function(item, index) { return [index, item]; });
|
|
223
|
+
}
|
|
224
|
+
var anchor = t.parentNode ? t.parentNode.querySelector('[data-sinth-for-anchor="' + t.dataset.sinthFor + '"]') : null;
|
|
225
|
+
if (anchor) {
|
|
226
|
+
var cur2 = anchor.nextSibling;
|
|
227
|
+
while (cur2 && cur2 !== t) { var nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
|
|
228
|
+
anchor.remove();
|
|
229
|
+
}
|
|
230
|
+
var fa = document.createElement('span');
|
|
231
|
+
fa.style.display = 'none';
|
|
232
|
+
fa.dataset.sinthForAnchor = t.dataset.sinthFor;
|
|
233
|
+
t.parentNode && t.parentNode.insertBefore(fa, t);
|
|
234
|
+
var _loopIdx = 0;
|
|
235
|
+
entries.forEach(function(entry) {
|
|
236
|
+
var _k = entry[0];
|
|
237
|
+
var _v = entry[1];
|
|
238
|
+
var _item = (t.dataset && t.dataset.sinthItem) ? t.dataset.sinthItem : '__item__';
|
|
239
|
+
var _key = t.dataset && t.dataset.sinthKey ? t.dataset.sinthKey : null;
|
|
240
|
+
var _idx = t.dataset && t.dataset.sinthIndex ? t.dataset.sinthIndex : null;
|
|
241
|
+
_loopIdx++;
|
|
242
|
+
if (t.dataset.sinthIfReplace) {
|
|
243
|
+
var _rp = document.getElementById(t.dataset.sinthIfReplace);
|
|
244
|
+
if (_rp) _rp.parentNode.removeChild(_rp);
|
|
245
|
+
}
|
|
246
|
+
var loopCtx = {};
|
|
247
|
+
if (_item) loopCtx[_item] = _v;
|
|
248
|
+
if (_key) loopCtx[_key] = _k;
|
|
249
|
+
if (_idx) loopCtx[_idx] = _loopIdx - 1;
|
|
250
|
+
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
251
|
+
frag.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
252
|
+
try {
|
|
253
|
+
var exprFn = __X[el.dataset.exprId];
|
|
254
|
+
if (exprFn) el.textContent = exprFn(loopCtx);
|
|
255
|
+
} catch(e) {}
|
|
256
|
+
el.classList.remove('sinth-expr');
|
|
257
|
+
});
|
|
258
|
+
frag.querySelectorAll('template[data-sinth-if-expr]').forEach(function(ifT) {
|
|
259
|
+
var condFn = __X[ifT.dataset.sinthIfExpr];
|
|
260
|
+
var cond = false;
|
|
261
|
+
try { if (condFn) cond = condFn(loopCtx); } catch(e) {}
|
|
262
|
+
if (cond) {
|
|
263
|
+
var ifContent = document.createRange().createContextualFragment(ifT.innerHTML);
|
|
264
|
+
ifContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
265
|
+
try {
|
|
266
|
+
var exprFn2 = __X[el2.dataset.exprId];
|
|
267
|
+
if (exprFn2) el2.textContent = exprFn2(loopCtx);
|
|
268
|
+
} catch(e) {}
|
|
269
|
+
});
|
|
270
|
+
ifT.parentNode.insertBefore(ifContent, ifT);
|
|
271
|
+
} else {
|
|
272
|
+
var elseT = ifT.nextElementSibling;
|
|
273
|
+
if (elseT && elseT.hasAttribute('data-sinth-else')) {
|
|
274
|
+
var elseContent = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
275
|
+
elseContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
276
|
+
try {
|
|
277
|
+
var exprFn2 = __X[el2.dataset.exprId];
|
|
278
|
+
if (exprFn2) el2.textContent = exprFn2(loopCtx);
|
|
279
|
+
} catch(e) {}
|
|
280
|
+
});
|
|
281
|
+
ifT.parentNode.insertBefore(elseContent, ifT);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
if (${needsDelay}) {
|
|
286
|
+
frag.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
287
|
+
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, loopCtx); });
|
|
288
|
+
}
|
|
289
|
+
t.parentNode && t.parentNode.insertBefore(frag, t);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
`;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return helpers;
|
|
296
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { MixedBlockEntry } from "../types.ts";
|
|
2
|
+
|
|
3
|
+
export function buildRenderBody(opts: {
|
|
4
|
+
bodyHTML: string;
|
|
5
|
+
logicBlocks: string[];
|
|
6
|
+
mixedBlocks: MixedBlockEntry[]
|
|
7
|
+
needsLogic: boolean;
|
|
8
|
+
needsMixed: boolean;
|
|
9
|
+
needsIf: boolean;
|
|
10
|
+
needsFor: boolean;
|
|
11
|
+
needsExpr: boolean;
|
|
12
|
+
needsDelay: boolean;
|
|
13
|
+
}): string {
|
|
14
|
+
const { bodyHTML, logicBlocks, mixedBlocks, needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay } = opts;
|
|
15
|
+
let renderBody = "";
|
|
16
|
+
renderBody += ` var _sx = window.scrollX, _sy = window.scrollY;\n`;
|
|
17
|
+
|
|
18
|
+
if (needsLogic) {
|
|
19
|
+
renderBody += logicBlocks.map(b => b.replace(/^/gm, " ")).join("\n") + "\n";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (needsMixed) {
|
|
23
|
+
for (const mb of mixedBlocks) {
|
|
24
|
+
const ifJS = mb.ifJS ? mb.ifJS.trim() : "";
|
|
25
|
+
const elseJS = mb.elseJS ? mb.elseJS.trim() : "";
|
|
26
|
+
renderBody += ` (function() {
|
|
27
|
+
var __el = document.getElementById(${JSON.stringify(mb.replaceId || mb.id)});
|
|
28
|
+
if (__el) {
|
|
29
|
+
var __condFn = __X[${mb.conditionJS}];
|
|
30
|
+
if (__condFn ? __condFn() : false) {
|
|
31
|
+
${ifJS}
|
|
32
|
+
__el.innerHTML = ${JSON.stringify(mb.ifHTML)};
|
|
33
|
+
} else {
|
|
34
|
+
${elseJS}
|
|
35
|
+
__el.innerHTML = ${JSON.stringify(mb.elseHTML)};
|
|
36
|
+
}
|
|
37
|
+
__el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
38
|
+
__el.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);
|
|
39
|
+
${needsDelay ? `__el.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
40
|
+
__el.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });` : ""}
|
|
41
|
+
}
|
|
42
|
+
})();\n`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
renderBody += ` document.querySelectorAll('[data-sinth-remove]').forEach(function(el) {
|
|
47
|
+
var target = document.getElementById(el.dataset.sinthRemove);
|
|
48
|
+
if (target) target.remove();
|
|
49
|
+
});\n`;
|
|
50
|
+
|
|
51
|
+
if (needsIf) {
|
|
52
|
+
renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);\n`;
|
|
53
|
+
}
|
|
54
|
+
if (needsFor) {
|
|
55
|
+
renderBody += ` document.querySelectorAll('template[data-sinth-for]').forEach(sinthForBlock);\n`;
|
|
56
|
+
}
|
|
57
|
+
renderBody += ` document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
|
|
58
|
+
try { el.value = window[el.dataset.sinthValue] || ''; } catch(e) {}
|
|
59
|
+
});\n`;
|
|
60
|
+
renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
61
|
+
try { el.checked = !!window[el.dataset.sinthChecked]; } catch(e) {}
|
|
62
|
+
});\n`;
|
|
63
|
+
if (bodyHTML.includes("data-sinth-checked-expr")) {
|
|
64
|
+
renderBody += ` document.querySelectorAll('[data-sinth-checked-expr]').forEach(function(el) {
|
|
65
|
+
try {
|
|
66
|
+
var exprFn = __X[el.dataset.sinthCheckedExpr];
|
|
67
|
+
if (exprFn) el.checked = !!exprFn({});
|
|
68
|
+
} catch(e) {}
|
|
69
|
+
});\n`;
|
|
70
|
+
}
|
|
71
|
+
if (needsExpr) {
|
|
72
|
+
renderBody += ` document.querySelectorAll('.sinth-expr').forEach(sinthExpr);\n`;
|
|
73
|
+
}
|
|
74
|
+
if (needsDelay) {
|
|
75
|
+
renderBody += ` setTimeout(function() {
|
|
76
|
+
document.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
77
|
+
}, 0);\n`;
|
|
78
|
+
}
|
|
79
|
+
renderBody += ` window.scrollTo(_sx, _sy);\n`;
|
|
80
|
+
|
|
81
|
+
return renderBody;
|
|
82
|
+
}
|