Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function build(statics) {
/** Traverse a DOM tree and serialize it to hyperscript function calls */
function walk(n) {
if (n.nodeType != 1) {
if (n.nodeType == 3 && n.data) return field(n.data, ',');
if (n.nodeType == 3 && n.data) return `[${field(n.data)}]`;
return 'null';
}
let str = '',
Expand Down Expand Up @@ -79,12 +79,9 @@ function walk(n) {

/** Serialize a field to a String or reference for use in generated code. */
function field(value, sep) {
const matches = value.match(reg);
let strValue = JSON.stringify(value);
if (matches != null) {
if (matches[0] === value) return value;
strValue = strValue.replace(reg, `"${sep}$1${sep}"`).replace(/"[+,]"/g, '');
if (sep == ',') strValue = `[${strValue}]`;
}
return strValue;
return value
.split(reg)
.map((piece, i) => i % 2 ? piece : (piece && JSON.stringify(piece)))
.filter(piece => piece)
.join(sep);
}
12 changes: 12 additions & 0 deletions test/babel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,16 @@ describe('htm/babel', () => {
}).code
).toBe(`var name="world",vnode={type:1,tag:"div",props:{id:"hello"},children:[{type:3,tag:null,props:null,children:null,text:"hello, "},name],text:null};`);
});

test('produce valid code for valid templates', () => {
expect(
transform('html`<div>",${name}</div>`;', {
babelrc: false,
compact: true,
plugins: [
htmBabelPlugin
]
}).code
).toBe(`h("div",{},["\\",",name]);`);
});
});