htm-transform 0.1.2 → 0.1.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.
Files changed (3) hide show
  1. package/index.js +30 -1
  2. package/package.json +1 -1
  3. package/test.js +10 -0
package/index.js CHANGED
@@ -328,8 +328,37 @@ function resolvePlaceholders(nodes, placeholders) {
328
328
  if (key.startsWith("__EXPR_")) {
329
329
  // Handle spread props (key is the placeholder)
330
330
  newProps["...spread"] = { type: "expression", expr: placeholderMap.get(key) };
331
- } else if (typeof value === "string" && value.startsWith("__EXPR_")) {
331
+ } else if (typeof value === "string" && placeholderMap.has(value)) {
332
+ // Single placeholder value
332
333
  newProps[key] = { type: "expression", expr: placeholderMap.get(value) };
334
+ } else if (typeof value === "string" && value.includes("__EXPR_")) {
335
+ // Template string with placeholders - create TemplateLiteral
336
+ const parts = value.split(/(__EXPR_\d+__)/);
337
+ const quasis = [];
338
+ const expressions = [];
339
+
340
+ for (let i = 0; i < parts.length; i++) {
341
+ if (i % 2 === 0) {
342
+ // Static string part
343
+ quasis.push({
344
+ type: "TemplateElement",
345
+ value: { raw: parts[i], cooked: parts[i] },
346
+ tail: i === parts.length - 1,
347
+ });
348
+ } else {
349
+ // Expression placeholder
350
+ expressions.push(placeholderMap.get(parts[i]));
351
+ }
352
+ }
353
+
354
+ newProps[key] = {
355
+ type: "expression",
356
+ expr: {
357
+ type: "TemplateLiteral",
358
+ quasis,
359
+ expressions,
360
+ },
361
+ };
333
362
  } else {
334
363
  newProps[key] = value;
335
364
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "htm-transform",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Transform htm tagged templates into h function calls using acorn",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/test.js CHANGED
@@ -380,4 +380,14 @@ const result = myH("div", null, "Test");`;
380
380
 
381
381
  assert.strictEqual(normalize(output), normalize(expected));
382
382
  });
383
+
384
+ test("handles template literals in attribute values", () => {
385
+ const input = `const result = html\`<div class="\${css.foo} \${css.bar}">Test</div>\`;`;
386
+ const output = transform(input);
387
+ const expected = `const result = h("div", {
388
+ class: \`\${css.foo} \${css.bar}\`
389
+ }, "Test");`;
390
+
391
+ assert.strictEqual(normalize(output), normalize(expected));
392
+ });
383
393
  });