[IMP] app, compiler: introduce t-out

t-out automatically escaped content when it is a string not marked
with the `markup` function

t-out renders the raw content if it is a Block, or if it has been marked
with the `markup` funtion.

t-esc has been kept since it is safe and is optimized to render text nodes.

all t-raw calls are in fact the same as t-out.
This commit is contained in:
Lucas Perais (lpe)
2021-11-15 15:01:56 +01:00
committed by Aaron Bohy
parent 1761af9c24
commit b902edc1be
62 changed files with 1780 additions and 1306 deletions
+7 -7
View File
@@ -15,7 +15,7 @@ import {
ASTTForEach,
ASTTif,
ASTTKey,
ASTTRaw,
ASTTOut,
ASTTSet,
ASTTranslation,
ASTType,
@@ -208,7 +208,7 @@ export class CodeGenerator {
// define blocks and utility functions
this.addLine(`let { text, createBlock, list, multi, html, toggler, component } = bdom;`);
this.addLine(
`let { withDefault, getTemplate, prepareList, withKey, zero, call, callSlot, capture, isBoundary, shallowEqual, setContextValue, toNumber } = helpers;`
`let { withDefault, getTemplate, prepareList, withKey, zero, call, callSlot, capture, isBoundary, shallowEqual, setContextValue, toNumber, safeOutput } = helpers;`
);
if (this.shouldDefineAssign) {
this.addLine(`let assign = Object.assign;`);
@@ -377,8 +377,8 @@ export class CodeGenerator {
case ASTType.TEsc:
this.compileTEsc(ast, ctx);
break;
case ASTType.TRaw:
this.compileTRaw(ast, ctx);
case ASTType.TOut:
this.compileTOut(ast, ctx);
break;
case ASTType.TIf:
this.compileTIf(ast, ctx);
@@ -640,20 +640,20 @@ export class CodeGenerator {
}
}
compileTRaw(ast: ASTTRaw, ctx: Context) {
compileTOut(ast: ASTTOut, ctx: Context) {
let { block } = ctx;
if (block) {
this.insertAnchor(block);
}
block = this.createBlock(block, "html", ctx);
let expr = ast.expr === "0" ? "ctx[zero]" : compileExpr(ast.expr);
let expr = ast.expr === "0" ? "ctx[zero]" : `safeOutput(${compileExpr(ast.expr)})`;
if (ast.body) {
const nextId = BlockDescription.nextBlockId;
const subCtx: Context = createContext(ctx);
this.compileAST({ type: ASTType.Multi, content: ast.body }, subCtx);
expr = `withDefault(${expr}, b${nextId})`;
}
this.insertBlock(`html(${expr})`, block, ctx);
this.insertBlock(`${expr}`, block, ctx);
}
compileTIf(ast: ASTTif, ctx: Context, nextNode?: ASTDomNode) {