@yannosay/sinth 0.2.1 → 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.
- package/package.json +1 -1
- package/src/sinth.ts +68 -51
package/package.json
CHANGED
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 };
|
|
@@ -836,7 +838,7 @@ export class Parser {
|
|
|
836
838
|
|
|
837
839
|
// ── Component usages ──────────────────────────────────────
|
|
838
840
|
|
|
839
|
-
|
|
841
|
+
private parseCompUse(): CompUse {
|
|
840
842
|
const loc = this.peek().loc;
|
|
841
843
|
const name = this.consume(TT.IDENT).value;
|
|
842
844
|
const attrs: Attr[] = [];
|
|
@@ -851,18 +853,23 @@ export class Parser {
|
|
|
851
853
|
this.consume(TT.COLON);
|
|
852
854
|
// For event attributes, try parsing the rest as an expression
|
|
853
855
|
if (this.isEventAttribute(aname)) {
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
if (expr && !this.check(TT.COMMA) && !this.check(TT.RPAREN) && !this.check(TT.EOF)) {
|
|
857
|
-
// If there are more tokens, it's probably not a simple expression
|
|
858
|
-
this.pos = startTokens;
|
|
859
|
-
expr = null;
|
|
860
|
-
}
|
|
861
|
-
if (expr) {
|
|
862
|
-
value = { kind: "str", value: "__EXPR__" + JSON.stringify(expr) };
|
|
863
|
-
} else {
|
|
864
|
-
this.pos = startTokens;
|
|
856
|
+
// If the value is a quoted string, use it directly as handler code
|
|
857
|
+
if (this.check(TT.STRING)) {
|
|
865
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
|
+
}
|
|
866
873
|
}
|
|
867
874
|
} else {
|
|
868
875
|
value = this.parseLiteral();
|
|
@@ -1594,6 +1601,13 @@ function renderAttr(attr: Attr, paramMap: Map<string, string>): string {
|
|
|
1594
1601
|
if (value === null) return name;
|
|
1595
1602
|
if (value.kind === "null") return "";
|
|
1596
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
|
+
|
|
1597
1611
|
if (value.kind === "num") return `${name}="${value.value}"`;
|
|
1598
1612
|
|
|
1599
1613
|
let raw = value.value;
|
|
@@ -1606,20 +1620,21 @@ function renderAttr(attr: Attr, paramMap: Map<string, string>): string {
|
|
|
1606
1620
|
const jsExpr = compileExprToJS(expr);
|
|
1607
1621
|
const ev = eventAttrName(name);
|
|
1608
1622
|
if (ev) {
|
|
1609
|
-
return `${ev}="(function(){ ${jsExpr}; })()"`;
|
|
1623
|
+
return `${ev}="(function(){ ${jsExpr.replace(/"/g, '"')}; })()"`;
|
|
1610
1624
|
}
|
|
1611
1625
|
return `${name}="${escAttr(jsExpr)}"`;
|
|
1612
1626
|
} catch {
|
|
1613
1627
|
// Fall through to normal handling
|
|
1614
1628
|
}
|
|
1615
|
-
}
|
|
1616
|
-
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1617
1631
|
raw = interpolateAttr(raw, paramMap);
|
|
1618
1632
|
|
|
1619
1633
|
const ev = eventAttrName(name);
|
|
1620
1634
|
if (ev) {
|
|
1621
|
-
|
|
1622
|
-
|
|
1635
|
+
// Always append sinthRender() to event handlers
|
|
1636
|
+
const handler = raw.includes("(") ? raw : raw;
|
|
1637
|
+
return `${ev}="${escAttr(handler)};sinthRender()"`;
|
|
1623
1638
|
}
|
|
1624
1639
|
return `${name}="${escAttr(raw)}"`;
|
|
1625
1640
|
}
|
|
@@ -2078,11 +2093,13 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2078
2093
|
if (componentScripts.length > 0) {
|
|
2079
2094
|
scriptTags.push(`<script>\n${componentScripts.join("\n\n")}\n</script>`);
|
|
2080
2095
|
}
|
|
2081
|
-
|
|
2096
|
+
for (const s of pageScripts) {
|
|
2082
2097
|
const extra = Object.entries(s.attrs)
|
|
2083
2098
|
.map(([k, v]) => v === "true" ? k : `${k}="${escAttr(v)}"`)
|
|
2084
2099
|
.join(" ");
|
|
2085
|
-
|
|
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>`);
|
|
2086
2103
|
}
|
|
2087
2104
|
|
|
2088
2105
|
const html = [
|
|
@@ -2092,53 +2109,49 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2092
2109
|
`<body data-s="${hash}">`,
|
|
2093
2110
|
bodyHTML,
|
|
2094
2111
|
`<script>
|
|
2095
|
-
// v0.2.0: Sinth variable declarations
|
|
2112
|
+
// v0.2.0: Sinth variable declarations (global scope)
|
|
2096
2113
|
${file.varDecls.map(v => {
|
|
2097
|
-
if (!v.value) return `
|
|
2114
|
+
if (!v.value) return `var ${v.name};`;
|
|
2098
2115
|
const val = litToString(v.value);
|
|
2099
2116
|
|
|
2100
|
-
// Variable reference: __VAR__count → count
|
|
2101
2117
|
if (v.varType !== "str[]" && val.startsWith("__VAR__")) {
|
|
2102
2118
|
const refName = val.slice(7);
|
|
2103
|
-
return `
|
|
2119
|
+
return `var ${v.name} = ${refName};`;
|
|
2104
2120
|
}
|
|
2105
2121
|
|
|
2106
|
-
// Array literal: __ARR__["a","b"]
|
|
2107
2122
|
if (val.startsWith("__ARR__")) {
|
|
2108
|
-
return `
|
|
2123
|
+
return `var ${v.name} = ${val.slice(7)};`;
|
|
2109
2124
|
}
|
|
2110
2125
|
|
|
2111
|
-
if (v.varType === "str") return `
|
|
2112
|
-
return `
|
|
2126
|
+
if (v.varType === "str") return `var ${v.name} = ${JSON.stringify(val)};`;
|
|
2127
|
+
return `var ${v.name} = ${val};`;
|
|
2113
2128
|
}).join("\n")}
|
|
2114
2129
|
|
|
2115
|
-
// v0.2.0: Store original page HTML for full re-render
|
|
2116
|
-
const _sinthBodyHTML = document.body.innerHTML;
|
|
2117
|
-
|
|
2118
2130
|
function sinthRender() {
|
|
2119
|
-
// Save current scroll position
|
|
2120
2131
|
const scrollX = window.scrollX;
|
|
2121
2132
|
const scrollY = window.scrollY;
|
|
2122
2133
|
|
|
2123
|
-
// Re-render the entire body from the original HTML
|
|
2124
|
-
document.body.innerHTML = _sinthBodyHTML;
|
|
2125
|
-
|
|
2126
|
-
// Re-process all templates recursively
|
|
2127
2134
|
function processTemplates(root) {
|
|
2128
2135
|
root.querySelectorAll('template[data-sinth-if]').forEach(t => {
|
|
2129
|
-
const cond =
|
|
2136
|
+
const cond = eval(t.dataset.sinthIf);
|
|
2130
2137
|
if (cond) {
|
|
2131
2138
|
const frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
2132
2139
|
t.replaceWith(frag);
|
|
2133
|
-
|
|
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);
|
|
2134
2145
|
} else {
|
|
2135
2146
|
const elseT = t.nextElementSibling;
|
|
2136
2147
|
if (elseT && elseT.matches && elseT.matches('template[data-sinth-else]')) {
|
|
2137
2148
|
const frag = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
2138
2149
|
elseT.replaceWith(frag);
|
|
2150
|
+
root.querySelectorAll('.sinth-expr').forEach(el => {
|
|
2151
|
+
try { el.textContent = eval(el.dataset.expr); } catch(e) {}
|
|
2152
|
+
});
|
|
2139
2153
|
processTemplates(root);
|
|
2140
2154
|
}
|
|
2141
|
-
t.remove();
|
|
2142
2155
|
}
|
|
2143
2156
|
});
|
|
2144
2157
|
root.querySelectorAll('template[data-sinth-for]').forEach(t => {
|
|
@@ -2147,41 +2160,45 @@ function sinthRender() {
|
|
|
2147
2160
|
const itemName = t.dataset.sinthItem;
|
|
2148
2161
|
const frag = document.createDocumentFragment();
|
|
2149
2162
|
arr.forEach(item => {
|
|
2150
|
-
// Fill the expression spans with the actual loop value BEFORE adding to DOM
|
|
2151
2163
|
let itemHTML = t.innerHTML;
|
|
2152
2164
|
const regex = new RegExp('<span class="sinth-expr" data-expr="' + itemName + '"></span>', 'g');
|
|
2153
2165
|
itemHTML = itemHTML.replace(regex, '<span class="sinth-expr" data-expr="' + itemName + '">' + item + '</span>');
|
|
2154
2166
|
frag.append(document.createRange().createContextualFragment(itemHTML));
|
|
2155
2167
|
});
|
|
2156
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
|
+
});
|
|
2157
2173
|
processTemplates(root);
|
|
2158
2174
|
}
|
|
2159
2175
|
});
|
|
2160
2176
|
}
|
|
2161
2177
|
processTemplates(document.body);
|
|
2162
2178
|
|
|
2163
|
-
//
|
|
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)
|
|
2164
2190
|
document.querySelectorAll('.sinth-expr').forEach(el => {
|
|
2165
2191
|
try { el.textContent = eval(el.dataset.expr); } catch(e) {}
|
|
2166
2192
|
});
|
|
2167
2193
|
|
|
2168
|
-
// Update onclick handlers
|
|
2169
|
-
document.querySelectorAll('[onclick]').forEach(el => {
|
|
2170
|
-
const oc = el.getAttribute('onclick');
|
|
2171
|
-
if (oc && oc.startsWith('(function(){') && !oc.includes('sinthRender()')) {
|
|
2172
|
-
const inner = oc.slice(12, -4);
|
|
2173
|
-
el.setAttribute('onclick', '(function(){' + inner + ';sinthRender();})()');
|
|
2174
|
-
}
|
|
2175
|
-
});
|
|
2176
|
-
|
|
2177
|
-
// Restore scroll
|
|
2178
2194
|
window.scrollTo(scrollX, scrollY);
|
|
2179
2195
|
}
|
|
2180
2196
|
|
|
2181
2197
|
// Initial render
|
|
2182
2198
|
sinthRender();
|
|
2183
2199
|
</script>`,
|
|
2184
|
-
scriptTags.join("\n"),
|
|
2200
|
+
scriptTags.join("\n"),
|
|
2201
|
+
"</body>",
|
|
2185
2202
|
"</html>",
|
|
2186
2203
|
].join("\n");
|
|
2187
2204
|
|