Skip to content

Commit 6d4b3dc

Browse files
Slot locals
1 parent 049f153 commit 6d4b3dc

File tree

1 file changed

+63
-32
lines changed

1 file changed

+63
-32
lines changed

src/index.js

+63-32
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function processNodes(tree, options, messages) {
5757

5858
options.expressions.locals = attributes;
5959

60-
options.expressions.locals.slots = getFilledSlotNames(options.slotTagName, node.content);
60+
options.expressions.locals.$slots = parseSlotsLocals(options.slotTagName, html, node.content);
6161

6262
const plugins = [...options.plugins, expressions(options.expressions)];
6363

@@ -169,6 +169,64 @@ function parseLocals(options, {attrs}, html) {
169169
return {attributes, locals};
170170
}
171171

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+
172230
/**
173231
* Merge slots content
174232
* @param {Object} tree
@@ -181,11 +239,12 @@ function parseLocals(options, {attrs}, html) {
181239
function mergeSlots(tree, node, strict, {slotTagName, fallbackSlotTagName}) {
182240
const slots = getSlots(slotTagName, tree, fallbackSlotTagName); // Slot in component.html
183241
const fillSlots = getSlots(slotTagName, node.content); // Slot in page.html
242+
const clean = content => content.replace(/(\n|\t)/g, '').trim();
184243

185244
// Retrieve main content, means everything that is not inside slots
186245
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) {
189248
fillSlots[defaultSlotName] = [{tag: slotTagName, attrs: {name: defaultSlotName}, content: [...contentOutsideSlots]}];
190249
}
191250
}
@@ -222,6 +281,7 @@ function mergeSlots(tree, node, strict, {slotTagName, fallbackSlotTagName}) {
222281
);
223282
}
224283

284+
225285
delete fillSlots[slotName];
226286
}
227287

@@ -304,35 +364,6 @@ function getSlots(tag, content = [], fallbackSlotTagName = false) {
304364
return slots;
305365
}
306366

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-
336367
/**
337368
* Apply plugins to tree
338369
* @param {Object} tree

0 commit comments

Comments
 (0)