html-react-parser 5.1.18 → 5.2.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.
@@ -29,150 +29,6 @@
29
29
 
30
30
  var domparser = {};
31
31
 
32
- var hasRequiredDomparser;
33
-
34
- function requireDomparser () {
35
- if (hasRequiredDomparser) return domparser;
36
- hasRequiredDomparser = 1;
37
- Object.defineProperty(domparser, "__esModule", { value: true });
38
- domparser.default = domparser$1;
39
- // constants
40
- var HTML = 'html';
41
- var HEAD = 'head';
42
- var BODY = 'body';
43
- var FIRST_TAG_REGEX = /<([a-zA-Z]+[0-9]?)/; // e.g., <h1>
44
- // match-all-characters in case of newlines (DOTALL)
45
- var HEAD_TAG_REGEX = /<head[^]*>/i;
46
- var BODY_TAG_REGEX = /<body[^]*>/i;
47
- // falls back to `parseFromString` if `createHTMLDocument` cannot be used
48
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
49
- var parseFromDocument = function (html, tagName) {
50
- /* istanbul ignore next */
51
- throw new Error('This browser does not support `document.implementation.createHTMLDocument`');
52
- };
53
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
54
- var parseFromString = function (html, tagName) {
55
- /* istanbul ignore next */
56
- throw new Error('This browser does not support `DOMParser.prototype.parseFromString`');
57
- };
58
- var DOMParser = typeof window === 'object' && window.DOMParser;
59
- /**
60
- * DOMParser (performance: slow).
61
- *
62
- * @see https://developer.mozilla.org/docs/Web/API/DOMParser#Parsing_an_SVG_or_HTML_document
63
- */
64
- if (typeof DOMParser === 'function') {
65
- var domParser_1 = new DOMParser();
66
- var mimeType_1 = 'text/html';
67
- /**
68
- * Creates an HTML document using `DOMParser.parseFromString`.
69
- *
70
- * @param html - The HTML string.
71
- * @param tagName - The element to render the HTML (with 'body' as fallback).
72
- * @returns - Document.
73
- */
74
- parseFromString = function (html, tagName) {
75
- if (tagName) {
76
- /* istanbul ignore next */
77
- html = "<".concat(tagName, ">").concat(html, "</").concat(tagName, ">");
78
- }
79
- return domParser_1.parseFromString(html, mimeType_1);
80
- };
81
- parseFromDocument = parseFromString;
82
- }
83
- /**
84
- * DOMImplementation (performance: fair).
85
- *
86
- * @see https://developer.mozilla.org/docs/Web/API/DOMImplementation/createHTMLDocument
87
- */
88
- if (typeof document === 'object' && document.implementation) {
89
- var htmlDocument_1 = document.implementation.createHTMLDocument();
90
- /**
91
- * Use HTML document created by `document.implementation.createHTMLDocument`.
92
- *
93
- * @param html - The HTML string.
94
- * @param tagName - The element to render the HTML (with 'body' as fallback).
95
- * @returns - Document
96
- */
97
- parseFromDocument = function (html, tagName) {
98
- if (tagName) {
99
- var element = htmlDocument_1.documentElement.querySelector(tagName);
100
- if (element) {
101
- element.innerHTML = html;
102
- }
103
- return htmlDocument_1;
104
- }
105
- htmlDocument_1.documentElement.innerHTML = html;
106
- return htmlDocument_1;
107
- };
108
- }
109
- /**
110
- * Template (performance: fast).
111
- *
112
- * @see https://developer.mozilla.org/docs/Web/HTML/Element/template
113
- */
114
- var template = typeof document === 'object' && document.createElement('template');
115
- var parseFromTemplate;
116
- if (template && template.content) {
117
- /**
118
- * Uses a template element (content fragment) to parse HTML.
119
- *
120
- * @param html - HTML string.
121
- * @returns - Nodes.
122
- */
123
- parseFromTemplate = function (html) {
124
- template.innerHTML = html;
125
- return template.content.childNodes;
126
- };
127
- }
128
- /**
129
- * Parses HTML string to DOM nodes.
130
- *
131
- * @param html - HTML markup.
132
- * @returns - DOM nodes.
133
- */
134
- function domparser$1(html) {
135
- var _a, _b;
136
- var match = html.match(FIRST_TAG_REGEX);
137
- var firstTagName = match && match[1] ? match[1].toLowerCase() : '';
138
- switch (firstTagName) {
139
- case HTML: {
140
- var doc = parseFromString(html);
141
- // the created document may come with filler head/body elements,
142
- // so make sure to remove them if they don't actually exist
143
- if (!HEAD_TAG_REGEX.test(html)) {
144
- var element = doc.querySelector(HEAD);
145
- (_a = element === null || element === void 0 ? void 0 : element.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(element);
146
- }
147
- if (!BODY_TAG_REGEX.test(html)) {
148
- var element = doc.querySelector(BODY);
149
- (_b = element === null || element === void 0 ? void 0 : element.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(element);
150
- }
151
- return doc.querySelectorAll(HTML);
152
- }
153
- case HEAD:
154
- case BODY: {
155
- var elements = parseFromDocument(html).querySelectorAll(firstTagName);
156
- // if there's a sibling element, then return both elements
157
- if (BODY_TAG_REGEX.test(html) && HEAD_TAG_REGEX.test(html)) {
158
- return elements[0].parentNode.childNodes;
159
- }
160
- return elements;
161
- }
162
- // low-level tag or text
163
- default: {
164
- if (parseFromTemplate) {
165
- return parseFromTemplate(html);
166
- }
167
- var element = parseFromDocument(html, BODY).querySelector(BODY);
168
- return element.childNodes;
169
- }
170
- }
171
- }
172
-
173
- return domparser;
174
- }
175
-
176
32
  var utilities$2 = {};
177
33
 
178
34
  var lib$2 = {};
@@ -964,6 +820,8 @@
964
820
  hasRequiredUtilities$2 = 1;
965
821
  Object.defineProperty(utilities$2, "__esModule", { value: true });
966
822
  utilities$2.formatAttributes = formatAttributes;
823
+ utilities$2.escapeSpecialCharacters = escapeSpecialCharacters;
824
+ utilities$2.revertEscapedCharacters = revertEscapedCharacters;
967
825
  utilities$2.formatDOM = formatDOM;
968
826
  var domhandler_1 = /*@__PURE__*/ requireLib$2();
969
827
  var constants_1 = requireConstants();
@@ -1008,6 +866,24 @@
1008
866
  }
1009
867
  return tagName;
1010
868
  }
