Path: blob/main/src/vs/base/browser/dompurify/dompurify.d.ts
5263 views
/*! @license DOMPurify 3.2.7 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.2.7/LICENSE */12import type { TrustedTypePolicy, TrustedHTML, TrustedTypesWindow } from 'trusted-types/lib/index.d.ts';34/**5* Configuration to control DOMPurify behavior.6*/7interface Config {8/**9* Extend the existing array of allowed attributes.10*/11ADD_ATTR?: string[] | undefined;12/**13* Extend the existing array of elements that can use Data URIs.14*/15ADD_DATA_URI_TAGS?: string[] | undefined;16/**17* Extend the existing array of allowed tags.18*/19ADD_TAGS?: string[] | undefined;20/**21* Extend the existing array of elements that are safe for URI-like values (be careful, XSS risk).22*/23ADD_URI_SAFE_ATTR?: string[] | undefined;24/**25* Allow ARIA attributes, leave other safe HTML as is (default is true).26*/27ALLOW_ARIA_ATTR?: boolean | undefined;28/**29* Allow HTML5 data attributes, leave other safe HTML as is (default is true).30*/31ALLOW_DATA_ATTR?: boolean | undefined;32/**33* Allow external protocol handlers in URL attributes (default is false, be careful, XSS risk).34* By default only `http`, `https`, `ftp`, `ftps`, `tel`, `mailto`, `callto`, `sms`, `cid` and `xmpp` are allowed.35*/36ALLOW_UNKNOWN_PROTOCOLS?: boolean | undefined;37/**38* Decide if self-closing tags in attributes are allowed.39* Usually removed due to a mXSS issue in jQuery 3.0.40*/41ALLOW_SELF_CLOSE_IN_ATTR?: boolean | undefined;42/**43* Allow only specific attributes.44*/45ALLOWED_ATTR?: string[] | undefined;46/**47* Allow only specific elements.48*/49ALLOWED_TAGS?: string[] | undefined;50/**51* Allow only specific namespaces. Defaults to:52* - `http://www.w3.org/1999/xhtml`53* - `http://www.w3.org/2000/svg`54* - `http://www.w3.org/1998/Math/MathML`55*/56ALLOWED_NAMESPACES?: string[] | undefined;57/**58* Allow specific protocols handlers in URL attributes via regex (be careful, XSS risk).59* Default RegExp:60* ```61* /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i;62* ```63*/64ALLOWED_URI_REGEXP?: RegExp | undefined;65/**66* Define how custom elements are handled.67*/68CUSTOM_ELEMENT_HANDLING?: {69/**70* Regular expression or function to match to allowed elements.71* Default is null (disallow any custom elements).72*/73tagNameCheck?: RegExp | ((tagName: string) => boolean) | null | undefined;74/**75* Regular expression or function to match to allowed attributes.76* Default is null (disallow any attributes not on the allow list).77*/78attributeNameCheck?: RegExp | ((attributeName: string, tagName?: string) => boolean) | null | undefined;79/**80* Allow custom elements derived from built-ins if they pass `tagNameCheck`. Default is false.81*/82allowCustomizedBuiltInElements?: boolean | undefined;83};84/**85* Add attributes to block-list.86*/87FORBID_ATTR?: string[] | undefined;88/**89* Add child elements to be removed when their parent is removed.90*/91FORBID_CONTENTS?: string[] | undefined;92/**93* Add elements to block-list.94*/95FORBID_TAGS?: string[] | undefined;96/**97* Glue elements like style, script or others to `document.body` and prevent unintuitive browser behavior in several edge-cases (default is false).98*/99FORCE_BODY?: boolean | undefined;100/**101* Map of non-standard HTML element names to support. Map to true to enable support. For example:102*103* ```104* HTML_INTEGRATION_POINTS: { foreignobject: true }105* ```106*/107HTML_INTEGRATION_POINTS?: Record<string, boolean> | undefined;108/**109* Sanitize a node "in place", which is much faster depending on how you use DOMPurify.110*/111IN_PLACE?: boolean | undefined;112/**113* Keep an element's content when the element is removed (default is true).114*/115KEEP_CONTENT?: boolean | undefined;116/**117* Map of MathML element names to support. Map to true to enable support. For example:118*119* ```120* MATHML_TEXT_INTEGRATION_POINTS: { mtext: true }121* ```122*/123MATHML_TEXT_INTEGRATION_POINTS?: Record<string, boolean> | undefined;124/**125* Change the default namespace from HTML to something different.126*/127NAMESPACE?: string | undefined;128/**129* Change the parser type so sanitized data is treated as XML and not as HTML, which is the default.130*/131PARSER_MEDIA_TYPE?: DOMParserSupportedType | undefined;132/**133* Return a DOM `DocumentFragment` instead of an HTML string (default is false).134*/135RETURN_DOM_FRAGMENT?: boolean | undefined;136/**137* Return a DOM `HTMLBodyElement` instead of an HTML string (default is false).138*/139RETURN_DOM?: boolean | undefined;140/**141* Return a TrustedHTML object instead of a string if possible.142*/143RETURN_TRUSTED_TYPE?: boolean | undefined;144/**145* Strip `{{ ... }}`, `${ ... }` and `<% ... %>` to make output safe for template systems.146* Be careful please, this mode is not recommended for production usage.147* Allowing template parsing in user-controlled HTML is not advised at all.148* Only use this mode if there is really no alternative.149*/150SAFE_FOR_TEMPLATES?: boolean | undefined;151/**152* Change how e.g. comments containing risky HTML characters are treated.153* Be very careful, this setting should only be set to `false` if you really only handle154* HTML and nothing else, no SVG, MathML or the like.155* Otherwise, changing from `true` to `false` will lead to XSS in this or some other way.156*/157SAFE_FOR_XML?: boolean | undefined;158/**159* Use DOM Clobbering protection on output (default is true, handle with care, minor XSS risks here).160*/161SANITIZE_DOM?: boolean | undefined;162/**163* Enforce strict DOM Clobbering protection via namespace isolation (default is false).164* When enabled, isolates the namespace of named properties (i.e., `id` and `name` attributes)165* from JS variables by prefixing them with the string `user-content-`166*/167SANITIZE_NAMED_PROPS?: boolean | undefined;168/**169* Supplied policy must define `createHTML` and `createScriptURL`.170*/171TRUSTED_TYPES_POLICY?: TrustedTypePolicy | undefined;172/**173* Controls categories of allowed elements.174*175* Note that the `USE_PROFILES` setting will override the `ALLOWED_TAGS` setting176* so don't use them together.177*/178USE_PROFILES?: false | UseProfilesConfig | undefined;179/**180* Return entire document including <html> tags (default is false).181*/182WHOLE_DOCUMENT?: boolean | undefined;183}184/**185* Defines categories of allowed elements.186*/187interface UseProfilesConfig {188/**189* Allow all safe MathML elements.190*/191mathMl?: boolean | undefined;192/**193* Allow all safe SVG elements.194*/195svg?: boolean | undefined;196/**197* Allow all save SVG Filters.198*/199svgFilters?: boolean | undefined;200/**201* Allow all safe HTML elements.202*/203html?: boolean | undefined;204}205206declare const _default: DOMPurify;207208interface DOMPurify {209/**210* Creates a DOMPurify instance using the given window-like object. Defaults to `window`.211*/212(root?: WindowLike): DOMPurify;213/**214* Version label, exposed for easier checks215* if DOMPurify is up to date or not216*/217version: string;218/**219* Array of elements that DOMPurify removed during sanitation.220* Empty if nothing was removed.221*/222removed: Array<RemovedElement | RemovedAttribute>;223/**224* Expose whether this browser supports running the full DOMPurify.225*/226isSupported: boolean;227/**228* Set the configuration once.229*230* @param cfg configuration object231*/232setConfig(cfg?: Config): void;233/**234* Removes the configuration.235*/236clearConfig(): void;237/**238* Provides core sanitation functionality.239*240* @param dirty string or DOM node241* @param cfg object242* @returns Sanitized TrustedHTML.243*/244sanitize(dirty: string | Node, cfg: Config & {245RETURN_TRUSTED_TYPE: true;246}): TrustedHTML;247/**248* Provides core sanitation functionality.249*250* @param dirty DOM node251* @param cfg object252* @returns Sanitized DOM node.253*/254sanitize(dirty: Node, cfg: Config & {255IN_PLACE: true;256}): Node;257/**258* Provides core sanitation functionality.259*260* @param dirty string or DOM node261* @param cfg object262* @returns Sanitized DOM node.263*/264sanitize(dirty: string | Node, cfg: Config & {265RETURN_DOM: true;266}): Node;267/**268* Provides core sanitation functionality.269*270* @param dirty string or DOM node271* @param cfg object272* @returns Sanitized document fragment.273*/274sanitize(dirty: string | Node, cfg: Config & {275RETURN_DOM_FRAGMENT: true;276}): DocumentFragment;277/**278* Provides core sanitation functionality.279*280* @param dirty string or DOM node281* @param cfg object282* @returns Sanitized string.283*/284sanitize(dirty: string | Node, cfg?: Config): string;285/**286* Checks if an attribute value is valid.287* Uses last set config, if any. Otherwise, uses config defaults.288*289* @param tag Tag name of containing element.290* @param attr Attribute name.291* @param value Attribute value.292* @returns Returns true if `value` is valid. Otherwise, returns false.293*/294isValidAttribute(tag: string, attr: string, value: string): boolean;295/**296* Adds a DOMPurify hook.297*298* @param entryPoint entry point for the hook to add299* @param hookFunction function to execute300*/301addHook(entryPoint: BasicHookName, hookFunction: NodeHook): void;302/**303* Adds a DOMPurify hook.304*305* @param entryPoint entry point for the hook to add306* @param hookFunction function to execute307*/308addHook(entryPoint: ElementHookName, hookFunction: ElementHook): void;309/**310* Adds a DOMPurify hook.311*312* @param entryPoint entry point for the hook to add313* @param hookFunction function to execute314*/315addHook(entryPoint: DocumentFragmentHookName, hookFunction: DocumentFragmentHook): void;316/**317* Adds a DOMPurify hook.318*319* @param entryPoint entry point for the hook to add320* @param hookFunction function to execute321*/322addHook(entryPoint: 'uponSanitizeElement', hookFunction: UponSanitizeElementHook): void;323/**324* Adds a DOMPurify hook.325*326* @param entryPoint entry point for the hook to add327* @param hookFunction function to execute328*/329addHook(entryPoint: 'uponSanitizeAttribute', hookFunction: UponSanitizeAttributeHook): void;330/**331* Remove a DOMPurify hook at a given entryPoint332* (pops it from the stack of hooks if hook not specified)333*334* @param entryPoint entry point for the hook to remove335* @param hookFunction optional specific hook to remove336* @returns removed hook337*/338removeHook(entryPoint: BasicHookName, hookFunction?: NodeHook): NodeHook | undefined;339/**340* Remove a DOMPurify hook at a given entryPoint341* (pops it from the stack of hooks if hook not specified)342*343* @param entryPoint entry point for the hook to remove344* @param hookFunction optional specific hook to remove345* @returns removed hook346*/347removeHook(entryPoint: ElementHookName, hookFunction?: ElementHook): ElementHook | undefined;348/**349* Remove a DOMPurify hook at a given entryPoint350* (pops it from the stack of hooks if hook not specified)351*352* @param entryPoint entry point for the hook to remove353* @param hookFunction optional specific hook to remove354* @returns removed hook355*/356removeHook(entryPoint: DocumentFragmentHookName, hookFunction?: DocumentFragmentHook): DocumentFragmentHook | undefined;357/**358* Remove a DOMPurify hook at a given entryPoint359* (pops it from the stack of hooks if hook not specified)360*361* @param entryPoint entry point for the hook to remove362* @param hookFunction optional specific hook to remove363* @returns removed hook364*/365removeHook(entryPoint: 'uponSanitizeElement', hookFunction?: UponSanitizeElementHook): UponSanitizeElementHook | undefined;366/**367* Remove a DOMPurify hook at a given entryPoint368* (pops it from the stack of hooks if hook not specified)369*370* @param entryPoint entry point for the hook to remove371* @param hookFunction optional specific hook to remove372* @returns removed hook373*/374removeHook(entryPoint: 'uponSanitizeAttribute', hookFunction?: UponSanitizeAttributeHook): UponSanitizeAttributeHook | undefined;375/**376* Removes all DOMPurify hooks at a given entryPoint377*378* @param entryPoint entry point for the hooks to remove379*/380removeHooks(entryPoint: HookName): void;381/**382* Removes all DOMPurify hooks.383*/384removeAllHooks(): void;385}386/**387* An element removed by DOMPurify.388*/389interface RemovedElement {390/**391* The element that was removed.392*/393element: Node;394}395/**396* An element removed by DOMPurify.397*/398interface RemovedAttribute {399/**400* The attribute that was removed.401*/402attribute: Attr | null;403/**404* The element that the attribute was removed.405*/406from: Node;407}408type BasicHookName = 'beforeSanitizeElements' | 'afterSanitizeElements' | 'uponSanitizeShadowNode';409type ElementHookName = 'beforeSanitizeAttributes' | 'afterSanitizeAttributes';410type DocumentFragmentHookName = 'beforeSanitizeShadowDOM' | 'afterSanitizeShadowDOM';411type UponSanitizeElementHookName = 'uponSanitizeElement';412type UponSanitizeAttributeHookName = 'uponSanitizeAttribute';413type HookName = BasicHookName | ElementHookName | DocumentFragmentHookName | UponSanitizeElementHookName | UponSanitizeAttributeHookName;414type NodeHook = (this: DOMPurify, currentNode: Node, hookEvent: null, config: Config) => void;415type ElementHook = (this: DOMPurify, currentNode: Element, hookEvent: null, config: Config) => void;416type DocumentFragmentHook = (this: DOMPurify, currentNode: DocumentFragment, hookEvent: null, config: Config) => void;417type UponSanitizeElementHook = (this: DOMPurify, currentNode: Node, hookEvent: UponSanitizeElementHookEvent, config: Config) => void;418type UponSanitizeAttributeHook = (this: DOMPurify, currentNode: Element, hookEvent: UponSanitizeAttributeHookEvent, config: Config) => void;419interface UponSanitizeElementHookEvent {420tagName: string;421allowedTags: Record<string, boolean>;422}423interface UponSanitizeAttributeHookEvent {424attrName: string;425attrValue: string;426keepAttr: boolean;427allowedAttributes: Record<string, boolean>;428forceKeepAttr: boolean | undefined;429}430/**431* A `Window`-like object containing the properties and types that DOMPurify requires.432*/433type WindowLike = Pick<typeof globalThis, 'DocumentFragment' | 'HTMLTemplateElement' | 'Node' | 'Element' | 'NodeFilter' | 'NamedNodeMap' | 'HTMLFormElement' | 'DOMParser'> & {434document?: Document;435MozNamedAttrMap?: typeof window.NamedNodeMap;436} & Pick<TrustedTypesWindow, 'trustedTypes'>;437438export { type Config, type DOMPurify, type DocumentFragmentHook, type ElementHook, type HookName, type NodeHook, type RemovedAttribute, type RemovedElement, type UponSanitizeAttributeHook, type UponSanitizeAttributeHookEvent, type UponSanitizeElementHook, type UponSanitizeElementHookEvent, type WindowLike, _default as default };439440441