@yannosay/sinth 1.2.2 → 1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
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",
@@ -1071,7 +1071,7 @@ export function buildRuntime(opts: {
1071
1071
  ? `let __X = [${exprRegistry.map((js) => `function(_ctx){ return ${js}; }`).join(",")}];\n`
1072
1072
  : "";
1073
1073
 
1074
- let renderFunc = needsRender ? `var _sinthRenderPending = false;\nfunction sinthRender() {\n if (_sinthRenderPending) return;\n _sinthRenderPending = true;\n requestAnimationFrame(function() {\n _sinthRenderPending = false;\n${renderBody.replace(/^/gm, " ")}\n });\n}\nsinthRender();` : "";
1074
+ let renderFunc = needsRender ? `function sinthRender() {\n${renderBody.replace(/^/gm, " ")}\n}\nsinthRender();` : "";
1075
1075
 
1076
1076
  if (needsFullscreenSync) {
1077
1077
  renderFunc += `
@@ -1178,9 +1178,10 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
1178
1178
  const pageVars = new Set<string>();
1179
1179
  file.uses.forEach(u => collectChildVars(u, pageVars));
1180
1180
  for (const v of pageVars) {
1181
- if (!declaredVars.has(v)) {
1181
+ const rootVar = v.split('.')[0];
1182
+ if (!declaredVars.has(rootVar)) {
1182
1183
  throw new SinthError(
1183
- `Variable '${v}' is used but never declared. Add 'var str ${v}' or 'var int ${v}' before using it.`
1184
+ `Variable '${rootVar}' is used but never declared. Add 'var str ${rootVar}' or 'var int ${rootVar}' before using it.`
1184
1185
  );
1185
1186
  }
1186
1187
  }
@@ -1189,9 +1190,10 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
1189
1190
  const fnBodyVars = new Set<string>();
1190
1191
  fn.body.forEach(c => collectChildVars(c, fnBodyVars));
1191
1192
  for (const v of fnBodyVars) {
1192
- if (!fnParamNames.has(v) && !declaredVars.has(v) && !declaredFuncs.has(v)) {
1193
+ const rootVar = v.split('.')[0];
1194
+ if (!fnParamNames.has(rootVar) && !declaredVars.has(rootVar) && !declaredFuncs.has(rootVar)) {
1193
1195
  throw new SinthError(
1194
- `Variable '${v}' used in function '${fn.name}' is not declared. It must be a parameter or a page-level variable.`
1196
+ `Variable '${rootVar}' used in function '${fn.name}' is not declared. It must be a parameter or a page-level variable.`
1195
1197
  );
1196
1198
  }
1197
1199
  }
@@ -28,12 +28,33 @@ export function compileCustomElement(cel: CustomElDecl, opts: { sharedRuntime: b
28
28
 
29
29
  const bodyHTML = cel.body.map(c => renderChild(c, childCtx, new Map(), 0)).join("\n");
30
30
 
31
- let rewrittenHTML = bodyHTML;
32
- for (const name of varNames) {
33
- rewrittenHTML = rewrittenHTML.replace(new RegExp(`\\b${name}\\b`, 'g'), `host._${name}`);
31
+ function replaceVarsOutsideStrings(text: string, varNames: string[], replacement: (name: string) => string): string {
32
+ const varPattern = varNames.join('|');
33
+ const regex = new RegExp(`("[^"]*"|'[^']*'|\`[^\`]*\`)|\\b(${varPattern})\\b`, 'g');
34
+ const parts: string[] = [];
35
+ let lastIndex = 0;
36
+ let match;
37
+
38
+ while ((match = regex.exec(text)) !== null) {
39
+ parts.push(text.substring(lastIndex, match.index));
40
+ if (match[1]) {
41
+ parts.push(match[1]);
42
+ } else if (match[2]) {
43
+ parts.push(replacement(match[2]));
44
+ }
45
+ lastIndex = regex.lastIndex;
46
+ }
47
+ parts.push(text.substring(lastIndex));
48
+ return parts.join('');
34
49
  }
50
+
51
+ let rewrittenHTML = bodyHTML;
52
+ rewrittenHTML = rewrittenHTML.replace(/onclick="([^"]+)"/g, (match, content) => {
53
+ const replaced = replaceVarsOutsideStrings(content, varNames, (name) => `host._${name}`);
54
+ return `data-sinth-click="${replaced}"`;
55
+ });
56
+ rewrittenHTML = replaceVarsOutsideStrings(rewrittenHTML, varNames, (name) => `host._${name}`);
35
57
  rewrittenHTML = rewrittenHTML.replace(/sinthRender\(\)/g, 'host._render()');
