html-react-parser 1.4.3 → 1.4.4
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 +5 -10
- package/dist/html-react-parser.js +45 -10
- 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/lib/dom-to-react.js +18 -9
- package/lib/utilities.js +27 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -50,7 +50,6 @@ parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')
|
|
|
50
50
|
- [Parser throws an error](#parser-throws-an-error)
|
|
51
51
|
- [Is SSR supported?](#is-ssr-supported)
|
|
52
52
|
- [Elements aren't nested correctly](#elements-arent-nested-correctly)
|
|
53
|
-
- [Warning: validateDOMNesting(...): Whitespace text nodes cannot appear as a child of table](#warning-validatedomnesting-whitespace-text-nodes-cannot-appear-as-a-child-of-table)
|
|
54
53
|
- [Don't change case of tags](#dont-change-case-of-tags)
|
|
55
54
|
- [TS Error: Property 'attribs' does not exist on type 'DOMNode'](#ts-error-property-attribs-does-not-exist-on-type-domnode)
|
|
56
55
|
- [Can I enable `trim` for certain elements?](#can-i-enable-trim-for-certain-elements)
|
|
@@ -351,16 +350,16 @@ By default, whitespace is preserved:
|
|
|
351
350
|
parse('<br>\n'); // [React.createElement('br'), '\n']
|
|
352
351
|
```
|
|
353
352
|
|
|
354
|
-
|
|
353
|
+
But certain elements like `<table>` will strip out invalid whitespace:
|
|
355
354
|
|
|
356
355
|
```js
|
|
357
|
-
parse('<
|
|
356
|
+
parse('<table>\n</table>'); // React.createElement('table')
|
|
358
357
|
```
|
|
359
358
|
|
|
360
|
-
|
|
359
|
+
To remove whitespace, enable the `trim` option:
|
|
361
360
|
|
|
362
|
-
```
|
|
363
|
-
|
|
361
|
+
```js
|
|
362
|
+
parse('<br>\n', { trim: true }); // React.createElement('br')
|
|
364
363
|
```
|
|
365
364
|
|
|
366
365
|
However, intentional whitespace may be stripped out:
|
|
@@ -427,10 +426,6 @@ parse('<div /><div />'); // returns single element instead of array of elements
|
|
|
427
426
|
|
|
428
427
|
See [#158](https://github.com/remarkablemark/html-react-parser/issues/158).
|
|
429
428
|
|
|
430
|
-
### Warning: validateDOMNesting(...): Whitespace text nodes cannot appear as a child of table
|
|
431
|
-
|
|
432
|
-
Enable the [trim](#trim) option. See [#155](https://github.com/remarkablemark/html-react-parser/issues/155).
|
|
433
|
-
|
|
434
429
|
### Don't change case of tags
|
|
435
430
|
|
|
436
431
|
Tags are lowercased by default. To prevent that from happening, pass the [htmlparser2 option](#htmlparser2):
|
|
@@ -1279,11 +1279,37 @@
|
|
|
1279
1279
|
*/
|
|
1280
1280
|
var PRESERVE_CUSTOM_ATTRIBUTES = React$1.version.split('.')[0] >= 16;
|
|
1281
1281
|
|
|
1282
|
+
// Taken from
|
|
1283
|
+
// https://github.com/facebook/react/blob/cae635054e17a6f107a39d328649137b83f25972/packages/react-dom/src/client/validateDOMNesting.js#L213
|
|
1284
|
+
var elementsWithNoTextChildren = new Set([
|
|
1285
|
+
'tr',
|
|
1286
|
+
'tbody',
|
|
1287
|
+
'thead',
|
|
1288
|
+
'tfoot',
|
|
1289
|
+
'colgroup',
|
|
1290
|
+
'table',
|
|
1291
|
+
'head',
|
|
1292
|
+
'html',
|
|
1293
|
+
'frameset'
|
|
1294
|
+
]);
|
|
1295
|
+
|
|
1296
|
+
/**
|
|
1297
|
+
* Checks if the given node can contain text nodes
|
|
1298
|
+
*
|
|
1299
|
+
* @param {DomElement} node
|
|
1300
|
+
* @returns {boolean}
|
|
1301
|
+
*/
|
|
1302
|
+
function canTextBeChildOfNode$1(node) {
|
|
1303
|
+
return !elementsWithNoTextChildren.has(node.name);
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1282
1306
|
var utilities$3 = {
|
|
1283
1307
|
PRESERVE_CUSTOM_ATTRIBUTES: PRESERVE_CUSTOM_ATTRIBUTES,
|
|
1284
1308
|
invertObject: invertObject,
|
|
1285
1309
|
isCustomComponent: isCustomComponent,
|
|
1286
|
-
setStyleProp: setStyleProp$1
|
|
1310
|
+
setStyleProp: setStyleProp$1,
|
|
1311
|
+
canTextBeChildOfNode: canTextBeChildOfNode$1,
|
|
1312
|
+
elementsWithNoTextChildren: elementsWithNoTextChildren
|
|
1287
1313
|
};
|
|
1288
1314
|
|
|
1289
1315
|
var reactProperty = lib$1;
|
|
@@ -1369,6 +1395,7 @@
|
|
|
1369
1395
|
var utilities$1 = utilities$3;
|
|
1370
1396
|
|
|
1371
1397
|
var setStyleProp = utilities$1.setStyleProp;
|
|
1398
|
+
var canTextBeChildOfNode = utilities$1.canTextBeChildOfNode;
|
|
1372
1399
|
|
|
1373
1400
|
/**
|
|
1374
1401
|
* Converts DOM nodes to JSX element(s).
|
|
@@ -1389,11 +1416,11 @@
|
|
|
1389
1416
|
|
|
1390
1417
|
var result = [];
|
|
1391
1418
|
var node;
|
|
1419
|
+
var isWhitespace;
|
|
1392
1420
|
var hasReplace = typeof options.replace === 'function';
|
|
1393
1421
|
var replaceElement;
|
|
1394
1422
|
var props;
|
|
1395
1423
|
var children;
|
|
1396
|
-
var data;
|
|
1397
1424
|
var trim = options.trim;
|
|
1398
1425
|
|
|
1399
1426
|
for (var i = 0, len = nodes.length; i < len; i++) {
|
|
@@ -1417,15 +1444,23 @@
|
|
|
1417
1444
|
}
|
|
1418
1445
|
|
|
1419
1446
|
if (node.type === 'text') {
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
} else {
|
|
1427
|
-
result.push(node.data);
|
|
1447
|
+
isWhitespace = !node.data.trim().length;
|
|
1448
|
+
|
|
1449
|
+
if (isWhitespace && node.parent && !canTextBeChildOfNode(node.parent)) {
|
|
1450
|
+
// We have a whitespace node that can't be nested in its parent
|
|
1451
|
+
// so skip it
|
|
1452
|
+
continue;
|
|
1428
1453
|
}
|
|
1454
|
+
|
|
1455
|
+
if (trim && isWhitespace) {
|
|
1456
|
+
// Trim is enabled and we have a whitespace node
|
|
1457
|
+
// so skip it
|
|
1458
|
+
continue;
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
// We have a text node that's not whitespace and it can be nested
|
|
1462
|
+
// in its parent so add it to the results
|
|
1463
|
+
result.push(node.data);
|
|
1429
1464
|
continue;
|
|
1430
1465
|
}
|
|
1431
1466
|
|