Skip to content
Merged
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
187 changes: 92 additions & 95 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
const CACHE = {};

export default function html(statics) {
const str = statics.join('\0');
const tpl = CACHE[str] || (CACHE[str] = build(str));
const key = statics.reduce((key, s) => key + s.length + '$' + s, '');
const tpl = CACHE[key] || (CACHE[key] = build(statics));
// eslint-disable-next-line prefer-rest-params
return tpl(this, arguments);
}
Expand All @@ -38,15 +38,13 @@ const MODE_ATTRIBUTE = 13;
const MODE_SKIP = 47;

/** Create a template function given strings from a tagged template. */
function build(input) {
function build(statics) {
let out = 'return ';
let buffer = '';
let mode = MODE_WHITESPACE;
let fieldIndex = 1;
let field = '';
let hasChildren = 0;
let propCount = 0;
let spreads = 0;
let props = '';
let quote = 0;
let spread, slash, charCode, inTag, propName, propHasValue;

Expand All @@ -64,22 +62,19 @@ function build(input) {
}
else if (mode === MODE_ATTRIBUTE || (mode === MODE_WHITESPACE && buffer === '...')) {
if (mode === MODE_WHITESPACE) {
spread = true;
if (!spreads++) {
if (propCount === 0) out += ',Object.assign({},';
else out = out.replace(/,\(\{(.*?)$/, ',Object.assign({},{$1') + '},';
if (!spread) {
spread = true;
if (!props) props = 'Object.assign({},';
else props = 'Object.assign({},' + props + '},';
}
out += field + ',{';
propCount++;
props += field + ',{';
}
else if (propName) {
if (!spread) out += ',';
if (propCount === 0) out += '({';
out += propName + ':';
out += field || ((propHasValue || buffer) && JSON.stringify(buffer)) || 'true';
if (!props) props += '{';
else if (!props.endsWith('{')) props += ',';
props += JSON.stringify(propName) + ':';
props += field || ((propHasValue || buffer) && JSON.stringify(buffer)) || 'true';
propName = '';
spread = false;
propCount++;
}
propHasValue = false;
}
Expand All @@ -94,96 +89,98 @@ function build(input) {
buffer = field = '';
}

for (let i=0; i<input.length; i++) {
charCode = input.charCodeAt(i);
field = '';

if (charCode === QUOTE_SINGLE || charCode === QUOTE_DOUBLE) {
if (quote === charCode) {
quote = 0;
continue;
}
if (quote === 0) {
quote = charCode;
continue;
}
}

if (charCode === 0) {
for (let i=0; i<statics.length; i++) {
if (i > 0) {
if (!inTag) commit();
field = `$_h[${fieldIndex++}]`;
field = `$_h[${i}]`;
commit();
continue;
}

const input = statics[i];
for (let j=0; j<input.length; j++) {
charCode = input.charCodeAt(j);
field = '';

if (quote === 0) {
switch (charCode) {
case TAG_START:
if (!inTag) {
// commit buffer
commit();
inTag = true;
propCount = 0;
slash = spread = propHasValue = false;
mode = MODE_TAGNAME;
continue;
}

case TAG_END:
if (inTag) {
commit();
if (mode !== MODE_SKIP) {
if (propCount === 0) {
out += ',null';
if (charCode === QUOTE_SINGLE || charCode === QUOTE_DOUBLE) {
if (quote === charCode) {
quote = 0;
continue;
}
if (quote === 0) {
quote = charCode;
continue;
}
}

if (quote === 0) {
switch (charCode) {
case TAG_START:
if (!inTag) {
// commit buffer
commit();
inTag = true;
props = '';
slash = spread = propHasValue = false;
mode = MODE_TAGNAME;
continue;
}

case TAG_END:
if (inTag) {
commit();
if (mode !== MODE_SKIP) {
if (!props) {
out += ',null';
}
else {
out += ',' + props + '}' + (spread ? ')' : '');
}
}
else {
out += '})';
if (slash) {
out += ')';
}
spread = inTag = false;
props = '';
mode = MODE_TEXT;
continue;
}
if (slash) {
out += ')';

case EQUALS:
if (inTag) {
mode = MODE_ATTRIBUTE;
propHasValue = true;
propName = buffer;
buffer = '';
continue;
}
inTag = false;
propCount = 0;
mode = MODE_TEXT;
continue;
}

case EQUALS:
if (inTag) {
mode = MODE_ATTRIBUTE;
propHasValue = true;
propName = buffer;
buffer = '';
continue;
}

case SLASH:
if (inTag) {
if (!slash) {
slash = true;
// </foo>
if (mode === MODE_TAGNAME && !field && !buffer.trim().length) {
buffer = field = '';
mode = MODE_SKIP;
case SLASH:
if (inTag) {
if (!slash) {
slash = true;
// </foo>
if (mode === MODE_TAGNAME && !field && !buffer.trim().length) {
buffer = field = '';
mode = MODE_SKIP;
}
}
continue;
}
case TAB:
case NEWLINE:
case RETURN:
case SPACE:
// <a disabled>
if (inTag) {
commit();
mode = MODE_WHITESPACE;
continue;
}
continue;
}
case TAB:
case NEWLINE:
case RETURN:
case SPACE:
// <a disabled>
if (inTag) {
commit();
continue;
}
}
}
}

buffer += input.charAt(i);

buffer += input.charAt(j);
}
}
commit();
return Function('h', '$_h', out);
Expand Down
26 changes: 26 additions & 0 deletions test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ describe('htm', () => {
test('single prop with static value', () => {
expect(html`<a href="/hello" />`).toEqual({ tag: 'a', props: { href: '/hello' }, children: [] });
});

test('single prop with static value followed by a single boolean prop', () => {
expect(html`<a href="/hello" b />`).toEqual({ tag: 'a', props: { href: '/hello', b: true }, children: [] });
});

test('two props with static values', () => {
expect(html`<a href="/hello" target="_blank" />`).toEqual({ tag: 'a', props: { href: '/hello', target: '_blank' }, children: [] });
Expand All @@ -75,6 +79,9 @@ describe('htm', () => {
expect(html`<a b ...${{ foo: 'bar' }} />`).toEqual({ tag: 'a', props: { b: true, foo: 'bar' }, children: [] });
expect(html`<a b c ...${{ foo: 'bar' }} />`).toEqual({ tag: 'a', props: { b: true, c: true, foo: 'bar' }, children: [] });
expect(html`<a ...${{ foo: 'bar' }} b />`).toEqual({ tag: 'a', props: { b: true, foo: 'bar' }, children: [] });
expect(html`<a b="1" ...${{ foo: 'bar' }} />`).toEqual({ tag: 'a', props: { b: '1', foo: 'bar' }, children: [] });
expect(html`<a x="1"><b y="2" ...${{ c: 'bar' }}/></a>`).toEqual(h('a', { x: '1' }, h('b', { y: '2', c: 'bar' }) ));
expect(html`<a ...${{ c: 'bar' }}><b ...${{ d: 'baz' }}/></a>`).toEqual(h('a', { c: 'bar' }, h('b', { d: 'baz' }) ));
});

test('mixed spread + static props', () => {
Expand Down Expand Up @@ -134,4 +141,23 @@ describe('htm', () => {
</a>
`).toEqual(h('a', null, 'before', 'foo', h('b', null), 'bar', 'after'));
});

test('hyphens (-) are allowed in attribute names', () => {
expect(html`<a b-c></a>`).toEqual(h('a', { 'b-c': true }));
});

test('NUL characters are allowed in attribute values', () => {
expect(html`<a b="\0"></a>`).toEqual(h('a', { b: '\0' }));
expect(html`<a b="\0" c=${'foo'}></a>`).toEqual(h('a', { b: '\0', c: 'foo' }));
});

test('NUL characters are allowed in text', () => {
expect(html`<a>\0</a>`).toEqual(h('a', null, '\0'));
expect(html`<a>\0${'foo'}</a>`).toEqual(h('a', null, '\0', 'foo'));
});

test('cache key should be unique', () => {
html`<a b="${'foo'}" />`;
expect(html`<a b="\0" />`).toEqual(h('a', { b: '\0' }));
});
});