@yannosay/sinth 0.3.1 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/sinth.ts +64 -62
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
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
@@ -2133,99 +2133,101 @@ for (const s of pageScripts) {
2133
2133
  ${file.varDecls.map(v => {
2134
2134
  if (!v.value) return `var ${v.name};`;
2135
2135
  const val = litToString(v.value);
2136
-
2137
2136
  if (v.varType !== "str[]" && val.startsWith("__VAR__")) {
2138
2137
  const refName = val.slice(7);
2139
2138
  return `var ${v.name} = ${refName};`;
2140
2139
  }
2141
-
2142
2140
  if (val.startsWith("__ARR__")) {
2143
2141
  return `var ${v.name} = ${val.slice(7)};`;
2144
2142
  }
2145
-
2146
2143
  if (v.varType === "str") return `var ${v.name} = ${JSON.stringify(val)};`;
2147
2144
  return `var ${v.name} = ${val};`;
2148
2145
  }).join("\n")}
2149
-
2146
+ ${(bodyHTML.includes('data-sinth-if') || bodyHTML.includes('data-sinth-for') || bodyHTML.includes('data-sinth-delay') || bodyHTML.includes('sinth-expr')) ? `
2150
2147
  function sinthRender() {
2151
2148
  const scrollX = window.scrollX;
2152
2149
  const scrollY = window.scrollY;
2153
-
2154
- function processTemplates(root) {
2150
+ ${(bodyHTML.includes('data-sinth-if') || bodyHTML.includes('data-sinth-for')) ? `
2151
+ function processTemplates(root) {${bodyHTML.includes('data-sinth-if') ? `
2155
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
+ }
2156
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 + '"]');
2157
2160
  if (cond) {
2158
- const frag = document.createRange().createContextualFragment(t.innerHTML);
2159
- t.replaceWith(frag);
2160
- // Update expressions in the newly inserted fragment
2161
- root.querySelectorAll('.sinth-expr').forEach(el => {
2162
- try { el.textContent = eval(el.dataset.expr); } catch(e) {}
2163
- });
2164
- processTemplates(root);
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
+ }
2165
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
2166
2185
  const elseT = t.nextElementSibling;
2167
2186
  if (elseT && elseT.matches && elseT.matches('template[data-sinth-else]')) {
2168
- const frag = document.createRange().createContextualFragment(elseT.innerHTML);
2169
- elseT.replaceWith(frag);
2170
- root.querySelectorAll('.sinth-expr').forEach(el => {
2171
- try { el.textContent = eval(el.dataset.expr); } catch(e) {}
2172
- });
2173
- processTemplates(root);
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
+ }
2174
2203
  }
2175
2204
  }
2176
- });
2177
- root.querySelectorAll('template[data-sinth-for]').forEach(t => {
2178
- const arr = eval(t.dataset.sinthFor);
2179
- if (arr && Array.isArray(arr)) {
2180
- const itemName = t.dataset.sinthItem;
2181
- const frag = document.createDocumentFragment();
2182
- arr.forEach(item => {
2183
- let itemHTML = t.innerHTML;
2184
- const regex = new RegExp('<span class="sinth-expr" data-expr="' + itemName + '"></span>', 'g');
2185
- itemHTML = itemHTML.replace(regex, '<span class="sinth-expr" data-expr="' + itemName + '">' + item + '</span>');
2186
- frag.append(document.createRange().createContextualFragment(itemHTML));
2187
- });
2188
- t.replaceWith(frag);
2189
- // Update expressions after for-loop expansion
2190
- root.querySelectorAll('.sinth-expr').forEach(el => {
2191
- try { el.textContent = eval(el.dataset.expr); } catch(e) {}
2192
- });
2193
- processTemplates(root);
2194
- }
2195
- });
2205
+ });` : ''}
2196
2206
  }
2197
- processTemplates(document.body);
2198
-
2199
- // Show delayed elements after DOM settles
2200
- setTimeout(() => {
2201
- // Handle static delays
2207
+ processTemplates(document.body);` : ''}${bodyHTML.includes('data-sinth-delay') ? `
2208
+ setTimeout(() => {
2202
2209
  document.querySelectorAll('[data-sinth-delay]').forEach(el => {
2203
- const delayMs = parseInt(el.dataset.sinthDelay) || 0;
2204
- if (delayMs > 0) {
2205
- setTimeout(() => { el.style.display = ''; }, delayMs);
2206
- }
2210
+ const delayMs = parseInt(el.dataset.sinthDelay) || 0;
2211
+ if (delayMs > 0) {
2212
+ setTimeout(() => { el.style.display = ''; }, delayMs);
2213
+ }
2207
2214
  });
2208
- // Handle expression-based delays (variables)
2209
2215
  document.querySelectorAll('[data-sinth-delay-expr]').forEach(el => {
2210
- try {
2211
- const delayMs = parseInt(eval(el.dataset.sinthDelayExpr)) || 0;
2212
- if (delayMs > 0) {
2213
- setTimeout(() => { el.style.display = ''; }, delayMs);
2214
- }
2215
- } catch(e) {}
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) {}
2216
2222
  });
2217
- }, 0);
2218
-
2219
- // Update any remaining expression spans (from initial render)
2223
+ }, 0);` : ''}${bodyHTML.includes('sinth-expr') ? `
2220
2224
  document.querySelectorAll('.sinth-expr').forEach(el => {
2221
2225
  try { el.textContent = eval(el.dataset.expr); } catch(e) {}
2222
- });
2223
-
2226
+ });` : ''}
2224
2227
  window.scrollTo(scrollX, scrollY);
2225
2228
  }
2226
2229
 
2227
- // Initial render
2228
- sinthRender();
2230
+ sinthRender();` : ''}
2229
2231
  </script>`,
2230
2232
  scriptTags.join("\n"),
2231
2233
  "</body>",