@yannosay/sinth 0.14.3 → 0.14.5
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/package.json +1 -1
- package/src/core/compiler.ts +16 -14
- package/src/core/lexer.ts +2 -0
- package/src/core/parser.ts +113 -15
- package/src/core/runtime/helpers.ts +79 -65
- package/src/core/runtime/render.ts +12 -12
- package/src/core/types.ts +4 -3
package/package.json
CHANGED
package/src/core/compiler.ts
CHANGED
|
@@ -533,6 +533,7 @@ depth: number,
|
|
|
533
533
|
const bodyHTML = ifBlock.body.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
534
534
|
const elseHTML = (ifBlock.elseBody ?? []).map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
535
535
|
let replaceAttr = "";
|
|
536
|
+
let persistAttr = ifBlock.persist ? ` data-sinth-if-persist="true"` : "";
|
|
536
537
|
const firstComp = ifBlock.body.find(c => c.kind === "use") as CompUse | undefined;
|
|
537
538
|
if (firstComp) {
|
|
538
539
|
const idAttr = firstComp.attrs.find(a => a.name === "id");
|
|
@@ -574,7 +575,7 @@ depth: number,
|
|
|
574
575
|
}
|
|
575
576
|
}
|
|
576
577
|
return (
|
|
577
|
-
`<template data-sinth-if-id="${tplId}" data-sinth-if-expr="${condId}"${replaceAttr}${delayAttr}${delayHideAttr}>${bodyHTML}</template>` +
|
|
578
|
+
`<template data-sinth-if-id="${tplId}" data-sinth-if-expr="${condId}"${replaceAttr}${delayAttr}${delayHideAttr}${persistAttr}>${bodyHTML}</template>` +
|
|
578
579
|
(elseHTML ? `<template data-sinth-else data-sinth-if-id="${tplId}"${elseDelayAttr}${elseDelayHideAttr}>${elseHTML}</template>` : "")
|
|
579
580
|
);
|
|
580
581
|
}
|
|
@@ -983,20 +984,20 @@ export function buildRuntime(opts: {
|
|
|
983
984
|
);
|
|
984
985
|
}
|
|
985
986
|
const defaults: Record<string, string> = { str: '""', int: "0", bool: "false", "str[]": "[]" };
|
|
986
|
-
return `
|
|
987
|
+
return `let ${v.name} = ${defaults[v.varType] ?? "undefined"};`;
|
|
987
988
|
}
|
|
988
989
|
const val = litToString(v.value);
|
|
989
|
-
if (val.startsWith("__VAR__")) return `
|
|
990
|
-
if (val.startsWith("__ARR__")) return `
|
|
991
|
-
if (v.varType === "obj") return `
|
|
992
|
-
if (v.varType === "str") return `
|
|
990
|
+
if (val.startsWith("__VAR__")) return `let ${v.name} = ${val.slice(7)};`;
|
|
991
|
+
if (val.startsWith("__ARR__")) return `let ${v.name} = ${val.slice(7)};`;
|
|
992
|
+
if (v.varType === "obj") return `let ${v.name} = ${val};`;
|
|
993
|
+
if (v.varType === "str") return `let ${v.name} = ${JSON.stringify(val)};`;
|
|
993
994
|
if (v.varType === "str[]" && typeof val === 'string' && val.startsWith("__ARR__")) {
|
|
994
995
|
try {
|
|
995
996
|
const arr = JSON.parse(val.slice(7));
|
|
996
|
-
return `
|
|
997
|
-
} catch { return `
|
|
997
|
+
return `let ${v.name} = ${JSON.stringify(arr)};`;
|
|
998
|
+
} catch { return `let ${v.name} = ${val.slice(7)};`; }
|
|
998
999
|
}
|
|
999
|
-
return `
|
|
1000
|
+
return `let ${v.name} = ${val};`;
|
|
1000
1001
|
}).join("\n");
|
|
1001
1002
|
|
|
1002
1003
|
if (!needsRender && !needsDelay) {
|
|
@@ -1012,7 +1013,7 @@ export function buildRuntime(opts: {
|
|
|
1012
1013
|
});
|
|
1013
1014
|
|
|
1014
1015
|
const exprArrayJS = exprRegistry.length > 0
|
|
1015
|
-
? `
|
|
1016
|
+
? `let __X = [${exprRegistry.map((js) => `function(_ctx){ return ${js}; }`).join(",")}];\n`
|
|
1016
1017
|
: "";
|
|
1017
1018
|
|
|
1018
1019
|
let renderFunc = needsRender ? `function sinthRender() {\n${renderBody}}\nsinthRender();` : "";
|
|
@@ -1020,9 +1021,9 @@ export function buildRuntime(opts: {
|
|
|
1020
1021
|
if (needsFullscreenSync) {
|
|
1021
1022
|
renderFunc += `
|
|
1022
1023
|
(function() {
|
|
1023
|
-
|
|
1024
|
+
let syncedEls = document.querySelectorAll('[data-sinth-fullscreen-sync]');
|
|
1024
1025
|
if (syncedEls.length > 0) {
|
|
1025
|
-
|
|
1026
|
+
let vars = [];
|
|
1026
1027
|
syncedEls.forEach(function(el) { vars.push(el.dataset.sinthFullscreenSync); });
|
|
1027
1028
|
document.addEventListener('fullscreenchange', function() {
|
|
1028
1029
|
if (!document.fullscreenElement) {
|
|
@@ -1034,7 +1035,7 @@ export function buildRuntime(opts: {
|
|
|
1034
1035
|
})();`;
|
|
1035
1036
|
}
|
|
1036
1037
|
|
|
1037
|
-
const pageCode = `// Sinth
|
|
1038
|
+
const pageCode = `// Sinth Page Runtime
|
|
1038
1039
|
${varLines}
|
|
1039
1040
|
${exprArrayJS}
|
|
1040
1041
|
${renderFunc}`;
|
|
@@ -1100,6 +1101,7 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
|
|
|
1100
1101
|
}
|
|
1101
1102
|
if (child.kind === "use") {
|
|
1102
1103
|
(child as CompUse).attrs.forEach(a => {
|
|
1104
|
+
if (a.name === "id") return;
|
|
1103
1105
|
if (a.value?.kind === "str") {
|
|
1104
1106
|
const v = a.value.value;
|
|
1105
1107
|
if (v.startsWith("__EXPR__") || v.startsWith("__MULTI_EXPR__")) {
|
|
@@ -1225,7 +1227,7 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
|
|
|
1225
1227
|
const extra = Object.entries(s.attrs)
|
|
1226
1228
|
.map(([k, v]) => v === "true" ? k : `${k}="${escAttr(v)}"`)
|
|
1227
1229
|
.join(" ");
|
|
1228
|
-
const globalised = s.raw.replace(/\b(let|const)\b/g, "
|
|
1230
|
+
const globalised = s.raw.replace(/\b(let|const)\b/g, "let");
|
|
1229
1231
|
scriptTags.push(`<script${extra ? " " + extra : ""}>\n${globalised}\n</script>`);
|
|
1230
1232
|
}
|
|
1231
1233
|
|
package/src/core/lexer.ts
CHANGED
|
@@ -18,6 +18,7 @@ const KEYWORDS: Record<string, TT> = {
|
|
|
18
18
|
"custom": TT.KW_CUSTOM,
|
|
19
19
|
"if": TT.KW_IF,
|
|
20
20
|
"else": TT.KW_ELSE,
|
|
21
|
+
"elif": TT.KW_ELSE,
|
|
21
22
|
"for": TT.KW_FOR,
|
|
22
23
|
"in": TT.KW_IN,
|
|
23
24
|
"remove": TT.KW_REMOVE,
|
|
@@ -62,6 +63,7 @@ export class Lexer {
|
|
|
62
63
|
if (ch === "+") { tokens.push(this.single(TT.OP_PLUS, loc)); continue; }
|
|
63
64
|
if (ch === "*") { tokens.push(this.single(TT.OP_STAR, loc)); continue; }
|
|
64
65
|
if (ch === "/") { tokens.push(this.single(TT.OP_SLASH, loc)); continue; }
|
|
66
|
+
if (ch === "%") { tokens.push(this.single(TT.OP_MOD, loc)); continue; }
|
|
65
67
|
if (ch === ";") { tokens.push(this.single(TT.OP_SEMI, loc)); continue; }
|
|
66
68
|
if (ch === "<") { tokens.push(this.single(TT.OP_LT, loc)); continue; }
|
|
67
69
|
if (ch === ">") { tokens.push(this.single(TT.OP_GT, loc)); continue; }
|
package/src/core/parser.ts
CHANGED
|
@@ -335,7 +335,36 @@ private parseArrayLiteral(): Literal {
|
|
|
335
335
|
return null;
|
|
336
336
|
}
|
|
337
337
|
|
|
338
|
-
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
private parsePrimary(): Expression {
|
|
341
|
+
const tok = this.peek();
|
|
342
|
+
if (tok.type === TT.NUMBER || tok.type === TT.STRING || tok.type === TT.BOOL_TRUE || tok.type === TT.BOOL_FALSE || tok.type === TT.NULL_LIT) {
|
|
343
|
+
const lit = this.parseLiteral();
|
|
344
|
+
return { kind: "literal", value: lit };
|
|
345
|
+
}
|
|
346
|
+
if (tok.type === TT.IDENT) {
|
|
347
|
+
const name = this.consume(TT.IDENT).value;
|
|
348
|
+
let expr: Expression;
|
|
349
|
+
if (this.check(TT.DOT)) {
|
|
350
|
+
this.consume(TT.DOT);
|
|
351
|
+
expr = { kind: "variable", name: name + "." + this.consume(TT.IDENT).value };
|
|
352
|
+
} else {
|
|
353
|
+
expr = { kind: "variable", name };
|
|
354
|
+
}
|
|
355
|
+
return expr;
|
|
356
|
+
}
|
|
357
|
+
if (tok.type === TT.LPAREN) {
|
|
358
|
+
this.consume(TT.LPAREN);
|
|
359
|
+
const inner = this.parseExpression();
|
|
360
|
+
this.consume(TT.RPAREN);
|
|
361
|
+
return inner!;
|
|
362
|
+
}
|
|
363
|
+
throw new SinthError(`Expected expression`, this.peek().loc);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private parseComparison(left: Expression): Expression {
|
|
367
|
+
const savedPos = this.pos;
|
|
339
368
|
const t = this.peek().type;
|
|
340
369
|
let op: BinaryOp | null = null;
|
|
341
370
|
if (t === TT.OP_EQEQ) op = "==";
|
|
@@ -344,19 +373,61 @@ private parseArrayLiteral(): Literal {
|
|
|
344
373
|
else if (t === TT.OP_GT) op = ">";
|
|
345
374
|
else if (t === TT.OP_LTEQ) op = "<=";
|
|
346
375
|
else if (t === TT.OP_GTEQ) op = ">=";
|
|
347
|
-
else if (t === TT.OP_PLUS) op = "+";
|
|
348
|
-
else if (t === TT.OP_MINUS) op = "-";
|
|
349
|
-
else if (t === TT.OP_STAR) op = "*";
|
|
350
|
-
else if (t === TT.OP_SLASH) op = "/";
|
|
351
|
-
else if (t === TT.IDENT && this.peek().value === "and") op = "and";
|
|
352
|
-
else if (t === TT.IDENT && this.peek().value === "or") op = "or";
|
|
353
|
-
|
|
354
376
|
if (!op) return left;
|
|
355
377
|
this.pos++;
|
|
378
|
+
const rightPrimary = this.parsePrimary();
|
|
379
|
+
const rightWithPostfix = this.parsePostfix(rightPrimary);
|
|
380
|
+
const rightWithCall = this.parseCallExpr(rightWithPostfix);
|
|
381
|
+
const right = this.parseArithmetic(rightWithCall);
|
|
382
|
+
if (!right) {
|
|
383
|
+
this.pos = savedPos;
|
|
384
|
+
return left;
|
|
385
|
+
}
|
|
386
|
+
return this.parseComparison({ kind: "binary", left, op, right });
|
|
387
|
+
}
|
|
356
388
|
|
|
389
|
+
private parseLogical(left: Expression): Expression {
|
|
390
|
+
const savedPos = this.pos;
|
|
391
|
+
const t = this.peek().type;
|
|
392
|
+
let op: BinaryOp | null = null;
|
|
393
|
+
if (t === TT.IDENT && this.peek().value === "and") op = "and";
|
|
394
|
+
else if (t === TT.IDENT && this.peek().value === "or") op = "or";
|
|
395
|
+
if (!op) return left;
|
|
396
|
+
this.pos++;
|
|
357
397
|
const right = this.parseExpression();
|
|
358
|
-
if (!right)
|
|
359
|
-
|
|
398
|
+
if (!right) {
|
|
399
|
+
this.pos = savedPos;
|
|
400
|
+
return left;
|
|
401
|
+
}
|
|
402
|
+
return this.parseLogical({ kind: "binary", left, op, right });
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
private parseArithmetic(left: Expression): Expression {
|
|
406
|
+
while (true) {
|
|
407
|
+
const t = this.peek().type;
|
|
408
|
+
let op: BinaryOp | null = null;
|
|
409
|
+
if (t === TT.OP_PLUS) op = "+";
|
|
410
|
+
else if (t === TT.OP_MINUS) op = "-";
|
|
411
|
+
else if (t === TT.OP_STAR) op = "*";
|
|
412
|
+
else if (t === TT.OP_SLASH) op = "/";
|
|
413
|
+
else if (t === TT.OP_MOD) op = "%";
|
|
414
|
+
if (!op) break;
|
|
415
|
+
this.pos++;
|
|
416
|
+
const rightPrimary = this.parsePrimary();
|
|
417
|
+
const rightWithPostfix = this.parsePostfix(rightPrimary);
|
|
418
|
+
const rightWithCall = this.parseCallExpr(rightWithPostfix);
|
|
419
|
+
const right = this.parseArithmetic(rightWithCall);
|
|
420
|
+
if (!right) throw new SinthError(`Expected expression after '${op}'`, this.peek().loc);
|
|
421
|
+
left = { kind: "binary", left, op, right };
|
|
422
|
+
}
|
|
423
|
+
return left;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
private parseBinaryRHS(left: Expression): Expression {
|
|
427
|
+
left = this.parseArithmetic(left);
|
|
428
|
+
left = this.parseComparison(left);
|
|
429
|
+
left = this.parseLogical(left);
|
|
430
|
+
return left;
|
|
360
431
|
}
|
|
361
432
|
|
|
362
433
|
private parsePostfix(base: Expression): Expression {
|
|
@@ -507,16 +578,43 @@ private parseArrayLiteral(): Literal {
|
|
|
507
578
|
const condition = this.parseExpression();
|
|
508
579
|
if (!condition) throw new SinthError("Expected condition after 'if'", this.peek().loc);
|
|
509
580
|
|
|
510
|
-
|
|
581
|
+
let persist = false;
|
|
582
|
+
if (this.check(TT.IDENT) && this.peek().value === "persist") {
|
|
583
|
+
this.consume(TT.IDENT);
|
|
584
|
+
persist = true;
|
|
585
|
+
}
|
|
511
586
|
this.consume(TT.LBRACE);
|
|
512
587
|
const body = this.parseUnifiedBody();
|
|
513
588
|
this.consume(TT.RBRACE);
|
|
514
589
|
|
|
515
590
|
let elseBody: Child[] | undefined;
|
|
516
591
|
if (this.check(TT.KW_ELSE)) {
|
|
517
|
-
this.consume(TT.KW_ELSE);
|
|
518
|
-
if (this.check(TT.KW_IF)) {
|
|
519
|
-
|
|
592
|
+
const elseTok = this.consume(TT.KW_ELSE);
|
|
593
|
+
if (this.check(TT.KW_IF) || elseTok.value === "elif") {
|
|
594
|
+
if (elseTok.value === "elif") {
|
|
595
|
+
const cond = this.parseExpression();
|
|
596
|
+
if (!cond) throw new SinthError("Expected condition after 'elif'", this.peek().loc);
|
|
597
|
+
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after elif condition", this.peek().loc);
|
|
598
|
+
this.consume(TT.LBRACE);
|
|
599
|
+
const elifBody = this.parseUnifiedBody();
|
|
600
|
+
this.consume(TT.RBRACE);
|
|
601
|
+
const elifBlock: IfBlock = { kind: "if", condition: cond, body: elifBody, loc: elseTok.loc };
|
|
602
|
+
if (this.check(TT.KW_ELSE)) {
|
|
603
|
+
const nextElse = this.consume(TT.KW_ELSE);
|
|
604
|
+
if (this.check(TT.KW_IF) || nextElse.value === "elif") {
|
|
605
|
+
this.pos--;
|
|
606
|
+
elifBlock.elseBody = [this.parseIfBlock()];
|
|
607
|
+
} else {
|
|
608
|
+
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after else", this.peek().loc);
|
|
609
|
+
this.consume(TT.LBRACE);
|
|
610
|
+
elifBlock.elseBody = this.parseUnifiedBody();
|
|
611
|
+
this.consume(TT.RBRACE);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
elseBody = [elifBlock];
|
|
615
|
+
} else {
|
|
616
|
+
elseBody = [this.parseIfBlock()];
|
|
617
|
+
}
|
|
520
618
|
} else {
|
|
521
619
|
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after else", this.peek().loc);
|
|
522
620
|
this.consume(TT.LBRACE);
|
|
@@ -525,7 +623,7 @@ private parseArrayLiteral(): Literal {
|
|
|
525
623
|
}
|
|
526
624
|
}
|
|
527
625
|
|
|
528
|
-
return { kind: "if", condition, body, elseBody, loc };
|
|
626
|
+
return { kind: "if", condition, body, elseBody, persist, loc };
|
|
529
627
|
}
|
|
530
628
|
|
|
531
629
|
private parseUnifiedBody(): Child[] {
|
|
@@ -12,7 +12,7 @@ export function generateHelpers(opts: {
|
|
|
12
12
|
helpers += `
|
|
13
13
|
function sinthExpr(el) {
|
|
14
14
|
try {
|
|
15
|
-
|
|
15
|
+
let exprFn = __X[el.dataset.exprId];
|
|
16
16
|
if (exprFn) el.textContent = exprFn({});
|
|
17
17
|
} catch(e) {}
|
|
18
18
|
}
|
|
@@ -23,15 +23,15 @@ function sinthExpr(el) {
|
|
|
23
23
|
helpers += `
|
|
24
24
|
function sinthReplaceInsert(t, anchor, ifId, replaceId) {
|
|
25
25
|
if (anchor) {
|
|
26
|
-
|
|
27
|
-
while (cur && cur !== t) {
|
|
26
|
+
let cur = anchor.nextSibling;
|
|
27
|
+
while (cur && cur !== t) { let nx = cur.nextSibling; cur.remove(); cur = nx; }
|
|
28
28
|
} else {
|
|
29
29
|
anchor = document.createElement('span');
|
|
30
30
|
anchor.style.display = 'none';
|
|
31
31
|
anchor.dataset.sinthIfAnchor = ifId;
|
|
32
32
|
t.parentNode.insertBefore(anchor, t);
|
|
33
33
|
}
|
|
34
|
-
|
|
34
|
+
let _rp = null, _rpParent = null, _rpNext = null;
|
|
35
35
|
if (replaceId) {
|
|
36
36
|
_rp = document.getElementById(replaceId);
|
|
37
37
|
if (_rp) {
|
|
@@ -43,14 +43,14 @@ function sinthReplaceInsert(t, anchor, ifId, replaceId) {
|
|
|
43
43
|
anchor._sinthReplacedNext = _rpNext;
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
let frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
47
47
|
frag.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
48
48
|
if (${needsDelay}) {
|
|
49
49
|
frag.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
50
50
|
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
let fragFirst = frag.firstChild;
|
|
53
|
+
let fragLast = frag.lastChild;
|
|
54
54
|
if (_rpParent && _rpNext) {
|
|
55
55
|
_rpParent.insertBefore(frag, _rpNext);
|
|
56
56
|
} else if (_rpParent) {
|
|
@@ -72,9 +72,9 @@ function sinthReplaceInsert(t, anchor, ifId, replaceId) {
|
|
|
72
72
|
function sinthDelay(el) {
|
|
73
73
|
if (el.dataset.sinthDelayDone) { return; }
|
|
74
74
|
el.dataset.sinthDelayDone = '1';
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
let ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
76
|
+
let hideEl = el.dataset.sinthDelayHide !== 'false';
|
|
77
|
+
let show = function() {
|
|
78
78
|
if (hideEl) el.style.display = '';
|
|
79
79
|
el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
80
80
|
};
|
|
@@ -90,9 +90,9 @@ function sinthDelay(el) {
|
|
|
90
90
|
function sinthDelayExpr(el, _ctx) {
|
|
91
91
|
_ctx = _ctx || {};
|
|
92
92
|
try {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
93
|
+
let fn = __X[el.dataset.sinthDelayExprId];
|
|
94
|
+
let ms = fn ? parseInt(fn(_ctx)) || 0 : 0;
|
|
95
|
+
let show = function() {
|
|
96
96
|
el.style.display = '';
|
|
97
97
|
el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
98
98
|
};
|
|
@@ -106,14 +106,21 @@ function sinthDelayExpr(el, _ctx) {
|
|
|
106
106
|
if (needsIf) {
|
|
107
107
|
helpers += `
|
|
108
108
|
function sinthIfBlock(t) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
109
|
+
let ifId = t.dataset.sinthIfId;
|
|
110
|
+
let anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
|
|
111
|
+
let condFn = __X[t.dataset.sinthIfExpr];
|
|
112
|
+
let cond = condFn ? condFn() : false;
|
|
113
113
|
if (cond) {
|
|
114
|
-
|
|
114
|
+
let elseA = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
115
|
+
if (elseA) {
|
|
116
|
+
let c = elseA.nextSibling;
|
|
117
|
+
while (c && c !== t) { let n = c.nextSibling; c.remove(); c = n; }
|
|
118
|
+
elseA.remove();
|
|
119
|
+
}
|
|
120
|
+
if (t.dataset.sinthIfPersist === "true" && anchor) return;
|
|
121
|
+
let _hasContent = anchor || t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
115
122
|
if (t.dataset.sinthIfDelayHide === 'false' && t.dataset.sinthIfDelay && _hasContent) {
|
|
116
|
-
|
|
123
|
+
let dms = parseInt(t.dataset.sinthIfDelay) || 0;
|
|
117
124
|
setTimeout(function() {
|
|
118
125
|
sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
|
|
119
126
|
}, dms);
|
|
@@ -121,17 +128,18 @@ function sinthIfBlock(t) {
|
|
|
121
128
|
anchor = sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
|
|
122
129
|
}
|
|
123
130
|
} else {
|
|
124
|
-
|
|
131
|
+
let runElse = function() {
|
|
132
|
+
if (t.dataset.sinthIfPersist === "true") return;
|
|
125
133
|
if (anchor) {
|
|
126
134
|
if (anchor._sinthReplaced) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
135
|
+
let insFirst = anchor._sinthInsertedFirst;
|
|
136
|
+
let insLast = anchor._sinthInsertedLast;
|
|
137
|
+
let rpParent = anchor._sinthReplacedParent;
|
|
138
|
+
let rpNext = anchor._sinthReplacedNext;
|
|
131
139
|
if (insFirst && insLast) {
|
|
132
|
-
|
|
140
|
+
let cur = insFirst;
|
|
133
141
|
while (cur && cur !== insLast) {
|
|
134
|
-
|
|
142
|
+
let next = cur.nextSibling;
|
|
135
143
|
cur.remove();
|
|
136
144
|
cur = next;
|
|
137
145
|
}
|
|
@@ -147,47 +155,53 @@ function sinthIfBlock(t) {
|
|
|
147
155
|
sinthDelay(anchor._sinthReplaced);
|
|
148
156
|
}
|
|
149
157
|
} else {
|
|
150
|
-
|
|
151
|
-
while (cur2 && cur2 !== t) {
|
|
158
|
+
let cur2 = anchor.nextSibling;
|
|
159
|
+
while (cur2 && cur2 !== t) { let nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
|
|
152
160
|
}
|
|
153
161
|
anchor.remove();
|
|
154
162
|
}
|
|
155
|
-
|
|
163
|
+
let elseT = t.nextElementSibling;
|
|
156
164
|
if (elseT && elseT.hasAttribute('data-sinth-else')) {
|
|
157
|
-
|
|
158
|
-
|
|
165
|
+
let elseIfId = elseT.dataset.sinthIfId;
|
|
166
|
+
let ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + elseIfId + '"]');
|
|
159
167
|
if (ea) {
|
|
160
|
-
|
|
161
|
-
while (cur3 && cur3 !== t) {
|
|
168
|
+
let cur3 = ea.nextSibling;
|
|
169
|
+
while (cur3 && cur3 !== t) { let nx3 = cur3.nextSibling; cur3.remove(); cur3 = nx3; }
|
|
162
170
|
} else {
|
|
163
171
|
ea = document.createElement('span');
|
|
164
172
|
ea.style.display = 'none';
|
|
165
173
|
ea.dataset.sinthIfAnchor = '__else__' + elseIfId;
|
|
166
174
|
t.parentNode.insertBefore(ea, t);
|
|
167
175
|
}
|
|
168
|
-
|
|
176
|
+
let ef = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
169
177
|
ef.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
170
178
|
if (${needsDelay}) {
|
|
171
179
|
ef.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
172
180
|
ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
|
|
173
181
|
}
|
|
182
|
+
let innerTemplates = ef.querySelectorAll('template[data-sinth-if-expr]');
|
|
174
183
|
t.parentNode.insertBefore(ef, t);
|
|
184
|
+
for (let i = 0; i < innerTemplates.length; i++) sinthIfBlock(innerTemplates[i]);
|
|
175
185
|
ef.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
176
186
|
delete el.dataset.sinthDelayDone;
|
|
177
187
|
sinthDelay(el);
|
|
178
188
|
});
|
|
179
189
|
} else {
|
|
180
|
-
|
|
190
|
+
let ea2 = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
181
191
|
if (ea2) {
|
|
182
|
-
|
|
183
|
-
while (ec && ec !== t) {
|
|
192
|
+
let ec = ea2.nextSibling;
|
|
193
|
+
while (ec && ec !== t) { let en = ec.nextSibling; ec.remove(); ec = en; }
|
|
184
194
|
ea2.remove();
|
|
185
195
|
}
|
|
196
|
+
if (anchor) {
|
|
197
|
+
let ac = anchor.nextSibling;
|
|
198
|
+
while (ac && ac !== t) { let an = ac.nextSibling; ac.remove(); ac = an; }
|
|
199
|
+
}
|
|
186
200
|
}
|
|
187
201
|
};
|
|
188
|
-
|
|
202
|
+
let elseT = t.nextElementSibling;
|
|
189
203
|
if (elseT && elseT.hasAttribute('data-sinth-else') && elseT.dataset.sinthIfDelayHide === 'false' && elseT.dataset.sinthIfDelay && anchor) {
|
|
190
|
-
|
|
204
|
+
let edms = parseInt(elseT.dataset.sinthIfDelay) || 0;
|
|
191
205
|
setTimeout(runElse, edms);
|
|
192
206
|
} else {
|
|
193
207
|
runElse();
|
|
@@ -200,7 +214,7 @@ function sinthIfBlock(t) {
|
|
|
200
214
|
if (needsFor) {
|
|
201
215
|
helpers += `
|
|
202
216
|
function hashString(str) {
|
|
203
|
-
|
|
217
|
+
let hash = 0, i, chr;
|
|
204
218
|
for (i = 0; i < str.length; i++) {
|
|
205
219
|
chr = str.charCodeAt(i);
|
|
206
220
|
hash = ((hash << 5) - hash) + chr;
|
|
@@ -209,76 +223,76 @@ function hashString(str) {
|
|
|
209
223
|
return String(hash);
|
|
210
224
|
}
|
|
211
225
|
function sinthForBlock(t) {
|
|
212
|
-
|
|
226
|
+
let source = window[t.dataset.sinthFor];
|
|
213
227
|
if (source === undefined) source = [];
|
|
214
|
-
|
|
228
|
+
let newHash = '';
|
|
215
229
|
try { newHash = hashString(JSON.stringify(source)); } catch(e) { newHash = ''; }
|
|
216
230
|
if (t.dataset.sinthForHash && t.dataset.sinthForHash === newHash) {
|
|
217
231
|
return;
|
|
218
232
|
}
|
|
219
233
|
t.dataset.sinthForHash = newHash;
|
|
220
|
-
|
|
221
|
-
|
|
234
|
+
let isObj = (typeof source === 'object' && source !== null && !Array.isArray(source));
|
|
235
|
+
let entries;
|
|
222
236
|
if (isObj) {
|
|
223
237
|
entries = Object.entries(source);
|
|
224
238
|
} else {
|
|
225
239
|
if (!Array.isArray(source)) source = [];
|
|
226
240
|
entries = source.map(function(item, index) { return [index, item]; });
|
|
227
241
|
}
|
|
228
|
-
|
|
242
|
+
let anchor = t.parentNode ? t.parentNode.querySelector('[data-sinth-for-anchor="' + t.dataset.sinthFor + '"]') : null;
|
|
229
243
|
if (anchor) {
|
|
230
|
-
|
|
231
|
-
while (cur2 && cur2 !== t) {
|
|
244
|
+
let cur2 = anchor.nextSibling;
|
|
245
|
+
while (cur2 && cur2 !== t) { let nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
|
|
232
246
|
anchor.remove();
|
|
233
247
|
}
|
|
234
|
-
|
|
248
|
+
let fa = document.createElement('span');
|
|
235
249
|
fa.style.display = 'none';
|
|
236
250
|
fa.dataset.sinthForAnchor = t.dataset.sinthFor;
|
|
237
251
|
t.parentNode && t.parentNode.insertBefore(fa, t);
|
|
238
|
-
|
|
252
|
+
let _loopIdx = 0;
|
|
239
253
|
entries.forEach(function(entry) {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
254
|
+
let _k = entry[0];
|
|
255
|
+
let _v = entry[1];
|
|
256
|
+
let _item = (t.dataset && t.dataset.sinthItem) ? t.dataset.sinthItem : '__item__';
|
|
257
|
+
let _key = t.dataset && t.dataset.sinthKey ? t.dataset.sinthKey : null;
|
|
258
|
+
let _idx = t.dataset && t.dataset.sinthIndex ? t.dataset.sinthIndex : null;
|
|
245
259
|
_loopIdx++;
|
|
246
260
|
if (t.dataset.sinthIfReplace) {
|
|
247
|
-
|
|
261
|
+
let _rp = document.getElementById(t.dataset.sinthIfReplace);
|
|
248
262
|
if (_rp) _rp.parentNode.removeChild(_rp);
|
|
249
263
|
}
|
|
250
|
-
|
|
264
|
+
let loopCtx = {};
|
|
251
265
|
if (_item) loopCtx[_item] = _v;
|
|
252
266
|
if (_key) loopCtx[_key] = _k;
|
|
253
267
|
if (_idx) loopCtx[_idx] = _loopIdx - 1;
|
|
254
|
-
|
|
268
|
+
let frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
255
269
|
frag.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
256
270
|
try {
|
|
257
|
-
|
|
271
|
+
let exprFn = __X[el.dataset.exprId];
|
|
258
272
|
if (exprFn) el.textContent = exprFn(loopCtx);
|
|
259
273
|
} catch(e) {}
|
|
260
274
|
el.classList.remove('sinth-expr');
|
|
261
275
|
});
|
|
262
276
|
frag.querySelectorAll('template[data-sinth-if-expr]').forEach(function(ifT) {
|
|
263
|
-
|
|
264
|
-
|
|
277
|
+
let condFn = __X[ifT.dataset.sinthIfExpr];
|
|
278
|
+
let cond = false;
|
|
265
279
|
try { if (condFn) cond = condFn(loopCtx); } catch(e) {}
|
|
266
280
|
if (cond) {
|
|
267
|
-
|
|
281
|
+
let ifContent = document.createRange().createContextualFragment(ifT.innerHTML);
|
|
268
282
|
ifContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
269
283
|
try {
|
|
270
|
-
|
|
284
|
+
let exprFn2 = __X[el2.dataset.exprId];
|
|
271
285
|
if (exprFn2) el2.textContent = exprFn2(loopCtx);
|
|
272
286
|
} catch(e) {}
|
|
273
287
|
});
|
|
274
288
|
ifT.parentNode.insertBefore(ifContent, ifT);
|
|
275
289
|
} else {
|
|
276
|
-
|
|
290
|
+
let elseT = ifT.nextElementSibling;
|
|
277
291
|
if (elseT && elseT.hasAttribute('data-sinth-else')) {
|
|
278
|
-
|
|
292
|
+
let elseContent = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
279
293
|
elseContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
280
294
|
try {
|
|
281
|
-
|
|
295
|
+
let exprFn2 = __X[el2.dataset.exprId];
|
|
282
296
|
if (exprFn2) el2.textContent = exprFn2(loopCtx);
|
|
283
297
|
} catch(e) {}
|
|
284
298
|
});
|
|
@@ -13,7 +13,7 @@ export function buildRenderBody(opts: {
|
|
|
13
13
|
}): string {
|
|
14
14
|
const { bodyHTML, logicBlocks, mixedBlocks, needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay } = opts;
|
|
15
15
|
let renderBody = "";
|
|
16
|
-
renderBody += `
|
|
16
|
+
renderBody += ` let _sx = window.scrollX, _sy = window.scrollY;\n`;
|
|
17
17
|
|
|
18
18
|
if (needsLogic) {
|
|
19
19
|
renderBody += logicBlocks.map(b => b.replace(/^/gm, " ")).join("\n") + "\n";
|
|
@@ -24,9 +24,9 @@ export function buildRenderBody(opts: {
|
|
|
24
24
|
const ifJS = mb.ifJS ? mb.ifJS.trim() : "";
|
|
25
25
|
const elseJS = mb.elseJS ? mb.elseJS.trim() : "";
|
|
26
26
|
renderBody += ` (function() {
|
|
27
|
-
|
|
27
|
+
let __el = document.getElementById(${JSON.stringify(mb.replaceId || mb.id)});
|
|
28
28
|
if (__el) {
|
|
29
|
-
|
|
29
|
+
let __condFn = __X[${mb.conditionJS}];
|
|
30
30
|
if (__condFn ? __condFn() : false) {
|
|
31
31
|
${ifJS}
|
|
32
32
|
__el.innerHTML = ${JSON.stringify(mb.ifHTML)};
|
|
@@ -47,7 +47,7 @@ export function buildRenderBody(opts: {
|
|
|
47
47
|
renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);\n`;
|
|
48
48
|
}
|
|
49
49
|
renderBody += ` document.querySelectorAll('[data-sinth-remove]').forEach(function(el) {
|
|
50
|
-
|
|
50
|
+
let target = document.getElementById(el.dataset.sinthRemove);
|
|
51
51
|
if (target) target.remove();
|
|
52
52
|
});\n`;
|
|
53
53
|
if (needsFor) {
|
|
@@ -65,16 +65,16 @@ export function buildRenderBody(opts: {
|
|
|
65
65
|
if (bodyHTML.includes("data-sinth-checked-expr")) {
|
|
66
66
|
renderBody += ` document.querySelectorAll('[data-sinth-checked-expr]').forEach(function(el) {
|
|
67
67
|
try {
|
|
68
|
-
|
|
68
|
+
let exprFn = __X[el.dataset.sinthCheckedExpr];
|
|
69
69
|
if (exprFn) el.checked = !!exprFn({});
|
|
70
70
|
} catch(e) {}
|
|
71
71
|
});\n`;
|
|
72
72
|
}
|
|
73
73
|
if (needsDelay) {
|
|
74
74
|
renderBody += ` document.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
75
|
-
|
|
75
|
+
let newContent = '';
|
|
76
76
|
el.querySelectorAll('.sinth-expr').forEach(function(exprEl) {
|
|
77
|
-
|
|
77
|
+
let exprFn = __X[exprEl.dataset.exprId];
|
|
78
78
|
if (exprFn) newContent += exprFn({});
|
|
79
79
|
});
|
|
80
80
|
if (el._sinthLastContent === undefined) {
|
|
@@ -87,17 +87,17 @@ export function buildRenderBody(opts: {
|
|
|
87
87
|
}
|
|
88
88
|
if (needsExpr) {
|
|
89
89
|
renderBody += ` document.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
90
|
-
|
|
90
|
+
let delayParent = el.closest('[data-sinth-delay]');
|
|
91
91
|
if (!delayParent || delayParent.dataset.sinthDelayDone) {
|
|
92
92
|
sinthExpr(el);
|
|
93
93
|
}
|
|
94
94
|
});\n`;
|
|
95
95
|
}
|
|
96
96
|
renderBody += ` document.querySelectorAll('[data-sinth-hide]').forEach(function(el) {
|
|
97
|
-
|
|
97
|
+
let exprId = el.dataset.sinthHide;
|
|
98
98
|
if (exprId) {
|
|
99
99
|
try {
|
|
100
|
-
|
|
100
|
+
let exprFn = __X[exprId];
|
|
101
101
|
if (exprFn) el.style.display = exprFn({}) ? 'none' : '';
|
|
102
102
|
} catch(e) {}
|
|
103
103
|
} else {
|
|
@@ -105,10 +105,10 @@ export function buildRenderBody(opts: {
|
|
|
105
105
|
}
|
|
106
106
|
});\n`;
|
|
107
107
|
renderBody += ` document.querySelectorAll('[data-sinth-fullscreen]').forEach(function(el) {
|
|
108
|
-
|
|
108
|
+
let exprId = el.dataset.sinthFullscreen;
|
|
109
109
|
if (exprId) {
|
|
110
110
|
try {
|
|
111
|
-
|
|
111
|
+
let exprFn = __X[exprId];
|
|
112
112
|
if (exprFn) {
|
|
113
113
|
if (exprFn({})) {
|
|
114
114
|
if (document.documentElement.requestFullscreen) document.documentElement.requestFullscreen();
|
package/src/core/types.ts
CHANGED
|
@@ -20,7 +20,7 @@ export enum TT {
|
|
|
20
20
|
KW_CUSTOM_EL, KW_CUSTOM,
|
|
21
21
|
KW_IF, KW_ELSE, KW_FOR, KW_IN, KW_REMOVE,
|
|
22
22
|
KW_FUNCTION, KW_RETURN,
|
|
23
|
-
OP_PLUS, OP_MINUS, OP_STAR, OP_SLASH, OP_SEMI,
|
|
23
|
+
OP_PLUS, OP_MINUS, OP_STAR, OP_SLASH, OP_MOD, OP_SEMI,
|
|
24
24
|
OP_LT, OP_GT, OP_NEQ, OP_EQEQ, OP_LTEQ, OP_GTEQ,
|
|
25
25
|
OP_ARROW,
|
|
26
26
|
OP_NOT,
|
|
@@ -36,7 +36,7 @@ export type LitNull = { kind: "null" };
|
|
|
36
36
|
export type Literal = LitStr | LitNum | LitBool | LitNull;
|
|
37
37
|
|
|
38
38
|
export type UnaryOp = "not" | "-";
|
|
39
|
-
export type BinaryOp = "+" | "-" | "*" | "/" | "==" | "!=" | "<" | ">" | "<=" | ">=" | "and" | "or";
|
|
39
|
+
export type BinaryOp = "+" | "-" | "*" | "/" | "%" | "==" | "!=" | "<" | ">" | "<=" | ">=" | "and" | "or";
|
|
40
40
|
export type AssignOp = "=" | "+=" | "-=";
|
|
41
41
|
export type PostfixOp = "++" | "--";
|
|
42
42
|
|
|
@@ -61,7 +61,7 @@ export interface TextNode { kind: "text"; value: string;
|
|
|
61
61
|
export interface CompUse { kind: "use"; name: string; attrs: Attr[]; children: Child[]; loc: Loc }
|
|
62
62
|
export interface ExprNode { kind: "expr"; expression: Expression; loc: Loc }
|
|
63
63
|
export interface AssignStmt { kind: "assign_stmt"; expression: Expression; loc: Loc }
|
|
64
|
-
export interface IfBlock { kind: "if"; condition: Expression; body: Child[]; elseBody?: Child[]; loc: Loc }
|
|
64
|
+
export interface IfBlock { kind: "if"; condition: Expression; body: Child[]; elseBody?: Child[]; persist?: boolean; loc: Loc }
|
|
65
65
|
export interface ForLoop { kind: "for"; keyVar?: string; itemVar: string; indexVar?: string; arrayVar: string; body: Child[]; loc: Loc }
|
|
66
66
|
export interface RemoveStmt { kind: "remove"; target: string; loc: Loc }
|
|
67
67
|
export interface ReturnStmt { kind: "return"; expression?: Expression; loc: Loc }
|
|
@@ -126,6 +126,7 @@ export interface MixedBlockEntry {
|
|
|
126
126
|
elseJS: string;
|
|
127
127
|
elseHTML: string;
|
|
128
128
|
replaceId?: string;
|
|
129
|
+
persist?: boolean;
|
|
129
130
|
}
|
|
130
131
|
|
|
131
132
|
export interface CompileCtx {
|