@@ -3,6 +3,20 @@ var tagRE = /<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g;
3
3
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
+ // 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
+ }
6
20
7
21
module . exports = function parse ( html , options ) {
8
22
options || ( options = { } ) ;
@@ -37,10 +51,7 @@ module.exports = function parse(html, options) {
37
51
}
38
52
39
53
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 ) ;
44
55
}
45
56
46
57
byTag [ current . tagName ] = current ;
@@ -66,18 +77,7 @@ module.exports = function parse(html, options) {
66
77
// if we're at the root, push a base text node. otherwise add as
67
78
// a child to the current node.
68
79
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 ) ;
81
81
}
82
82
}
83
83
} ) ;
0 commit comments