Skip to content

WIP: Add nested rendering #13

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 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
5,215 changes: 2,611 additions & 2,604 deletions package-lock.json

Large diffs are not rendered by default.

32 changes: 25 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"scripts": {
"install": "npm run is_not_builded && npm run build",
"is_not_builded": "node -e 'fs.existsSync(`lib`) && process.exit(1)'",
Copy link
Author

Choose a reason for hiding this comment

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

I use my fork in my project and this script needed for install from git.

Copy link
Author

Choose a reason for hiding this comment

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

After review I will remove all temp solutions

Copy link
Author

Choose a reason for hiding this comment

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

UPD: Added lib/ to git.

"build": "npm run clean && tsc --project .",
"clean": "rimraf lib/ yarn-error.log npm-debug.log",
"lint": "eslint . --ext .ts",
Expand Down Expand Up @@ -53,7 +55,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"
Expand All @@ -64,12 +68,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 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
4 changes: 3 additions & 1 deletion src/override.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* An internal representation of an element that can include a React element to use as an override
*/
import { AvailableProps } from './react';

export interface EnrichedElement extends Element {
override?: (props: any, textContent?: string) => React.ReactElement<any>;
}
Expand All @@ -10,7 +12,7 @@ export interface EnrichedElement extends Element {
*/
export interface SelectorsToElement {
[keyof: string]: (
props?: any,
props?: AvailableProps,
textContent?: string
) => React.ReactElement<any>;
}
Expand Down
Loading