html-react-parser 3.0.16 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -18
- package/dist/html-react-parser.js +11 -4
- package/dist/html-react-parser.js.map +1 -1
- package/dist/html-react-parser.min.js +1 -1
- package/dist/html-react-parser.min.js.map +1 -1
- package/index.d.ts +6 -0
- package/lib/dom-to-react.js +5 -3
- package/lib/utilities.js +6 -1
- package/package.json +20 -20
package/README.md
CHANGED
|
@@ -38,13 +38,15 @@ parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')
|
|
|
38
38
|
- [replace element and children](#replace-element-and-children)
|
|
39
39
|
- [replace element attributes](#replace-element-attributes)
|
|
40
40
|
- [replace and remove element](#replace-and-remove-element)
|
|
41
|
+
- [transform](#transform)
|
|
41
42
|
- [library](#library)
|
|
42
43
|
- [htmlparser2](#htmlparser2)
|
|
43
44
|
- [trim](#trim)
|
|
44
45
|
- [Migration](#migration)
|
|
45
|
-
- [
|
|
46
|
-
- [
|
|
47
|
-
- [
|
|
46
|
+
- [v4](#v4)
|
|
47
|
+
- [v3](#v3)
|
|
48
|
+
- [v2](#v2)
|
|
49
|
+
- [v1](#v1)
|
|
48
50
|
- [FAQ](#faq)
|
|
49
51
|
- [Is this XSS safe?](#is-this-xss-safe)
|
|
50
52
|
- [Does invalid HTML get sanitized?](#does-invalid-html-get-sanitized)
|
|
@@ -64,7 +66,6 @@ parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')
|
|
|
64
66
|
- [Financial Contributors](#financial-contributors)
|
|
65
67
|
- [Individuals](#individuals)
|
|
66
68
|
- [Organizations](#organizations)
|
|
67
|
-
- [Enterprise](#enterprise)
|
|
68
69
|
- [Support](#support)
|
|
69
70
|
- [License](#license)
|
|
70
71
|
|
|
@@ -97,13 +98,15 @@ yarn add html-react-parser
|
|
|
97
98
|
|
|
98
99
|
## Usage
|
|
99
100
|
|
|
100
|
-
Import
|
|
101
|
+
Import ES module:
|
|
101
102
|
|
|
102
103
|
```js
|
|
103
|
-
// ES Modules
|
|
104
104
|
import parse from 'html-react-parser';
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Or require CommonJS module:
|
|
105
108
|
|
|
106
|
-
|
|
109
|
+
```js
|
|
107
110
|
const parse = require('html-react-parser');
|
|
108
111
|
```
|
|
109
112
|
|
|
@@ -301,6 +304,21 @@ HTML output:
|
|
|
301
304
|
<p></p>
|
|
302
305
|
```
|
|
303
306
|
|
|
307
|
+
### transform
|
|
308
|
+
|
|
309
|
+
The `transform` option allows you to transform each element individually after it's parsed.
|
|
310
|
+
|
|
311
|
+
The `transform` callback's first argument is the React element:
|
|
312
|
+
|
|
313
|
+
```jsx
|
|
314
|
+
parse('<br>', {
|
|
315
|
+
transform: (reactNode, domNode, index) => {
|
|
316
|
+
// this will wrap every element in a div
|
|
317
|
+
return <div>{reactNode}</div>;
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
```
|
|
321
|
+
|
|
304
322
|
### library
|
|
305
323
|
|
|
306
324
|
The `library` option specifies the UI library. The default library is **React**.
|
|
@@ -375,17 +393,21 @@ parse('<p> </p>', { trim: true }); // React.createElement('p')
|
|
|
375
393
|
|
|
376
394
|
## Migration
|
|
377
395
|
|
|
378
|
-
###
|
|
396
|
+
### v4
|
|
397
|
+
|
|
398
|
+
[htmlparser2](https://github.com/fb55/htmlparser2) has been upgraded to [v9](https://github.com/fb55/htmlparser2/releases/tag/v9.0.0).
|
|
399
|
+
|
|
400
|
+
### v3
|
|
379
401
|
|
|
380
402
|
[domhandler](https://github.com/fb55/domhandler) has been upgraded to v5 so some [parser options](https://github.com/fb55/htmlparser2/wiki/Parser-options) like `normalizeWhitespace` have been removed.
|
|
381
403
|
|
|
382
|
-
Also, it's recommended to upgrade to the latest version of [
|
|
404
|
+
Also, it's recommended to upgrade to the latest version of [TypeScript](https://www.npmjs.com/package/typescript).
|
|
383
405
|
|
|
384
|
-
### v2
|
|
406
|
+
### v2
|
|
385
407
|
|
|
386
408
|
Since [v2.0.0](https://github.com/remarkablemark/html-react-parser/releases/tag/v2.0.0), Internet Explorer (IE) is no longer supported.
|
|
387
409
|
|
|
388
|
-
### v1
|
|
410
|
+
### v1
|
|
389
411
|
|
|
390
412
|
TypeScript projects will need to update the types in [v1.0.0](https://github.com/remarkablemark/html-react-parser/releases/tag/v1.0.0).
|
|
391
413
|
|
|
@@ -558,12 +580,6 @@ Support this project with your organization. Your logo will show up here with a
|
|
|
558
580
|
[](https://opencollective.com/html-react-parser/organization/8/website)
|
|
559
581
|
[](https://opencollective.com/html-react-parser/organization/9/website)
|
|
560
582
|
|
|
561
|
-
## Enterprise
|
|
562
|
-
|
|
563
|
-
Available as part of the Tidelift Subscription.
|
|
564
|
-
|
|
565
|
-
The maintainers of html-react-parser and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-html-react-parser?utm_source=npm-html-react-parser&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
|
|
566
|
-
|
|
567
583
|
## Support
|
|
568
584
|
|
|
569
585
|
- [GitHub Sponsors](https://b.remarkabl.org/github-sponsors)
|
|
@@ -572,7 +588,7 @@ The maintainers of html-react-parser and thousands of other packages are working
|
|
|
572
588
|
- [Patreon](https://b.remarkabl.org/patreon)
|
|
573
589
|
- [Ko-fi](https://b.remarkabl.org/ko-fi)
|
|
574
590
|
- [Liberapay](https://b.remarkabl.org/liberapay)
|
|
575
|
-
- [
|
|
591
|
+
- [Teespring](https://b.remarkabl.org/teespring)
|
|
576
592
|
|
|
577
593
|
## License
|
|
578
594
|
|
|
@@ -2398,13 +2398,18 @@
|
|
|
2398
2398
|
return !elementsWithNoTextChildren.has(node.name);
|
|
2399
2399
|
}
|
|
2400
2400
|
|
|
2401
|
+
function returnFirstArg(arg) {
|
|
2402
|
+
return arg;
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2401
2405
|
var utilities$2 = {
|
|
2402
2406
|
PRESERVE_CUSTOM_ATTRIBUTES: PRESERVE_CUSTOM_ATTRIBUTES,
|
|
2403
2407
|
invertObject: invertObject,
|
|
2404
2408
|
isCustomComponent: isCustomComponent,
|
|
2405
2409
|
setStyleProp: setStyleProp$1,
|
|
2406
2410
|
canTextBeChildOfNode: canTextBeChildOfNode$1,
|
|
2407
|
-
elementsWithNoTextChildren: elementsWithNoTextChildren
|
|
2411
|
+
elementsWithNoTextChildren: elementsWithNoTextChildren,
|
|
2412
|
+
returnFirstArg: returnFirstArg
|
|
2408
2413
|
};
|
|
2409
2414
|
|
|
2410
2415
|
var reactProperty = lib;
|
|
@@ -2513,6 +2518,7 @@
|
|
|
2513
2518
|
* @param {DomElement[]} nodes - DOM nodes.
|
|
2514
2519
|
* @param {object} [options={}] - Options.
|
|
2515
2520
|
* @param {Function} [options.replace] - Replacer.
|
|
2521
|
+
* @param {Function} [options.transform] - Transform.
|
|
2516
2522
|
* @param {object} [options.library] - Library (React, Preact, etc.).
|
|
2517
2523
|
* @returns - String or JSX element(s).
|
|
2518
2524
|
*/
|
|
@@ -2528,6 +2534,7 @@
|
|
|
2528
2534
|
var node;
|
|
2529
2535
|
var isWhitespace;
|
|
2530
2536
|
var hasReplace = typeof options.replace === 'function';
|
|
2537
|
+
var transform = options.transform || utilities.returnFirstArg;
|
|
2531
2538
|
var replaceElement;
|
|
2532
2539
|
var props;
|
|
2533
2540
|
var children;
|
|
@@ -2548,7 +2555,7 @@
|
|
|
2548
2555
|
key: replaceElement.key || i
|
|
2549
2556
|
});
|
|
2550
2557
|
}
|
|
2551
|
-
result.push(replaceElement);
|
|
2558
|
+
result.push(transform(replaceElement, node, i));
|
|
2552
2559
|
continue;
|
|
2553
2560
|
}
|
|
2554
2561
|
}
|
|
@@ -2570,7 +2577,7 @@
|
|
|
2570
2577
|
|
|
2571
2578
|
// We have a text node that's not whitespace and it can be nested
|
|
2572
2579
|
// in its parent so add it to the results
|
|
2573
|
-
result.push(node.data);
|
|
2580
|
+
result.push(transform(node.data, node, i));
|
|
2574
2581
|
continue;
|
|
2575
2582
|
}
|
|
2576
2583
|
|
|
@@ -2617,7 +2624,7 @@
|
|
|
2617
2624
|
props.key = i;
|
|
2618
2625
|
}
|
|
2619
2626
|
|
|
2620
|
-
result.push(createElement(node.name, props, children));
|
|
2627
|
+
result.push(transform(createElement(node.name, props, children), node, i));
|
|
2621
2628
|
}
|
|
2622
2629
|
|
|
2623
2630
|
return result.length === 1 ? result[0] : result;
|