html-react-parser 1.4.3 → 1.4.7

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 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
- To remove whitespace, enable the `trim` option:
353
+ But certain elements like `<table>` will strip out invalid whitespace:
355
354
 
356
355
  ```js
357
- parse('<br>\n', { trim: true }); // React.createElement('br')
356
+ parse('<table>\n</table>'); // React.createElement('table')
358
357
  ```
359
358
 
360
- This fixes the warning:
359
+ To remove whitespace, enable the `trim` option:
361
360
 
362
- ```
363
- Warning: validateDOMNesting(...): Whitespace text nodes cannot appear as a child of <table>. Make sure you don't have any extra whitespace between tags on each line of your source code.
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;
@@ -1298,12 +1324,18 @@
1298
1324
  var attributesToProps$2 = function attributesToProps(attributes) {
1299
1325
  attributes = attributes || {};
1300
1326
 
1327
+ var valueOnlyInputs = {
1328
+ reset: true,
1329
+ submit: true
1330
+ };
1331
+
1301
1332
  var attributeName;
1302
1333
  var attributeNameLowerCased;
1303
1334
  var attributeValue;
1304
1335
  var propName;
1305
1336
  var propertyInfo;
1306
1337
  var props = {};
1338
+ var inputIsValueOnly = attributes.type && valueOnlyInputs[attributes.type];
1307
1339
 
1308
1340
  for (attributeName in attributes) {
1309
1341
  attributeValue = attributes[attributeName];
@@ -1323,7 +1355,10 @@
1323
1355
 
1324
1356
  // convert attribute to uncontrolled component prop (e.g., `value` to `defaultValue`)
1325
1357
  // https://reactjs.org/docs/uncontrolled-components.html
1326
- if (propName === 'checked' || propName === 'value') {
1358
+ if (
1359
+ (propName === 'checked' || propName === 'value') &&
1360
+ !inputIsValueOnly
1361
+ ) {
1327
1362
  propName = getPropName('default' + attributeNameLowerCased);
1328
1363
  }
1329
1364
 
@@ -1369,6 +1404,7 @@
1369
1404
  var utilities$1 = utilities$3;
1370
1405
 
1371
1406
  var setStyleProp = utilities$1.setStyleProp;
1407
+ var canTextBeChildOfNode = utilities$1.canTextBeChildOfNode;
1372
1408
 
1373
1409
  /**
1374
1410
  * Converts DOM nodes to JSX element(s).
@@ -1389,11 +1425,11 @@
1389
1425
 
1390
1426
  var result = [];
1391
1427
  var node;
1428
+ var isWhitespace;
1392
1429
  var hasReplace = typeof options.replace === 'function';
1393
1430
  var replaceElement;
1394
1431
  var props;
1395
1432
  var children;
1396
- var data;
1397
1433
  var trim = options.trim;
1398
1434
 
1399
1435
  for (var i = 0, len = nodes.length; i < len; i++) {
@@ -1417,15 +1453,23 @@
1417
1453
  }
1418
1454
 
1419
1455
  if (node.type === 'text') {
1420
- // if trim option is enabled, skip whitespace text nodes
1421
- if (trim) {
1422
- data = node.data.trim();
1423
- if (data) {
1424
- result.push(node.data);
1425
- }
1426
- } else {
1427
- result.push(node.data);
1456
+ isWhitespace = !node.data.trim().length;
1457
+
1458
+ if (isWhitespace && node.parent && !canTextBeChildOfNode(node.parent)) {
1459
+ // We have a whitespace node that can't be nested in its parent
1460
+ // so skip it
1461
+ continue;
1428
1462
  }
1463
+
1464
+ if (trim && isWhitespace) {
1465
+ // Trim is enabled and we have a whitespace node
1466
+ // so skip it
1467
+ continue;
1468
+ }
1469
+
1470
+ // We have a text node that's not whitespace and it can be nested
1471
+ // in its parent so add it to the results
1472
+ result.push(node.data);
1429
1473
  continue;
1430
1474
  }
1431
1475