@yannosay/sinth 1.12.3 → 1.12.4

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.
@@ -52,7 +52,7 @@ function compileFile(filePath, opts) {
52
52
  const file = (0, resolver_1.parseFile)(absPath);
53
53
  const cfg = { projectRoot: opts.projectRoot, libraryPaths: opts.libraryPaths };
54
54
  const { allDefs, customEls, cssLinks, jsLinks } = (0, resolver_1.resolveImports)(file, cfg);
55
- const hash = (0, utils_1.fnv1a)(absPath);
55
+ const hash = "_" + (0, utils_1.fnv1a)(absPath);
56
56
  const allVarDecls = file.varDecls;
57
57
  const functionDefs = file.functions;
58
58
  const fnLocalVarNames = new Set();
@@ -104,10 +104,10 @@ function inlineFunctionBody(fnDef, args, ctx) {
104
104
  for (const c of fnDef.body) {
105
105
  const substituted = subChild(c);
106
106
  if (substituted.kind === "assign_stmt") {
107
- statements.push((0, expr_1.compileExprToJS)(substituted.expression, ctx.loopVars, ctx.namespace, ctx.declaredVars) + ";");
107
+ statements.push((0, expr_1.compileExprToJS)(substituted.expression, ctx.loopVars, ctx.namespace, ctx.declaredVars));
108
108
  }
109
109
  else if (substituted.kind === "expr") {
110
- statements.push((0, expr_1.compileExprToJS)(substituted.expression, ctx.loopVars, ctx.namespace, ctx.declaredVars) + ";");
110
+ statements.push((0, expr_1.compileExprToJS)(substituted.expression, ctx.loopVars, ctx.namespace, ctx.declaredVars));
111
111
  }
112
112
  else if (substituted.kind === "if") {
113
113
  statements.push((0, expr_1.compileIfToJS)(substituted, ctx.loopVars, ctx.namespace, ctx.declaredVars));
@@ -127,13 +127,13 @@ function inlineFunctionBody(fnDef, args, ctx) {
127
127
  const bodyStmts = fl.body.map((bc) => {
128
128
  const s = subChild(bc);
129
129
  if (s.kind === "assign_stmt")
130
- return (0, expr_1.compileExprToJS)(s.expression, innerLoopVars, ctx.namespace, ctx.declaredVars) + ";";
130
+ return (0, expr_1.compileExprToJS)(s.expression, innerLoopVars, ctx.namespace, ctx.declaredVars);
131
131
  if (s.kind === "if")
132
132
  return (0, expr_1.compileIfToJS)(s, innerLoopVars, ctx.namespace, ctx.declaredVars);
133
133
  if (s.kind === "expr")
134
- return (0, expr_1.compileExprToJS)(s.expression, innerLoopVars, ctx.namespace, ctx.declaredVars) + ";";
134
+ return (0, expr_1.compileExprToJS)(s.expression, innerLoopVars, ctx.namespace, ctx.declaredVars);
135
135
  return "";
136
- }).filter(Boolean).join(" ");
136
+ }).filter(Boolean).join("; ");
137
137
  const nsArrayVar = (ctx.namespace && ctx.declaredVars && ctx.declaredVars.has(fl.arrayVar)) ? ctx.namespace + "_" + fl.arrayVar : fl.arrayVar;
138
138
  if (fl.indexVar) {
139
139
  const alreadyDeclared = fnDef.body.some((bc) => bc.kind === "var" && bc.name === fl.indexVar);
@@ -194,7 +194,7 @@ function inlineFunctionBody(fnDef, args, ctx) {
194
194
  }
195
195
  if (statements.length === 0)
196
196
  return null;
197
- return statements.join(" ");
197
+ return statements.join("; ");
198
198
  }
199
199
  function resolveBuiltinTag(name, attrs) {
200
200
  if (name === "Heading") {
@@ -353,7 +353,13 @@ function renderAttr(attr, paramMap, ctx) {
353
353
  return `${name}="${value.value}"`;
354
354
  let raw = value.value;
355
355
  if (raw.startsWith("__ACTION_JS__")) {
356
- const js = raw.substring("__ACTION_JS__".length);
356
+ let js = raw.substring("__ACTION_JS__".length).replace(/;\s*$/, '');
357
+ if (ctx.namespace && ctx.declaredVars) {
358
+ for (const v of ctx.declaredVars) {
359
+ const regex = new RegExp(`\\b${v}\\b`, 'g');
360
+ js = js.replace(regex, ctx.namespace + '_' + v);
361
+ }
362
+ }
357
363
  const ev = eventAttrName(name);
358
364
  if (ev)
359
365
  return `${ev}="(function(){ ${js.replace(/"/g, """)}; ${renderFn}(); })()"`;
@@ -363,7 +369,14 @@ function renderAttr(attr, paramMap, ctx) {
363
369
  const exprJson = raw.substring("__MULTI_EXPR__".length);
364
370
  try {
365
371
  const exprs = JSON.parse(exprJson);
366
- const jsExprs = exprs.map(e => (0, expr_1.compileExprToJS)(e, undefined, ctx.namespace, ctx.declaredVars)).join("; ");
372
+ let jsExprs = exprs.map(e => (0, expr_1.compileExprToJS)(e, undefined, ctx.namespace, ctx.declaredVars)).join("; ");
373
+ jsExprs = jsExprs.replace(/;\s*$/, '');
374
+ if (ctx.namespace && ctx.declaredVars) {
375
+ for (const v of ctx.declaredVars) {
376
+ const regex = new RegExp(`\\b${v}\\b`, 'g');
377
+ jsExprs = jsExprs.replace(regex, ctx.namespace + '_' + v);
378
+ }
379
+ }
367
380
  const ev = eventAttrName(name);
368
381
  let bumps = "";
369
382
  if (ctx.diffingEnabled) {
@@ -392,11 +405,18 @@ function renderAttr(attr, paramMap, ctx) {
392
405
  if (fnDef) {
393
406
  const inlinedJS = inlineFunctionBody(fnDef, expr.args ?? [], ctx);
394
407
  if (inlinedJS) {
395
- return `${ev}="(function(){ ${inlinedJS.replace(/"/g, """)}; ${renderFn}(); })()"`;
408
+ const handlerJS = inlinedJS.replace(/;\s*$/, '');
409
+ return `${ev}="(function(){ ${handlerJS.replace(/"/g, """)}; ${renderFn}(); })()"`;
396
410
  }
397
411
  }
398
412
  }
399
413
  let jsExpr = (0, expr_1.compileExprToJS)(expr, ctx?.loopVars, ctx.namespace, ctx.declaredVars);
414
+ if (ctx.namespace && ctx.declaredVars && !ctx.scopePrefix) {
415
+ for (const v of ctx.declaredVars) {
416
+ const regex = new RegExp(`\\b${v}\\b`, 'g');
417
+ jsExpr = jsExpr.replace(regex, ctx.namespace + '_' + v);
418
+ }
419
+ }
400
420
  if (ctx.scopePrefix && ctx.scopeVar) {
401
421
  if (expr.kind === "variable" && expr.name && ctx.varDecls?.some(v => v.name === expr.name)) {
402
422
  jsExpr = `${ctx.scopePrefix}${expr.name}`;
@@ -414,7 +434,7 @@ function renderAttr(attr, paramMap, ctx) {
414
434
  bump = `;window._bump&&window._bump('${expr.target.replace(/'/g, "\\'")}')`;
415
435
  }
416
436
  if (ev) {
417
- const handlerBody = `${jsExpr}${bump}; ${renderCall}`;
437
+ const handlerBody = `${jsExpr.replace(/;\s*$/, '')}${bump}; ${renderCall}`;
418
438
  if (ctx.scopePrefix) {
419
439
  return emitCEAction(ctx, ev, handlerBody);
420
440
  }
@@ -1203,14 +1223,15 @@ function buildRuntime(opts) {
1203
1223
  const ns = opts.namespace ? "_" + opts.namespace : "";
1204
1224
  const renderFn = "sinthRender" + ns;
1205
1225
  const needsMixed = mixedBlocks.length > 0;
1206
- const needsExpr = bodyHTML.includes("sinth-expr") || needsMixed;
1226
+ const needsDynamicAttrs = bodyHTML.includes("data-sinth-style") || bodyHTML.includes("data-sinth-value") || bodyHTML.includes("data-sinth-step") || bodyHTML.includes("data-sinth-checked");
1227
+ const needsExpr = bodyHTML.includes("sinth-expr") || needsMixed || needsDynamicAttrs;
1207
1228
  const needsIf = bodyHTML.includes("data-sinth-if") || needsMixed;
1208
1229
  const needsFor = bodyHTML.includes("data-sinth-for") || bodyHTML.includes("data-sinth-for-expr");
1209
1230
  const needsDelay = bodyHTML.includes("data-sinth-delay") || bodyHTML.includes("data-sinth-delay-expr-id") || mixedBlocks.some(mb => mb.ifHTML.includes("data-sinth-delay") || mb.ifHTML.includes("data-sinth-delay-expr-id") || mb.elseHTML.includes("data-sinth-delay") || mb.elseHTML.includes("data-sinth-delay-expr-id"));
1210
1231
  const needsLogic = logicBlocks.length > 0;
1211
1232
  const needsFullscreen = bodyHTML.includes("data-sinth-fullscreen");
1212
1233
  const needsFullscreenSync = bodyHTML.includes("data-sinth-fullscreen-sync");
1213
- const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic || bodyHTML.includes("data-sinth-hide") || needsFullscreen || needsFullscreenSync || needsDelay;
1234
+ const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic || bodyHTML.includes("data-sinth-hide") || needsFullscreen || needsFullscreenSync || needsDelay || needsDynamicAttrs;
1214
1235
  const varLines = varDecls.map(v => {
1215
1236
  const vn = ns ? ns.slice(1) + "_" + v.name : v.name;
1216
1237
  if (!v.value) {
@@ -1270,7 +1291,7 @@ function buildRuntime(opts) {
1270
1291
  const memoVarDecls = [...memoVars].map(m => `var ${m};`).join("\n");
1271
1292
  if (!needsRender) {
1272
1293
  const out = [varLines, memoVarDecls].filter(Boolean).join("\n");
1273
- return out ? `// Sinth compiled runtime\n${out}` : "";
1294
+ return out ? `// Sinth Compiled Runtime\n${out}` : "";
1274
1295
  }
1275
1296
  const helpers = (0, helpers_1.generateHelpers)({ needsExpr, needsIf, needsFor, needsDelay, needsMixed, ns });
1276
1297
  const exprVarUpdates = varDecls
@@ -1349,11 +1370,11 @@ ${memoVarDecls}
1349
1370
  ${varLines}
1350
1371
  ${renderFunc}`;
1351
1372
  if (opts.sharedRuntime && helpers.trim()) {
1352
- const sharedCode = `// Sinth shared runtime
1373
+ const sharedCode = `// Sinth Shared Runtime
1353
1374
  ${helpers}`;
1354
1375
  return { page: pageCode, shared: sharedCode };
1355
1376
  }
1356
- return `// Sinth compiled runtime
1377
+ return `// Sinth Compiled Runtime
1357
1378
  ${forDataJS}
1358
1379
  ${functionsJS ? functionsJS + "\n" : ""}${helpers}
1359
1380
  ${exprArrayJS}
@@ -40,6 +40,14 @@ function sinthReplaceInsert(t, anchor, ifId, replaceId) {
40
40
  _rpParent = _rp.parentNode;
41
41
  _rpNext = _rp.nextSibling;
42
42
  _rp.parentNode.removeChild(_rp);
43
+ let src = _rp.previousSibling;
44
+ while (src && src !== _rp.parentNode.firstChild) {
45
+ if (src.dataset && src.dataset.sinthIfAnchor) {
46
+ anchor._sinthReplaceSourceIfId = src.dataset.sinthIfAnchor;
47
+ break;
48
+ }
49
+ src = src.previousSibling;
50
+ }
43
51
  anchor._sinthReplaced = _rp;
44
52
  anchor._sinthReplacedParent = _rpParent;
45
53
  anchor._sinthReplacedNext = _rpNext;
@@ -117,7 +125,9 @@ function sinthDelayExpr(el, _ctx) {
117
125
  helpers += `
118
126
  function sinthIfBlock(t) {
119
127
  let ifId = t.dataset.sinthIfId;
120
- let anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
128
+ let replaceId = t.dataset.sinthIfReplace || null;
129
+ let anchorKey = replaceId ? '__replace__' + replaceId : ifId;
130
+ let anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + anchorKey + '"]');
121
131
  let condFn = ${X}[t.dataset.sinthIfExpr];
122
132
  let cond = condFn ? condFn() : false;
123
133
  if (cond) {
@@ -133,10 +143,10 @@ function sinthIfBlock(t) {
133
143
  if (t.dataset.sinthIfDelayHide === 'false' && t.dataset.sinthIfDelay && _hasContent) {
134
144
  let dms = parseInt(t.dataset.sinthIfDelay) || 0;
135
145
  setTimeout(function() {
136
- sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
146
+ sinthReplaceInsert(t, anchor, anchorKey, replaceId);
137
147
  }, dms);
138
148
  } else {
139
- anchor = sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
149
+ anchor = sinthReplaceInsert(t, anchor, anchorKey, replaceId);
140
150
  }
141
151
  } else {
142
152
  let runElse = function() {
@@ -150,6 +160,7 @@ function sinthIfBlock(t) {
150
160
  }
151
161
  if (anchor) {
152
162
  if (anchor._sinthReplaced) {
163
+ var restored = anchor._sinthReplaced;
153
164
  let insFirst = anchor._sinthInsertedFirst;
154
165
  let insLast = anchor._sinthInsertedLast;
155
166
  let rpParent = anchor._sinthReplacedParent;
@@ -164,22 +175,27 @@ function sinthIfBlock(t) {
164
175
  if (insLast) insLast.remove();
165
176
  }
166
177
  if (rpParent && rpNext) {
167
- rpParent.insertBefore(anchor._sinthReplaced, rpNext);
178
+ rpParent.insertBefore(restored, rpNext);
168
179
  } else if (rpParent) {
169
- rpParent.appendChild(anchor._sinthReplaced);
180
+ rpParent.appendChild(restored);
170
181
  }
171
- if (anchor._sinthReplaced) {
172
- anchor._sinthReplaced.querySelectorAll('.sinth-expr').forEach(sinthExpr);
173
- if (anchor._sinthReplaced.dataset.sinthDelay) {
174
- delete anchor._sinthReplaced.dataset.sinthDelayDone;
175
- sinthDelay(anchor._sinthReplaced);
182
+ if (restored.id) {
183
+ var allDup = t.parentNode.querySelectorAll('[id="' + restored.id + '"]');
184
+ for (var di = 0; di < allDup.length; di++) {
185
+ if (allDup[di] !== restored) allDup[di].remove();
176
186
  }
177
- anchor._sinthReplaced = null;
178
- anchor._sinthReplacedParent = null;
179
- anchor._sinthReplacedNext = null;
180
- anchor._sinthInsertedFirst = null;
181
- anchor._sinthInsertedLast = null;
182
187
  }
188
+ restored.querySelectorAll('.sinth-expr').forEach(sinthExpr);
189
+ if (restored.dataset.sinthDelay) {
190
+ delete restored.dataset.sinthDelayDone;
191
+ sinthDelay(restored);
192
+ }
193
+ anchor._sinthReplaced = null;
194
+ anchor._sinthReplacedParent = null;
195
+ anchor._sinthReplacedNext = null;
196
+ anchor._sinthInsertedFirst = null;
197
+ anchor._sinthInsertedLast = null;
198
+ anchor._sinthReplaceSourceIfId = null;
183
199
  } else {
184
200
  let cur2 = anchor.nextSibling;
185
201
  while (cur2 && cur2 !== t) { let nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "1.12.3",
3
+ "version": "1.12.4",
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",