import { TrustedTypePolicy, TrustedTypePolicyFactory } from 'trusted-types/lib'; import { ChangeMethodReturnType } from '../types/types.d.js'; /** * Type representation of return values of `getAttributeType` and * `getPropertyType` methods of native Trusted Types API. * * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicyFactory/getAttributeType} * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicyFactory/getPropertyType} */ type TrustedType = 'TrustedHTML' | 'TrustedScript' | 'TrustedScriptURL'; /** * Type representation of Trusted Types enum. * * NOTE: This is intentionally interface not enum to prevent * overlapping with actual enum provided by CoreLibs. * * @see {@link TrustedType} for more information. */ interface TrustedTypeEnum { HTML: 'TrustedHTML'; Script: 'TrustedScript'; ScriptURL: 'TrustedScriptURL'; } /** * Trusted Types Policy API utilities. * * This interface extends the native `TrustedTypePolicy` and `TrustedTypePolicyFactory` * to provide a more user-friendly API for working with Trusted Types. In case if * environment doesn't support Trusted Types API, it provides polyfilled methods * and properties to ensure compatibility. */ interface PolicyApi extends RemappedTrustedTypePolicy, FactoryStaticMethods { /** * Is Trusted Types API supported. */ isSupported: boolean; /** * TrustedType enum attached to PolicyApi. * * Reason why we attach it to instance because inside * of script and scriptlet we can't import and to not * pollute global env with custom variables. * * @example * api.policy.TrustedType.HTML // "TrustedHTML" */ TrustedType: TrustedTypeEnum; /** * Creates Trusted Type depending on `type`: * - `TrustedHTML` * - `TrustedScript` * - `TrustedScriptURL` * - or returns back `input` if none of them applicable. * * @example * divElement.innerHTML = api.policy.create(api.policy.TrustedType.HTML, '
'); * * @param type Trusted Type. * @param input Input from which creates Trusted Type. * @returns Created value. */ create(type: TrustedType, input: string): string; /** * Converts `value` of `attribute` into one of the Trusted Types: * - `TrustedHTML` * - `TrustedScript` * - `TrustedScriptURL` * - or returns back `value` if none of them applicable (`null`). * * @example * const trustedScriptURL = api.policy.convertAttributeToTrusted("script", "src", 'SOME_URL'); * scriptElement.setAttribute("src", trustedScriptURL); * * @param tagName Name of an HTML tag. * @param attribute Attribute. * @param value Value of attribute that needs to be converted. * @param elementNS Element namespace, if empty defaults to the HTML namespace. * @param attrNS Attribute namespace, if empty defaults to null. * @returns Converted value. */ convertAttributeToTrusted(tagName: string, attribute: string, value: string, elementNS?: string, attrNS?: string): string; /** * Converts `value` of `property` into one of the Trusted Types: * - `TrustedHTML` * - `TrustedScript` * - `TrustedScriptURL` * - or returns back `value` if none of them applicable (`null`). * * @example * divElement.innerHTML = api.policy.convertPropertyToTrusted("div", "innerHTML", ""); * * @param tagName Name of an HTML tag. * @param property Property. * @param value Value or property. * @param elementNS Element namespace, if empty defaults to the HTML namespace. * @returns Converted value. */ convertPropertyToTrusted(tagName: string, property: string, value: string, elementNS?: string): string; } /** * Names of "is" methods of native `TrustedTypePolicyFactory`. */ type IsMethodNames = 'isHTML' | 'isScript' | 'isScriptURL'; /** * Remapped `TrustedTypePolicyFactory` by changing return type of "is" methods to `boolean`: * `(value: unknown) => value is TrustedSomething` -> `(value: unknown) => boolean`. * * This is needed because Trusted Types are not supported in `lib.dom.d.ts` types. * For example: `TrustedScript` is not declared globally, so `isScript` method * will return `boolean` instead of `value is TrustedScript`. */ type RemappedTrustedTypePolicyFactory = ChangeMethodReturnType