@yannosay/sinth 0.2.0 → 0.3.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/sinth.ts +71 -52
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
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",
package/src/sinth.ts CHANGED
@@ -733,6 +733,8 @@ export class Parser {
733
733
  // += (only if next-next token is =)
734
734
  const nextTok = this.tokens[this.pos + 1];
735
735
  if (opTok.type === TT.OP_PLUS && nextTok && nextTok.type === TT.EQUALS) {
736
+ this.consume(TT.OP_PLUS); // consume '+'
737
+ this.consume(TT.EQUALS); // consume '='
736
738
  const right = this.parseExpression();
737
739
  if (!right) throw new SinthError("Expected expression after +=", this.peek().loc);
738
740
  return { kind: "assign", target: name, op: "+=", right };
@@ -802,7 +804,9 @@ export class Parser {
802
804
  this.consume(TT.LPAREN);
803
805
  while (!this.check(TT.RPAREN)) {
804
806
  const ploc = this.peek().loc;
805
- const pname = this.consume(TT.IDENT).value;
807
+ // allow keywords as parameter names (e.g., "for" in custom-element declarations)
808
+ const nameTok = this.check(TT.IDENT) ? this.consume(TT.IDENT) : this.consume(this.peek().type);
809
+ const pname = nameTok.value;
806
810
  let defaultVal: Literal | undefined;
807
811
  if (this.check(TT.EQUALS)) { this.consume(TT.EQUALS); defaultVal = this.parseLiteral(); }
808
812
  params.push({ name: pname, defaultVal, loc: ploc });
@@ -834,7 +838,7 @@ export class Parser {
834
838
 
835
839
  // ── Component usages ──────────────────────────────────────
836
840
 
837
- private parseCompUse(): CompUse {
841
+ private parseCompUse(): CompUse {
838
842
  const loc = this.peek().loc;
839
843
  const name = this.consume(TT.IDENT).value;
840
844
  const attrs: Attr[] = [];
@@ -849,18 +853,23 @@ export class Parser {
849
853
  this.consume(TT.COLON);
850
854
  // For event attributes, try parsing the rest as an expression
851
855
  if (this.isEventAttribute(aname)) {
852
- const startTokens = this.pos;
853
- let expr = this.parseExpression();
854
- if (expr && !this.check(TT.COMMA) && !this.check(TT.RPAREN) && !this.check(TT.EOF)) {
855
- // If there are more tokens, it's probably not a simple expression
856
- this.pos = startTokens;
857
- expr = null;
858
- }
859
- if (expr) {
860
- value = { kind: "str", value: "__EXPR__" + JSON.stringify(expr) };
861
- } else {
862
- this.pos = startTokens;
856
+ // If the value is a quoted string, use it directly as handler code
857
+ if (this.check(TT.STRING)) {
863
858
  value = this.parseLiteral();
859
+ } else {
860
+ const startTokens = this.pos;
861
+ let expr = this.parseExpression();
862
+ if (expr && !this.check(TT.COMMA) && !this.check(TT.RPAREN) && !this.check(TT.EOF)) {
863
+ // If there are more tokens, it's probably not a simple expression
864
+ this.pos = startTokens;
865
+ expr = null;
866
+ }
867
+ if (expr) {
868
+ value = { kind: "str", value: "__EXPR__" + JSON.stringify(expr) };
869
+ } else {
870
+ this.pos = startTokens;
871
+ value = this.parseLiteral();
872
+ }
864
873
  }
865
874
  } else {
866
875
  value = this.parseLiteral();
@@ -1592,6 +1601,13 @@ function renderAttr(attr: Attr, paramMap: Map<string, string>): string {
1592
1601
  if (value === null) return name;
1593
1602
  if (value.kind === "null") return "";
1594
1603
  if (value.kind === "bool") return value.value ? name : "";
1604
+
1605
+ // --- delay handling (must be before generic number/string cases) ---
1606
+ if (name === "delay") {
1607
+ const delayVal = value.kind === "num" ? String(value.value) : litToString(value);
1608
+ return `data-sinth-delay="${escAttr(delayVal)}" style="display:none"`;
1609
+ }
1610
+
1595
1611
  if (value.kind === "num") return `${name}="${value.value}"`;
1596
1612
 
1597
1613
  let raw = value.value;
@@ -1604,20 +1620,21 @@ function renderAttr(attr: Attr, paramMap: Map<string, string>): string {
1604
1620
  const jsExpr = compileExprToJS(expr);
1605
1621
  const ev = eventAttrName(name);
1606
1622
  if (ev) {
1607
- return `${ev}="(function(){ ${jsExpr}; })()"`;
1623
+ return `${ev}="(function(){ ${jsExpr.replace(/"/g, '&quot;')}; })()"`;
1608
1624
  }
1609
1625
  return `${name}="${escAttr(jsExpr)}"`;
1610
1626
  } catch {
1611
1627
  // Fall through to normal handling
1612
1628
  }
1613
- }
1614
-
1629
+ }
1630
+
1615
1631
  raw = interpolateAttr(raw, paramMap);
1616
1632
 
1617
1633
  const ev = eventAttrName(name);
1618
1634
  if (ev) {
1619
- const call = raw.includes("(") ? raw : `${raw}(event)`;
1620
- return `${ev}="${escAttr(call)}"`;
1635
+ // Always append sinthRender() to event handlers
1636
+ const handler = raw.includes("(") ? raw : raw;
1637
+ return `${ev}="${escAttr(handler)};sinthRender()"`;
1621
1638
  }
1622
1639
  return `${name}="${escAttr(raw)}"`;
1623
1640
  }
@@ -2076,11 +2093,13 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
2076
2093
  if (componentScripts.length > 0) {
2077
2094
  scriptTags.push(`<script>\n${componentScripts.join("\n\n")}\n</script>`);
2078
2095
  }
2079
- for (const s of pageScripts) {
2096
+ for (const s of pageScripts) {
2080
2097
  const extra = Object.entries(s.attrs)
2081
2098
  .map(([k, v]) => v === "true" ? k : `${k}="${escAttr(v)}"`)
2082
2099
  .join(" ");
2083
- scriptTags.push(`<script${extra ? " " + extra : ""}>\n${s.raw}\n</script>`);
2100
+ // Convert let/const to var for global accessibility
2101
+ const globalRaw = s.raw.replace(/\b(let|const)\b/g, 'var');
2102
+ scriptTags.push(`<script${extra ? " " + extra : ""}>\n${globalRaw}\n</script>`);
2084
2103
  }
2085
2104
 
2086
2105
  const html = [
@@ -2090,53 +2109,49 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
2090
2109
  `<body data-s="${hash}">`,
2091
2110
  bodyHTML,
2092
2111
  `<script>
2093
- // v0.2.0: Sinth variable declarations
2112
+ // v0.2.0: Sinth variable declarations (global scope)
2094
2113
  ${file.varDecls.map(v => {
2095
- if (!v.value) return `let ${v.name};`; // uninitialized
2114
+ if (!v.value) return `var ${v.name};`;
2096
2115
  const val = litToString(v.value);
2097
2116
 
2098
- // Variable reference: __VAR__count → count
2099
2117
  if (v.varType !== "str[]" && val.startsWith("__VAR__")) {
2100
2118
  const refName = val.slice(7);
2101
- return `let ${v.name} = ${refName};`;
2119
+ return `var ${v.name} = ${refName};`;
2102
2120
  }
2103
2121
 
2104
- // Array literal: __ARR__["a","b"]
2105
2122
  if (val.startsWith("__ARR__")) {
2106
- return `let ${v.name} = ${val.slice(7)};`;
2123
+ return `var ${v.name} = ${val.slice(7)};`;
2107
2124
  }
2108
2125
 
2109
- if (v.varType === "str") return `let ${v.name} = ${JSON.stringify(val)};`;
2110
- return `let ${v.name} = ${val};`;
2126
+ if (v.varType === "str") return `var ${v.name} = ${JSON.stringify(val)};`;
2127
+ return `var ${v.name} = ${val};`;
2111
2128
  }).join("\n")}
2112
2129
 
2113
- // v0.2.0: Store original page HTML for full re-render
2114
- const _sinthBodyHTML = document.body.innerHTML;
2115
-
2116
2130
  function sinthRender() {
2117
- // Save current scroll position
2118
2131
  const scrollX = window.scrollX;
2119
2132
  const scrollY = window.scrollY;
2120
2133
 
2121
- // Re-render the entire body from the original HTML
2122
- document.body.innerHTML = _sinthBodyHTML;
2123
-
2124
- // Re-process all templates recursively
2125
2134
  function processTemplates(root) {
2126
2135
  root.querySelectorAll('template[data-sinth-if]').forEach(t => {
2127
- const cond = new Function('return ' + t.dataset.sinthIf)();
2136
+ const cond = eval(t.dataset.sinthIf);
2128
2137
  if (cond) {
2129
2138
  const frag = document.createRange().createContextualFragment(t.innerHTML);
2130
2139
  t.replaceWith(frag);
2131
- processTemplates(root); // Re-process in case templates were nested
2140
+ // Update expressions in the newly inserted fragment
2141
+ root.querySelectorAll('.sinth-expr').forEach(el => {
2142
+ try { el.textContent = eval(el.dataset.expr); } catch(e) {}
2143
+ });
2144
+ processTemplates(root);
2132
2145
  } else {
2133
2146
  const elseT = t.nextElementSibling;
2134
2147
  if (elseT && elseT.matches && elseT.matches('template[data-sinth-else]')) {
2135
2148
  const frag = document.createRange().createContextualFragment(elseT.innerHTML);
2136
2149
  elseT.replaceWith(frag);
2150
+ root.querySelectorAll('.sinth-expr').forEach(el => {
2151
+ try { el.textContent = eval(el.dataset.expr); } catch(e) {}
2152
+ });
2137
2153
  processTemplates(root);
2138
2154
  }
2139
- t.remove();
2140
2155
  }
2141
2156
  });
2142
2157
  root.querySelectorAll('template[data-sinth-for]').forEach(t => {
@@ -2145,41 +2160,45 @@ function sinthRender() {
2145
2160
  const itemName = t.dataset.sinthItem;
2146
2161
  const frag = document.createDocumentFragment();
2147
2162
  arr.forEach(item => {
2148
- // Fill the expression spans with the actual loop value BEFORE adding to DOM
2149
2163
  let itemHTML = t.innerHTML;
2150
2164
  const regex = new RegExp('<span class="sinth-expr" data-expr="' + itemName + '"></span>', 'g');
2151
2165
  itemHTML = itemHTML.replace(regex, '<span class="sinth-expr" data-expr="' + itemName + '">' + item + '</span>');
2152
2166
  frag.append(document.createRange().createContextualFragment(itemHTML));
2153
2167
  });
2154
2168
  t.replaceWith(frag);
2169
+ // Update expressions after for-loop expansion
2170
+ root.querySelectorAll('.sinth-expr').forEach(el => {
2171
+ try { el.textContent = eval(el.dataset.expr); } catch(e) {}
2172
+ });
2155
2173
  processTemplates(root);
2156
2174
  }
2157
2175
  });
2158
2176
  }
2159
2177
  processTemplates(document.body);
2160
2178
 
2161
- // Update all expression spans
2179
+ // Show delayed elements after DOM settles
2180
+ setTimeout(() => {
2181
+ document.querySelectorAll('[data-sinth-delay]').forEach(el => {
2182
+ const delayMs = parseInt(el.dataset.sinthDelay) || 0;
2183
+ if (delayMs > 0) {
2184
+ setTimeout(() => { el.style.display = ''; }, delayMs);
2185
+ }
2186
+ });
2187
+ }, 0);
2188
+
2189
+ // Update any remaining expression spans (from initial render)
2162
2190
  document.querySelectorAll('.sinth-expr').forEach(el => {
2163
2191
  try { el.textContent = eval(el.dataset.expr); } catch(e) {}
2164
2192
  });
2165
2193
 
2166
- // Update onclick handlers
2167
- document.querySelectorAll('[onclick]').forEach(el => {
2168
- const oc = el.getAttribute('onclick');
2169
- if (oc && oc.startsWith('(function(){') && !oc.includes('sinthRender()')) {
2170
- const inner = oc.slice(12, -4);
2171
- el.setAttribute('onclick', '(function(){' + inner + ';sinthRender();})()');
2172
- }
2173
- });
2174
-
2175
- // Restore scroll
2176
2194
  window.scrollTo(scrollX, scrollY);
2177
2195
  }
2178
2196
 
2179
2197
  // Initial render
2180
2198
  sinthRender();
2181
2199
  </script>`,
2182
- scriptTags.join("\n"), "</body>",
2200
+ scriptTags.join("\n"),
2201
+ "</body>",
2183
2202
  "</html>",
2184
2203
  ].join("\n");
2185
2204