From 363a89dd3a051b72334cd4d0c867616fdc4ee40d Mon Sep 17 00:00:00 2001 From: yehuya <> Date: Tue, 21 Jan 2025 19:46:46 +0200 Subject: [PATCH 1/2] fix: handle undefined Element in DOMPurify initialization --- src/purify.ts | 3 ++- test/test-suite.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/purify.ts b/src/purify.ts index 631b70cd..458a8e92 100644 --- a/src/purify.ts +++ b/src/purify.ts @@ -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 diff --git a/test/test-suite.js b/test/test-suite.js index 17329d5c..4317f143 100644 --- a/test/test-suite.js +++ b/test/test-suite.js @@ -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'); }); From bc72d44b2e3cec00d8ef2427869d03cb668969fa Mon Sep 17 00:00:00 2001 From: yehuya <> Date: Wed, 22 Jan 2025 13:16:33 +0200 Subject: [PATCH 2/2] Fix tests --- test/test-suite.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test-suite.js b/test/test-suite.js index 4317f143..1885fc05 100644 --- a/test/test-suite.js +++ b/test/test-suite.js @@ -871,15 +871,15 @@ undefined ); assert.strictEqual( - typeof DOMPurify({ ...window, Element: undefined }).version, + typeof DOMPurify({ document, Element: undefined }).version, 'string' ); assert.strictEqual( - DOMPurify({ ...window, Element: undefined }).isSupported, + DOMPurify({ document, Element: undefined }).isSupported, false ); assert.strictEqual( - DOMPurify({ ...window, Element: undefined }).sanitize, + DOMPurify({ document, Element: undefined }).sanitize, undefined ); assert.strictEqual(typeof DOMPurify(window).version, 'string');