Skip to content

Commit 2eb6955

Browse files
committed
Add option to not ignore extra whitespace
1 parent 79ddeb8 commit 2eb6955

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lib/parse.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ var parseTag = require('./parse-tag');
44
// re-used obj for quick lookups of components
55
var empty = Object.create ? Object.create(null) : {};
66
// common logic for pushing a child node onto a list
7-
function pushTextNode(list, html, start) {
7+
function pushTextNode(list, html, start, ignoreWhitespace) {
88
// calculate correct end of the content slice in case there's
99
// no tag after the text node.
1010
var end = html.indexOf('<', start);
1111
var content = html.slice(start, end === -1 ? undefined : end);
1212
// if a node is nothing but whitespace, no need to add it.
13-
if (!/^\s*$/.test(content)) {
13+
if (!ignoreWhitespace || !/^\s*$/.test(content)) {
1414
list.push({
1515
type: 'text',
1616
content: content
@@ -21,6 +21,9 @@ function pushTextNode(list, html, start) {
2121
module.exports = function parse(html, options) {
2222
options || (options = {});
2323
options.components || (options.components = empty);
24+
if (options.ignoreWhitespace === void 0) {
25+
options.ignoreWhitespace = true;
26+
}
2427
var result = [];
2528
var current;
2629
var level = -1;
@@ -53,7 +56,7 @@ module.exports = function parse(html, options) {
5356
}
5457

5558
if (!current.voidElement && !inComponent && nextChar && nextChar !== '<') {
56-
pushTextNode(current.children, html, start);
59+
pushTextNode(current.children, html, start, options.ignoreWhitespace);
5760
}
5861

5962
byTag[current.tagName] = current;
@@ -81,7 +84,7 @@ module.exports = function parse(html, options) {
8184
// if we're at the root, push a base text node. otherwise add as
8285
// a child to the current node.
8386
parent = level === -1 ? result : arr[level].children;
84-
pushTextNode(parent, html, start);
87+
pushTextNode(parent, html, start, options.ignoreWhitespace);
8588
}
8689
}
8790
});

0 commit comments

Comments
 (0)