Skip to content

Commit 2998742

Browse files
Merge pull request #129 from davidbailey00/support-preact
Add support for Preact
2 parents b103aae + 6c6b8ec commit 2998742

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,36 @@ parse('<p><br id="remove"></p>', {
194194
});
195195
```
196196

197+
### library
198+
199+
The `library` option allows you to specify which component library is used to create elements. React is used by default if this option is not specified.
200+
201+
Here's an example showing how to use Preact:
202+
203+
```js
204+
parse('<br>', {
205+
library: require('preact')
206+
});
207+
```
208+
209+
Or, using a custom library:
210+
211+
```js
212+
parse('<br>', {
213+
library: {
214+
cloneElement: () => {
215+
/* ... */
216+
},
217+
createElement: () => {
218+
/* ... */
219+
},
220+
isValidElement: () => {
221+
/* ... */
222+
}
223+
}
224+
});
225+
```
226+
197227
## FAQ
198228

199229
#### Is this library XSS safe?

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import htmlToDOM from 'html-dom-parser';
88
export interface HTMLReactParserOptions {
99
// TODO: Replace `object` by type for objects like `{ type: 'h1', props: { children: 'Heading' } }`
1010
replace(domNode: DomElement): React.ReactElement | object | undefined | false;
11+
library?: object;
1112
}
1213

1314
/**

lib/dom-to-react.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
var React = require('react');
21
var attributesToProps = require('./attributes-to-props');
32
var utilities = require('./utilities');
43

5-
var cloneElement = React.cloneElement;
6-
var createElement = React.createElement;
7-
var isValidElement = React.isValidElement;
8-
94
/**
105
* Converts DOM nodes to React elements.
116
*
@@ -16,6 +11,12 @@ var isValidElement = React.isValidElement;
1611
*/
1712
function domToReact(nodes, options) {
1813
options = options || {};
14+
15+
var React = options.library || require('react');
16+
var cloneElement = React.cloneElement;
17+
var createElement = React.createElement;
18+
var isValidElement = React.isValidElement;
19+
1920
var result = [];
2021
var node;
2122
var hasReplace = typeof options.replace === 'function';

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"lint-staged": "^8.2.1",
5555
"mocha": "^6.1.4",
5656
"nyc": "^14.1.1",
57+
"preact": "10.0.4",
5758
"prettier": "^1.18.2",
5859
"react": "^16",
5960
"react-dom": "^16",

test/dom-to-react.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const assert = require('assert');
22
const React = require('react');
3+
const Preact = require('preact');
34
const htmlToDOM = require('html-dom-parser');
45
const domToReact = require('../lib/dom-to-react');
56
const { data, render } = require('./helpers/');
@@ -159,6 +160,13 @@ describe('dom-to-react parser', () => {
159160
);
160161
});
161162

163+
it('handles using a custom component library', () => {
164+
const html = data.html.single;
165+
const preactElement = domToReact(htmlToDOM(html), { library: Preact });
166+
167+
assert.deepEqual(preactElement, Preact.createElement('p', {}, 'foo'));
168+
});
169+
162170
it('does not modify keys for replacement if it has one', () => {
163171
const html = [data.html.single, data.html.customElement].join('');
164172

0 commit comments

Comments
 (0)