[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 Géry Debongnie
parent 2806cda420
commit 6ea40a66da
62 changed files with 1780 additions and 1306 deletions
+26
View File
@@ -1,5 +1,7 @@
import { BDom, multi, text, toggler } from "../blockdom";
import { validateProps } from "../component/props_validation";
import { Markup } from "../utils";
import { html } from "../blockdom/index";
/**
* This file contains utility functions that will be injected in each template,
@@ -94,6 +96,29 @@ function shallowEqual(l1: any[], l2: any[]): boolean {
return true;
}
/*
* Safely outputs `value` as a block depending on the nature of `value`
*/
export function safeOutput(value: any): ReturnType<typeof toggler> {
if (!value) {
return value;
}
let safeKey;
let block;
if (value instanceof Markup) {
safeKey = `string_safe`;
block = html(value as string);
} else if (typeof value === "string") {
safeKey = "string_unsafe";
block = text(value);
} else {
// Assuming it is a block
safeKey = "block_safe";
block = value;
}
return toggler(safeKey, block);
}
export const UTILS = {
withDefault,
zero: Symbol("zero"),
@@ -106,4 +131,5 @@ export const UTILS = {
shallowEqual,
toNumber,
validateProps,
safeOutput,
};