@matthesketh/utopia-compiler 0.0.5 → 0.1.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/dist/index.cjs CHANGED
@@ -400,7 +400,7 @@ var CodeGenerator = class {
400
400
  this.emit(`const ${fragVar} = createElement('div')`);
401
401
  if (this.scopeId) {
402
402
  this.helpers.add("setAttr");
403
- this.emit(`setAttr(${fragVar}, '${this.scopeId}', '')`);
403
+ this.emit(`setAttr(${fragVar}, '${escapeStr(this.scopeId)}', '')`);
404
404
  }
405
405
  for (const node of ast) {
406
406
  const childVar = this.genNode(node, scope);
@@ -453,7 +453,7 @@ ${fnBody}
453
453
  this.emit(`const ${elVar} = createElement('${node.tag}')`);
454
454
  if (this.scopeId) {
455
455
  this.helpers.add("setAttr");
456
- this.emit(`setAttr(${elVar}, '${this.scopeId}', '')`);
456
+ this.emit(`setAttr(${elVar}, '${escapeStr(this.scopeId)}', '')`);
457
457
  }
458
458
  for (const attr of node.attrs) {
459
459
  this.helpers.add("setAttr");
@@ -603,14 +603,14 @@ ${fnBody}
603
603
  const propEntries = [];
604
604
  for (const a of node.attrs) {
605
605
  if (a.value !== null) {
606
- propEntries.push(`${a.name}: '${escapeStr(a.value)}'`);
606
+ propEntries.push(`'${escapeStr(a.name)}': '${escapeStr(a.value)}'`);
607
607
  } else {
608
- propEntries.push(`${a.name}: true`);
608
+ propEntries.push(`'${escapeStr(a.name)}': true`);
609
609
  }
610
610
  }
611
611
  for (const d of node.directives) {
612
612
  if (d.kind === "bind" && d.arg) {
613
- propEntries.push(`${d.arg}: ${this.resolveExpression(d.expression, scope)}`);
613
+ propEntries.push(`'${escapeStr(d.arg)}': ${this.resolveExpression(d.expression, scope)}`);
614
614
  }
615
615
  }
616
616
  const propsStr = propEntries.length > 0 ? `{ ${propEntries.join(", ")} }` : "{}";
@@ -647,7 +647,7 @@ ${fnBody}
647
647
  }
648
648
  };
649
649
  function isComponentTag(tag) {
650
- return /^[A-Z]/.test(tag);
650
+ return /^[A-Z][a-zA-Z0-9_$]*$/.test(tag);
651
651
  }
652
652
  function escapeStr(s) {
653
653
  return s.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
@@ -680,8 +680,28 @@ var ENTITY_MAP = {
680
680
  };
681
681
  function decodeEntities(text) {
682
682
  return text.replace(/&(?:#(\d+)|#x([0-9a-fA-F]+)|(\w+));/g, (match, dec, hex, named) => {
683
- if (dec) return String.fromCodePoint(parseInt(dec, 10));
684
- if (hex) return String.fromCodePoint(parseInt(hex, 16));
683
+ if (dec) {
684
+ const code = parseInt(dec, 10);
685
+ if (code >= 0 && code <= 1114111) {
686
+ try {
687
+ return String.fromCodePoint(code);
688
+ } catch {
689
+ return match;
690
+ }
691
+ }
692
+ return match;
693
+ }
694
+ if (hex) {
695
+ const code = parseInt(hex, 16);
696
+ if (code >= 0 && code <= 1114111) {
697
+ try {
698
+ return String.fromCodePoint(code);
699
+ } catch {
700
+ return match;
701
+ }
702
+ }
703
+ return match;
704
+ }
685
705
  if (named) return ENTITY_MAP[`&${named};`] ?? match;
686
706
  return match;
687
707
  });
package/dist/index.js CHANGED
@@ -368,7 +368,7 @@ var CodeGenerator = class {
368
368
  this.emit(`const ${fragVar} = createElement('div')`);
369
369
  if (this.scopeId) {
370
370
  this.helpers.add("setAttr");
371
- this.emit(`setAttr(${fragVar}, '${this.scopeId}', '')`);
371
+ this.emit(`setAttr(${fragVar}, '${escapeStr(this.scopeId)}', '')`);
372
372
  }
373
373
  for (const node of ast) {
374
374
  const childVar = this.genNode(node, scope);
@@ -421,7 +421,7 @@ ${fnBody}
421
421
  this.emit(`const ${elVar} = createElement('${node.tag}')`);
422
422
  if (this.scopeId) {
423
423
  this.helpers.add("setAttr");
424
- this.emit(`setAttr(${elVar}, '${this.scopeId}', '')`);
424
+ this.emit(`setAttr(${elVar}, '${escapeStr(this.scopeId)}', '')`);
425
425
  }
426
426
  for (const attr of node.attrs) {
427
427
  this.helpers.add("setAttr");
@@ -571,14 +571,14 @@ ${fnBody}
571
571
  const propEntries = [];
572
572
  for (const a of node.attrs) {
573
573
  if (a.value !== null) {
574
- propEntries.push(`${a.name}: '${escapeStr(a.value)}'`);
574
+ propEntries.push(`'${escapeStr(a.name)}': '${escapeStr(a.value)}'`);
575
575
  } else {
576
- propEntries.push(`${a.name}: true`);
576
+ propEntries.push(`'${escapeStr(a.name)}': true`);
577
577
  }
578
578
  }
579
579
  for (const d of node.directives) {
580
580
  if (d.kind === "bind" && d.arg) {
581
- propEntries.push(`${d.arg}: ${this.resolveExpression(d.expression, scope)}`);
581
+ propEntries.push(`'${escapeStr(d.arg)}': ${this.resolveExpression(d.expression, scope)}`);
582
582
  }
583
583
  }
584
584
  const propsStr = propEntries.length > 0 ? `{ ${propEntries.join(", ")} }` : "{}";
@@ -615,7 +615,7 @@ ${fnBody}
615
615
  }
616
616
  };
617
617
  function isComponentTag(tag) {
618
- return /^[A-Z]/.test(tag);
618
+ return /^[A-Z][a-zA-Z0-9_$]*$/.test(tag);
619
619
  }
620
620
  function escapeStr(s) {
621
621
  return s.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
@@ -648,8 +648,28 @@ var ENTITY_MAP = {
648
648
  };
649
649
  function decodeEntities(text) {
650
650
  return text.replace(/&(?:#(\d+)|#x([0-9a-fA-F]+)|(\w+));/g, (match, dec, hex, named) => {
651
- if (dec) return String.fromCodePoint(parseInt(dec, 10));
652
- if (hex) return String.fromCodePoint(parseInt(hex, 16));
651
+ if (dec) {
652
+ const code = parseInt(dec, 10);
653
+ if (code >= 0 && code <= 1114111) {
654
+ try {
655
+ return String.fromCodePoint(code);
656
+ } catch {
657
+ return match;
658
+ }
659
+ }
660
+ return match;
661
+ }
662
+ if (hex) {
663
+ const code = parseInt(hex, 16);
664
+ if (code >= 0 && code <= 1114111) {
665
+ try {
666
+ return String.fromCodePoint(code);
667
+ } catch {
668
+ return match;
669
+ }
670
+ }
671
+ return match;
672
+ }
653
673
  if (named) return ENTITY_MAP[`&${named};`] ?? match;
654
674
  return match;
655
675
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matthesketh/utopia-compiler",
3
- "version": "0.0.5",
3
+ "version": "0.1.0",
4
4
  "description": "Compiler for .utopia single-file components",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -39,7 +39,7 @@
39
39
  "dist"
40
40
  ],
41
41
  "dependencies": {
42
- "@matthesketh/utopia-core": "0.0.5"
42
+ "@matthesketh/utopia-core": "0.1.0"
43
43
  },
44
44
  "scripts": {
45
45
  "build": "tsup src/index.ts --format esm,cjs --dts",