Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore JS-style comments during parse #214

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 24 additions & 2 deletions src/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 === '>') {
Expand Down
30 changes: 30 additions & 0 deletions test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,34 @@ describe('htm', () => {
expect(html`<a><!-- ${'Hello, world!'} --></a>`).toEqual(h('a', null));
expect(html`<a><!--> Hello, world <!--></a>`).toEqual(h('a', null));
});

test('ignore JS single line comments', () => {
expect(html`<div
// comment
id="foo" />`).toEqual(h('div', { id: 'foo' }));
expect(html`<div id="foo"
// comment
/>`).toEqual(h('div', { id: 'foo' }));
expect(html`<div id="foo" //comment
/>`).toEqual(h('div', { id: 'foo' }));

// False positives
expect(html`<div id="// foo"
/>`).toEqual(h('div', { id: '// foo' }));
expect(html`<div>// no comment</div>`).toEqual(h('div', null, '// no comment'));
});

test('ignore JS multi line comments', () => {
expect(html`<div
/* comment */
id="foo" />`).toEqual(h('div', { id: 'foo' }));
expect(html`<div id="foo"
/* comment */
/>`).toEqual(h('div', { id: 'foo' }));
expect(html`<div id="foo" /* comment */ />`).toEqual(h('div', { id: 'foo' }));

// False positives
expect(html`<div id="/* foo */" />`).toEqual(h('div', { id: '/* foo */' }));
expect(html`<div>/*foo*/</div>`).toEqual(h('div', null, '/*foo*/'));
});
});