@@ -57,7 +57,7 @@ function processNodes(tree, options, messages) {
57
57
58
58
options . expressions . locals = attributes ;
59
59
60
- options . expressions . locals . slots = getFilledSlotNames ( options . slotTagName , node . content ) ;
60
+ options . expressions . locals . $ slots = parseSlotsLocals ( options . slotTagName , html , node . content ) ;
61
61
62
62
const plugins = [ ...options . plugins , expressions ( options . expressions ) ] ;
63
63
@@ -169,6 +169,64 @@ function parseLocals(options, {attrs}, html) {
169
169
return { attributes, locals} ;
170
170
}
171
171
172
+ /**
173
+ * Parse slots locals and set which one is filled
174
+ * @param {String } tag
175
+ * @param html
176
+ * @param {Object } content
177
+ * @return {Object }
178
+ */
179
+ function parseSlotsLocals ( tag , html , content = [ ] ) {
180
+ const slots = [ ] ;
181
+ const getNodeName = ( node ) => {
182
+ let name = node . attrs && node . attrs . name ;
183
+
184
+ if ( ! name ) {
185
+ name = Object . keys ( { ...node . attrs } ) . find ( name => ! Object . keys ( slotTypes ) . includes ( name ) && name !== 'type' && name !== defaultSlotType ) ;
186
+
187
+ if ( ! name ) {
188
+ name = defaultSlotName ;
189
+ }
190
+ }
191
+
192
+ return name ;
193
+ }
194
+
195
+ match . call ( html , { tag} , node => {
196
+ const name = getNodeName ( node ) ;
197
+
198
+ slots [ name ] = {
199
+ filled : false
200
+ } ;
201
+
202
+ return node ;
203
+ } ) ;
204
+
205
+ match . call ( content , { tag} , node => {
206
+ const name = getNodeName ( node ) ;
207
+
208
+ const locals = Object . fromEntries ( Object . entries ( node . attrs ) . filter ( ( [ attribute ] ) => ! [ name , 'type' ] . includes ( attribute ) ) ) ;
209
+
210
+ if ( locals ) {
211
+ // Parse JSON locals
212
+ Object . keys ( locals ) . forEach ( local => {
213
+ try {
214
+ locals [ local ] = JSON . parse ( locals [ local ] ) ;
215
+ } catch { }
216
+ } ) ;
217
+ }
218
+
219
+ slots [ name ] = {
220
+ filled : true ,
221
+ locals : locals
222
+ } ;
223
+
224
+ return node ;
225
+ } ) ;
226
+
227
+ return slots ;
228
+ }
229
+
172
230
/**
173
231
* Merge slots content
174
232
* @param {Object } tree
@@ -181,11 +239,12 @@ function parseLocals(options, {attrs}, html) {
181
239
function mergeSlots ( tree , node , strict , { slotTagName, fallbackSlotTagName} ) {
182
240
const slots = getSlots ( slotTagName , tree , fallbackSlotTagName ) ; // Slot in component.html
183
241
const fillSlots = getSlots ( slotTagName , node . content ) ; // Slot in page.html
242
+ const clean = content => content . replace ( / ( \n | \t ) / g, '' ) . trim ( ) ;
184
243
185
244
// Retrieve main content, means everything that is not inside slots
186
245
if ( node . content ) {
187
- const contentOutsideSlots = node . content . filter ( content => content . tag !== slotTagName ) ;
188
- if ( contentOutsideSlots . length > 0 ) {
246
+ const contentOutsideSlots = node . content . filter ( content => ( content . tag !== slotTagName ) ) ;
247
+ if ( contentOutsideSlots . filter ( c => typeof c !== 'string' || clean ( c ) !== '' ) . length > 0 ) {
189
248
fillSlots [ defaultSlotName ] = [ { tag : slotTagName , attrs : { name : defaultSlotName } , content : [ ...contentOutsideSlots ] } ] ;
190
249
}
191
250
}
@@ -222,6 +281,7 @@ function mergeSlots(tree, node, strict, {slotTagName, fallbackSlotTagName}) {
222
281
) ;
223
282
}
224
283
284
+
225
285
delete fillSlots [ slotName ] ;
226
286
}
227
287
@@ -304,35 +364,6 @@ function getSlots(tag, content = [], fallbackSlotTagName = false) {
304
364
return slots ;
305
365
}
306
366
307
- /**
308
- * Get all filled slots names so we can
309
- * pass as locals to check if slot is filled
310
- * @param {String } tag
311
- * @param {Object } content
312
- * @return {Object }
313
- */
314
- function getFilledSlotNames ( tag , content = [ ] ) {
315
- const slotNames = [ ] ;
316
-
317
- match . call ( content , { tag} , node => {
318
- let name = node . attrs && node . attrs . name ;
319
-
320
- if ( ! name ) {
321
- name = Object . keys ( { ...node . attrs } ) . find ( name => ! Object . keys ( slotTypes ) . includes ( name ) && name !== 'type' && name !== defaultSlotType ) ;
322
-
323
- if ( ! name ) {
324
- name = defaultSlotName ;
325
- }
326
- }
327
-
328
- slotNames [ name ] = true ;
329
-
330
- return node ;
331
- } ) ;
332
-
333
- return slotNames ;
334
- }
335
-
336
367
/**
337
368
* Apply plugins to tree
338
369
* @param {Object } tree
0 commit comments