1
1
import Block from './Block.js' ;
2
+ import { trimStart , trimEnd } from '../../utils/trim.js' ;
2
3
3
4
function isElseIf ( node ) {
4
5
return node && node . children . length === 1 && node . children [ 0 ] . type === 'IfBlock' ;
@@ -23,7 +24,7 @@ const preprocessors = {
23
24
} ) ;
24
25
25
26
blocks . push ( node . _block ) ;
26
- preprocessChildren ( generator , node . _block , node . children ) ;
27
+ preprocessChildren ( generator , node . _block , node ) ;
27
28
28
29
if ( node . _block . dependencies . size > 0 ) {
29
30
dynamic = true ;
@@ -38,7 +39,7 @@ const preprocessors = {
38
39
} ) ;
39
40
40
41
blocks . push ( node . else . _block ) ;
41
- preprocessChildren ( generator , node . else . _block , node . else . children ) ;
42
+ preprocessChildren ( generator , node . else . _block , node . else ) ;
42
43
43
44
if ( node . else . _block . dependencies . size > 0 ) {
44
45
dynamic = true ;
@@ -97,7 +98,7 @@ const preprocessors = {
97
98
} ) ;
98
99
99
100
generator . blocks . push ( node . _block ) ;
100
- preprocessChildren ( generator , node . _block , node . children ) ;
101
+ preprocessChildren ( generator , node . _block , node ) ;
101
102
block . addDependencies ( node . _block . dependencies ) ;
102
103
node . _block . hasUpdateMethod = node . _block . dependencies . size > 0 ;
103
104
@@ -107,7 +108,7 @@ const preprocessors = {
107
108
} ) ;
108
109
109
110
generator . blocks . push ( node . else . _block ) ;
110
- preprocessChildren ( generator , node . else . _block , node . else . children ) ;
111
+ preprocessChildren ( generator , node . else . _block , node . else ) ;
111
112
node . else . _block . hasUpdateMethod = node . else . _block . dependencies . size > 0 ;
112
113
}
113
114
} ,
@@ -131,35 +132,56 @@ const preprocessors = {
131
132
132
133
const isComponent = generator . components . has ( node . name ) || node . name === ':Self' ;
133
134
134
- if ( isComponent ) {
135
- const name = block . getUniqueName ( ( node . name === ':Self' ? generator . name : node . name ) . toLowerCase ( ) ) ;
135
+ if ( node . children . length ) {
136
+ if ( isComponent ) {
137
+ const name = block . getUniqueName ( ( node . name === ':Self' ? generator . name : node . name ) . toLowerCase ( ) ) ;
136
138
137
- node . _block = block . child ( {
138
- name : generator . getUniqueName ( `create_${ name } _yield_fragment` )
139
- } ) ;
139
+ node . _block = block . child ( {
140
+ name : generator . getUniqueName ( `create_${ name } _yield_fragment` )
141
+ } ) ;
140
142
141
- generator . blocks . push ( node . _block ) ;
142
- preprocessChildren ( generator , node . _block , node . children ) ;
143
- block . addDependencies ( node . _block . dependencies ) ;
144
- node . _block . hasUpdateMethod = node . _block . dependencies . size > 0 ;
145
- }
143
+ generator . blocks . push ( node . _block ) ;
144
+ preprocessChildren ( generator , node . _block , node ) ;
145
+ block . addDependencies ( node . _block . dependencies ) ;
146
+ node . _block . hasUpdateMethod = node . _block . dependencies . size > 0 ;
147
+ }
146
148
147
- else {
148
- preprocessChildren ( generator , block , node . children ) ;
149
+ else {
150
+ preprocessChildren ( generator , block , node ) ;
151
+ }
149
152
}
150
153
}
151
154
} ;
152
155
153
156
preprocessors . RawMustacheTag = preprocessors . MustacheTag ;
154
157
155
- function preprocessChildren ( generator , block , children ) {
156
- children . forEach ( child => {
158
+ function preprocessChildren ( generator , block , node ) {
159
+ // glue text nodes together
160
+ const cleaned = [ ] ;
161
+ let lastChild ;
162
+
163
+ node . children . forEach ( child => {
164
+ if ( child . type === 'Comment' ) return ;
165
+
166
+ if ( child . type === 'Text' && lastChild && lastChild . type === 'Text' ) {
167
+ lastChild . data += child . data ;
168
+ lastChild . end = child . end ;
169
+ } else {
170
+ cleaned . push ( child ) ;
171
+ }
172
+
173
+ lastChild = child ;
174
+ } ) ;
175
+
176
+ node . children = cleaned ;
177
+
178
+ cleaned . forEach ( child => {
157
179
const preprocess = preprocessors [ child . type ] ;
158
180
if ( preprocess ) preprocess ( generator , block , child ) ;
159
181
} ) ;
160
182
}
161
183
162
- export default function preprocess ( generator , children ) {
184
+ export default function preprocess ( generator , node ) {
163
185
const block = new Block ( {
164
186
generator,
165
187
name : generator . alias ( 'create_main_fragment' ) ,
@@ -177,8 +199,21 @@ export default function preprocess ( generator, children ) {
177
199
} ) ;
178
200
179
201
generator . blocks . push ( block ) ;
180
- preprocessChildren ( generator , block , children ) ;
202
+ preprocessChildren ( generator , block , node ) ;
181
203
block . hasUpdateMethod = block . dependencies . size > 0 ;
182
204
205
+ // trim leading and trailing whitespace from the top level
206
+ const firstChild = node . children [ 0 ] ;
207
+ if ( firstChild && firstChild . type === 'Text' ) {
208
+ firstChild . data = trimStart ( firstChild . data ) ;
209
+ if ( ! firstChild . data ) node . children . shift ( ) ;
210
+ }
211
+
212
+ const lastChild = node . children [ node . children . length - 1 ] ;
213
+ if ( lastChild && lastChild . type === 'Text' ) {
214
+ lastChild . data = trimEnd ( lastChild . data ) ;
215
+ if ( ! lastChild . data ) node . children . pop ( ) ;
216
+ }
217
+
183
218
return block ;
184
219
}
0 commit comments