html-react-parser 6.1.0 → 6.1.1
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/dist/html-react-parser.js +144 -132
- package/dist/html-react-parser.js.map +1 -1
- package/dist/html-react-parser.min.js +1 -1
- package/dist/html-react-parser.min.js.map +1 -1
- package/lib/dom-to-react.d.ts.map +1 -1
- package/lib/dom-to-react.js +12 -0
- package/lib/dom-to-react.js.map +1 -1
- package/package.json +17 -17
- package/src/dom-to-react.ts +16 -0
|
@@ -2478,138 +2478,6 @@
|
|
|
2478
2478
|
|
|
2479
2479
|
var domToReact = {};
|
|
2480
2480
|
|
|
2481
|
-
var hasRequiredDomToReact;
|
|
2482
|
-
|
|
2483
|
-
function requireDomToReact () {
|
|
2484
|
-
if (hasRequiredDomToReact) return domToReact;
|
|
2485
|
-
hasRequiredDomToReact = 1;
|
|
2486
|
-
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
|
|
2487
|
-
var __importDefault = (domToReact && domToReact.__importDefault) || function (mod) {
|
|
2488
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
2489
|
-
};
|
|
2490
|
-
Object.defineProperty(domToReact, "__esModule", { value: true });
|
|
2491
|
-
domToReact.default = domToReact$1;
|
|
2492
|
-
const react_1 = require$$0;
|
|
2493
|
-
const attributes_to_props_1 = __importDefault(requireAttributesToProps());
|
|
2494
|
-
const utilities_1 = requireUtilities();
|
|
2495
|
-
const React = {
|
|
2496
|
-
cloneElement: react_1.cloneElement,
|
|
2497
|
-
createElement: react_1.createElement,
|
|
2498
|
-
isValidElement: react_1.isValidElement,
|
|
2499
|
-
};
|
|
2500
|
-
/**
|
|
2501
|
-
* Converts DOM nodes to JSX element(s).
|
|
2502
|
-
*
|
|
2503
|
-
* @param nodes - DOM nodes.
|
|
2504
|
-
* @param options - Options.
|
|
2505
|
-
* @returns - String or JSX element(s).
|
|
2506
|
-
*/
|
|
2507
|
-
function domToReact$1(nodes, options = {}) {
|
|
2508
|
-
var _a, _b, _c, _d, _e;
|
|
2509
|
-
const reactElements = [];
|
|
2510
|
-
const hasReplace = typeof options.replace === 'function';
|
|
2511
|
-
const transform = (_a = options.transform) !== null && _a !== void 0 ? _a : utilities_1.returnFirstArg;
|
|
2512
|
-
const { cloneElement, createElement, isValidElement } = (_b = options.library) !== null && _b !== void 0 ? _b : React;
|
|
2513
|
-
const nodesLength = nodes.length;
|
|
2514
|
-
for (let index = 0; index < nodesLength; index++) {
|
|
2515
|
-
const node = nodes[index];
|
|
2516
|
-
// replace with custom React element (if present)
|
|
2517
|
-
if (hasReplace) {
|
|
2518
|
-
let replaceElement = (_c = options.replace) === null || _c === void 0 ? void 0 : _c.call(options, node, index);
|
|
2519
|
-
if (isValidElement(replaceElement)) {
|
|
2520
|
-
// set "key" prop for sibling elements
|
|
2521
|
-
// https://react.dev/learn/rendering-lists#rules-of-keys
|
|
2522
|
-
if (nodesLength > 1) {
|
|
2523
|
-
replaceElement = cloneElement(replaceElement, {
|
|
2524
|
-
key: (_d = replaceElement.key) !== null && _d !== void 0 ? _d : index,
|
|
2525
|
-
});
|
|
2526
|
-
}
|
|
2527
|
-
reactElements.push(transform(replaceElement, node, index));
|
|
2528
|
-
continue;
|
|
2529
|
-
}
|
|
2530
|
-
}
|
|
2531
|
-
if (node.type === 'text') {
|
|
2532
|
-
const isWhitespace = !node.data.trim().length;
|
|
2533
|
-
// We have a whitespace node that can't be nested in its parent
|
|
2534
|
-
// so skip it
|
|
2535
|
-
if (isWhitespace &&
|
|
2536
|
-
node.parent &&
|
|
2537
|
-
!(0, utilities_1.canTextBeChildOfNode)(node.parent)) {
|
|
2538
|
-
continue;
|
|
2539
|
-
}
|
|
2540
|
-
// Trim is enabled and we have a whitespace node
|
|
2541
|
-
// so skip it
|
|
2542
|
-
if (options.trim && isWhitespace) {
|
|
2543
|
-
continue;
|
|
2544
|
-
}
|
|
2545
|
-
// We have a text node that's not whitespace and it can be nested
|
|
2546
|
-
// in its parent so add it to the results
|
|
2547
|
-
reactElements.push(transform(node.data, node, index));
|
|
2548
|
-
continue;
|
|
2549
|
-
}
|
|
2550
|
-
const element = node;
|
|
2551
|
-
let props = {};
|
|
2552
|
-
if (skipAttributesToProps(element)) {
|
|
2553
|
-
(0, utilities_1.setStyleProp)(element.attribs.style, element.attribs);
|
|
2554
|
-
props = element.attribs;
|
|
2555
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
2556
|
-
}
|
|
2557
|
-
else if (element.attribs) {
|
|
2558
|
-
props = (0, attributes_to_props_1.default)(element.attribs, element.name);
|
|
2559
|
-
}
|
|
2560
|
-
let children;
|
|
2561
|
-
switch (node.type) {
|
|
2562
|
-
case 'script':
|
|
2563
|
-
case 'style':
|
|
2564
|
-
// prevent text in <script> or <style> from being escaped
|
|
2565
|
-
// https://react.dev/reference/react-dom/components/common#dangerously-setting-the-inner-html
|
|
2566
|
-
if (node.children[0]) {
|
|
2567
|
-
props.dangerouslySetInnerHTML = {
|
|
2568
|
-
__html: node.children[0].data,
|
|
2569
|
-
};
|
|
2570
|
-
}
|
|
2571
|
-
break;
|
|
2572
|
-
case 'tag':
|
|
2573
|
-
// setting textarea value in children is an antipattern in React
|
|
2574
|
-
// https://react.dev/reference/react-dom/components/textarea#caveats
|
|
2575
|
-
if (node.name === 'textarea' && node.children[0]) {
|
|
2576
|
-
props.defaultValue = node.children[0].data;
|
|
2577
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
2578
|
-
}
|
|
2579
|
-
else if ((_e = node.children) === null || _e === void 0 ? void 0 : _e.length) {
|
|
2580
|
-
// continue recursion of creating React elements (if applicable)
|
|
2581
|
-
children = domToReact$1(node.children, options);
|
|
2582
|
-
}
|
|
2583
|
-
break;
|
|
2584
|
-
// skip all other cases (e.g., comment)
|
|
2585
|
-
default:
|
|
2586
|
-
continue;
|
|
2587
|
-
}
|
|
2588
|
-
// set "key" prop for sibling elements
|
|
2589
|
-
// https://react.dev/learn/rendering-lists#rules-of-keys
|
|
2590
|
-
if (nodesLength > 1) {
|
|
2591
|
-
props.key = index;
|
|
2592
|
-
}
|
|
2593
|
-
reactElements.push(transform(createElement(node.name, props, children), node, index));
|
|
2594
|
-
}
|
|
2595
|
-
return reactElements.length === 1 ? reactElements[0] : reactElements;
|
|
2596
|
-
}
|
|
2597
|
-
/**
|
|
2598
|
-
* Determines whether DOM element attributes should be transformed to props.
|
|
2599
|
-
* Web Components should not have their attributes transformed except for `style`.
|
|
2600
|
-
*
|
|
2601
|
-
* @param node - Element node.
|
|
2602
|
-
* @returns - Whether the node attributes should be converted to props.
|
|
2603
|
-
*/
|
|
2604
|
-
function skipAttributesToProps(node) {
|
|
2605
|
-
return (utilities_1.PRESERVE_CUSTOM_ATTRIBUTES &&
|
|
2606
|
-
node.type === 'tag' &&
|
|
2607
|
-
(0, utilities_1.isCustomComponent)(node.name, node.attribs));
|
|
2608
|
-
}
|
|
2609
|
-
|
|
2610
|
-
return domToReact;
|
|
2611
|
-
}
|
|
2612
|
-
|
|
2613
2481
|
/** Types of elements found in htmlparser2's DOM */
|
|
2614
2482
|
var ElementType;
|
|
2615
2483
|
(function (ElementType) {
|
|
@@ -3207,6 +3075,150 @@
|
|
|
3207
3075
|
|
|
3208
3076
|
var require$$3 = /*@__PURE__*/getAugmentedNamespace(dist);
|
|
3209
3077
|
|
|
3078
|
+
var hasRequiredDomToReact;
|
|
3079
|
+
|
|
3080
|
+
function requireDomToReact () {
|
|
3081
|
+
if (hasRequiredDomToReact) return domToReact;
|
|
3082
|
+
hasRequiredDomToReact = 1;
|
|
3083
|
+
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
|
|
3084
|
+
var __importDefault = (domToReact && domToReact.__importDefault) || function (mod) {
|
|
3085
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
3086
|
+
};
|
|
3087
|
+
Object.defineProperty(domToReact, "__esModule", { value: true });
|
|
3088
|
+
domToReact.default = domToReact$1;
|
|
3089
|
+
const domhandler_1 = require$$3;
|
|
3090
|
+
const react_1 = require$$0;
|
|
3091
|
+
const attributes_to_props_1 = __importDefault(requireAttributesToProps());
|
|
3092
|
+
const utilities_1 = requireUtilities();
|
|
3093
|
+
const React = {
|
|
3094
|
+
cloneElement: react_1.cloneElement,
|
|
3095
|
+
createElement: react_1.createElement,
|
|
3096
|
+
isValidElement: react_1.isValidElement,
|
|
3097
|
+
};
|
|
3098
|
+
/**
|
|
3099
|
+
* Converts DOM nodes to JSX element(s).
|
|
3100
|
+
*
|
|
3101
|
+
* @param nodes - DOM nodes.
|
|
3102
|
+
* @param options - Options.
|
|
3103
|
+
* @returns - String or JSX element(s).
|
|
3104
|
+
*/
|
|
3105
|
+
function domToReact$1(nodes, options = {}) {
|
|
3106
|
+
var _a, _b, _c, _d, _e;
|
|
3107
|
+
const reactElements = [];
|
|
3108
|
+
const hasReplace = typeof options.replace === 'function';
|
|
3109
|
+
const transform = (_a = options.transform) !== null && _a !== void 0 ? _a : utilities_1.returnFirstArg;
|
|
3110
|
+
const { cloneElement, createElement, isValidElement } = (_b = options.library) !== null && _b !== void 0 ? _b : React;
|
|
3111
|
+
const nodesLength = nodes.length;
|
|
3112
|
+
normalizeDOMNodes(nodes);
|
|
3113
|
+
for (let index = 0; index < nodesLength; index++) {
|
|
3114
|
+
const node = nodes[index];
|
|
3115
|
+
// replace with custom React element (if present)
|
|
3116
|
+
if (hasReplace) {
|
|
3117
|
+
let replaceElement = (_c = options.replace) === null || _c === void 0 ? void 0 : _c.call(options, node, index);
|
|
3118
|
+
if (isValidElement(replaceElement)) {
|
|
3119
|
+
// set "key" prop for sibling elements
|
|
3120
|
+
// https://react.dev/learn/rendering-lists#rules-of-keys
|
|
3121
|
+
if (nodesLength > 1) {
|
|
3122
|
+
replaceElement = cloneElement(replaceElement, {
|
|
3123
|
+
key: (_d = replaceElement.key) !== null && _d !== void 0 ? _d : index,
|
|
3124
|
+
});
|
|
3125
|
+
}
|
|
3126
|
+
reactElements.push(transform(replaceElement, node, index));
|
|
3127
|
+
continue;
|
|
3128
|
+
}
|
|
3129
|
+
}
|
|
3130
|
+
if (node.type === 'text') {
|
|
3131
|
+
const isWhitespace = !node.data.trim().length;
|
|
3132
|
+
// We have a whitespace node that can't be nested in its parent
|
|
3133
|
+
// so skip it
|
|
3134
|
+
if (isWhitespace &&
|
|
3135
|
+
node.parent &&
|
|
3136
|
+
!(0, utilities_1.canTextBeChildOfNode)(node.parent)) {
|
|
3137
|
+
continue;
|
|
3138
|
+
}
|
|
3139
|
+
// Trim is enabled and we have a whitespace node
|
|
3140
|
+
// so skip it
|
|
3141
|
+
if (options.trim && isWhitespace) {
|
|
3142
|
+
continue;
|
|
3143
|
+
}
|
|
3144
|
+
// We have a text node that's not whitespace and it can be nested
|
|
3145
|
+
// in its parent so add it to the results
|
|
3146
|
+
reactElements.push(transform(node.data, node, index));
|
|
3147
|
+
continue;
|
|
3148
|
+
}
|
|
3149
|
+
const element = node;
|
|
3150
|
+
let props = {};
|
|
3151
|
+
if (skipAttributesToProps(element)) {
|
|
3152
|
+
(0, utilities_1.setStyleProp)(element.attribs.style, element.attribs);
|
|
3153
|
+
props = element.attribs;
|
|
3154
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
3155
|
+
}
|
|
3156
|
+
else if (element.attribs) {
|
|
3157
|
+
props = (0, attributes_to_props_1.default)(element.attribs, element.name);
|
|
3158
|
+
}
|
|
3159
|
+
let children;
|
|
3160
|
+
switch (node.type) {
|
|
3161
|
+
case 'script':
|
|
3162
|
+
case 'style':
|
|
3163
|
+
// prevent text in <script> or <style> from being escaped
|
|
3164
|
+
// https://react.dev/reference/react-dom/components/common#dangerously-setting-the-inner-html
|
|
3165
|
+
if (node.children[0]) {
|
|
3166
|
+
props.dangerouslySetInnerHTML = {
|
|
3167
|
+
__html: node.children[0].data,
|
|
3168
|
+
};
|
|
3169
|
+
}
|
|
3170
|
+
break;
|
|
3171
|
+
case 'tag':
|
|
3172
|
+
// setting textarea value in children is an antipattern in React
|
|
3173
|
+
// https://react.dev/reference/react-dom/components/textarea#caveats
|
|
3174
|
+
if (node.name === 'textarea' && node.children[0]) {
|
|
3175
|
+
props.defaultValue = node.children[0].data;
|
|
3176
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
3177
|
+
}
|
|
3178
|
+
else if ((_e = node.children) === null || _e === void 0 ? void 0 : _e.length) {
|
|
3179
|
+
// continue recursion of creating React elements (if applicable)
|
|
3180
|
+
children = domToReact$1(node.children, options);
|
|
3181
|
+
}
|
|
3182
|
+
break;
|
|
3183
|
+
// skip all other cases (e.g., comment)
|
|
3184
|
+
default:
|
|
3185
|
+
continue;
|
|
3186
|
+
}
|
|
3187
|
+
// set "key" prop for sibling elements
|
|
3188
|
+
// https://react.dev/learn/rendering-lists#rules-of-keys
|
|
3189
|
+
if (nodesLength > 1) {
|
|
3190
|
+
props.key = index;
|
|
3191
|
+
}
|
|
3192
|
+
reactElements.push(transform(createElement(node.name, props, children), node, index));
|
|
3193
|
+
}
|
|
3194
|
+
return reactElements.length === 1 ? reactElements[0] : reactElements;
|
|
3195
|
+
}
|
|
3196
|
+
function normalizeDOMNodes(nodes) {
|
|
3197
|
+
for (const node of nodes) {
|
|
3198
|
+
if (node.type === 'tag' ||
|
|
3199
|
+
node.type === 'script' ||
|
|
3200
|
+
node.type === 'style') {
|
|
3201
|
+
Object.setPrototypeOf(node, domhandler_1.Element.prototype);
|
|
3202
|
+
normalizeDOMNodes(node.children);
|
|
3203
|
+
}
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
/**
|
|
3207
|
+
* Determines whether DOM element attributes should be transformed to props.
|
|
3208
|
+
* Web Components should not have their attributes transformed except for `style`.
|
|
3209
|
+
*
|
|
3210
|
+
* @param node - Element node.
|
|
3211
|
+
* @returns - Whether the node attributes should be converted to props.
|
|
3212
|
+
*/
|
|
3213
|
+
function skipAttributesToProps(node) {
|
|
3214
|
+
return (utilities_1.PRESERVE_CUSTOM_ATTRIBUTES &&
|
|
3215
|
+
node.type === 'tag' &&
|
|
3216
|
+
(0, utilities_1.isCustomComponent)(node.name, node.attribs));
|
|
3217
|
+
}
|
|
3218
|
+
|
|
3219
|
+
return domToReact;
|
|
3220
|
+
}
|
|
3221
|
+
|
|
3210
3222
|
var hasRequiredLib;
|
|
3211
3223
|
|
|
3212
3224
|
function requireLib () {
|