Skip to content

Add customWindow argument to parse #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsdom should be a devDependency no?

},
"peerDependencies": {
"react": "^16.2.0"
Expand All @@ -64,12 +66,26 @@
}
},
"jest": {
"preset": "ts-jest",
"moduleNameMapper": {
"\\.(css|less|sass|scss)$": "<rootDir>/tests/styleMock.js"
},
"setupFilesAfterEnv": [
"<rootDir>tests/setupTests.js"
"projects": [
{
"displayName": "test-jsdom",
"testEnvironment": "jsdom",
"preset": "ts-jest",
"setupFilesAfterEnv": [
"<rootDir>tests/setupTests.js"
]
},
{
"displayName": "test-nodejs",
"testEnvironment": "node",
"preset": "ts-jest",
"setupFilesAfterEnv": [
"<rootDir>tests/setupTests.js"
],
"testMatch": [
"**/?(*.)+(node-spec).ts?(x)"
]
}
]
}
}
11 changes: 8 additions & 3 deletions src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as dompurify from 'dompurify';
import * as createDOMPurify from 'dompurify';

import { Config } from './config';

Expand All @@ -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;
}
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions tests/api.node-spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test with no custom window to test if it is getting the default window correctly?

const elements = htmlStringToReact.parse(
'<em key="1"><b key="2">It\' is working</b></em>',
{},
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);
});
});