@@ -4,7 +4,7 @@ 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 , ignoreWhitespace ) {
7
+ function pushTextNode ( list , html , level , 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 ) ;
@@ -14,7 +14,11 @@ function pushTextNode(list, html, start, ignoreWhitespace) {
14
14
if ( / ^ \s * $ / . test ( content ) ) {
15
15
content = ' ' ;
16
16
}
17
- if ( ! ignoreWhitespace || content !== ' ' ) {
17
+ // don't add whitespace-only text nodes if they would be trailing text nodes
18
+ // or if they would be leading whitespace-only text nodes:
19
+ // * end > -1 indicates this is not a trailing text node
20
+ // * leading node is when level is -1 and list has length 0
21
+ if ( ( ! ignoreWhitespace && end > - 1 && level + list . length >= 0 ) || content !== ' ' ) {
18
22
list . push ( {
19
23
type : 'text' ,
20
24
content : content
@@ -57,7 +61,7 @@ module.exports = function parse(html, options) {
57
61
}
58
62
59
63
if ( ! current . voidElement && ! inComponent && nextChar && nextChar !== '<' ) {
60
- pushTextNode ( current . children , html , start , options . ignoreWhitespace ) ;
64
+ pushTextNode ( current . children , html , level , start , options . ignoreWhitespace ) ;
61
65
}
62
66
63
67
byTag [ current . tagName ] = current ;
@@ -85,14 +89,14 @@ module.exports = function parse(html, options) {
85
89
// if we're at the root, push a base text node. otherwise add as
86
90
// a child to the current node.
87
91
parent = level === - 1 ? result : arr [ level ] . children ;
88
- pushTextNode ( parent , html , start , options . ignoreWhitespace ) ;
92
+ pushTextNode ( parent , html , level , start , options . ignoreWhitespace ) ;
89
93
}
90
94
}
91
95
} ) ;
92
96
93
97
// If the "html" passed isn't actually html, add it as a text node.
94
98
if ( ! result . length && html . length ) {
95
- pushTextNode ( result , html , 0 , options . ignoreWhitespace ) ;
99
+ pushTextNode ( result , html , 0 , 0 , options . ignoreWhitespace ) ;
96
100
}
97
101
98
102
return result ;
0 commit comments