Skip to content

Commit 5735e64

Browse files
authored
Merge pull request #1128 from ssi02014/imp/types
refac: improve types
2 parents ec86d90 + 51be95d commit 5735e64

File tree

10 files changed

+85
-54
lines changed

10 files changed

+85
-54
lines changed

dist/purify.cjs.js

Lines changed: 15 additions & 9 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.mjs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ if (!seal) {
2727
};
2828
}
2929
if (!apply) {
30-
apply = function apply(fun, thisValue, args) {
31-
return fun.apply(thisValue, args);
30+
apply = function apply(func, thisArg) {
31+
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
32+
args[_key - 2] = arguments[_key];
33+
}
34+
return func.apply(thisArg, args);
3235
};
3336
}
3437
if (!construct) {
35-
construct = function construct(Func, args) {
38+
construct = function construct(Func) {
39+
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
40+
args[_key2 - 1] = arguments[_key2];
41+
}
3642
return new Func(...args);
3743
};
3844
}
@@ -61,8 +67,8 @@ function unapply(func) {
6167
if (thisArg instanceof RegExp) {
6268
thisArg.lastIndex = 0;
6369
}
64-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
65-
args[_key - 1] = arguments[_key];
70+
for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
71+
args[_key3 - 1] = arguments[_key3];
6672
}
6773
return apply(func, thisArg, args);
6874
};
@@ -73,12 +79,12 @@ function unapply(func) {
7379
* @param func - The constructor function to be wrapped and called.
7480
* @returns A new function that constructs an instance of the given constructor function with the provided arguments.
7581
*/
76-
function unconstruct(func) {
82+
function unconstruct(Func) {
7783
return function () {
78-
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
79-
args[_key2] = arguments[_key2];
84+
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
85+
args[_key4] = arguments[_key4];
8086
}
81-
return construct(func, args);
87+
return construct(Func, args);
8288
};
8389
}
8490
/**

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

Lines changed: 15 additions & 9 deletions
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

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: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,15 +1000,12 @@ 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 {
1011-
arrayForEach(hooks, (hook) => {
1003+
function _executeHooks<T extends HookFunction>(
1004+
hooks: HookFunction[],
1005+
currentNode: Parameters<T>[0],
1006+
data: Parameters<T>[1]
1007+
): void {
1008+
arrayForEach(hooks, (hook: T) => {
10121009
hook.call(DOMPurify, currentNode, data, CONFIG);
10131010
});
10141011
}
@@ -1132,7 +1129,7 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
11321129
/* Get the element's text content */
11331130
content = currentNode.textContent;
11341131

1135-
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], (expr) => {
1132+
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], (expr: RegExp) => {
11361133
content = stringReplace(content, expr, ' ');
11371134
});
11381135

@@ -1353,7 +1350,7 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
13531350

13541351
/* Sanitize attribute content to be template-safe */
13551352
if (SAFE_FOR_TEMPLATES) {
1356-
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], (expr) => {
1353+
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], (expr: RegExp) => {
13571354
value = stringReplace(value, expr, ' ');
13581355
});
13591356
}
@@ -1614,7 +1611,7 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
16141611

16151612
/* Sanitize final string template-safe */
16161613
if (SAFE_FOR_TEMPLATES) {
1617-
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], (expr) => {
1614+
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], (expr: RegExp) => {
16181615
serializedHTML = stringReplace(serializedHTML, expr, ' ');
16191616
});
16201617
}
@@ -1645,15 +1642,21 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
16451642
return _isValidAttribute(lcTag, lcName, value);
16461643
};
16471644

1648-
DOMPurify.addHook = function (entryPoint, hookFunction) {
1645+
DOMPurify.addHook = function (
1646+
entryPoint: keyof HooksMap,
1647+
hookFunction: HookFunction
1648+
) {
16491649
if (typeof hookFunction !== 'function') {
16501650
return;
16511651
}
16521652

16531653
arrayPush(hooks[entryPoint], hookFunction);
16541654
};
16551655

1656-
DOMPurify.removeHook = function (entryPoint, hookFunction) {
1656+
DOMPurify.removeHook = function (
1657+
entryPoint: keyof HooksMap,
1658+
hookFunction: HookFunction
1659+
) {
16571660
if (hookFunction !== undefined) {
16581661
const index = arrayLastIndexOf(hooks[entryPoint], hookFunction);
16591662

@@ -1665,7 +1668,7 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
16651668
return arrayPop(hooks[entryPoint]);
16661669
};
16671670

1668-
DOMPurify.removeHooks = function (entryPoint) {
1671+
DOMPurify.removeHooks = function (entryPoint: keyof HooksMap) {
16691672
hooks[entryPoint] = [];
16701673
};
16711674

@@ -1950,6 +1953,10 @@ interface HooksMap {
19501953
uponSanitizeAttribute: UponSanitizeAttributeHook[];
19511954
}
19521955

1956+
type ArrayElement<T> = T extends Array<infer U> ? U : never;
1957+
1958+
type HookFunction = ArrayElement<HooksMap[keyof HooksMap]>;
1959+
19531960
export type HookName =
19541961
| BasicHookName
19551962
| ElementHookName

src/utils.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,29 @@ let { freeze, seal, create } = Object; // eslint-disable-line import/no-mutable-
1010
let { apply, construct } = typeof Reflect !== 'undefined' && Reflect;
1111

1212
if (!freeze) {
13-
freeze = function (x) {
13+
freeze = function <T>(x: T): T {
1414
return x;
1515
};
1616
}
1717

1818
if (!seal) {
19-
seal = function (x) {
19+
seal = function <T>(x: T): T {
2020
return x;
2121
};
2222
}
2323

2424
if (!apply) {
25-
apply = function (fun, thisValue, args) {
26-
return fun.apply(thisValue, args);
25+
apply = function <T>(
26+
func: (thisArg: any, ...args: any[]) => T,
27+
thisArg: any,
28+
...args: any[]
29+
): T {
30+
return func.apply(thisArg, args);
2731
};
2832
}
2933

3034
if (!construct) {
31-
construct = function (Func, args) {
35+
construct = function <T>(Func: new (...args: any[]) => T, ...args: any[]): T {
3236
return new Func(...args);
3337
};
3438
}
@@ -78,8 +82,10 @@ function unapply<T>(
7882
* @param func - The constructor function to be wrapped and called.
7983
* @returns A new function that constructs an instance of the given constructor function with the provided arguments.
8084
*/
81-
function unconstruct<T>(func: (...args: any[]) => T): (...args: any[]) => T {
82-
return (...args: any[]): T => construct(func, args);
85+
function unconstruct<T>(
86+
Func: new (...args: any[]) => T
87+
): (...args: any[]) => T {
88+
return (...args: any[]): T => construct(Func, args);
8389
}
8490

8591
/**

0 commit comments

Comments
 (0)