@@ -3,6 +3,20 @@ var tagRE = /<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g;
33var parseTag = require ( './parse-tag' ) ;
44// re-used obj for quick lookups of components
55var 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
721module . 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 } ) ;
0 commit comments