Skip to content

Commit e972e6e

Browse files
authored
Merge pull request #1031 from reduckted/feature/hook-parameter-types
Changed the type of the "node" in hooks to be as specific as possible
2 parents 83ce1cc + cdd33d3 commit e972e6e

File tree

10 files changed

+280
-152
lines changed

10 files changed

+280
-152
lines changed

dist/purify.cjs.d.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,21 @@ interface DOMPurify {
297297
* @param entryPoint entry point for the hook to add
298298
* @param hookFunction function to execute
299299
*/
300-
addHook(entryPoint: BasicHookName, hookFunction: Hook): void;
300+
addHook(entryPoint: BasicHookName, hookFunction: NodeHook): void;
301+
/**
302+
* Adds a DOMPurify hook.
303+
*
304+
* @param entryPoint entry point for the hook to add
305+
* @param hookFunction function to execute
306+
*/
307+
addHook(entryPoint: ElementHookName, hookFunction: ElementHook): void;
308+
/**
309+
* Adds a DOMPurify hook.
310+
*
311+
* @param entryPoint entry point for the hook to add
312+
* @param hookFunction function to execute
313+
*/
314+
addHook(entryPoint: DocumentFragmentHookName, hookFunction: DocumentFragmentHook): void;
301315
/**
302316
* Adds a DOMPurify hook.
303317
*
@@ -319,7 +333,23 @@ interface DOMPurify {
319333
* @param entryPoint entry point for the hook to remove
320334
* @returns removed(popped) hook
321335
*/
322-
removeHook(entryPoint: BasicHookName): Hook | undefined;
336+
removeHook(entryPoint: BasicHookName): NodeHook | undefined;
337+
/**
338+
* Remove a DOMPurify hook at a given entryPoint
339+
* (pops it from the stack of hooks if more are present)
340+
*
341+
* @param entryPoint entry point for the hook to remove
342+
* @returns removed(popped) hook
343+
*/
344+
removeHook(entryPoint: ElementHookName): ElementHook | undefined;
345+
/**
346+
* Remove a DOMPurify hook at a given entryPoint
347+
* (pops it from the stack of hooks if more are present)
348+
*
349+
* @param entryPoint entry point for the hook to remove
350+
* @returns removed(popped) hook
351+
*/
352+
removeHook(entryPoint: DocumentFragmentHookName): DocumentFragmentHook | undefined;
323353
/**
324354
* Remove a DOMPurify hook at a given entryPoint
325355
* (pops it from the stack of hooks if more are present)
@@ -369,13 +399,17 @@ interface RemovedAttribute {
369399
*/
370400
from: Node;
371401
}
372-
type BasicHookName = 'beforeSanitizeElements' | 'afterSanitizeElements' | 'beforeSanitizeAttributes' | 'afterSanitizeAttributes' | 'beforeSanitizeShadowDOM' | 'uponSanitizeShadowNode' | 'afterSanitizeShadowDOM';
402+
type BasicHookName = 'beforeSanitizeElements' | 'afterSanitizeElements' | 'uponSanitizeShadowNode';
403+
type ElementHookName = 'beforeSanitizeAttributes' | 'afterSanitizeAttributes';
404+
type DocumentFragmentHookName = 'beforeSanitizeShadowDOM' | 'afterSanitizeShadowDOM';
373405
type UponSanitizeElementHookName = 'uponSanitizeElement';
374406
type UponSanitizeAttributeHookName = 'uponSanitizeAttribute';
375-
type HookName = BasicHookName | UponSanitizeElementHookName | UponSanitizeAttributeHookName;
376-
type Hook = (this: DOMPurify, currentNode: Node, hookEvent: null, config: Config) => void;
407+
type HookName = BasicHookName | ElementHookName | DocumentFragmentHookName | UponSanitizeElementHookName | UponSanitizeAttributeHookName;
408+
type NodeHook = (this: DOMPurify, currentNode: Node, hookEvent: null, config: Config) => void;
409+
type ElementHook = (this: DOMPurify, currentNode: Element, hookEvent: null, config: Config) => void;
410+
type DocumentFragmentHook = (this: DOMPurify, currentNode: DocumentFragment, hookEvent: null, config: Config) => void;
377411
type UponSanitizeElementHook = (this: DOMPurify, currentNode: Node, hookEvent: UponSanitizeElementHookEvent, config: Config) => void;
378-
type UponSanitizeAttributeHook = (this: DOMPurify, currentNode: Node, hookEvent: UponSanitizeAttributeHookEvent, config: Config) => void;
412+
type UponSanitizeAttributeHook = (this: DOMPurify, currentNode: Element, hookEvent: UponSanitizeAttributeHookEvent, config: Config) => void;
379413
interface UponSanitizeElementHookEvent {
380414
tagName: string;
381415
allowedTags: Record<string, boolean>;
@@ -396,7 +430,7 @@ type WindowLike = Pick<typeof globalThis, 'DocumentFragment' | 'HTMLTemplateElem
396430
trustedTypes?: typeof window.trustedTypes;
397431
};
398432

399-
export { type Config, type DOMPurify, type Hook, type HookName, type RemovedAttribute, type RemovedElement, type UponSanitizeAttributeHook, type UponSanitizeAttributeHookEvent, type UponSanitizeElementHook, type UponSanitizeElementHookEvent, type WindowLike };
433+
export { 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 };
400434

