@yannosay/sinth 0.3.0 → 0.3.2
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 +86 -54
package/package.json
CHANGED
package/src/sinth.ts
CHANGED
|
@@ -872,10 +872,21 @@ private parseCompUse(): CompUse {
|
|
|
872
872
|
}
|
|
873
873
|
}
|
|
874
874
|
} else {
|
|
875
|
-
|
|
875
|
+
// Try literal first, then variable reference
|
|
876
|
+
if (this.check(TT.STRING) || this.check(TT.NUMBER) ||
|
|
877
|
+
this.check(TT.BOOL_TRUE) || this.check(TT.BOOL_FALSE) ||
|
|
878
|
+
this.check(TT.NULL_LIT) || this.check(TT.LBRACKET)) {
|
|
879
|
+
value = this.parseLiteral();
|
|
880
|
+
} else if (this.check(TT.IDENT)) {
|
|
881
|
+
const varName = this.consume(TT.IDENT).value;
|
|
882
|
+
value = { kind: "str", value: varName };
|
|
883
|
+
} else {
|
|
884
|
+
value = this.parseLiteral();
|
|
885
|
+
}
|
|
876
886
|
}
|
|
877
887
|
}
|
|
878
888
|
attrs.push({ name: aname, value, loc: aloc });
|
|
889
|
+
|
|
879
890
|
if (!this.check(TT.RPAREN)) this.consume(TT.COMMA);
|
|
880
891
|
}
|
|
881
892
|
this.consume(TT.RPAREN);
|
|
@@ -1602,11 +1613,20 @@ function renderAttr(attr: Attr, paramMap: Map<string, string>): string {
|
|
|
1602
1613
|
if (value.kind === "null") return "";
|
|
1603
1614
|
if (value.kind === "bool") return value.value ? name : "";
|
|
1604
1615
|
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1616
|
+
if (name === "delay") {
|
|
1617
|
+
// Support both numbers and variable references
|
|
1618
|
+
let delayVal: string;
|
|
1619
|
+
if (value.kind === "num") {
|
|
1620
|
+
delayVal = String(value.value);
|
|
1621
|
+
} else {
|
|
1622
|
+
delayVal = litToString(value);
|
|
1623
|
+
// If it looks like a variable name (not a number), treat it as a JS expression
|
|
1624
|
+
if (!/^\d+$/.test(delayVal)) {
|
|
1625
|
+
return `data-sinth-delay-expr="${escAttr(delayVal)}" style="display:none"`;
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1608
1628
|
return `data-sinth-delay="${escAttr(delayVal)}" style="display:none"`;
|
|
1609
|
-
|
|
1629
|
+
}
|
|
1610
1630
|
|
|
1611
1631
|
if (value.kind === "num") return `${name}="${value.value}"`;
|
|
1612
1632
|
|
|
@@ -2113,70 +2133,78 @@ for (const s of pageScripts) {
|
|
|
2113
2133
|
${file.varDecls.map(v => {
|
|
2114
2134
|
if (!v.value) return `var ${v.name};`;
|
|
2115
2135
|
const val = litToString(v.value);
|
|
2116
|
-
|
|
2117
2136
|
if (v.varType !== "str[]" && val.startsWith("__VAR__")) {
|
|
2118
2137
|
const refName = val.slice(7);
|
|
2119
2138
|
return `var ${v.name} = ${refName};`;
|
|
2120
2139
|
}
|
|
2121
|
-
|
|
2122
2140
|
if (val.startsWith("__ARR__")) {
|
|
2123
2141
|
return `var ${v.name} = ${val.slice(7)};`;
|
|
2124
2142
|
}
|
|
2125
|
-
|
|
2126
2143
|
if (v.varType === "str") return `var ${v.name} = ${JSON.stringify(val)};`;
|
|
2127
2144
|
return `var ${v.name} = ${val};`;
|
|
2128
2145
|
}).join("\n")}
|
|
2129
|
-
|
|
2146
|
+
${(bodyHTML.includes('data-sinth-if') || bodyHTML.includes('data-sinth-for') || bodyHTML.includes('data-sinth-delay') || bodyHTML.includes('sinth-expr')) ? `
|
|
2130
2147
|
function sinthRender() {
|
|
2131
2148
|
const scrollX = window.scrollX;
|
|
2132
2149
|
const scrollY = window.scrollY;
|
|
2133
|
-
|
|
2134
|
-
function processTemplates(root) {
|
|
2150
|
+
${(bodyHTML.includes('data-sinth-if') || bodyHTML.includes('data-sinth-for')) ? `
|
|
2151
|
+
function processTemplates(root) {${bodyHTML.includes('data-sinth-if') ? `
|
|
2135
2152
|
root.querySelectorAll('template[data-sinth-if]').forEach(t => {
|
|
2153
|
+
// Store original HTML on first pass
|
|
2154
|
+
if (!t.dataset.sinthOriginal) {
|
|
2155
|
+
t.dataset.sinthOriginal = t.innerHTML;
|
|
2156
|
+
}
|
|
2136
2157
|
const cond = eval(t.dataset.sinthIf);
|
|
2158
|
+
// Find any previously inserted content
|
|
2159
|
+
const existing = t.parentNode.querySelector('[data-sinth-if-anchor="' + t.dataset.sinthIf + '"]');
|
|
2137
2160
|
if (cond) {
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2161
|
+
if (!existing) {
|
|
2162
|
+
// Insert new content with anchor marker
|
|
2163
|
+
const anchor = document.createElement('span');
|
|
2164
|
+
anchor.style.display = 'none';
|
|
2165
|
+
anchor.dataset.sinthIfAnchor = t.dataset.sinthIf;
|
|
2166
|
+
const frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
2167
|
+
t.parentNode.insertBefore(anchor, t);
|
|
2168
|
+
t.parentNode.insertBefore(frag, t);${bodyHTML.includes('sinth-expr') ? `
|
|
2169
|
+
root.querySelectorAll('.sinth-expr').forEach(el => {
|
|
2170
|
+
try { el.textContent = eval(el.dataset.expr); } catch(e) {}
|
|
2171
|
+
});` : ''}
|
|
2172
|
+
}
|
|
2145
2173
|
} else {
|
|
2174
|
+
// Remove inserted content if condition is false
|
|
2175
|
+
if (existing) {
|
|
2176
|
+
let next = existing.nextSibling;
|
|
2177
|
+
while (next && next !== t && !(next.dataset && next.dataset.sinthIfAnchor)) {
|
|
2178
|
+
const toRemove = next;
|
|
2179
|
+
next = next.nextSibling;
|
|
2180
|
+
toRemove.remove();
|
|
2181
|
+
}
|
|
2182
|
+
existing.remove();
|
|
2183
|
+
}
|
|
2184
|
+
// Also handle else
|
|
2146
2185
|
const elseT = t.nextElementSibling;
|
|
2147
2186
|
if (elseT && elseT.matches && elseT.matches('template[data-sinth-else]')) {
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2187
|
+
if (!elseT.dataset.sinthOriginal) {
|
|
2188
|
+
elseT.dataset.sinthOriginal = elseT.innerHTML;
|
|
2189
|
+
}
|
|
2190
|
+
const elseAnchor = t.parentNode.querySelector('[data-sinth-if-anchor="else-' + t.dataset.sinthIf + '"]');
|
|
2191
|
+
const elseExisting = elseAnchor ? elseAnchor.nextSibling : null;
|
|
2192
|
+
if (!elseExisting || elseExisting === t) {
|
|
2193
|
+
const anchor = document.createElement('span');
|
|
2194
|
+
anchor.style.display = 'none';
|
|
2195
|
+
anchor.dataset.sinthIfAnchor = 'else-' + t.dataset.sinthIf;
|
|
2196
|
+
const frag = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
2197
|
+
t.parentNode.insertBefore(anchor, t);
|
|
2198
|
+
t.parentNode.insertBefore(frag, t);${bodyHTML.includes('sinth-expr') ? `
|
|
2199
|
+
root.querySelectorAll('.sinth-expr').forEach(el => {
|
|
2200
|
+
try { el.textContent = eval(el.dataset.expr); } catch(e) {}
|
|
2201
|
+
});` : ''}
|
|
2202
|
+
}
|
|
2154
2203
|
}
|
|
2155
2204
|
}
|
|
2156
|
-
})
|
|
2157
|
-
root.querySelectorAll('template[data-sinth-for]').forEach(t => {
|
|
2158
|
-
const arr = eval(t.dataset.sinthFor);
|
|
2159
|
-
if (arr && Array.isArray(arr)) {
|
|
2160
|
-
const itemName = t.dataset.sinthItem;
|
|
2161
|
-
const frag = document.createDocumentFragment();
|
|
2162
|
-
arr.forEach(item => {
|
|
2163
|
-
let itemHTML = t.innerHTML;
|
|
2164
|
-
const regex = new RegExp('<span class="sinth-expr" data-expr="' + itemName + '"></span>', 'g');
|
|
2165
|
-
itemHTML = itemHTML.replace(regex, '<span class="sinth-expr" data-expr="' + itemName + '">' + item + '</span>');
|
|
2166
|
-
frag.append(document.createRange().createContextualFragment(itemHTML));
|
|
2167
|
-
});
|
|
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
|
-
});
|
|
2173
|
-
processTemplates(root);
|
|
2174
|
-
}
|
|
2175
|
-
});
|
|
2205
|
+
});` : ''}
|
|
2176
2206
|
}
|
|
2177
|
-
processTemplates(document.body)
|
|
2178
|
-
|
|
2179
|
-
// Show delayed elements after DOM settles
|
|
2207
|
+
processTemplates(document.body);` : ''}${bodyHTML.includes('data-sinth-delay') ? `
|
|
2180
2208
|
setTimeout(() => {
|
|
2181
2209
|
document.querySelectorAll('[data-sinth-delay]').forEach(el => {
|
|
2182
2210
|
const delayMs = parseInt(el.dataset.sinthDelay) || 0;
|
|
@@ -2184,18 +2212,22 @@ function sinthRender() {
|
|
|
2184
2212
|
setTimeout(() => { el.style.display = ''; }, delayMs);
|
|
2185
2213
|
}
|
|
2186
2214
|
});
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2215
|
+
document.querySelectorAll('[data-sinth-delay-expr]').forEach(el => {
|
|
2216
|
+
try {
|
|
2217
|
+
const delayMs = parseInt(eval(el.dataset.sinthDelayExpr)) || 0;
|
|
2218
|
+
if (delayMs > 0) {
|
|
2219
|
+
setTimeout(() => { el.style.display = ''; }, delayMs);
|
|
2220
|
+
}
|
|
2221
|
+
} catch(e) {}
|
|
2222
|
+
});
|
|
2223
|
+
}, 0);` : ''}${bodyHTML.includes('sinth-expr') ? `
|
|
2190
2224
|
document.querySelectorAll('.sinth-expr').forEach(el => {
|
|
2191
2225
|
try { el.textContent = eval(el.dataset.expr); } catch(e) {}
|
|
2192
|
-
})
|
|
2193
|
-
|
|
2226
|
+
});` : ''}
|
|
2194
2227
|
window.scrollTo(scrollX, scrollY);
|
|
2195
2228
|
}
|
|
2196
2229
|
|
|
2197
|
-
|
|
2198
|
-
sinthRender();
|
|
2230
|
+
sinthRender();` : ''}
|
|
2199
2231
|
</script>`,
|
|
2200
2232
|
scriptTags.join("\n"),
|
|
2201
2233
|
"</body>",
|