html-react-parser 5.2.16 → 5.2.17

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
@@ -614,9 +614,9 @@ npm run benchmark
614
614
  Output of benchmark run on MacBook Pro 2024:
615
615
 
616
616
  ```
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)
617
+ html-to-react - Single x 1,270,990 ops/sec ±0.21% (100 runs sampled)
618
+ html-to-react - Multiple x 522,472 ops/sec ±0.64% (94 runs sampled)
619
+ html-to-react - Complex x 54,028 ops/sec ±0.82% (98 runs sampled)
620
620
  ```
621
621
 
622
622
  Run [Size Limit](https://github.com/ai/size-limit):
@@ -824,6 +824,7 @@
824
824
  hasRequiredUtilities$2 = 1;
825
825
  Object.defineProperty(utilities$2, "__esModule", { value: true });
826
826
  utilities$2.formatAttributes = formatAttributes;
827
+ utilities$2.hasOpenTag = hasOpenTag;
827
828
  utilities$2.escapeSpecialCharacters = escapeSpecialCharacters;
828
829
  utilities$2.revertEscapedCharacters = revertEscapedCharacters;
829
830
  utilities$2.formatDOM = formatDOM;
@@ -870,6 +871,28 @@
870
871
  }
871
872
  return tagName;
872
873
  }
874
+ /**
875
+ * Checks if an HTML string contains an opening tag (case-insensitive).
876
+ *
877
+ * @param html - HTML string.
878
+ * @param tagName - Tag name to search for (e.g., 'head' or 'body').
879
+ * @returns - Whether the tag is found.
880
+ */
881
+ function hasOpenTag(html, tagName) {
882
+ var openTag = '<' + tagName;
883
+ var index = html.toLowerCase().indexOf(openTag);
884
+ if (index === -1) {
885
+ return false;
886
+ }
887
+ var char = html[index + openTag.length];
888
+ // the character after the tag name must be '>' or whitespace (for attributes)
889
+ return (char === '>' ||
890
+ char === ' ' ||
891
+ char === '\t' ||
892
+ char === '\n' ||
893
+ char === '\r' ||
894
+ char === '/');
895
+ }
873
896
  /**
874
897
  * Escapes special characters before parsing.
875
898
  *
@@ -919,11 +942,9 @@
919
942
  break;
920
943
  }
921
944
  case 3:
922
- /* istanbul ignore next */
923
945
  current = new domhandler_1.Text(revertEscapedCharacters((_a = node.nodeValue) !== null && _a !== void 0 ? _a : ''));
924
946
  break;
925
947
  case 8:
926
- /* istanbul ignore next */
927
948
  current = new domhandler_1.Comment((_b = node.nodeValue) !== null && _b !== void 0 ? _b : '');
928
949
  break;
929
950
  default:
@@ -969,20 +990,17 @@
969
990
  var HEAD = 'head';
970
991
  var BODY = 'body';
971
992
  var FIRST_TAG_REGEX = /<([a-zA-Z]+[0-9]?)/; // e.g., <h1>
972
- // match-all-characters in case of newlines (DOTALL)
973
- var HEAD_TAG_REGEX = /<head[^]*>/i;
974
- var BODY_TAG_REGEX = /<body[^]*>/i;
975
993
  // falls back to `parseFromString` if `createHTMLDocument` cannot be used
976
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
994
+ /* eslint-disable @typescript-eslint/no-unused-vars */
995
+ /* istanbul ignore start */
977
996
  var parseFromDocument = function (html, tagName) {
978
- /* istanbul ignore next */
979
997
  throw new Error('This browser does not support `document.implementation.createHTMLDocument`');
980
998
  };
981
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
982
999
  var parseFromString = function (html, tagName) {
983
- /* istanbul ignore next */
984
1000
  throw new Error('This browser does not support `DOMParser.prototype.parseFromString`');
985
1001
  };
1002
+ /* istanbul ignore stop */
1003
+ /* eslint-enable @typescript-eslint/no-unused-vars */
986
1004
  var DOMParser = typeof window === 'object' && window.DOMParser;
987
1005
  /**
988
1006
  * DOMParser (performance: slow).
@@ -1000,10 +1018,11 @@
1000
1018
  * @returns - Document.
1001
1019
  */
