Skip to content

Commit

Permalink
fix: handle undefined Element in DOMPurify initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
yehuya committed Jan 21, 2025
1 parent f41b45d commit 363a89d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/purify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ function createDOMPurify(window: WindowLike = getGlobal()): DOMPurify {
if (
!window ||
!window.document ||
window.document.nodeType !== NODE_TYPE.document
window.document.nodeType !== NODE_TYPE.document ||
!window.Element
) {
// Not running in a browser, provide a factory function
// so that you can pass your own Window
Expand Down
12 changes: 12 additions & 0 deletions test/test-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,18 @@
DOMPurify({ document: 'not really a document' }).sanitize,
undefined
);
assert.strictEqual(
typeof DOMPurify({ ...window, Element: undefined }).version,
'string'
);
assert.strictEqual(
DOMPurify({ ...window, Element: undefined }).isSupported,
false
);
assert.strictEqual(
DOMPurify({ ...window, Element: undefined }).sanitize,
undefined
);
assert.strictEqual(typeof DOMPurify(window).version, 'string');
assert.strictEqual(typeof DOMPurify(window).sanitize, 'function');
});
Expand Down

0 comments on commit 363a89d

Please sign in to comment.