html-react-parser 4.1.0 → 4.2.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/README.md CHANGED
@@ -161,7 +161,9 @@ parse('<br>', {
161
161
  });
162
162
  ```
163
163
 
164
- Console output:
164
+ <details>
165
+ <summary>Console output</summary>
166
+ <p>
165
167
 
166
168
  ```js
167
169
  Element {
@@ -177,6 +179,9 @@ Element {
177
179
  }
178
180
  ```
179
181
 
182
+ </p>
183
+ </details>
184
+
180
185
  The element is replaced if a **valid** React element is returned:
181
186
 
182
187
  ```jsx
@@ -245,7 +250,9 @@ const options = {
245
250
  parse(html, options);
246
251
  ```
247
252
 
248
- HTML output:
253
+ <details>
254
+ <summary>HTML output</summary>
255
+ <p>
249
256
 
250
257
  <!-- prettier-ignore-start -->
251
258
 
@@ -259,6 +266,9 @@ HTML output:
259
266
 
260
267
  <!-- prettier-ignore-end -->
261
268
 
269
+ </p>
270
+ </details>
271
+
262
272
  #### replace element attributes
263
273
 
264
274
  Convert DOM attributes to React props with `attributesToProps`:
@@ -282,12 +292,17 @@ const options = {
282
292
  parse(html, options);
283
293
  ```
284
294
 
285
- HTML output:
295
+ <details>
296
+ <summary>HTML output</summary>
297
+ <p>
286
298
 
287
299
  ```html
288
300
  <div class="prettify" style="background:#fff;text-align:center"></div>
289
301
  ```
290
302
 
303
+ </p>
304
+ </details>
305
+
291
306
  #### replace and remove element
292
307
 
293
308
  [Exclude](https://replit.com/@remarkablemark/html-react-parser-56) an element from rendering by replacing it with `<React.Fragment>`:
@@ -298,12 +313,17 @@ parse('<p><br id="remove"></p>', {
298
313
  });
299
314
  ```
300
315
 
301
- HTML output:
316
+ <details>
317
+ <summary>HTML output</summary>
318
+ <p>
302
319
 
303
320
  ```html
304
321
  <p></p>
305
322
  ```
306
323
 
324
+ </p>
325
+ </details>
326
+
307
327
  ### transform
308
328
 
309
329
  The `transform` option allows you to transform each element individually after it's parsed.
@@ -522,7 +542,7 @@ If you see the TypeScript error:
522
542
  node_modules/htmlparser2/lib/index.d.ts:2:23 - error TS1005: ',' expected.
523
543
 
524
544
  2 export { Parser, type ParserOptions };
525
- ~~~~~~~~~~~~~
545
+ ~~~~~~~~~~~~~
526
546
  ```
527
547
 
528
548
  Then upgrade to the latest version of [typescript](https://www.npmjs.com/package/typescript). See [#748](https://github.com/remarkablemark/html-react-parser/issues/748).
@@ -2291,14 +2291,12 @@
2291
2291
  throw new TypeError('First argument must be an object');
2292
2292
  }
2293
2293
 
2294
- var key;
2295
- var value;
2296
2294
  var isOverridePresent = typeof override === 'function';
2297
2295
  var overrides = {};
2298
2296
  var result = {};
2299
2297
 
2300
- for (key in obj) {
2301
- value = obj[key];
2298
+ for (var key in obj) {
2299
+ var value = obj[key];
2302
2300
 
2303
2301
  if (isOverridePresent) {
2304
2302
  overrides = override(key, value);
@@ -2325,31 +2323,34 @@
2325
2323
  * @param {object} props - The props being passed to the element.
2326
2324
  * @returns - Whether tag is custom component.
2327
2325
  */
2326
+
2327
+ var RESERVED_SVG_MATHML_ELEMENTS = new Set([
2328
+ 'annotation-xml',
2329
+ 'color-profile',
2330
+ 'font-face',
2331
+ 'font-face-src',
2332
+ 'font-face-uri',
2333
+ 'font-face-format',
2334
+ 'font-face-name',
2335
+ 'missing-glyph'
2336
+ ]);
2337
+
2328
2338
  function isCustomComponent(tagName, props) {
2329
2339
  if (tagName.indexOf('-') === -1) {
2330
2340
  return props && typeof props.is === 'string';
2331
2341
  }
2332
-
2333
- switch (tagName) {
2334
- // These are reserved SVG and MathML elements.
2335
- // We don't mind this whitelist too much because we expect it to never grow.
2336
- // The alternative is to track the namespace in a few places which is convoluted.
2337
- // https://w3c.github.io/webcomponents/spec/custom/#custom-elements-core-concepts
2338
- case 'annotation-xml':
2339
- case 'color-profile':
2340
- case 'font-face':
2341
- case 'font-face-src':
2342
- case 'font-face-uri':
2343
- case 'font-face-format':
2344
- case 'font-face-name':
2345
- case 'missing-glyph':
2346
- return false;
2347
- default:
2348
- return true;
2342
+ // These are reserved SVG and MathML elements.
2343
+ // We don't mind this whitelist too much because we expect it to never grow.
2344
+ // The alternative is to track the namespace in a few places which is convoluted.
2345
+ // https://w3c.github.io/webcomponents/spec/custom/#custom-elements-core-concepts
2346
+ if (RESERVED_SVG_MATHML_ELEMENTS.has(tagName)) {
2347
+ return false;
2349
2348
  }
2349
+ return true;
2350
2350
  }
2351
2351
 
2352
- var styleToJSOptions = { reactCompat: true };
2352
+ // styleToJSOptions
2353
+ var STYLE_TO_JS_OPTIONS = { reactCompat: true };
2353
2354
 
2354
2355
  /**
2355
2356
  * Sets style prop.
@@ -2362,7 +2363,7 @@
2362
2363
  return;
2363
2364
  }
2364
2365
  try {
2365
- props.style = styleToJS(style, styleToJSOptions);
2366
+ props.style = styleToJS(style, STYLE_TO_JS_OPTIONS);
2366
2367
  } catch (err) {
2367
2368
  props.style = {};
2368
2369
  }
@@ -2376,7 +2377,7 @@
2376
2377
 
2377
2378
  // Taken from
2378
2379
  // https://github.com/facebook/react/blob/cae635054e17a6f107a39d328649137b83f25972/packages/react-dom/src/client/validateDOMNesting.js#L213
2379
- var elementsWithNoTextChildren = new Set([
2380
+ var ELEMENTS_WITH_NO_TEXT_CHILDREN = new Set([
2380
2381
  'tr',
2381
2382
  'tbody',
2382
2383
  'thead',
@@ -2395,7 +2396,7 @@
2395
2396
  * @returns - Whether node can contain text nodes.
2396
2397
  */
2397
2398
  function canTextBeChildOfNode$1(node) {
2398
- return !elementsWithNoTextChildren.has(node.name);
2399
+ return !ELEMENTS_WITH_NO_TEXT_CHILDREN.has(node.name);
2399
2400
  }
2400
2401
 
2401
2402
  function returnFirstArg(arg) {
@@ -2404,11 +2405,11 @@
2404
2405
 
2405
2406
  var utilities$2 = {
2406
2407
  PRESERVE_CUSTOM_ATTRIBUTES: PRESERVE_CUSTOM_ATTRIBUTES,
2408
+ ELEMENTS_WITH_NO_TEXT_CHILDREN: ELEMENTS_WITH_NO_TEXT_CHILDREN,
2407
2409
  invertObject: invertObject,
2408
2410
  isCustomComponent: isCustomComponent,
2409
2411
  setStyleProp: setStyleProp$1,
2410
2412
  canTextBeChildOfNode: canTextBeChildOfNode$1,
2411
- elementsWithNoTextChildren: elementsWithNoTextChildren,
2412
2413
  returnFirstArg: returnFirstArg
2413
2414
  };
2414
2415