36
- rewrittenHTML = rewrittenHTML.replace(/onclick="([^"]+)"/g, 'data-sinth-click="$1"');
37
58
 
38
59
  const escapedHTML = rewrittenHTML.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$/g, '\\$');
39
60
 
@@ -42,11 +63,7 @@ export function compileCustomElement(cel: CustomElDecl, opts: { sharedRuntime: b
42
63
 
43
64
  const exprArrayJS = childCtx.exprRegistry.length > 0
44
65
  ? `let __X = [${childCtx.exprRegistry.map(js => {
45
- let fixed = js;
46
- for (const name of varNames) {
47
- fixed = fixed.replace(new RegExp(`\\b${name}\\b`, 'g'), `_ctx.host._${name}`);
48
- }
49
- return `function(_ctx){ return ${fixed}; }`;
66
+ return `function(_ctx){ return ${replaceVarsOutsideStrings(js, varNames, (name) => `_ctx.host._${name}`)}; }`;
50
67
  }).join(",")}];\n`
51
68
  : "";
52
69
 
@@ -33,14 +33,21 @@ function sinthReplaceInsert(t, anchor, ifId, replaceId) {
33
33
  }
34
34
  let _rp = null, _rpParent = null, _rpNext = null;
35
35
  if (replaceId) {
36
- _rp = document.getElementById(replaceId);
37
- if (_rp) {
38
- _rpParent = _rp.parentNode;
39
- _rpNext = _rp.nextSibling;
40
- _rp.parentNode.removeChild(_rp);
41
- anchor._sinthReplaced = _rp;
42
- anchor._sinthReplacedParent = _rpParent;
43
- anchor._sinthReplacedNext = _rpNext;
36
+ if (anchor._sinthReplaced) {
37
+ _rp = anchor._sinthReplaced;
38
+ _rpParent = anchor._sinthReplacedParent;
39
+ _rpNext = anchor._sinthReplacedNext;
40
+ if (_rp.parentNode) _rp.parentNode.removeChild(_rp);
41
+ } else {
42
+ _rp = document.getElementById(replaceId);
43
+ if (_rp) {
44
+ _rpParent = _rp.parentNode;
45
+ _rpNext = _rp.nextSibling;
46
+ _rp.parentNode.removeChild(_rp);
47
+ anchor._sinthReplaced = _rp;
48
+ anchor._sinthReplacedParent = _rpParent;
49
+ anchor._sinthReplacedNext = _rpNext;
50
+ }
44
51
  }
45
52
  }
46
53
  let frag = document.createRange().createContextualFragment(t.innerHTML);
@@ -118,6 +125,7 @@ function sinthIfBlock(t) {
118
125
  elseA.remove();
119
126
  }
120
127
  if (t.dataset.sinthIfPersist === "true" && anchor) return;
128
+ if (anchor && anchor._sinthReplaced && !t.dataset.sinthIfDelayHide) return;
121
129
  let _hasContent = anchor || t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
122
130
  if (t.dataset.sinthIfDelayHide === 'false' && t.dataset.sinthIfDelay && _hasContent) {
123
131
  let dms = parseInt(t.dataset.sinthIfDelay) || 0;
@@ -150,9 +158,17 @@ function sinthIfBlock(t) {
150
158
  } else if (rpParent) {
151
159
  rpParent.appendChild(anchor._sinthReplaced);
152
160
  }
153
- if (anchor._sinthReplaced && anchor._sinthReplaced.dataset.sinthDelay) {
154
- delete anchor._sinthReplaced.dataset.sinthDelayDone;
155
- sinthDelay(anchor._sinthReplaced);
161
+ if (anchor._sinthReplaced) {
162
+ anchor._sinthReplaced.querySelectorAll('.sinth-expr').forEach(sinthExpr);
163
+ if (anchor._sinthReplaced.dataset.sinthDelay) {
164
+ delete anchor._sinthReplaced.dataset.sinthDelayDone;
165
+ sinthDelay(anchor._sinthReplaced);
166
+ }
167
+ anchor._sinthReplaced = null;
168
+ anchor._sinthReplacedParent = null;
169
+ anchor._sinthReplacedNext = null;
170
+ anchor._sinthInsertedFirst = null;
171
+ anchor._sinthInsertedLast = null;
156
172
  }
157
173
  } else {
158
174
  let cur2 = anchor.nextSibling;