Skip to content

Commit 51be95d

Browse files
committed
feat: HookFunction type
1 parent 7f6c4e0 commit 51be95d

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

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.mjs.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.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.min.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.

src/purify.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,14 +1000,11 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
10001000
return typeof Node === 'function' && value instanceof Node;
10011001
};
10021002

1003-
function _executeHooks<
1004-
T extends
1005-
| NodeHook
1006-
| ElementHook
1007-
| DocumentFragmentHook
1008-
| UponSanitizeElementHook
1009-
| UponSanitizeAttributeHook
1010-
>(hooks: T[], currentNode: Parameters<T>[0], data: Parameters<T>[1]): void {
1003+
function _executeHooks<T extends HookFunction>(
1004+
hooks: HookFunction[],
1005+
currentNode: Parameters<T>[0],
1006+
data: Parameters<T>[1]
1007+
): void {
10111008
arrayForEach(hooks, (hook: T) => {
10121009
hook.call(DOMPurify, currentNode, data, CONFIG);
10131010
});
@@ -1647,7 +1644,7 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
16471644

16481645
DOMPurify.addHook = function (
16491646
entryPoint: keyof HooksMap,
1650-
hookFunction: ArrayElement<HooksMap[keyof HooksMap]>
1647+
hookFunction: HookFunction
16511648
) {
16521649
if (typeof hookFunction !== 'function') {
16531650
return;
@@ -1658,7 +1655,7 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
16581655

16591656
DOMPurify.removeHook = function (
16601657
entryPoint: keyof HooksMap,
1661-
hookFunction: ArrayElement<HooksMap[keyof HooksMap]>
1658+
hookFunction: HookFunction
16621659
) {
16631660
if (hookFunction !== undefined) {
16641661
const index = arrayLastIndexOf(hooks[entryPoint], hookFunction);
@@ -1956,6 +1953,10 @@ interface HooksMap {
19561953
uponSanitizeAttribute: UponSanitizeAttributeHook[];
19571954
}
19581955

1956+
type ArrayElement<T> = T extends Array<infer U> ? U : never;
1957+
1958+
type HookFunction = ArrayElement<HooksMap[keyof HooksMap]>;
1959+
19591960
export type HookName =
19601961
| BasicHookName
19611962
| ElementHookName
@@ -2028,5 +2029,3 @@ export type WindowLike = Pick<
20282029
document?: Document;
20292030
MozNamedAttrMap?: typeof window.NamedNodeMap;
20302031
} & Pick<TrustedTypesWindow, 'trustedTypes'>;
2031-
2032-
type ArrayElement<T> = T extends Array<infer U> ? U : never;

0 commit comments

Comments
 (0)