From 562467386aa381bc1b1cae9bfb53a581cf4a1a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Tesa=C5=99?= Date: Tue, 10 Sep 2019 06:19:38 +0200 Subject: [PATCH] Update trusted types definitions (#38092) --- types/trusted-types/index.d.ts | 22 ++++++++++-- types/trusted-types/trusted-types-tests.ts | 39 ++++++++++++---------- types/trusted-types/tsconfig.json | 3 +- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/types/trusted-types/index.d.ts b/types/trusted-types/index.d.ts index 78949ba997..6de9054b42 100644 --- a/types/trusted-types/index.d.ts +++ b/types/trusted-types/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/WICG/trusted-types // Definitions by: Jakub Vrana , // Damien Engels +// Emanuel Tesar // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.1 @@ -42,13 +43,30 @@ declare class TrustedTypePolicyFactory { policyOptions: Pick, expose?: boolean, ): Pick; - getExposedPolicy(name: string): TrustedTypePolicy|null; getPolicyNames(): string[]; isHTML(value: any): value is TrustedHTML; isScript(value: any): value is TrustedScript; isScriptURL(value: any): value is TrustedScriptURL; isURL(value: any): value is TrustedURL; + getAttributeType(tagName: string, attrName: string, elemNs?: string, attrNs?: string): string|null; + getPropertyType(tagName: string, propName: string, elemNs?: string): string|null; + defaultPolicy?: TrustedTypePolicy; + emptyHTML: TrustedHTML; } -declare const TrustedTypes: TrustedTypePolicyFactory; +declare const trustedTypes: TrustedTypePolicyFactory; + +declare global { + interface Window { + trustedTypes: TrustedTypePolicyFactory; + TrustedHTML: TrustedHTML; + TrustedScript: TrustedScript; + TrustedScriptURL: TrustedScriptURL; + TrustedURL: TrustedURL; + TrustedTypePolicyOptions: TrustedTypePolicyOptions; + TrustedTypePolicyFactory: TrustedTypePolicyFactory; + } +} + +export default trustedTypes; diff --git a/types/trusted-types/trusted-types-tests.ts b/types/trusted-types/trusted-types-tests.ts index e1f754288b..81931de820 100644 --- a/types/trusted-types/trusted-types-tests.ts +++ b/types/trusted-types/trusted-types-tests.ts @@ -1,4 +1,9 @@ -const policy = { +import TT from 'trusted-types'; + +// $ExpectType TrustedTypePolicyFactory +TT; + +const rules = { createHTML: (s: string) => s, createScript: (s: string) => s, createScriptURL: (s: string) => s, @@ -6,42 +11,40 @@ const policy = { }; // $ExpectType string[] -TrustedTypes.getPolicyNames(); -TrustedTypes.createPolicy('default', policy, true); -// $ExpectType TrustedTypePolicy | null -TrustedTypes.getExposedPolicy('default'); +window.trustedTypes.getPolicyNames(); +window.trustedTypes.createPolicy('default', rules, true); -const trustedTypes = TrustedTypes.createPolicy('test', policy); +const policy = window.trustedTypes.createPolicy('test', rules); // $ExpectType string -const policyName = trustedTypes.name; +policy.name; // $ExpectType TrustedHTML -trustedTypes.createHTML(''); +policy.createHTML(''); // $ExpectType TrustedScript -trustedTypes.createScript(''); +policy.createScript(''); // $ExpectType TrustedScriptURL -trustedTypes.createScriptURL(''); +policy.createScriptURL(''); // $ExpectType TrustedURL -trustedTypes.createURL(''); +policy.createURL(''); -const htmlOnlyPolicy = TrustedTypes.createPolicy('htmlOnly', { +const htmlOnlyPolicy = window.trustedTypes.createPolicy('htmlOnly', { createHTML: (html: string) => { return html; }, }); // $ExpectType string -const htmlOnlyName = htmlOnlyPolicy.name; +htmlOnlyPolicy.name; // $ExpectType TrustedHTML const html = htmlOnlyPolicy.createHTML(''); // $ExpectError -const script = htmlOnlyPolicy.createScript(''); +htmlOnlyPolicy.createScript(''); // $ExpectType boolean -TrustedTypes.isHTML(html); +window.trustedTypes.isHTML(html); // $ExpectType boolean -TrustedTypes.isScript(html); +window.trustedTypes.isScript(html); // $ExpectType boolean -TrustedTypes.isScriptURL(html); +window.trustedTypes.isScriptURL(html); // $ExpectType boolean -TrustedTypes.isURL(html); +window.trustedTypes.isURL(html); diff --git a/types/trusted-types/tsconfig.json b/types/trusted-types/tsconfig.json index 8bfe83a032..0c92eb35f0 100644 --- a/types/trusted-types/tsconfig.json +++ b/types/trusted-types/tsconfig.json @@ -2,7 +2,8 @@ "compilerOptions": { "module": "commonjs", "lib": [ - "es6" + "es6", + "dom" ], "noImplicitAny": true, "noImplicitThis": true,