File tree Expand file tree Collapse file tree 4 files changed +56
-12
lines changed Expand file tree Collapse file tree 4 files changed +56
-12
lines changed Original file line number Diff line number Diff line change 5353 "typescript" : " 3.4.5"
5454 },
5555 "dependencies" : {
56- "dompurify" : " 1.0.10"
56+ "@types/jsdom" : " 12.2.3" ,
57+ "dompurify" : " 1.0.10" ,
58+ "jsdom" : " 15.1.1"
5759 },
5860 "peerDependencies" : {
5961 "react" : " ^16.2.0"
6466 }
6567 },
6668 "jest" : {
67- "preset" : " ts-jest" ,
68- "moduleNameMapper" : {
69- "\\ .(css|less|sass|scss)$" : " <rootDir>/tests/styleMock.js"
70- },
71- "setupFilesAfterEnv" : [
72- " <rootDir>tests/setupTests.js"
69+ "projects" : [
70+ {
71+ "displayName" : " test-jsdom" ,
72+ "testEnvironment" : " jsdom" ,
73+ "preset" : " ts-jest" ,
74+ "setupFilesAfterEnv" : [
75+ " <rootDir>tests/setupTests.js"
76+ ]
77+ },
78+ {
79+ "displayName" : " test-nodejs" ,
80+ "testEnvironment" : " node" ,
81+ "preset" : " ts-jest" ,
82+ "setupFilesAfterEnv" : [
83+ " <rootDir>tests/setupTests.js"
84+ ],
85+ "testMatch" : [
86+ " **/?(*.)+(node-spec).ts?(x)"
87+ ]
88+ }
7389 ]
7490 }
7591}
Original file line number Diff line number Diff line change 1- import * as dompurify from 'dompurify' ;
1+ import * as createDOMPurify from 'dompurify' ;
22
33import { Config } from './config' ;
44
@@ -21,6 +21,11 @@ export function getAttributes(elementAttributes: NamedNodeMap) {
2121 return attributes ;
2222}
2323
24- export function parse ( html : string , config : Config ) : DocumentFragment {
25- return dompurify . sanitize ( html , config . dom ) as DocumentFragment ;
24+ export function parse (
25+ html : string ,
26+ config : Config ,
27+ customWindow ?: Window
28+ ) : DocumentFragment {
29+ const dompurifyInstance = createDOMPurify ( customWindow ) ;
30+ return dompurifyInstance . sanitize ( html , config . dom ) as DocumentFragment ;
2631}
Original file line number Diff line number Diff line change @@ -5,14 +5,15 @@ import { render } from './react';
55
66export function parse (
77 html : React . ReactNode ,
8- userOptions ?: Config
8+ userOptions ?: Config ,
9+ customWindow ?: Window
910) : React . ReactNode [ ] {
1011 if ( typeof html !== 'string' ) {
1112 return [ html ] ;
1213 }
1314
1415 const options = getConfig ( userOptions ) ;
15- const document = dom . parse ( html , options ) ;
16+ const document = dom . parse ( html , options , customWindow ) ;
1617
1718 if ( options . overrides ) {
1819 override ( document , options . overrides ) ;
Original file line number Diff line number Diff line change 1+ import * as React from 'react' ;
2+ import { render } from 'enzyme' ;
3+ import { JSDOM } from 'jsdom' ;
4+
5+ const dom = new JSDOM ( '' ) ;
6+ const customWindow = dom . window ;
7+
8+ import * as htmlStringToReact from '../src/index' ;
9+
10+ describe ( 'Public NodeJS API' , ( ) => {
11+ it ( 'should convert a string to an array of react nodes with custom window' , ( ) => {
12+ const elements = htmlStringToReact . parse (
13+ '<em key="1"><b key="2">It\' is working</b></em>' ,
14+ { } ,
15+ customWindow
16+ ) ;
17+ const wrapper = render ( React . createElement ( 'div' , null , elements ) ) ;
18+ expect ( wrapper . text ( ) ) . toEqual ( "It' is working" ) ;
19+ expect ( wrapper . find ( 'em' ) ) . toHaveLength ( 1 ) ;
20+ expect ( wrapper . find ( 'b' ) ) . toHaveLength ( 1 ) ;
21+ } ) ;
22+ } ) ;
You can’t perform that action at this time.
0 commit comments