@yannosay/sinth 0.14.6 → 0.15.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "0.14.6",
3
+ "version": "0.15.1",
4
4
  "description": "A declarative & reactive language that compiles to pure HTML, CSS, and JavaScript.",
5
5
  "author": "Yannosay Productions",
6
6
  "license": "AGPL-3.0",
@@ -42,4 +42,4 @@
42
42
  "engines": {
43
43
  "node": ">=18.0.0"
44
44
  }
45
- }
45
+ }
@@ -626,6 +626,58 @@ private parseArrayLiteral(): Literal {
626
626
  return { kind: "if", condition, body, elseBody, persist, loc };
627
627
  }
628
628
 
629
+ private parseRawIfStatement(): string {
630
+ this.consume(TT.KW_IF);
631
+ const cond = this.parseExpression();
632
+ if (!cond) throw new SinthError("Expected condition after 'if'", this.peek().loc);
633
+ const condJS = compileExprToJS(cond);
634
+ this.consume(TT.LBRACE);
635
+ let depth = 1;
636
+ const bodyTokens: Token[] = [];
637
+ while (depth > 0 && !this.check(TT.EOF)) {
638
+ const t = this.peek();
639
+ if (t.type === TT.LBRACE) depth++;
640
+ else if (t.type === TT.RBRACE) {
641
+ depth--;
642
+ if (depth === 0) break;
643
+ }
644
+ bodyTokens.push(this.consume(t.type));
645
+ }
646
+ this.consume(TT.RBRACE);
647
+ const rawBody = bodyTokens.map(t => {
648
+ if (t.type === TT.STRING) return '"' + t.value + '"';
649
+ return t.value;
650
+ }).join(' ');
651
+ let result = `if (${condJS}) { ${rawBody} }`;
652
+ if (this.check(TT.KW_ELSE)) {
653
+ this.consume(TT.KW_ELSE);
654
+ if (this.check(TT.KW_IF)) {
655
+ const elseIfJS = this.parseRawIfStatement();
656
+ result += ` else ${elseIfJS}`;
657
+ } else {
658
+ this.consume(TT.LBRACE);
659
+ depth = 1;
660
+ const elseTokens: Token[] = [];
661
+ while (depth > 0 && !this.check(TT.EOF)) {
662
+ const t = this.peek();
663
+ if (t.type === TT.LBRACE) depth++;
664
+ else if (t.type === TT.RBRACE) {
665
+ depth--;
666
+ if (depth === 0) break;
667
+ }
668
+ elseTokens.push(this.consume(t.type));
669
+ }
670
+ this.consume(TT.RBRACE);
671
+ const elseBody = elseTokens.map(t => {
672
+ if (t.type === TT.STRING) return '"' + t.value + '"';
673
+ return t.value;
674
+ }).join(' ');
675
+ result += ` else { ${elseBody} }`;
676
+ }
677
+ }
678
+ return result;
679
+ }
680
+
629
681
  private parseUnifiedBody(): Child[] {
630
682
  const children: Child[] = [];
631
683
 
@@ -892,8 +944,8 @@ private parseArrayLiteral(): Literal {
892
944
  while (this.check(TT.OP_SEMI)) {
893
945
  this.consume(TT.OP_SEMI);
894
946
  if (this.check(TT.KW_IF)) {
895
- const ifBlock = this.parseIfBlock();
896
- actions.push({ kind: "if", js: compileIfToJS(ifBlock) });
947
+ const jsIf = this.parseRawIfStatement();
948
+ actions.push({ kind: "if", js: jsIf });
897
949
  } else {
898
950
  const next = this.parseExpression();
899
951
  if (next) actions.push({ kind: "expr", expr: next } as any);
@@ -35,6 +35,7 @@ export function buildRenderBody(opts: {
35
35
  __el.innerHTML = ${JSON.stringify(mb.elseHTML)};
36
36
  }
37
37
  __el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
38
+ __el.querySelectorAll('.sinth-expr').forEach(function(el) { el.classList.remove('sinth-expr'); });
38
39
  __el.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);
39
40
  ${needsDelay ? `__el.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
40
41
  __el.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });` : ""}
@@ -57,10 +58,10 @@ export function buildRenderBody(opts: {
57
58
  try { if (document.activeElement !== el) { el.value = Function('"use strict"; return (' + el.dataset.sinthValue + ')')(); } } catch(e) {}
58
59
  });\n`;
59
60
  renderBody += ` document.querySelectorAll('[data-sinth-step]').forEach(function(el) {
60
- try { el.step = window[el.dataset.sinthStep] || 1; } catch(e) {}
61
+ try { el.step = Function('"use strict"; return (' + el.dataset.sinthStep + ')')() || 1; } catch(e) {}
61
62
  });\n`;
62
63
  renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
63
- try { el.checked = !!window[el.dataset.sinthChecked]; } catch(e) {}
64
+ try { el.checked = !!Function('"use strict"; return (' + el.dataset.sinthChecked + ')')(); } catch(e) {}
64
65
  });\n`;
65
66
  if (bodyHTML.includes("data-sinth-checked-expr")) {
66
67
  renderBody += ` document.querySelectorAll('[data-sinth-checked-expr]').forEach(function(el) {