Skip to content

Commit 84a2f01

Browse files
committed
Merge pull request #3 from rayd/fix/parsing-trailing-text-nodes
Fix parsing trailing text nodes.
2 parents 3dc7124 + 7a9d6b4 commit 84a2f01

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

lib/parse.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,21 @@ module.exports = function parse(html, options) {
6363
level--;
6464
if (!inComponent && nextChar !== '<' && nextChar) {
6565
// trailing text node
66-
arr[level].children.push({
67-
type: 'text',
68-
content: html.slice(start, html.indexOf('<', start))
69-
});
66+
// if we're at the root, push a base text node. otherwise add as
67+
// a child to the current node.
68+
parent = level === -1 ? result : arr[level].children;
69+
70+
// calculate correct end of the content slice in case there's
71+
// no tag after the text node.
72+
var end = html.indexOf('<', start);
73+
var content = html.slice(start, end === -1 ? undefined : end);
74+
// if a node is nothing but whitespace, no need to add it.
75+
if (!/^\s*$/.test(content)) {
76+
parent.push({
77+
type: 'text',
78+
content: content
79+
});
80+
}
7081
}
7182
}
7283
});

test/parse.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,80 @@ test('parse', function (t) {
292292
{ type: 'text', content: ' 10 ' },
293293
]
294294
}], 'should not give voidElements children');
295+
296+
html = '<div></div>\n';
297+
parsed = HTML.parse(html);
298+
t.deepEqual(parsed, [{
299+
type: 'tag',
300+
name: 'div',
301+
attrs: {},
302+
voidElement: false,
303+
children: []
304+
}], 'should not explode on trailing whitespace');
305+
306+
html = '<div>Hi</div> There ';
307+
parsed = HTML.parse(html);
308+
t.deepEqual(parsed, [{
309+
type: 'tag',
310+
name: 'div',
311+
attrs: {},
312+
voidElement: false,
313+
children: [
314+
{ type: 'text', content: 'Hi' }
315+
]
316+
},{
317+
type: 'text', content: ' There '
318+
}], 'should handle trailing text nodes at the top-level');
319+
320+
html = '<div>Hi</div> There <span>something</span> <a></a>else ';
321+
parsed = HTML.parse(html);
322+
t.deepEqual(parsed, [{
323+
type: 'tag',
324+
name: 'div',
325+
attrs: {},
326+
voidElement: false,
327+
children: [
328+
{ type: 'text', content: 'Hi' }
329+
]
330+
},{
331+
type: 'text', content: ' There '
332+
},{
333+
type: 'tag',
334+
name: 'span',
335+
attrs: {},
336+
voidElement: false,
337+
children: [
338+
{ type: 'text', content: 'something' }
339+
]
340+
},{
341+
type: 'tag',
342+
name: 'a',
343+
attrs: {},
344+
voidElement: false,
345+
children: []
346+
},{
347+
type: 'text', content: 'else '
348+
}], 'should handle text nodes in the middle of tags at the top-level');
349+
350+
html = '<div>Hi</div>\n\n <span>There</span> \t ';
351+
parsed = HTML.parse(html);
352+
t.deepEqual(parsed, [{
353+
type: 'tag',
354+
name: 'div',
355+
attrs: {},
356+
voidElement: false,
357+
children: [
358+
{ type: 'text', content: 'Hi' }
359+
]
360+
},{
361+
type: 'tag',
362+
name: 'span',
363+
attrs: {},
364+
voidElement: false,
365+
children: [
366+
{ type: 'text', content: 'There' }
367+
]
368+
}], 'should remove text nodes that are nothing but whitespace');
295369
t.end();
296370
});
297371

0 commit comments

Comments
 (0)