diff --git a/src/build.mjs b/src/build.mjs index 927c372..3132b01 100644 --- a/src/build.mjs +++ b/src/build.mjs @@ -5,8 +5,10 @@ const MODE_TEXT = 1; const MODE_WHITESPACE = 2; const MODE_TAGNAME = 3; const MODE_COMMENT = 4; -const MODE_PROP_SET = 5; -const MODE_PROP_APPEND = 6; +const MODE_COMMENT_SINGLE = 5; +const MODE_COMMENT_MULTI = 6; +const MODE_PROP_SET = 7; +const MODE_PROP_APPEND = 8; const CHILD_APPEND = 0; const CHILD_RECURSE = 2; @@ -221,6 +223,26 @@ export const build = function(statics) { buffer += char; } } + else if (mode === MODE_WHITESPACE && char === '/' && statics[i][j+1] === '/') { + mode = MODE_COMMENT_SINGLE; + } + else if (mode === MODE_WHITESPACE && char === '/' && statics[i][j+1] === '*') { + mode = MODE_COMMENT_MULTI; + } + else if (mode === MODE_COMMENT_SINGLE) { + // Ignore everything until newline + if (char === '\n' || char === '\r') { + mode = MODE_WHITESPACE; + buffer = ''; + } + } + else if (mode === MODE_COMMENT_MULTI) { + if (char === '*' && statics[i][j+1] === '/') { + mode = MODE_WHITESPACE; + buffer = ''; + j++; + } + } else if (mode === MODE_COMMENT) { // Ignore everything until the last three characters are '-', '-' and '>' if (buffer === '--' && char === '>') { diff --git a/test/index.test.mjs b/test/index.test.mjs index 640e055..4301ffe 100644 --- a/test/index.test.mjs +++ b/test/index.test.mjs @@ -215,4 +215,34 @@ describe('htm', () => { expect(html``).toEqual(h('a', null)); expect(html` Hello, world `).toEqual(h('a', null)); }); + + test('ignore JS single line comments', () => { + expect(html`
`).toEqual(h('div', { id: 'foo' })); + expect(html``).toEqual(h('div', { id: 'foo' })); + expect(html``).toEqual(h('div', { id: 'foo' })); + + // False positives + expect(html``).toEqual(h('div', { id: '// foo' })); + expect(html`