Skip to content

Commit 17337e5

Browse files
authored
Merge pull request #1073 from donmccurdy/fix/trusted-types-lib
fix(trusted-types): Use 'trusted-types/lib'
2 parents b4dec78 + 1794199 commit 17337e5

File tree

5 files changed

+11
-27
lines changed

5 files changed

+11
-27
lines changed

dist/purify.cjs.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
/// <reference types="trusted-types" />
21
/*! @license DOMPurify 3.2.4 | (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.4/LICENSE */
32

3+
import { TrustedTypePolicy, TrustedHTML, TrustedTypesWindow } from 'trusted-types/lib';
4+
45
/**
56
* Configuration to control DOMPurify behavior.
67
*/
@@ -433,8 +434,7 @@ interface UponSanitizeAttributeHookEvent {
433434
type WindowLike = Pick<typeof globalThis, 'DocumentFragment' | 'HTMLTemplateElement' | 'Node' | 'Element' | 'NodeFilter' | 'NamedNodeMap' | 'HTMLFormElement' | 'DOMParser'> & {
434435
document?: Document;
435436
MozNamedAttrMap?: typeof window.NamedNodeMap;
436-
trustedTypes?: typeof window.trustedTypes;
437-
};
437+
} & Pick<TrustedTypesWindow, 'trustedTypes'>;
438438

439439
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 };
440440

dist/purify.es.d.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
/// <reference types="trusted-types" />
21
/*! @license DOMPurify 3.2.4 | (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.4/LICENSE */
32

3+
import { TrustedTypePolicy, TrustedHTML, TrustedTypesWindow } from 'trusted-types/lib';
4+
45
/**
56
* Configuration to control DOMPurify behavior.
67
*/
@@ -433,7 +434,6 @@ interface UponSanitizeAttributeHookEvent {
433434
type WindowLike = Pick<typeof globalThis, 'DocumentFragment' | 'HTMLTemplateElement' | 'Node' | 'Element' | 'NodeFilter' | 'NamedNodeMap' | 'HTMLFormElement' | 'DOMParser'> & {
434435
document?: Document;
435436
MozNamedAttrMap?: typeof window.NamedNodeMap;
436-
trustedTypes?: typeof window.trustedTypes;
437-
};
437+
} & Pick<TrustedTypesWindow, 'trustedTypes'>;
438438

439439
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 };

scripts/fix-types.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const path = require('node:path');
77
// Note that this script is intended to run on the type declaration file that is
88
// output by Rollup, and not the type declaration file generated from TypeScript.
99
await fixCjsTypes(path.resolve(__dirname, '../dist/purify.cjs.d.ts'));
10-
await fixEsmTypes(path.resolve(__dirname, '../dist/purify.es.d.mts'));
1110
})().catch((ex) => {
1211
console.error(ex);
1312
process.exitCode = 1;
@@ -37,22 +36,5 @@ async function fixCjsTypes(fileName) {
3736
// for certain configurations, so add a `@ts-ignore` comment before it.
3837
fixed += '\n// @ts-ignore\nexport = _default;\n';
3938

40-
await fs.writeFile(fileName, addTrustedTypesReference(fixed));
41-
}
42-
43-
/**
44-
* Fixes the ESM type declarations file.
45-
* @param {string} fileName
46-
*/
47-
async function fixEsmTypes(fileName) {
48-
let types = await fs.readFile(fileName, { encoding: 'utf-8' });
49-
await fs.writeFile(fileName, addTrustedTypesReference(types));
50-
}
51-
52-
function addTrustedTypesReference(types) {
53-
// We need to tell TypeScript that we use the type declarations from
54-
// `trusted-types` so that it ends up in our type declaration type).
55-
// Without this, the references to trusted-types in the type declaration
56-
// file can cause compilation errors for certain configurations.
57-
return `/// <reference types="trusted-types" />\n${types}`;
39+
await fs.writeFile(fileName, fixed);
5840
}

src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable @typescript-eslint/indent */
22

3+
import type { TrustedTypePolicy } from 'trusted-types/lib';
4+
35
/**
46
* Configuration to control DOMPurify behavior.
57
*/

src/purify.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable @typescript-eslint/indent */
22

3+
import type { TrustedHTML, TrustedTypesWindow } from 'trusted-types/lib';
34
import type { Config, UseProfilesConfig } from './config';
45
import * as TAGS from './tags.js';
56
import * as ATTRS from './attrs.js';
@@ -2006,5 +2007,4 @@ export type WindowLike = Pick<
20062007
> & {
20072008
document?: Document;
20082009
MozNamedAttrMap?: typeof window.NamedNodeMap;
2009-
trustedTypes?: typeof window.trustedTypes;
2010-
};
2010+
} & Pick<TrustedTypesWindow, 'trustedTypes'>;

0 commit comments

Comments
 (0)