[FIX] parser: give t-set-slot="default" priority over the content

Currently, if a component has a default slot defined with t-set-slot,
and also content that compiles to something (eg, text or even a comment
node), the content takes priority over the t-set-slot. As t-set-slot is
more explicity, it should have priority.
This commit is contained in:
Samuel Degueldre
2022-10-10 13:54:20 +02:00
committed by Géry Debongnie
parent d546244fc3
commit ba1a270c93
3 changed files with 50 additions and 2 deletions
+3 -2
View File
@@ -781,8 +781,9 @@ function parseComponent(node: Element, ctx: ParsingContext): AST | null {
// default slot
const defaultContent = parseChildNodes(clone, ctx);
if (defaultContent) {
slots = slots || {};
slots = slots || {};
// t-set-slot="default" has priority over content
if (defaultContent && !slots.default) {
slots.default = { content: defaultContent, on, attrs: null, scope: defaultSlotScope };
}
}
@@ -2835,6 +2835,36 @@ exports[`slots t-set t-value in a slot 2`] = `
}"
`;
exports[`slots t-set-slot=default has priority over rest of the content 1`] = `
"function anonymous(app, bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
let { capture, markRaw } = helpers;
const comp1 = app.createComponent(\`Child\`, true, true, false, true);
function slot1(ctx, node, key = \\"\\") {
return text(\`some other text\`);
}
return function template(ctx, node, key = \\"\\") {
const ctx1 = capture(ctx);
return comp1({slots: markRaw({'default': {__render: slot1, __ctx: ctx1}})}, key + \`__1\`, node, this, null);
}
}"
`;
exports[`slots t-set-slot=default has priority over rest of the content 2`] = `
"function anonymous(app, bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
let { callSlot } = helpers;
return function template(ctx, node, key = \\"\\") {
return callSlot(ctx, node, key, 'default', false, {});
}
}"
`;
exports[`slots t-slot in recursive templates 1`] = `
"function anonymous(app, bdom, helpers
) {
+17
View File
@@ -45,6 +45,23 @@ describe("slots", () => {
expect(fixture.innerHTML).toBe("some text");
});
test("t-set-slot=default has priority over rest of the content", async () => {
class Child extends Component {
static template = xml`<t t-slot="default"/>`;
}
class Parent extends Component {
static template = xml`<Child>
some text
<t t-set-slot="default">some other text</t>
</Child>`;
static components = { Child };
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe("some other text");
});
test("simple slot with slot scope", async () => {
let child: any;
class Child extends Component {