@yannosay/sinth 1.2.2 → 1.2.3

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.2.3",
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",
@@ -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