Skip to content

Commit 19c36e8

Browse files
committed
Apply same child node logic when in an open tag or not. This way we don't try adding text nodes that are nothing but whitespace inside of others.
1 parent 1a57b36 commit 19c36e8

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

lib/parse.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ var tagRE = /<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g;
33
var parseTag = require('./parse-tag');
44
// re-used obj for quick lookups of components
55
var empty = Object.create ? Object.create(null) : {};
6+
// common logic for pushing a child node onto a list
7+
function pushTextNode(list, html, start) {
8+
// calculate correct end of the content slice in case there's
9+
// no tag after the text node.
10+
var end = html.indexOf('<', start);
11+
var content = html.slice(start, end === -1 ? undefined : end);
12+
// if a node is nothing but whitespace, no need to add it.
13+
if (!/^\s*$/.test(content)) {
14+
list.push({
15+
type: 'text',
16+
content: content
17+
});
18+
}
19+
}
620

721
module.exports = function parse(html, options) {
822
options || (options = {});
@@ -37,10 +51,7 @@ module.exports = function parse(html, options) {
3751
}
3852

3953
if (!current.voidElement && !inComponent && nextChar && nextChar !== '<') {
40-
current.children.push({
41-
type: 'text',
42-
content: html.slice(start, html.indexOf('<', start))
43-
});
54+
pushTextNode(current.children, html, start);
4455
}
4556

4657
byTag[current.tagName] = current;
@@ -66,18 +77,7 @@ module.exports = function parse(html, options) {
6677
// if we're at the root, push a base text node. otherwise add as
6778
// a child to the current node.
6879
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-
}
80+
pushTextNode(parent, html, start);
8181
}
8282
}
8383
});

test/parse.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ test('parse', function (t) {
347347
type: 'text', content: 'else '
348348
}], 'should handle text nodes in the middle of tags at the top-level');
349349

350-
html = '<div>Hi</div>\n\n <span>There</span> \t ';
350+
html = '<div>Hi</div>\n\n <span>There</span> \t <iframe>\n\t</iframe>';
351351
parsed = HTML.parse(html);
352352
t.deepEqual(parsed, [{
353353
type: 'tag',
@@ -365,6 +365,12 @@ test('parse', function (t) {
365365
children: [
366366
{ type: 'text', content: 'There' }
367367
]
368+
},{
369+
type: 'tag',
370+
name: 'iframe',
371+
attrs: {},
372+
voidElement: false,
373+
children: []
368374
}], 'should remove text nodes that are nothing but whitespace');
369375
t.end();
370376
});

0 commit comments

Comments
 (0)