html-react-parser 6.1.0 → 6.1.2
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 +233 -234
- 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 +21 -20
- package/src/dom-to-react.ts +16 -0
|
@@ -1871,16 +1871,16 @@
|
|
|
1871
1871
|
return lib;
|
|
1872
1872
|
}
|
|
1873
1873
|
|
|
1874
|
-
var utilities
|
|
1874
|
+
var utilities = {};
|
|
1875
1875
|
|
|
1876
|
-
var cjs$
|
|
1876
|
+
var cjs$1 = {};
|
|
1877
1877
|
|
|
1878
|
-
var cjs
|
|
1879
|
-
var hasRequiredCjs$
|
|
1878
|
+
var cjs;
|
|
1879
|
+
var hasRequiredCjs$1;
|
|
1880
1880
|
|
|
1881
|
-
function requireCjs$
|
|
1882
|
-
if (hasRequiredCjs$
|
|
1883
|
-
hasRequiredCjs$
|
|
1881
|
+
function requireCjs$1 () {
|
|
1882
|
+
if (hasRequiredCjs$1) return cjs;
|
|
1883
|
+
hasRequiredCjs$1 = 1;
|
|
1884
1884
|
|
|
1885
1885
|
// http://www.w3.org/TR/CSS21/grammar.html
|
|
1886
1886
|
// https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027
|
|
@@ -2140,22 +2140,22 @@
|
|
|
2140
2140
|
return str ? str.replace(TRIM_REGEX, EMPTY_STRING) : EMPTY_STRING;
|
|
2141
2141
|
}
|
|
2142
2142
|
|
|
2143
|
-
cjs
|
|
2143
|
+
cjs = index;
|
|
2144
2144
|
|
|
2145
|
-
return cjs
|
|
2145
|
+
return cjs;
|
|
2146
2146
|
}
|
|
2147
2147
|
|
|
2148
|
-
var hasRequiredCjs
|
|
2148
|
+
var hasRequiredCjs;
|
|
2149
2149
|
|
|
2150
|
-
function requireCjs
|
|
2151
|
-
if (hasRequiredCjs
|
|
2152
|
-
hasRequiredCjs
|
|
2153
|
-
var __importDefault = (cjs$
|
|
2150
|
+
function requireCjs () {
|
|
2151
|
+
if (hasRequiredCjs) return cjs$1;
|
|
2152
|
+
hasRequiredCjs = 1;
|
|
2153
|
+
var __importDefault = (cjs$1 && cjs$1.__importDefault) || function (mod) {
|
|
2154
2154
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
2155
2155
|
};
|
|
2156
|
-
Object.defineProperty(cjs$
|
|
2157
|
-
cjs$
|
|
2158
|
-
const inline_style_parser_1 = __importDefault(requireCjs$
|
|
2156
|
+
Object.defineProperty(cjs$1, "__esModule", { value: true });
|
|
2157
|
+
cjs$1.default = StyleToObject;
|
|
2158
|
+
const inline_style_parser_1 = __importDefault(requireCjs$1());
|
|
2159
2159
|
/**
|
|
2160
2160
|
* Parses inline style to object.
|
|
2161
2161
|
*
|
|
@@ -2193,105 +2193,92 @@
|
|
|
2193
2193
|
return styleObject;
|
|
2194
2194
|
}
|
|
2195
2195
|
|
|
2196
|
-
return cjs$
|
|
2196
|
+
return cjs$1;
|
|
2197
2197
|
}
|
|
2198
2198
|
|
|
2199
|
-
var
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
var
|
|
2209
|
-
var
|
|
2210
|
-
var
|
|
2211
|
-
var
|
|
2212
|
-
var
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2199
|
+
var dist$1;
|
|
2200
|
+
var hasRequiredDist;
|
|
2201
|
+
|
|
2202
|
+
function requireDist () {
|
|
2203
|
+
if (hasRequiredDist) return dist$1;
|
|
2204
|
+
hasRequiredDist = 1;
|
|
2205
|
+
//#region \0rolldown/runtime.js
|
|
2206
|
+
var __create = Object.create;
|
|
2207
|
+
var __defProp = Object.defineProperty;
|
|
2208
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
2209
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
2210
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
2211
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
2212
|
+
var __copyProps = (to, from, except, desc) => {
|
|
2213
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
2214
|
+
key = keys[i];
|
|
2215
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
2216
|
+
get: ((k) => from[k]).bind(null, key),
|
|
2217
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
2218
|
+
});
|
|
2219
|
+
}
|
|
2220
|
+
return to;
|
|
2220
2221
|
};
|
|
2222
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(!mod || !mod.__esModule ? __defProp(target, "default", {
|
|
2223
|
+
value: mod,
|
|
2224
|
+
enumerable: true
|
|
2225
|
+
}) : target, mod));
|
|
2226
|
+
//#endregion
|
|
2227
|
+
let style_to_object = requireCjs();
|
|
2228
|
+
style_to_object = __toESM(style_to_object);
|
|
2229
|
+
//#region src/utilities.ts
|
|
2230
|
+
const CUSTOM_PROPERTY_REGEX = /^--[a-zA-Z0-9_-]+$/;
|
|
2231
|
+
const HYPHEN_REGEX = /-([a-z])/g;
|
|
2232
|
+
const NO_HYPHEN_REGEX = /^[^-]+$/;
|
|
2233
|
+
const VENDOR_PREFIX_REGEX = /^-(webkit|moz|ms|o|khtml)-/;
|
|
2234
|
+
const MS_VENDOR_PREFIX_REGEX = /^-(ms)-/;
|
|
2221
2235
|
/**
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
return character.toUpperCase();
|
|
2226
|
-
};
|
|
2236
|
+
* Checks whether to skip camelCase.
|
|
2237
|
+
*/
|
|
2238
|
+
const skipCamelCase = (property) => !property || NO_HYPHEN_REGEX.test(property) || CUSTOM_PROPERTY_REGEX.test(property);
|
|
2227
2239
|
/**
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2240
|
+
* Replacer that capitalizes first character.
|
|
2241
|
+
*/
|
|
2242
|
+
const capitalize = (match, character) => character.toUpperCase();
|
|
2231
2243
|
/**
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
else {
|
|
2245
|
-
// for non-React, remove first hyphen so vendor prefix is not capitalized
|
|
2246
|
-
property = property.replace(VENDOR_PREFIX_REGEX, trimHyphen);
|
|
2247
|
-
}
|
|
2248
|
-
return property.replace(HYPHEN_REGEX, capitalize);
|
|
2249
|
-
};
|
|
2250
|
-
utilities.camelCase = camelCase;
|
|
2251
|
-
|
|
2252
|
-
return utilities;
|
|
2253
|
-
}
|
|
2254
|
-
|
|
2255
|
-
var cjs;
|
|
2256
|
-
var hasRequiredCjs;
|
|
2257
|
-
|
|
2258
|
-
function requireCjs () {
|
|
2259
|
-
if (hasRequiredCjs) return cjs;
|
|
2260
|
-
hasRequiredCjs = 1;
|
|
2261
|
-
var __importDefault = (cjs && cjs.__importDefault) || function (mod) {
|
|
2262
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
2244
|
+
* Replacer that removes beginning hyphen of vendor prefix property.
|
|
2245
|
+
*/
|
|
2246
|
+
const trimHyphen = (match, prefix) => `${prefix}-`;
|
|
2247
|
+
/**
|
|
2248
|
+
* CamelCases a CSS property.
|
|
2249
|
+
*/
|
|
2250
|
+
const camelCase = (property, options = {}) => {
|
|
2251
|
+
if (skipCamelCase(property)) return property;
|
|
2252
|
+
property = property.toLowerCase();
|
|
2253
|
+
if (options.reactCompat) property = property.replace(MS_VENDOR_PREFIX_REGEX, trimHyphen);
|
|
2254
|
+
else property = property.replace(VENDOR_PREFIX_REGEX, trimHyphen);
|
|
2255
|
+
return property.replace(HYPHEN_REGEX, capitalize);
|
|
2263
2256
|
};
|
|
2264
|
-
|
|
2265
|
-
|
|
2257
|
+
//#endregion
|
|
2258
|
+
//#region src/index.ts
|
|
2266
2259
|
/**
|
|
2267
|
-
|
|
2268
|
-
|
|
2260
|
+
* Parses CSS inline style to JavaScript object (camelCased).
|
|
2261
|
+
*/
|
|
2269
2262
|
function StyleToJS(style, options) {
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
if (property && value) {
|
|
2277
|
-
output[(0, utilities_1.camelCase)(property, options)] = value;
|
|
2278
|
-
}
|
|
2279
|
-
});
|
|
2280
|
-
return output;
|
|
2263
|
+
const output = {};
|
|
2264
|
+
if (!style || typeof style !== "string") return output;
|
|
2265
|
+
(0, style_to_object.default)(style, (property, value) => {
|
|
2266
|
+
if (property && value) output[camelCase(property, options)] = value;
|
|
2267
|
+
});
|
|
2268
|
+
return output;
|
|
2281
2269
|
}
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
return cjs;
|
|
2270
|
+
//#endregion
|
|
2271
|
+
dist$1 = StyleToJS;
|
|
2272
|
+
return dist$1;
|
|
2286
2273
|
}
|
|
2287
2274
|
|
|
2288
2275
|
var hasRequiredUtilities;
|
|
2289
2276
|
|
|
2290
2277
|
function requireUtilities () {
|
|
2291
|
-
if (hasRequiredUtilities) return utilities
|
|
2278
|
+
if (hasRequiredUtilities) return utilities;
|
|
2292
2279
|
hasRequiredUtilities = 1;
|
|
2293
2280
|
(function (exports) {
|
|
2294
|
-
var __importDefault = (utilities
|
|
2281
|
+
var __importDefault = (utilities && utilities.__importDefault) || function (mod) {
|
|
2295
2282
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
2296
2283
|
};
|
|
2297
2284
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -2299,7 +2286,7 @@
|
|
|
2299
2286
|
exports.isCustomComponent = isCustomComponent;
|
|
2300
2287
|
exports.setStyleProp = setStyleProp;
|
|
2301
2288
|
const react_1 = require$$0;
|
|
2302
|
-
const style_to_js_1 = __importDefault(
|
|
2289
|
+
const style_to_js_1 = __importDefault(requireDist());
|
|
2303
2290
|
const RESERVED_SVG_MATHML_ELEMENTS = new Set([
|
|
2304
2291
|
'annotation-xml',
|
|
2305
2292
|
'color-profile',
|
|
@@ -2392,8 +2379,8 @@
|
|
|
2392
2379
|
const returnFirstArg = (arg) => arg;
|
|
2393
2380
|
exports.returnFirstArg = returnFirstArg;
|
|
2394
2381
|
|
|
2395
|
-
} (utilities
|
|
2396
|
-
return utilities
|
|
2382
|
+
} (utilities));
|
|
2383
|
+
return utilities;
|
|
2397
2384
|
}
|
|
2398
2385
|
|
|
2399
2386
|
var hasRequiredAttributesToProps;
|
|
@@ -2478,138 +2465,6 @@
|
|
|
2478
2465
|
|
|
2479
2466
|
var domToReact = {};
|
|
2480
2467
|
|
|
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
2468
|
/** Types of elements found in htmlparser2's DOM */
|
|
2614
2469
|
var ElementType;
|
|
2615
2470
|
(function (ElementType) {
|
|
@@ -3207,6 +3062,150 @@
|
|
|
3207
3062
|
|
|
3208
3063
|
var require$$3 = /*@__PURE__*/getAugmentedNamespace(dist);
|
|
3209
3064
|
|
|
3065
|
+
var hasRequiredDomToReact;
|
|
3066
|
+
|
|
3067
|
+
function requireDomToReact () {
|
|
3068
|
+
if (hasRequiredDomToReact) return domToReact;
|
|
3069
|
+
hasRequiredDomToReact = 1;
|
|
3070
|
+
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
|
|
3071
|
+
var __importDefault = (domToReact && domToReact.__importDefault) || function (mod) {
|
|
3072
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
3073
|
+
};
|
|
3074
|
+
Object.defineProperty(domToReact, "__esModule", { value: true });
|
|
3075
|
+
domToReact.default = domToReact$1;
|
|
3076
|
+
const domhandler_1 = require$$3;
|
|
3077
|
+
const react_1 = require$$0;
|
|
3078
|
+
const attributes_to_props_1 = __importDefault(requireAttributesToProps());
|
|
3079
|
+
const utilities_1 = requireUtilities();
|
|
3080
|
+
const React = {
|
|
3081
|
+
cloneElement: react_1.cloneElement,
|
|
3082
|
+
createElement: react_1.createElement,
|
|
3083
|
+
isValidElement: react_1.isValidElement,
|
|
3084
|
+
};
|
|
3085
|
+
/**
|
|
3086
|
+
* Converts DOM nodes to JSX element(s).
|
|
3087
|
+
*
|
|
3088
|
+
* @param nodes - DOM nodes.
|
|
3089
|
+
* @param options - Options.
|
|
3090
|
+
* @returns - String or JSX element(s).
|
|
3091
|
+
*/
|
|
3092
|
+
function domToReact$1(nodes, options = {}) {
|
|
3093
|
+
var _a, _b, _c, _d, _e;
|
|
3094
|
+
const reactElements = [];
|
|
3095
|
+
const hasReplace = typeof options.replace === 'function';
|
|
3096
|
+
const transform = (_a = options.transform) !== null && _a !== void 0 ? _a : utilities_1.returnFirstArg;
|
|
3097
|
+
const { cloneElement, createElement, isValidElement } = (_b = options.library) !== null && _b !== void 0 ? _b : React;
|
|
3098
|
+
const nodesLength = nodes.length;
|
|
3099
|
+
normalizeDOMNodes(nodes);
|
|
3100
|
+
for (let index = 0; index < nodesLength; index++) {
|
|
3101
|
+
const node = nodes[index];
|
|
3102
|
+
// replace with custom React element (if present)
|
|
3103
|
+
if (hasReplace) {
|
|
3104
|
+
let replaceElement = (_c = options.replace) === null || _c === void 0 ? void 0 : _c.call(options, node, index);
|
|
3105
|
+
if (isValidElement(replaceElement)) {
|
|
3106
|
+
// set "key" prop for sibling elements
|
|
3107
|
+
// https://react.dev/learn/rendering-lists#rules-of-keys
|
|
3108
|
+
if (nodesLength > 1) {
|
|
3109
|
+
replaceElement = cloneElement(replaceElement, {
|
|
3110
|
+
key: (_d = replaceElement.key) !== null && _d !== void 0 ? _d : index,
|
|
3111
|
+
});
|
|
3112
|
+
}
|
|
3113
|
+
reactElements.push(transform(replaceElement, node, index));
|
|
3114
|
+
continue;
|
|
3115
|
+
}
|
|
3116
|
+
}
|
|
3117
|
+
if (node.type === 'text') {
|
|
3118
|
+
const isWhitespace = !node.data.trim().length;
|
|
3119
|
+
// We have a whitespace node that can't be nested in its parent
|
|
3120
|
+
// so skip it
|
|
3121
|
+
if (isWhitespace &&
|
|
3122
|
+
node.parent &&
|
|
3123
|
+
!(0, utilities_1.canTextBeChildOfNode)(node.parent)) {
|
|
3124
|
+
continue;
|
|
3125
|
+
}
|
|
3126
|
+
// Trim is enabled and we have a whitespace node
|
|
3127
|
+
// so skip it
|
|
3128
|
+
if (options.trim && isWhitespace) {
|
|
3129
|
+
continue;
|
|
3130
|
+
}
|
|
3131
|
+
// We have a text node that's not whitespace and it can be nested
|
|
3132
|
+
// in its parent so add it to the results
|
|
3133
|
+
reactElements.push(transform(node.data, node, index));
|
|
3134
|
+
continue;
|
|
3135
|
+
}
|
|
3136
|
+
const element = node;
|
|
3137
|
+
let props = {};
|
|
3138
|
+
if (skipAttributesToProps(element)) {
|
|
3139
|
+
(0, utilities_1.setStyleProp)(element.attribs.style, element.attribs);
|
|
3140
|
+
props = element.attribs;
|
|
3141
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
3142
|
+
}
|
|
3143
|
+
else if (element.attribs) {
|
|
3144
|
+
props = (0, attributes_to_props_1.default)(element.attribs, element.name);
|
|
3145
|
+
}
|
|
3146
|
+
let children;
|
|
3147
|
+
switch (node.type) {
|
|
3148
|
+
case 'script':
|
|
3149
|
+
case 'style':
|
|
3150
|
+
// prevent text in <script> or <style> from being escaped
|
|
3151
|
+
// https://react.dev/reference/react-dom/components/common#dangerously-setting-the-inner-html
|
|
3152
|
+
if (node.children[0]) {
|
|
3153
|
+
props.dangerouslySetInnerHTML = {
|
|
3154
|
+
__html: node.children[0].data,
|
|
3155
|
+
};
|
|
3156
|
+
}
|
|
3157
|
+
break;
|
|
3158
|
+
case 'tag':
|
|
3159
|
+
// setting textarea value in children is an antipattern in React
|
|
3160
|
+
// https://react.dev/reference/react-dom/components/textarea#caveats
|
|
3161
|
+
if (node.name === 'textarea' && node.children[0]) {
|
|
3162
|
+
props.defaultValue = node.children[0].data;
|
|
3163
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
3164
|
+
}
|
|
3165
|
+
else if ((_e = node.children) === null || _e === void 0 ? void 0 : _e.length) {
|
|
3166
|
+
// continue recursion of creating React elements (if applicable)
|
|
3167
|
+
children = domToReact$1(node.children, options);
|
|
3168
|
+
}
|
|
3169
|
+
break;
|
|
3170
|
+
// skip all other cases (e.g., comment)
|
|
3171
|
+
default:
|
|
3172
|
+
continue;
|
|
3173
|
+
}
|
|
3174
|
+
// set "key" prop for sibling elements
|
|
3175
|
+
// https://react.dev/learn/rendering-lists#rules-of-keys
|
|
3176
|
+
if (nodesLength > 1) {
|
|
3177
|
+
props.key = index;
|
|
3178
|
+
}
|
|
3179
|
+
reactElements.push(transform(createElement(node.name, props, children), node, index));
|
|
3180
|
+
}
|
|
3181
|
+
return reactElements.length === 1 ? reactElements[0] : reactElements;
|
|
3182
|
+
}
|
|
3183
|
+
function normalizeDOMNodes(nodes) {
|
|
3184
|
+
for (const node of nodes) {
|
|
3185
|
+
if (node.type === 'tag' ||
|
|
3186
|
+
node.type === 'script' ||
|
|
3187
|
+
node.type === 'style') {
|
|
3188
|
+
Object.setPrototypeOf(node, domhandler_1.Element.prototype);
|
|
3189
|
+
normalizeDOMNodes(node.children);
|
|
3190
|
+
}
|
|
3191
|
+
}
|
|
3192
|
+
}
|
|
3193
|
+
/**
|
|
3194
|
+
* Determines whether DOM element attributes should be transformed to props.
|
|
3195
|
+
* Web Components should not have their attributes transformed except for `style`.
|
|
3196
|
+
*
|
|
3197
|
+
* @param node - Element node.
|
|
3198
|
+
* @returns - Whether the node attributes should be converted to props.
|
|
3199
|
+
*/
|
|
3200
|
+
function skipAttributesToProps(node) {
|
|
3201
|
+
return (utilities_1.PRESERVE_CUSTOM_ATTRIBUTES &&
|
|
3202
|
+
node.type === 'tag' &&
|
|
3203
|
+
(0, utilities_1.isCustomComponent)(node.name, node.attribs));
|
|
3204
|
+
}
|
|
3205
|
+
|
|
3206
|
+
return domToReact;
|
|
3207
|
+
}
|
|
3208
|
+
|
|
3210
3209
|
var hasRequiredLib;
|
|
3211
3210
|
|
|
3212
3211
|
function requireLib () {
|