diff --git a/src/app/template_helpers.ts b/src/app/template_helpers.ts index a641950d..36de4dad 100644 --- a/src/app/template_helpers.ts +++ b/src/app/template_helpers.ts @@ -135,7 +135,7 @@ export function safeOutput(value: any): ReturnType { } else if (value instanceof LazyValue) { safeKey = `lazy_value`; block = value.evaluate(); - } else if (typeof value === "string") { + } else if (value instanceof String || typeof value === "string") { safeKey = "string_unsafe"; block = text(value); } else { diff --git a/src/blockdom/text.ts b/src/blockdom/text.ts index 6bf7d882..25da7baf 100644 --- a/src/blockdom/text.ts +++ b/src/blockdom/text.ts @@ -9,11 +9,11 @@ const characterDataSetData = getDescriptor(characterDataProto, "data").set!; const nodeRemoveChild = nodeProto.removeChild; abstract class VSimpleNode { - text: string; + text: string | String; parentEl?: HTMLElement | undefined; el?: any; - constructor(text: string) { + constructor(text: string | String) { this.text = text; } @@ -65,7 +65,7 @@ class VComment extends VSimpleNode { patch() {} } -export function text(str: string): VNode { +export function text(str: string | String): VNode { return new VText(str); } diff --git a/tests/compiler/__snapshots__/t_out.test.ts.snap b/tests/compiler/__snapshots__/t_out.test.ts.snap index cf76a615..e8e7ba51 100644 --- a/tests/compiler/__snapshots__/t_out.test.ts.snap +++ b/tests/compiler/__snapshots__/t_out.test.ts.snap @@ -407,6 +407,36 @@ exports[`t-out variable 1`] = ` }" `; +exports[`t-out with a String class 1`] = ` +"function anonymous(bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, component, comment } = bdom; + let { safeOutput } = helpers; + + let block1 = createBlock(\`\`); + + return function template(ctx, node, key = \\"\\") { + let b2 = safeOutput(ctx['var']); + return block1([], [b2]); + } +}" +`; + +exports[`t-out with an extended String class 1`] = ` +"function anonymous(bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, component, comment } = bdom; + let { safeOutput } = helpers; + + let block1 = createBlock(\`\`); + + return function template(ctx, node, key = \\"\\") { + let b2 = safeOutput(ctx['var']); + return block1([], [b2]); + } +}" +`; + exports[`t-raw is deprecated should warn 1`] = ` "function anonymous(bdom, helpers ) { diff --git a/tests/compiler/t_out.test.ts b/tests/compiler/t_out.test.ts index dd5eaa89..a1a44ac9 100644 --- a/tests/compiler/t_out.test.ts +++ b/tests/compiler/t_out.test.ts @@ -37,6 +37,26 @@ describe("t-out", () => { expect(renderToString(template, { var: "ok" })).toBe("ok"); }); + test("with a String class", () => { + const template = ``; + expect(renderToString(template, { var: new String("ok") })).toBe("ok"); + }); + + test("with an extended String class", () => { + class LoveString extends String { + valueOf(): string { + return `<3 ${super.valueOf()} <3`; + } + toString(): string { + return this.valueOf(); + } + } + const template = ``; + expect(renderToString(template, { var: new LoveString("ok") })).toBe( + "<3 ok <3" + ); + }); + test("not escaping", () => { const template = `
`; expect(renderToString(template, { var: markup("") })).toBe("
");