Skip to content

Commit

Permalink
Merge pull request #1059 from yehuya/fixMissingWindowElement
Browse files Browse the repository at this point in the history
fix: handle undefined Element in DOMPurify initialization
  • Loading branch information
cure53 authored Jan 22, 2025
2 parents f41b45d + bc72d44 commit 72760ca
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({ document, Element: undefined }).version,
'string'
);
assert.strictEqual(
DOMPurify({ document, Element: undefined }).isSupported,
false
);
assert.strictEqual(
DOMPurify({ document, Element: undefined }).sanitize,
undefined
);
assert.strictEqual(typeof DOMPurify(window).version, 'string');
assert.strictEqual(typeof DOMPurify(window).sanitize, 'function');
});
Expand Down

0 comments on commit 72760ca

Please sign in to comment.