401435
// @ts-ignore
402436
export = _default;

dist/purify.cjs.js

Lines changed: 28 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.cjs.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.es.d.mts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,21 @@ interface DOMPurify {
297297
* @param entryPoint entry point for the hook to add
298298
* @param hookFunction function to execute
299299
*/
300-
addHook(entryPoint: BasicHookName, hookFunction: Hook): void;
300+
addHook(entryPoint: BasicHookName, hookFunction: NodeHook): void;
301+
/**
302+
* Adds a DOMPurify hook.
303+
*
304+
* @param entryPoint entry point for the hook to add
305+
* @param hookFunction function to execute
306+
*/
307+
addHook(entryPoint: ElementHookName, hookFunction: ElementHook): void;
308+
/**
309+
* Adds a DOMPurify hook.
310+
*
311+
* @param entryPoint entry point for the hook to add
312+
* @param hookFunction function to execute
313+
*/
314+
addHook(entryPoint: DocumentFragmentHookName, hookFunction: DocumentFragmentHook): void;
301315
/**
302316
* Adds a DOMPurify hook.
303317
*
@@ -319,7 +333,23 @@ interface DOMPurify {
319333
* @param entryPoint entry point for the hook to remove
320334
* @returns removed(popped) hook
321335
*/
322-
removeHook(entryPoint: BasicHookName): Hook | undefined;
336+
removeHook(entryPoint: BasicHookName): NodeHook | undefined;
337+
/**
338+
* Remove a DOMPurify hook at a given entryPoint
339+
* (pops it from the stack of hooks if more are present)
340+
*
341+
* @param entryPoint entry point for the hook to remove
342+
* @returns removed(popped) hook
343+
*/
344+
removeHook(entryPoint: ElementHookName): ElementHook | undefined;
345+
/**
346+
* Remove a DOMPurify hook at a given entryPoint
347+
* (pops it from the stack of hooks if more are present)
348+
*
349+
* @param entryPoint entry point for the hook to remove
350+
* @returns removed(popped) hook
351+
*/
352+
removeHook(entryPoint: DocumentFragmentHookName): DocumentFragmentHook | undefined;
323353
/**
324354
* Remove a DOMPurify hook at a given entryPoint
325355
* (pops it from the stack of hooks if more are present)
@@ -369,13 +399,17 @@ interface RemovedAttribute {
369399
*/
370400
from: Node;
371401
}
372-
type BasicHookName = 'beforeSanitizeElements' | 'afterSanitizeElements' | 'beforeSanitizeAttributes' | 'afterSanitizeAttributes' | 'beforeSanitizeShadowDOM' | 'uponSanitizeShadowNode' | 'afterSanitizeShadowDOM';
402+
type BasicHookName = 'beforeSanitizeElements' | 'afterSanitizeElements' | 'uponSanitizeShadowNode';
403+
type ElementHookName = 'beforeSanitizeAttributes' | 'afterSanitizeAttributes';
404+
type DocumentFragmentHookName = 'beforeSanitizeShadowDOM' | 'afterSanitizeShadowDOM';
373405
type UponSanitizeElementHookName = 'uponSanitizeElement';
374406
type UponSanitizeAttributeHookName = 'uponSanitizeAttribute';
375-
type HookName = BasicHookName | UponSanitizeElementHookName | UponSanitizeAttributeHookName;
376-
type Hook = (this: DOMPurify, currentNode: Node, hookEvent: null, config: Config) => void;
407+
type HookName = BasicHookName | ElementHookName | DocumentFragmentHookName | UponSanitizeElementHookName | UponSanitizeAttributeHookName;
408+
type NodeHook = (this: DOMPurify, currentNode: Node, hookEvent: null, config: Config) => void;
409+
type ElementHook = (this: DOMPurify, currentNode: Element, hookEvent: null, config: Config) => void;
410+
type DocumentFragmentHook = (this: DOMPurify, currentNode: DocumentFragment, hookEvent: null, config: Config) => void;
377411
type UponSanitizeElementHook = (this: DOMPurify, currentNode: Node, hookEvent: UponSanitizeElementHookEvent, config: Config) => void;
378-
type UponSanitizeAttributeHook = (this: DOMPurify, currentNode: Node, hookEvent: UponSanitizeAttributeHookEvent, config: Config) => void;
412+
type UponSanitizeAttributeHook = (this: DOMPurify, currentNode: Element, hookEvent: UponSanitizeAttributeHookEvent, config: Config) => void;
379413
interface UponSanitizeElementHookEvent {
380414
tagName: string;
381415
allowedTags: Record<string, boolean>;
@@ -396,4 +430,4 @@ type WindowLike = Pick<typeof globalThis, 'DocumentFragment' | 'HTMLTemplateElem
396430
trustedTypes?: typeof window.trustedTypes;
397431
};
398432

399-
export { type Config, type DOMPurify, type Hook, type HookName, type RemovedAttribute, type RemovedElement, type UponSanitizeAttributeHook, type UponSanitizeAttributeHookEvent, type UponSanitizeElementHook, type UponSanitizeElementHookEvent, type WindowLike, _default as default };
433+
export { 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 };

0 commit comments

Comments
 (0)