html-react-parser 5.2.14 → 5.2.16

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
@@ -611,12 +611,12 @@ Run benchmark:
611
611
  npm run benchmark
612
612
  ```
613
613
 
614
- Output of benchmark run on MacBook Pro 2021:
614
+ Output of benchmark run on MacBook Pro 2024:
615
615
 
616
616
  ```
617
- html-to-react - Single x 1,018,239 ops/sec ±0.43% (94 runs sampled)
618
- html-to-react - Multiple x 380,037 ops/sec ±0.61% (97 runs sampled)
619
- html-to-react - Complex x 35,091 ops/sec ±0.50% (96 runs sampled)
617
+ html-to-react - Single x 1,230,874 ops/sec ±0.21% (97 runs sampled)
618
+ html-to-react - Multiple x 502,028 ops/sec ±0.89% (94 runs sampled)
619
+ html-to-react - Complex x 54,401 ops/sec ±0.76% (93 runs sampled)
620
620
  ```
621
621
 
622
622
  Run [Size Limit](https://github.com/ai/size-limit):
@@ -810,7 +810,7 @@
810
810
  }, {});
811
811
  exports$1.CARRIAGE_RETURN = '\r';
812
812
  exports$1.CARRIAGE_RETURN_REGEX = new RegExp(exports$1.CARRIAGE_RETURN, 'g');
813
- exports$1.CARRIAGE_RETURN_PLACEHOLDER = "__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_".concat(Date.now(), "__");
813
+ exports$1.CARRIAGE_RETURN_PLACEHOLDER = "__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_".concat(Date.now().toString(), "__");
814
814
  exports$1.CARRIAGE_RETURN_PLACEHOLDER_REGEX = new RegExp(exports$1.CARRIAGE_RETURN_PLACEHOLDER, 'g');
815
815
 
816
816
  } (constants));
@@ -897,6 +897,7 @@
897
897
  * @returns - Nodes.
898
898
  */
899
899
  function formatDOM(nodes, parent, directive) {
900
+ var _a, _b, _c, _d;
900
901
  if (parent === void 0) { parent = null; }
901
902
  var domNodes = [];
902
903
  var current;
@@ -918,16 +919,19 @@
918
919
  break;
919
920
  }
920
921
  case 3:
921
- current = new domhandler_1.Text(revertEscapedCharacters(node.nodeValue));
922
+ /* istanbul ignore next */
923
+ current = new domhandler_1.Text(revertEscapedCharacters((_a = node.nodeValue) !== null && _a !== void 0 ? _a : ''));
922
924
  break;
923
925
  case 8:
924
- current = new domhandler_1.Comment(node.nodeValue);
926
+ /* istanbul ignore next */
927
+ current = new domhandler_1.Comment((_b = node.nodeValue) !== null && _b !== void 0 ? _b : '');
925
928
  break;
926
929
  default:
927
930
  continue;
928
931
  }
929
932
  // set previous node next
930
- var prev = domNodes[index - 1] || null;
933
+ var prev = (_c = domNodes[index - 1]) !== null && _c !== void 0 ? _c : null;
934
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
931
935
  if (prev) {
932
936
  prev.next = current;
933
937
  }
@@ -939,7 +943,7 @@
939
943
  }
940
944
  if (directive) {
941
945
  current = new domhandler_1.ProcessingInstruction(directive.substring(0, directive.indexOf(' ')).toLowerCase(), directive);
942
- current.next = domNodes[0] || null;
946
+ current.next = (_d = domNodes[0]) !== null && _d !== void 0 ? _d : null;
943
947
  current.parent = parent;
944
948
  domNodes.unshift(current);
945
949
  if (domNodes[1]) {
@@ -996,8 +1000,8 @@
996
1000
  * @returns - Document.
997
1001
  */
998
1002
  parseFromString = function (html, tagName) {
1003
+ /* istanbul ignore if */
999
1004
  if (tagName) {
1000
- /* istanbul ignore next */
1001
1005
  html = "<".concat(tagName, ">").concat(html, "</").concat(tagName, ">");
1002
1006
  }
1003
1007
  return domParser_1.parseFromString(html, mimeType_1);
@@ -1009,6 +1013,7 @@
1009
1013
  *
1010
1014
  * @see https://developer.mozilla.org/docs/Web/API/DOMImplementation/createHTMLDocument
1011
1015
  */
1016
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1012
1017
  if (typeof document === 'object' && document.implementation) {
1013
1018
  var htmlDocument_1 = document.implementation.createHTMLDocument();
1014
1019
  /**
@@ -1019,6 +1024,7 @@
1019
1024
  * @returns - Document
1020
1025
  */
1021
1026
  parseFromDocument = function (html, tagName) {
1027
+ /* istanbul ignore if */
1022
1028
  if (tagName) {
1023
1029
  var element = htmlDocument_1.documentElement.querySelector(tagName);
1024
1030
  if (element) {
@@ -1037,6 +1043,7 @@
1037
1043
  */
1038
1044
  var template = typeof document === 'object' && document.createElement('template');
1039
1045
  var parseFromTemplate;
1046
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1040
1047
  if (template && template.content) {
1041
1048
  /**
1042
1049
  * Uses a template element (content fragment) to parse HTML.
@@ -1049,6 +1056,8 @@
1049
1056
  return template.content.childNodes;
1050
1057
  };
1051
1058
  }
1059
+ /* istanbul ignore next */
1060
+ var createNodeList = function () { return document.createDocumentFragment().childNodes; };
1052
1061
  /**
1053
1062
  * Parses HTML string to DOM nodes.
1054
1063
  *
@@ -1056,11 +1065,11 @@
1056
1065
  * @returns - DOM nodes.
1057
1066
  */
1058
1067
  function domparser$1(html) {
1059
- var _a, _b;
1068
+ var _a, _b, _c, _d, _e, _f;
1060
1069
  // Escape special characters before parsing
1061
1070
  html = (0, utilities_1.escapeSpecialCharacters)(html);
1062
- var match = html.match(FIRST_TAG_REGEX);
1063
- var firstTagName = match && match[1] ? match[1].toLowerCase() : '';
1071
+ var match = FIRST_TAG_REGEX.exec(html);
1072
+ var firstTagName = (_a = match === null || match === void 0 ? void 0 : match[1]) === null || _a === void 0 ? void 0 : _a.toLowerCase();
1064
1073
  switch (firstTagName) {
1065
1074
  case HTML: {
1066
1075
  var doc = parseFromString(html);
@@ -1068,11 +1077,13 @@
1068
1077
  // so make sure to remove them if they don't actually exist
1069
1078
  if (!HEAD_TAG_REGEX.test(html)) {
1070
1079
  var element = doc.querySelector(HEAD);
1071
- (_a = element === null || element === void 0 ? void 0 : element.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(element);
1080
+ /* istanbul ignore next */
1081
+ (_b = element === null || element === void 0 ? void 0 : element.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(element);
1072
1082
  }
1073
1083
  if (!BODY_TAG_REGEX.test(html)) {
1074
1084
  var element = doc.querySelector(BODY);
1075
- (_b = element === null || element === void 0 ? void 0 : element.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(element);
1085
+ /* istanbul ignore next */
1086
+ (_c = element === null || element === void 0 ? void 0 : element.parentNode) === null || _c === void 0 ? void 0 : _c.removeChild(element);
1076
1087
  }
1077
1088
  return doc.querySelectorAll(HTML);
1078
1089
  }
@@ -1081,17 +1092,21 @@
1081
1092
  var elements = parseFromDocument(html).querySelectorAll(firstTagName);
1082
1093
  // if there's a sibling element, then return both elements
1083
1094
  if (BODY_TAG_REGEX.test(html) && HEAD_TAG_REGEX.test(html)) {
1084
- return elements[0].parentNode.childNodes;
1095
+ /* istanbul ignore next */
1096
+ return (_e = (_d = elements[0].parentNode) === null || _d === void 0 ? void 0 : _d.childNodes) !== null && _e !== void 0 ? _e : createNodeList();
1085
1097
  }
1086
1098
  return elements;
1087
1099
  }
1088
1100
  // low-level tag or text
1089
1101
  default: {
1102
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1090
1103
  if (parseFromTemplate) {
1091
1104
  return parseFromTemplate(html);
1092
1105
  }
1106
+ /* istanbul ignore next */
1093
1107
  var element = parseFromDocument(html, BODY).querySelector(BODY);
1094
- return element.childNodes;
1108
+ /* istanbul ignore next */
1109
+ return (_f = element === null || element === void 0 ? void 0 : element.childNodes) !== null && _f !== void 0 ? _f : createNodeList();
1095
1110
  }
1096
1111
  }
1097
1112
  }
@@ -1126,7 +1141,7 @@
1126
1141
  return [];
1127
1142
  }
1128
1143
  // match directive
1129
- var match = html.match(DIRECTIVE_REGEX);
1144
+ var match = DIRECTIVE_REGEX.exec(html);
1130
1145
  var directive = match ? match[1] : undefined;
1131
1146
  return (0, utilities_1.formatDOM)((0, domparser_1.default)(html), null, directive);
1132
1147
  }
@@ -2688,7 +2703,7 @@
2688
2703
  * @param arg - The argument to be returned.
2689
2704
  * @returns - The input argument `arg`.
2690
2705
  */
2691
- var returnFirstArg = function (arg) { return arg; }; // eslint-disable-line @typescript-eslint/no-explicit-any
2706
+ var returnFirstArg = function (arg) { return arg; };
2692
2707
  exports$1.returnFirstArg = returnFirstArg;
2693
2708
 
2694
2709
  } (utilities$1));
@@ -2742,7 +2757,7 @@
2742
2757
  propName = getPropName('default' + attributeNameLowerCased);
2743
2758
  }
2744
2759
  props[propName] = attributeValue;
2745
- switch (propertyInfo && propertyInfo.type) {
2760
+ switch (propertyInfo === null || propertyInfo === void 0 ? void 0 : propertyInfo.type) {
2746
2761
  case react_property_1.BOOLEAN:
2747
2762
  props[propName] = true;
2748
2763
  break;
@@ -2783,6 +2798,7 @@
2783
2798
  function requireDomToReact () {
2784
2799
  if (hasRequiredDomToReact) return domToReact;
2785
2800
  hasRequiredDomToReact = 1;
2801
+ /* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
2786
2802
  var __importDefault = (domToReact && domToReact.__importDefault) || function (mod) {
2787
2803
  return (mod && mod.__esModule) ? mod : { "default": mod };
2788
2804
  };
@@ -2804,23 +2820,24 @@
2804
2820
  * @returns - String or JSX element(s).
2805
2821
  */
2806
2822
  function domToReact$1(nodes, options) {
2823
+ var _a, _b, _c, _d, _e;
2807
2824
  if (options === void 0) { options = {}; }
2808
2825
  var reactElements = [];
2809
2826
  var hasReplace = typeof options.replace === 'function';
2810
- var transform = options.transform || utilities_1.returnFirstArg;
2811
- var _a = options.library || React, cloneElement = _a.cloneElement, createElement = _a.createElement, isValidElement = _a.isValidElement;
2827
+ var transform = (_a = options.transform) !== null && _a !== void 0 ? _a : utilities_1.returnFirstArg;
2828
+ var _f = (_b = options.library) !== null && _b !== void 0 ? _b : React, cloneElement = _f.cloneElement, createElement = _f.createElement, isValidElement = _f.isValidElement;
2812
2829
  var nodesLength = nodes.length;
2813
2830
  for (var index = 0; index < nodesLength; index++) {
2814
2831
  var node = nodes[index];
2815
2832
  // replace with custom React element (if present)
2816
2833
  if (hasReplace) {
2817
- var replaceElement = options.replace(node, index);
2834
+ var replaceElement = (_c = options.replace) === null || _c === void 0 ? void 0 : _c.call(options, node, index);
2818
2835
  if (isValidElement(replaceElement)) {
2819
2836
  // set "key" prop for sibling elements
2820
2837
  // https://react.dev/learn/rendering-lists#rules-of-keys
2821
2838
  if (nodesLength > 1) {
2822
2839
  replaceElement = cloneElement(replaceElement, {
2823
- key: replaceElement.key || index,
2840
+ key: (_d = replaceElement.key) !== null && _d !== void 0 ? _d : index,
2824
2841
  });
2825
2842
  }
2826
2843
  reactElements.push(transform(replaceElement, node, index));
@@ -2851,6 +2868,7 @@
2851
2868
  if (skipAttributesToProps(element)) {
2852
2869
  (0, utilities_1.setStyleProp)(element.attribs.style, element.attribs);
2853
2870
  props = element.attribs;
2871
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
2854
2872
  }
2855
2873
  else if (element.attribs) {
2856
2874
  props = (0, attributes_to_props_1.default)(element.attribs, element.name);
@@ -2872,8 +2890,9 @@
2872
2890
  // https://react.dev/reference/react-dom/components/textarea#caveats
2873
2891
  if (node.name === 'textarea' && node.children[0]) {
2874
2892
  props.defaultValue = node.children[0].data;
2893
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
2875
2894
  }
2876
- else if (node.children && node.children.length) {
2895
+ else if ((_e = node.children) === null || _e === void 0 ? void 0 : _e.length) {
2877
2896
  // continue recursion of creating React elements (if applicable)
2878
2897
  children = domToReact$1(node.children, options);
2879
2898
  }
@@ -2939,13 +2958,14 @@
2939
2958
  * @returns - React element(s), empty array, or string.
2940
2959
  */
2941
2960
  function HTMLReactParser(html, options) {
2961
+ var _a;
2942
2962
  if (typeof html !== 'string') {
2943
2963
  throw new TypeError('First argument must be a string');
2944
2964
  }
2945
2965
  if (!html) {
2946
2966
  return [];
2947
2967
  }
2948
- return (0, dom_to_react_1.default)((0, html_dom_parser_1.default)(html, (options === null || options === void 0 ? void 0 : options.htmlparser2) || domParserOptions), options);
2968
+ return (0, dom_to_react_1.default)((0, html_dom_parser_1.default)(html, (_a = options === null || options === void 0 ? void 0 : options.htmlparser2) !== null && _a !== void 0 ? _a : domParserOptions), options);
2949
2969
  }
2950
2970
 
2951
2971
  } (lib$3));