[FIX] t-out: allow expressions evaluating as number

This commit is contained in:
Géry Debongnie
2022-06-07 10:33:59 +02:00
committed by Sam Degueldre
parent 1fc88f626f
commit a3111eb9ca
3 changed files with 44 additions and 13 deletions
+12 -1
View File
@@ -132,13 +132,15 @@ export function safeOutput(value: any): ReturnType<typeof toggler> {
} }
let safeKey; let safeKey;
let block; let block;
switch (typeof value) {
case "object":
if (value instanceof Markup) { if (value instanceof Markup) {
safeKey = `string_safe`; safeKey = `string_safe`;
block = html(value as string); block = html(value as string);
} else if (value instanceof LazyValue) { } else if (value instanceof LazyValue) {
safeKey = `lazy_value`; safeKey = `lazy_value`;
block = value.evaluate(); block = value.evaluate();
} else if (value instanceof String || typeof value === "string") { } else if (value instanceof String) {
safeKey = "string_unsafe"; safeKey = "string_unsafe";
block = text(value); block = text(value);
} else { } else {
@@ -146,6 +148,15 @@ export function safeOutput(value: any): ReturnType<typeof toggler> {
safeKey = "block_safe"; safeKey = "block_safe";
block = value; block = value;
} }
break;
case "string":
safeKey = "string_unsafe";
block = text(value);
break;
default:
safeKey = "string_unsafe";
block = text(String(value));
}
return toggler(safeKey, block); return toggler(safeKey, block);
} }
@@ -79,6 +79,21 @@ exports[`t-out not escaping 1`] = `
}" }"
`; `;
exports[`t-out number literal 1`] = `
"function anonymous(app, bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
let { safeOutput } = helpers;
let block1 = createBlock(\`<span><block-child-0/></span>\`);
return function template(ctx, node, key = \\"\\") {
const b2 = safeOutput(1);
return block1([], [b2]);
}
}"
`;
exports[`t-out t-out 0 1`] = ` exports[`t-out t-out 0 1`] = `
"function anonymous(app, bdom, helpers "function anonymous(app, bdom, helpers
) { ) {
+5
View File
@@ -27,6 +27,11 @@ describe("t-out", () => {
expect(renderToString(template)).toBe("<span>ok</span>"); expect(renderToString(template)).toBe("<span>ok</span>");
}); });
test("number literal", () => {
const template = `<span><t t-out="1"/></span>`;
expect(renderToString(template)).toBe("<span>1</span>");
});
test("literal, no outside html element", () => { test("literal, no outside html element", () => {
const template = `<t t-out="'ok'"/>`; const template = `<t t-out="'ok'"/>`;
expect(renderToString(template)).toBe("ok"); expect(renderToString(template)).toBe("ok");