Skip to content

Commit 419f1a4

Browse files
committed
Fix text containing single or double quotes
1 parent d4220b2 commit 419f1a4

File tree

2 files changed

+37
-47
lines changed

2 files changed

+37
-47
lines changed

src/index.mjs

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -100,37 +100,35 @@ function build(statics) {
100100
commit();
101101
}
102102

103-
const input = statics[i];
104-
for (let j=0; j<input.length; j++) {
105-
charCode = input.charCodeAt(j);
106-
field = '';
103+
for (let j=0; j<statics[i].length; j++) {
104+
charCode = statics[i].charCodeAt(j);
107105

108-
if (charCode === QUOTE_SINGLE || charCode === QUOTE_DOUBLE) {
109-
if (quote === charCode) {
110-
quote = 0;
111-
continue;
112-
}
113-
if (quote === 0) {
114-
quote = charCode;
106+
if (!inTag) {
107+
if (charCode === TAG_START) {
108+
// commit buffer
109+
commit();
110+
inTag = true;
111+
spreadClose = propsClose = props = '';
112+
slash = propHasValue = false;
113+
mode = MODE_TAGNAME;
115114
continue;
116115
}
117116
}
118-
119-
if (quote === 0) {
120-
switch (charCode) {
121-
case TAG_START:
122-
if (!inTag) {
123-
// commit buffer
124-
commit();
125-
inTag = true;
126-
spreadClose = propsClose = props = '';
127-
slash = propHasValue = false;
128-
mode = MODE_TAGNAME;
129-
continue;
130-
}
131-
132-
case TAG_END:
133-
if (inTag) {
117+
else {
118+
if (charCode === QUOTE_SINGLE || charCode === QUOTE_DOUBLE) {
119+
if (quote === charCode) {
120+
quote = 0;
121+
continue;
122+
}
123+
if (quote === 0) {
124+
quote = charCode;
125+
continue;
126+
}
127+
}
128+
129+
if (quote === 0) {
130+
switch (charCode) {
131+
case TAG_END:
134132
commit();
135133
if (mode !== MODE_SKIP) {
136134
if (!props) {
@@ -147,43 +145,34 @@ function build(statics) {
147145
props = '';
148146
mode = MODE_TEXT;
149147
continue;
150-
}
151-
152-
case EQUALS:
153-
if (inTag) {
148+
case EQUALS:
154149
mode = MODE_ATTRIBUTE;
155150
propHasValue = true;
156151
propName = buffer;
157152
buffer = '';
158153
continue;
159-
}
160-
161-
case SLASH:
162-
if (inTag) {
154+
case SLASH:
163155
if (!slash) {
164156
slash = true;
165157
// </foo>
166-
if (mode === MODE_TAGNAME && !field && !buffer.trim().length) {
158+
if (mode === MODE_TAGNAME && !buffer.trim()) {
167159
buffer = field = '';
168160
mode = MODE_SKIP;
169161
}
170162
}
171163
continue;
172-
}
173-
case TAB:
174-
case NEWLINE:
175-
case RETURN:
176-
case SPACE:
177-
// <a disabled>
178-
if (inTag) {
164+
case TAB:
165+
case NEWLINE:
166+
case RETURN:
167+
case SPACE:
168+
// <a disabled>
179169
commit();
180170
mode = MODE_WHITESPACE;
181171
continue;
182-
}
172+
}
183173
}
184174
}
185-
186-
buffer += input.charAt(j);
175+
buffer += statics[i].charAt(j);
187176
}
188177
}
189178
commit();

test/index.test.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('htm', () => {
8686

8787
test('multiple spread props in one element', () => {
8888
expect(html`<a ...${{ foo: 'bar' }} ...${{ quux: 'baz' }} />`).toEqual({ tag: 'a', props: { foo: 'bar', quux: 'baz' }, children: [] });
89-
});
89+
});
9090

9191
test('mixed spread + static props', () => {
9292
expect(html`<a b ...${{ foo: 'bar' }} />`).toEqual({ tag: 'a', props: { b: true, foo: 'bar' }, children: [] });
@@ -107,6 +107,7 @@ describe('htm', () => {
107107
test('text child', () => {
108108
expect(html`<a>foo</a>`).toEqual({ tag: 'a', props: null, children: ['foo'] });
109109
expect(html`<a>foo bar</a>`).toEqual({ tag: 'a', props: null, children: ['foo bar'] });
110+
expect(html`<a>foo "<b /></a>`).toEqual({ tag: 'a', props: null, children: ['foo "', { tag: 'b', props: null, children: [] }] });
110111
});
111112

112113
test('dynamic child', () => {

0 commit comments

Comments
 (0)