Skip to content

Commit b97bc57

Browse files
Merge pull request #201 from remarkablemark/feat/types
feat(index): export `domhandler` node types and update README.md
2 parents 3a559e4 + 057ab48 commit b97bc57

File tree

4 files changed

+61
-25
lines changed

4 files changed

+61
-25
lines changed

README.md

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,30 @@ HTML to React parser that works on both the server (Node.js) and the client (bro
1515
HTMLReactParser(string[, options])
1616
```
1717

18-
The parser converts an HTML string to one or more [React elements](https://reactjs.org/docs/react-api.html#creating-react-elements):
18+
The parser converts an HTML string to one or more [React elements](https://reactjs.org/docs/react-api.html#creating-react-elements).
19+
20+
To replace an element with a custom element, check out the [replace option](#replacedomnode).
21+
22+
#### Example
1923

2024
```js
2125
const parse = require('html-react-parser');
22-
parse('<div>text</div>'); // equivalent to `React.createElement('div', {}, 'text')`
26+
parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')
2327
```
2428

25-
To replace an element with a custom element, check out the [replace option](#replacedomnode).
26-
27-
Demos:
28-
29-
[CodeSandbox](https://codesandbox.io/s/940pov1l4w) | [Repl.it](https://repl.it/@remarkablemark/html-react-parser) | [JSFiddle](https://jsfiddle.net/remarkablemark/7v86d800/) | [Examples](https://github.com/remarkablemark/html-react-parser/tree/master/examples)
29+
[CodeSandbox](https://codesandbox.io/s/940pov1l4w) | [CodeSandbox (TypeScript)](https://codesandbox.io/s/html-react-parser-z0kp6) | [Repl.it](https://repl.it/@remarkablemark/html-react-parser) | [JSFiddle](https://jsfiddle.net/remarkablemark/7v86d800/) | [Examples](https://github.com/remarkablemark/html-react-parser/tree/master/examples)
3030

3131
<details>
3232
<summary>Table of Contents</summary>
3333

3434
- [Install](#install)
3535
- [Usage](#usage)
36-
- [Options](#options)
37-
- [replace(domNode)](#replacedomnode)
36+
- [replace(domNode)](#replacedomnode)
3837
- [library](#library)
3938
- [htmlparser2](#htmlparser2)
4039
- [trim](#trim)
40+
- [Migration](#migration)
41+
- [v1.0.0](#v100)
4142
- [FAQ](#faq)
4243
- [Is this XSS safe?](#is-this-xss-safe)
4344
- [Does invalid HTML get sanitized?](#does-invalid-html-get-sanitized)
@@ -48,6 +49,7 @@ Demos:
4849
- [Elements aren't nested correctly](#elements-arent-nested-correctly)
4950
- [Warning: validateDOMNesting(...): Whitespace text nodes cannot appear as a child of table](#warning-validatedomnesting-whitespace-text-nodes-cannot-appear-as-a-child-of-table)
5051
- [Don't change case of tags](#dont-change-case-of-tags)
52+
- [TS Error: Property 'attribs' does not exist on type 'DOMNode'](#ts-error-property-attribs-does-not-exist-on-type-domnode)
5153
- [Performance](#performance)
5254
- [Contributors](#contributors)
5355
- [Code Contributors](#code-contributors)
@@ -133,9 +135,7 @@ parse(
133135
);
134136
```
135137

136-
### Options
137-
138-
#### replace(domNode)
138+
### replace(domNode)
139139

140140
The `replace` option allows you to replace an element with another React element.
141141

@@ -176,6 +176,20 @@ parse('<p id="replace">text</p>', {
176176
});
177177
```
178178

179+
For TypeScript projects, check that `domNode` is an instance of domhandler's `Element`:
180+
181+
```tsx
182+
import parse, { Element } from 'html-react-parser';
183+
184+
parse('<p id="replace">text</p>', {
185+
replace: domNode => {
186+
if (domNode instanceof Element && domNode.attribs.id === 'replace') {
187+
return <span>replaced</span>;
188+
}
189+
}
190+
});
191+
```
192+
179193
The following [example](https://repl.it/@remarkablemark/html-react-parser-replace-example) modifies the element along with its children:
180194

181195
```jsx
@@ -349,33 +363,53 @@ However, intentional whitespace may be stripped out:
349363
parse('<p> </p>', { trim: true }); // React.createElement('p')
350364
```
351365

366+
## Migration
367+
368+
### v1.0.0
369+
370+
TypeScript projects will need to check the types in [v1.0.0](https://github.com/remarkablemark/html-react-parser/releases/tag/v1.0.0).
371+
372+
For `replace` option:
373+
374+
```tsx
375+
import parse, { Element } from 'html-react-parser';
376+
377+
parse('<br class="remove">', {
378+
replace: domNode => {
379+
if (domNode instanceof Element && domNode.attribs.class === 'remove') {
380+
return <></>;
381+
}
382+
}
383+
});
384+
```
385+
352386
## FAQ
353387

354-
#### Is this XSS safe?
388+
### Is this XSS safe?
355389

356390
No, this library is _**not**_ [XSS (cross-site scripting)](https://wikipedia.org/wiki/Cross-site_scripting) safe. See [#94](https://github.com/remarkablemark/html-react-parser/issues/94).
357391

358-
#### Does invalid HTML get sanitized?
392+
### Does invalid HTML get sanitized?
359393

360394
No, this library does _**not**_ sanitize HTML. See [#124](https://github.com/remarkablemark/html-react-parser/issues/124), [#125](https://github.com/remarkablemark/html-react-parser/issues/125), and [#141](https://github.com/remarkablemark/html-react-parser/issues/141).
361395

362-
#### Are `<script>` tags parsed?
396+
### Are `<script>` tags parsed?
363397

364398
Although `<script>` tags and their contents are rendered on the server-side, they're not evaluated on the client-side. See [#98](https://github.com/remarkablemark/html-react-parser/issues/98).
365399

366-
#### Attributes aren't getting called
400+
### Attributes aren't getting called
367401

368402
The reason why your HTML attributes aren't getting called is because [inline event handlers](https://developer.mozilla.org/docs/Web/Guide/Events/Event_handlers) (e.g., `onclick`) are parsed as a _string_ rather than a _function_. See [#73](https://github.com/remarkablemark/html-react-parser/issues/73).
369403

370-
#### Parser throws an error
404+
### Parser throws an error
371405

372406
If the parser throws an erorr, check if your arguments are valid. See ["Does invalid HTML get sanitized?"](#does-invalid-html-get-sanitized).
373407

374-
#### Is SSR supported?
408+
### Is SSR supported?
375409

376410
Yes, server-side rendering on Node.js is supported by this library. See [demo](https://repl.it/@remarkablemark/html-react-parser-SSR).
377411

378-
#### Elements aren't nested correctly
412+
### Elements aren't nested correctly
379413

380414
If your elements are nested incorrectly, check to make sure your [HTML markup is valid](https://validator.w3.org/). The HTML to DOM parsing will be affected if you're using self-closing syntax (`/>`) on non-void elements:
381415

@@ -385,11 +419,11 @@ parse('<div /><div />'); // returns single element instead of array of elements
385419

386420
See [#158](https://github.com/remarkablemark/html-react-parser/issues/158).
387421

388-
#### Warning: validateDOMNesting(...): Whitespace text nodes cannot appear as a child of table
422+
### Warning: validateDOMNesting(...): Whitespace text nodes cannot appear as a child of table
389423

390424
Enable the [trim](#trim) option. See [#155](https://github.com/remarkablemark/html-react-parser/issues/155).
391425

392-
#### Don't change case of tags
426+
### Don't change case of tags
393427

394428
Tags are lowercased by default. To prevent that from happening, pass the [htmlparser2 option](#htmlparser2):
395429

@@ -410,6 +444,10 @@ parse('<CustomElement>', options); // React.createElement('CustomElement')
410444
411445
See [#62](https://github.com/remarkablemark/html-react-parser/issues/62) and [example](https://repl.it/@remarkablemark/html-react-parser-62).
412446
447+
### TS Error: Property 'attribs' does not exist on type 'DOMNode'
448+
449+
The TypeScript error happens because `DOMNode` needs be an instance of domhandler's `Element`. See [migration](#migration) or [#199](https://github.com/remarkablemark/html-react-parser/issues/199).
450+
413451
## Performance
414452
415453
Run benchmark:

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import domToReact from './lib/dom-to-react';
1515

1616
export { attributesToProps, domToReact, htmlToDOM };
1717
export type HTMLParser2Options = ParserOptions & DomHandlerOptions;
18+
export { Comment, Element, ProcessingInstruction, Text };
1819
export type DOMNode = Comment | Element | ProcessingInstruction | Text;
1920

2021
export interface HTMLReactParserOptions {

test/types/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import parse, {
2+
Element,
23
HTMLReactParserOptions,
34
domToReact,
45
htmlToDOM
56
} from 'html-react-parser';
6-
import { Element } from 'domhandler';
77
import * as React from 'react';
88

99
// $ExpectError

tslint.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)