From 0ca5991aa25e00d2bab25067937ab32d698c0d83 Mon Sep 17 00:00:00 2001 From: thetutlage Date: Fri, 28 Feb 2020 07:44:34 +0530 Subject: [PATCH] feat: add context.safe to mark a value as safe --- src/Context/index.ts | 15 ++++++++++++++- src/Contracts/index.ts | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Context/index.ts b/src/Context/index.ts index 06fe2bc..912a5c2 100644 --- a/src/Context/index.ts +++ b/src/Context/index.ts @@ -16,6 +16,10 @@ import { set } from 'lodash' import { Macroable } from 'macroable' import { ContextContract } from '../Contracts' +class SafeValue { + constructor (public value: any) {} +} + /** * Context is used at runtime to resolve values for a given * template. @@ -105,12 +109,21 @@ export class Context extends Macroable implements ContextContract { this._frames.shift() } + /** + * Mark output as safe + */ + public safe (value: T) { + return new SafeValue(value) + } + /** * Escapes the value to be HTML safe. Only strings are escaped * and rest all values will be returned as it is. */ public escape (input: T): T { - return typeof (input) === 'string' ? he.escape(input) : input + return typeof (input) === 'string' + ? he.escape(input) + : (input instanceof SafeValue ? input.value : input) } /** diff --git a/src/Contracts/index.ts b/src/Contracts/index.ts index 205d2ba..c09cbf7 100644 --- a/src/Contracts/index.ts +++ b/src/Contracts/index.ts @@ -66,6 +66,7 @@ export interface LoaderContract { export interface ContextContract { presenter: { state: any }, sharedState: any, + safe (value: T): { value: T }, newFrame (): void, setOnFrame (key: string, value: any): void, removeFrame (): void,