mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] component: allow using vars with body as props
Consider this scenario: - a variable v (with a body) is defined in a template - it is then passed to a sub component as a prop - and now, it is t-esc-ed. Before this commit, the displayed value was [object object], because the value actually passed to the sub component was a VDomArray (internal structure used to represent nodelists) This issue is actually quite a problem in practice, because values in a templates are translated, but not in attributes. Therefore, using a t-set directive with a body text content is the proper way to have translated values at runtime. We override in this commit the method toString of VDomArray to make sure it is properly displayed. Note that we considered changing the way props were generated (by trying to detect VDomArray, then calling vDomToString), but then the value would not be able to be used in a t-raw. Also, it is quite elegant to be able to format the VDomArray only at the end. closes #670
This commit is contained in:
@@ -1725,6 +1725,48 @@ describe("props evaluation ", () => {
|
||||
await widget.mount(fixture);
|
||||
expect(normalize(fixture.innerHTML)).toBe("<div><span>42</span></div>");
|
||||
});
|
||||
|
||||
test("t-set with a body expression can be used as textual prop", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`<span t-esc="props.val"/>`;
|
||||
}
|
||||
class Parent extends Component {
|
||||
static components = { Child };
|
||||
static template = xml`
|
||||
<div>
|
||||
<t t-set="abc">42</t>
|
||||
<Child val="abc"/>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
const widget = new Parent();
|
||||
await widget.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe("<div><span>42</span></div>");
|
||||
expect(env.qweb.templates[Parent.template].fn.toString()).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test("t-set with a body expression can be passed in props, and then t-raw", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`
|
||||
<span>
|
||||
<t t-esc="props.val"/>
|
||||
<t t-raw="props.val"/>
|
||||
</span>`;
|
||||
}
|
||||
class Parent extends Component {
|
||||
static components = { Child };
|
||||
static template = xml`
|
||||
<div>
|
||||
<t t-set="abc"><p>43</p></t>
|
||||
<Child val="abc"/>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
const widget = new Parent();
|
||||
await widget.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe("<div><span><p>4343</p><p>43</p></span></div>");
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("other directives with t-component", () => {
|
||||
|
||||
Reference in New Issue
Block a user