Skip to content

Commit d82b5bd

Browse files
authored
Merge pull request #30 from wrakky/custom_processNodes
Add preprocessNodes option
2 parents 4de7c2c + 22bd744 commit d82b5bd

File tree

7 files changed

+68
-24
lines changed

7 files changed

+68
-24
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ node_js:
44
- 8
55

66
env:
7-
- REACT_VERSION=next # 16 beta
7+
- REACT_VERSION=16
88
- REACT_VERSION=15
99

1010
script:

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ yarn add react-html-parser
2121

2222
```javascript
2323
import React from 'react';
24-
import ReactHtmlParser from 'react-html-parser';
24+
import ReactHtmlParser, { processNodes, convertNodeToElement, htmlparser2 } from 'react-html-parser';
2525

2626
class HtmlComponent extends React.Component {
2727
render() {
@@ -45,6 +45,7 @@ import ReactHtmlParser from 'react-html-parser';
4545
- `options`: Options object
4646
- decodeEntities=true *(boolean)*: Whether to decode html entities (defaults to true)
4747
- transform *(function)*: Transform function that is applied to every node
48+
- preprocessNodes *(function)*: Pre-process the nodes generated by `htmlparser2`
4849

4950
#### Transform Function
5051
The transform function will be called for every node that is parsed by the library.
@@ -86,6 +87,16 @@ function transform(node) {
8687
}
8788
```
8889

90+
#### preprocessNodes Function
91+
Allows pre-processing the nodes generated from the html by `htmlparser2` before being passed to the library and converted to React elements.
92+
93+
`function preprocessNodes(nodes)`
94+
##### Arguments
95+
- `nodes`: The entire node tree generated by `htmlparser2`.
96+
97+
##### Return type
98+
The `preprocessNodes` function should return a valid `htmlparser2` node tree.
99+
89100
### `function convertNodeToElement(node, index, transform)`
90101
Processes a node and returns the React element to be rendered. This function can be used in conjunction with the previously described `transform` function to continue to process a node after modifying it.
91102

@@ -108,3 +119,9 @@ function transform(node, index) {
108119
}
109120
}
110121
```
122+
123+
### `htmlparser2`
124+
The library exposes the `htmlparser2` library it uses. This allows consumers
125+
to use it without having to add it as a separate dependency.
126+
127+
See https://github.com/fb55/htmlparser2 for full details.

package-lock.json

+16-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/HtmlParser.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import processNodes from './processNodes';
88
* @param {Object} options Options to pass
99
* @returns {Array} List of top level React elements
1010
*/
11-
export default function HtmlParser(html, { decodeEntities=true, transform }={}) {
12-
const nodes = htmlparser2.parseDOM(html, { decodeEntities });
11+
export default function HtmlParser(html, {
12+
decodeEntities = true,
13+
transform,
14+
preprocessNodes = nodes => nodes
15+
}={}) {
16+
const nodes = preprocessNodes(htmlparser2.parseDOM(html, { decodeEntities }));
1317
return processNodes(nodes, transform);
1418
}

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ export default HtmlParser;
33

44
export { default as processNodes } from './processNodes';
55
export { default as convertNodeToElement } from './convertNodeToElement';
6+
7+
// expose htmlparser2 so it can be used if required
8+
export { default as htmlparser2 } from 'htmlparser2';

test/integration/integration.spec.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import ReactDOMServer from 'react-dom/server';
3-
import HtmlParser, { convertNodeToElement } from 'index';
3+
import HtmlParser, { convertNodeToElement, htmlparser2 } from 'index';
44

55
const reactVersion = parseInt(require('react/package.json').version.match(/^(\d+)\./)[1], 10);
66

@@ -170,4 +170,23 @@ describe('Integration tests: ', () => {
170170
);
171171
});
172172

173+
it('should preprocess nodes correctly', () => {
174+
test(
175+
'<div>preprocess test</div>',
176+
'<div>preprocess test</div><div>preprocess test</div>',
177+
{
178+
preprocessNodes(nodes) {
179+
return [
180+
...nodes,
181+
...nodes
182+
];
183+
}
184+
}
185+
);
186+
});
187+
188+
it('should expose htmlparser2', () => {
189+
expect(htmlparser2).toBeDefined();
190+
});
191+
173192
});

test/unit/HtmlParser.spec.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ describe('Testing: `HtmlParser`', () => {
2222
});
2323

2424
it('should apply the options', () => {
25-
const transform = function() {};
26-
expect(HtmlParser('html', { decodeEntities:false, transform })).toBe('processed');
25+
const transform = jasmine.createSpy('transform');
26+
const preprocessNodes = jasmine.createSpy('preprocessNodes').and.callFake(v => `preprocessed ${v}`);
27+
expect(HtmlParser('html', { decodeEntities:false, transform, preprocessNodes })).toBe('processed');
2728
expect(htmlparser2.parseDOM).toHaveBeenCalledWith('html', {decodeEntities: false});
28-
expect(processNodes).toHaveBeenCalledWith('parsed', transform);
29+
expect(processNodes).toHaveBeenCalledWith('preprocessed parsed', transform);
2930
});
3031

3132
});

0 commit comments

Comments
 (0)