@@ -4,13 +4,13 @@ var parseTag = require('./parse-tag');
4
4
// re-used obj for quick lookups of components
5
5
var empty = Object . create ? Object . create ( null ) : { } ;
6
6
// common logic for pushing a child node onto a list
7
- function pushTextNode ( list , html , start ) {
7
+ function pushTextNode ( list , html , start , ignoreWhitespace ) {
8
8
// calculate correct end of the content slice in case there's
9
9
// no tag after the text node.
10
10
var end = html . indexOf ( '<' , start ) ;
11
11
var content = html . slice ( start , end === - 1 ? undefined : end ) ;
12
12
// if a node is nothing but whitespace, no need to add it.
13
- if ( ! / ^ \s * $ / . test ( content ) ) {
13
+ if ( ! ignoreWhitespace || ! / ^ \s * $ / . test ( content ) ) {
14
14
list . push ( {
15
15
type : 'text' ,
16
16
content : content
@@ -21,6 +21,9 @@ function pushTextNode(list, html, start) {
21
21
module . exports = function parse ( html , options ) {
22
22
options || ( options = { } ) ;
23
23
options . components || ( options . components = empty ) ;
24
+ if ( options . ignoreWhitespace === void 0 ) {
25
+ options . ignoreWhitespace = true ;
26
+ }
24
27
var result = [ ] ;
25
28
var current ;
26
29
var level = - 1 ;
@@ -53,7 +56,7 @@ module.exports = function parse(html, options) {
53
56
}
54
57
55
58
if ( ! current . voidElement && ! inComponent && nextChar && nextChar !== '<' ) {
56
- pushTextNode ( current . children , html , start ) ;
59
+ pushTextNode ( current . children , html , start , options . ignoreWhitespace ) ;
57
60
}
58
61
59
62
byTag [ current . tagName ] = current ;
@@ -81,7 +84,7 @@ module.exports = function parse(html, options) {
81
84
// if we're at the root, push a base text node. otherwise add as
82
85
// a child to the current node.
83
86
parent = level === - 1 ? result : arr [ level ] . children ;
84
- pushTextNode ( parent , html , start ) ;
87
+ pushTextNode ( parent , html , start , options . ignoreWhitespace ) ;
85
88
}
86
89
}
87
90
} ) ;
0 commit comments