From 01073c56c2c5bcfdac61e00cad5109c7fea72434 Mon Sep 17 00:00:00 2001 From: Aleksandr Plesovskikh Date: Sat, 1 Jun 2019 22:21:58 +0300 Subject: [PATCH] New: Add customWindow argument to parse That add ability to use the lib in NodeJS --- package.json | 30 +++++++++++++++++++++++------- src/dom.ts | 11 ++++++++--- src/index.ts | 5 +++-- tests/api.node-spec.ts | 22 ++++++++++++++++++++++ 4 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 tests/api.node-spec.ts diff --git a/package.json b/package.json index b519d9a..32eda31 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,9 @@ "typescript": "3.4.5" }, "dependencies": { - "dompurify": "1.0.10" + "@types/jsdom": "12.2.3", + "dompurify": "1.0.10", + "jsdom": "15.1.1" }, "peerDependencies": { "react": "^16.2.0" @@ -64,12 +66,26 @@ } }, "jest": { - "preset": "ts-jest", - "moduleNameMapper": { - "\\.(css|less|sass|scss)$": "/tests/styleMock.js" - }, - "setupFilesAfterEnv": [ - "tests/setupTests.js" + "projects": [ + { + "displayName": "test-jsdom", + "testEnvironment": "jsdom", + "preset": "ts-jest", + "setupFilesAfterEnv": [ + "tests/setupTests.js" + ] + }, + { + "displayName": "test-nodejs", + "testEnvironment": "node", + "preset": "ts-jest", + "setupFilesAfterEnv": [ + "tests/setupTests.js" + ], + "testMatch": [ + "**/?(*.)+(node-spec).ts?(x)" + ] + } ] } } diff --git a/src/dom.ts b/src/dom.ts index 125e58f..16f7318 100644 --- a/src/dom.ts +++ b/src/dom.ts @@ -1,4 +1,4 @@ -import * as dompurify from 'dompurify'; +import * as createDOMPurify from 'dompurify'; import { Config } from './config'; @@ -21,6 +21,11 @@ export function getAttributes(elementAttributes: NamedNodeMap) { return attributes; } -export function parse(html: string, config: Config): DocumentFragment { - return dompurify.sanitize(html, config.dom) as DocumentFragment; +export function parse( + html: string, + config: Config, + customWindow?: Window +): DocumentFragment { + const dompurifyInstance = createDOMPurify(customWindow); + return dompurifyInstance.sanitize(html, config.dom) as DocumentFragment; } diff --git a/src/index.ts b/src/index.ts index e397079..6755621 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,14 +5,15 @@ import { render } from './react'; export function parse( html: React.ReactNode, - userOptions?: Config + userOptions?: Config, + customWindow?: Window ): React.ReactNode[] { if (typeof html !== 'string') { return [html]; } const options = getConfig(userOptions); - const document = dom.parse(html, options); + const document = dom.parse(html, options, customWindow); if (options.overrides) { override(document, options.overrides); diff --git a/tests/api.node-spec.ts b/tests/api.node-spec.ts new file mode 100644 index 0000000..d8c04b5 --- /dev/null +++ b/tests/api.node-spec.ts @@ -0,0 +1,22 @@ +import * as React from 'react'; +import { render } from 'enzyme'; +import { JSDOM } from 'jsdom'; + +const dom = new JSDOM(''); +const customWindow = dom.window; + +import * as htmlStringToReact from '../src/index'; + +describe('Public NodeJS API', () => { + it('should convert a string to an array of react nodes with custom window', () => { + const elements = htmlStringToReact.parse( + 'It\' is working', + {}, + customWindow + ); + const wrapper = render(React.createElement('div', null, elements)); + expect(wrapper.text()).toEqual("It' is working"); + expect(wrapper.find('em')).toHaveLength(1); + expect(wrapper.find('b')).toHaveLength(1); + }); +});