869
+ /**
870
+ * Escapes special characters before parsing.
871
+ *
872
+ * @param html - The HTML string.
873
+ * @returns - HTML string with escaped special characters.
874
+ */
875
+ function escapeSpecialCharacters(html) {
876
+ return html.replace(/\r/g, '\\r');
877
+ }
878
+ /**
879
+ * Reverts escaped special characters back to actual characters.
880
+ *
881
+ * @param text - The text with escaped characters.
882
+ * @returns - Text with escaped characters reverted.
883
+ */
884
+ function revertEscapedCharacters(text) {
885
+ return text.replace(/\\r/g, '\r');
886
+ }
1011
887
  /**
1012
888
  * Transforms DOM nodes to `domhandler` nodes.
1013
889
  *
@@ -1038,7 +914,7 @@
1038
914
  break;
1039
915
  }
1040
916
  case 3:
1041
- current = new domhandler_1.Text(node.nodeValue);
917
+ current = new domhandler_1.Text(revertEscapedCharacters(node.nodeValue));
1042
918
  break;
1043
919
  case 8:
1044
920
  current = new domhandler_1.Comment(node.nodeValue);
@@ -1072,6 +948,153 @@
1072
948
  return utilities$2;
1073
949
  }
1074
950
 
951
+ var hasRequiredDomparser;
952
+
953
+ function requireDomparser () {
954
+ if (hasRequiredDomparser) return domparser;
955
+ hasRequiredDomparser = 1;
956
+ Object.defineProperty(domparser, "__esModule", { value: true });
957
+ domparser.default = domparser$1;
958
+ var utilities_1 = requireUtilities$2();
959
+ // constants
960
+ var HTML = 'html';
961
+ var HEAD = 'head';
962
+ var BODY = 'body';
963
+ var FIRST_TAG_REGEX = /<([a-zA-Z]+[0-9]?)/; // e.g., <h1>
964
+ // match-all-characters in case of newlines (DOTALL)
965
+ var HEAD_TAG_REGEX = /<head[^]*>/i;
966
+ var BODY_TAG_REGEX = /<body[^]*>/i;
967
+ // falls back to `parseFromString` if `createHTMLDocument` cannot be used
968
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
969
+ var parseFromDocument = function (html, tagName) {
970
+ /* istanbul ignore next */
971
+ throw new Error('This browser does not support `document.implementation.createHTMLDocument`');
972
+ };
973
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
974
+ var parseFromString = function (html, tagName) {
975
+ /* istanbul ignore next */
976
+ throw new Error('This browser does not support `DOMParser.prototype.parseFromString`');
977
+ };
978
+ var DOMParser = typeof window === 'object' && window.DOMParser;
979
+ /**
980
+ * DOMParser (performance: slow).
981
+ *
982
+ * @see https://developer.mozilla.org/docs/Web/API/DOMParser#Parsing_an_SVG_or_HTML_document
983
+ */
984
+ if (typeof DOMParser === 'function') {
985
+ var domParser_1 = new DOMParser();
986
+ var mimeType_1 = 'text/html';
987
+ /**
988
+ * Creates an HTML document using `DOMParser.parseFromString`.
989
+ *
990
+ * @param html - The HTML string.
991
+ * @param tagName - The element to render the HTML (with 'body' as fallback).
992
+ * @returns - Document.
993
+ */
994
+ parseFromString = function (html, tagName) {
995
+ if (tagName) {
996
+ /* istanbul ignore next */
997
+ html = "<".concat(tagName, ">").concat(html, "</").concat(tagName, ">");
998
+ }
999
+ return domParser_1.parseFromString(html, mimeType_1);
1000
+ };
1001
+ parseFromDocument = parseFromString;
1002
+ }
1003
+ /**
1004
+ * DOMImplementation (performance: fair).
1005
+ *
1006
+ * @see https://developer.mozilla.org/docs/Web/API/DOMImplementation/createHTMLDocument
1007
+ */
1008
+ if (typeof document === 'object' && document.implementation) {
1009
+ var htmlDocument_1 = document.implementation.createHTMLDocument();
1010
+ /**
1011
+ * Use HTML document created by `document.implementation.createHTMLDocument`.
1012
+ *
1013
+ * @param html - The HTML string.
1014
+ * @param tagName - The element to render the HTML (with 'body' as fallback).
1015
+ * @returns - Document
1016
+ */
1017
+ parseFromDocument = function (html, tagName) {
1018
+ if (tagName) {
1019
+ var element = htmlDocument_1.documentElement.querySelector(tagName);
1020
+ if (element) {
1021
+ element.innerHTML = html;
1022
+ }
1023
+ return htmlDocument_1;
1024
+ }
1025
+ htmlDocument_1.documentElement.innerHTML = html;
1026
+ return htmlDocument_1;
1027
+ };
1028
+ }
1029
+ /**
1030
+ * Template (performance: fast).
1031
+ *
1032
+ * @see https://developer.mozilla.org/docs/Web/HTML/Element/template
1033
+ */
1034
+ var template = typeof document === 'object' && document.createElement('template');
1035
+ var parseFromTemplate;
1036
+ if (template && template.content) {
1037
+ /**
1038
+ * Uses a template element (content fragment) to parse HTML.
1039
+ *
1040
+ * @param html - HTML string.
1041
+ * @returns - Nodes.
1042
+ */
1043
+ parseFromTemplate = function (html) {
1044
+ template.innerHTML = html;
1045
+ return template.content.childNodes;
1046
+ };
1047
+ }
1048
+ /**
1049
+ * Parses HTML string to DOM nodes.
1050
+ *
1051
+ * @param html - HTML markup.
1052
+ * @returns - DOM nodes.
1053
+ */
1054
+ function domparser$1(html) {
1055
+ var _a, _b;
1056
+ // Escape special characters before parsing
1057
+ html = (0, utilities_1.escapeSpecialCharacters)(html);
1058
+ var match = html.match(FIRST_TAG_REGEX);
1059
+ var firstTagName = match && match[1] ? match[1].toLowerCase() : '';
1060
+ switch (firstTagName) {
1061
+ case HTML: {
1062
+ var doc = parseFromString(html);
1063
+ // the created document may come with filler head/body elements,
1064
+ // so make sure to remove them if they don't actually exist
1065
+ if (!HEAD_TAG_REGEX.test(html)) {
1066
+ var element = doc.querySelector(HEAD);
1067
+ (_a = element === null || element === void 0 ? void 0 : element.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(element);
1068
+ }
1069
+ if (!BODY_TAG_REGEX.test(html)) {
1070
+ var element = doc.querySelector(BODY);
1071
+ (_b = element === null || element === void 0 ? void 0 : element.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(element);
1072
+ }
1073
+ return doc.querySelectorAll(HTML);
1074
+ }
1075
+ case HEAD:
1076
+ case BODY: {
1077
+ var elements = parseFromDocument(html).querySelectorAll(firstTagName);
1078
+ // if there's a sibling element, then return both elements
1079
+ if (BODY_TAG_REGEX.test(html) && HEAD_TAG_REGEX.test(html)) {
1080
+ return elements[0].parentNode.childNodes;
1081
+ }
1082
+ return elements;
1083
+ }
1084
+ // low-level tag or text
1085
+ default: {
1086
+ if (parseFromTemplate) {
1087
+ return parseFromTemplate(html);
1088
+ }
1089
+ var element = parseFromDocument(html, BODY).querySelector(BODY);
1090
+ return element.childNodes;
1091
+ }
1092
+ }
1093
+ }
1094
+
1095
+ return domparser;
1096
+ }
1097
+
1075
1098
  var hasRequiredHtmlToDom;
1076
1099
 
1077
1100
  function requireHtmlToDom () {