html-react-parser 4.0.0 → 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 CHANGED
@@ -38,6 +38,7 @@ 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)
@@ -303,6 +304,21 @@ HTML output:
303
304
  <p></p>
304
305
  ```
305
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
+
306
322
  ### library
307
323
 
308
324
  The `library` option specifies the UI library. The default library is **React**.
@@ -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;