1002
1020
  parseFromString = function (html, tagName) {
1003
- /* istanbul ignore if */
1021
+ /* istanbul ignore start */
1004
1022
  if (tagName) {
1005
1023
  html = "<".concat(tagName, ">").concat(html, "</").concat(tagName, ">");
1006
1024
  }
1025
+ /* istanbul ignore stop */
1007
1026
  return domParser_1.parseFromString(html, mimeType_1);
1008
1027
  };
1009
1028
  parseFromDocument = parseFromString;
@@ -1024,7 +1043,7 @@
1024
1043
  * @returns - Document
1025
1044
  */
1026
1045
  parseFromDocument = function (html, tagName) {
1027
- /* istanbul ignore if */
1046
+ /* istanbul ignore start */
1028
1047
  if (tagName) {
1029
1048
  var element = htmlDocument_1.documentElement.querySelector(tagName);
1030
1049
  if (element) {
@@ -1032,6 +1051,7 @@
1032
1051
  }
1033
1052
  return htmlDocument_1;
1034
1053
  }
1054
+ /* istanbul ignore stop */
1035
1055
  htmlDocument_1.documentElement.innerHTML = html;
1036
1056
  return htmlDocument_1;
1037
1057
  };
@@ -1056,8 +1076,9 @@
1056
1076
  return template.content.childNodes;
1057
1077
  };
1058
1078
  }
1059
- /* istanbul ignore next */
1060
- var createNodeList = function () { return document.createDocumentFragment().childNodes; };
1079
+ var createNodeList = /* istanbul ignore next */ function () {
1080
+ return document.createDocumentFragment().childNodes;
1081
+ };
1061
1082
  /**
1062
1083
  * Parses HTML string to DOM nodes.
1063
1084
  *
@@ -1075,12 +1096,12 @@
1075
1096
  var doc = parseFromString(html);
1076
1097
  // the created document may come with filler head/body elements,
1077
1098
  // so make sure to remove them if they don't actually exist
1078
- if (!HEAD_TAG_REGEX.test(html)) {
1099
+ if (!(0, utilities_1.hasOpenTag)(html, HEAD)) {
1079
1100
  var element = doc.querySelector(HEAD);
1080
1101
  /* istanbul ignore next */
1081
1102
  (_b = element === null || element === void 0 ? void 0 : element.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(element);
1082
1103
  }
1083
- if (!BODY_TAG_REGEX.test(html)) {
1104
+ if (!(0, utilities_1.hasOpenTag)(html, BODY)) {
1084
1105
  var element = doc.querySelector(BODY);
1085
1106
  /* istanbul ignore next */
1086
1107
  (_c = element === null || element === void 0 ? void 0 : element.parentNode) === null || _c === void 0 ? void 0 : _c.removeChild(element);
@@ -1091,7 +1112,7 @@
1091
1112
  case BODY: {
1092
1113
  var elements = parseFromDocument(html).querySelectorAll(firstTagName);
1093
1114
  // if there's a sibling element, then return both elements
1094
- if (BODY_TAG_REGEX.test(html) && HEAD_TAG_REGEX.test(html)) {
1115
+ if ((0, utilities_1.hasOpenTag)(html, BODY) && (0, utilities_1.hasOpenTag)(html, HEAD)) {
1095
1116
  /* istanbul ignore next */
1096
1117
  return (_e = (_d = elements[0].parentNode) === null || _d === void 0 ? void 0 : _d.childNodes) !== null && _e !== void 0 ? _e : createNodeList();
1097
1118
  }
@@ -1103,10 +1124,10 @@
1103
1124
  if (parseFromTemplate) {
1104
1125
  return parseFromTemplate(html);
1105
1126
  }
1106
- /* istanbul ignore next */
1127
+ /* istanbul ignore start */
1107
1128
  var element = parseFromDocument(html, BODY).querySelector(BODY);
1108
- /* istanbul ignore next */
1109
1129
  return (_f = element === null || element === void 0 ? void 0 : element.childNodes) !== null && _f !== void 0 ? _f : createNodeList();
1130
+ /* istanbul ignore stop */
1110
1131
  }
1111
1132
  }
1112
1133
  }