Skip to content

Commit dfeac41

Browse files
committed
feat: upd onError logic, upd readme
1 parent 61558fb commit dfeac41

5 files changed

Lines changed: 55 additions & 34 deletions

File tree

‎README.md‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ parse("Lorem <div>ipsum</div>");
3333

3434
## API
3535

36-
### parse `(html: string, options = {}) => React.Fragment`
36+
### parse `(html: string, options = {}) => React.Fragment | null`
3737

3838
#### Arguments
3939

@@ -44,10 +44,11 @@ parse("Lorem <div>ipsum</div>");
4444
- `mapNode` (`function?`): Override Dom node
4545
- `mapElement` (`function?`): Override React element. Сalled after `components` override
4646
- `attrsMap` (`object?`): Map for converting dom attributes to react attributes
47+
- `onError` (`function?`): Listen errors, if we provide not a string `html` or catch an unexpected internal error
4748

4849
#### Return
4950

50-
Return `React.Fragment` with React nodes or empty, if `html` is not/empty `string`
51+
Return `React.Fragment | null`. We get `null` if html is not a string or catch an unexpected internal error
5152

5253
### sanitize `(html: string) => string`
5354

@@ -66,7 +67,7 @@ parse("<div>Lorem ipsum</div>", {
6667

6768
### components `{ [key: string]: (props: {}) => ReactNode }`
6869

69-
```ts
70+
```tsx
7071
function Typography(props) {
7172
return <span {...props} />;
7273
}
@@ -82,10 +83,12 @@ parse("<div>Lorem <b>ipsum</b></div>", {
8283

8384
JSX
8485

85-
```js
86-
<p>
87-
Lorem <span>ipsum</span>
88-
</p>
86+
```jsx
87+
<>
88+
<p>
89+
Lorem <span>ipsum</span>
90+
</p>
91+
</>
8992
```
9093

9194
### mapNode `(node: Node, key: number | string, options = {}) => Node | ReactNode }`
@@ -132,3 +135,16 @@ parse("<div>Lorem <b>ipsum</b></div>", {
132135
attrsMap: { ...svgAttrsMap, ...customMap },
133136
});
134137
```
138+
139+
### onError `(error: unknown, html: unknown) => void`
140+
141+
```tsx
142+
// if we provide incorrect html, or get internal error
143+
const res: null = parse(undefined, {
144+
onError: (error: unknown, html: unknown) => {
145+
bugsnag.notify(error, (event) => {
146+
event.addMetadata("htmlparser", { html });
147+
});
148+
},
149+
});
150+
```

‎__tests__/index.test.tsx‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ describe("parse", () => {
1313
'<div class="vce-row-container" data-vce-boxed-width="true"><div class="vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top" id="el-9a9a0daa" data-vce-do-apply="all el-9a9a0daa"><div class="vce-row-content" data-vce-element-content="true"><div class="vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first" id="el-8c088b93"><div class="vce-col-inner" data-vce-do-apply="border margin background el-8c088b93"><div class="vce-col-content" data-vce-element-content="true" data-vce-do-apply="padding el-8c088b93"><div></div></div></div></div></div></div></div><div class="vce-row-container" data-vce-boxed-width="true"><div class="vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top" id="el-f6493b28" data-vce-do-apply="all el-f6493b28"><div class="vce-row-content" data-vce-element-content="true"><div class="vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first" id="el-b5776850"><div class="vce-col-inner" data-vce-do-apply="border margin background el-b5776850"><div class="vce-col-content" data-vce-element-content="true" data-vce-do-apply="padding el-b5776850"><section class="vce-hero-section-container vce-hero-section-media--xs vce-hero-section-media--sm vce-hero-section-media--md vce-hero-section-media--lg"><div class="vce vce-hero-section" id="el-f2cf62c4" data-vce-do-apply="margin background border el-f2cf62c4"><div class="vce-hero-section--wrap-row vce-hero-section--background-position-center-center" style="background-image: url(\'\'); /* color: white; */ "><div class="vce-hero-section--wrap"><div class="vce-hero-section--content" data-vce-do-apply="padding el-f2cf62c4"><div class="vce-hero-section--content-container"><h1 style="color: #fff;">Highland Traditions of Scotland</h1>\n<p style="color: #fff;">The region became culturally distinguishable from the Lowlands from the later Middle Ages into the modern period.</p></div><div class="vce-button--style-basic-container vce-button--style-basic-container--align-center"><span class="vce-button--style-basic-wrapper vce" id="el-08abb633" data-vce-do-apply="margin el-08abb633"><button class="vce-button vce-button--style-basic vce-button--style-basic--border-square vce-button--style-basic--size-medium vce-button--style-basic--color-557cbf--fff" data-vce-do-apply="padding border background el-08abb633">Apply Now</button></span></div></div></div></div></div></section></div></div></div></div></div></div><div class="vce-row-container" data-vce-boxed-width="true"><div class="vce-row vce-row--col-gap-30 vce-row-equal-height vce-row-content--top" id="el-26c9385c" data-vce-do-apply="all el-26c9385c"><div class="vce-row-content" data-vce-element-content="true"><div class="vce-col vce-col--md-auto vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first" id="el-c45b6ad5"><div class="vce-col-inner" data-vce-do-apply="border margin background el-c45b6ad5"><div class="vce-col-content" data-vce-element-content="true" data-vce-do-apply="padding el-c45b6ad5"><div class="vce-simple-image-slider" style="--backgroundPosition:center center; --pointerColor:#fff; --pointerColorHover:#fd9a00;"><div class="vce-simple-image-slider-wrapper vce" id="el-82299bdd" data-vce-do-apply="all el-82299bdd"><div class="vce-simple-image-slider-helper vce-simple-image-slider-aspect-ratio--3-4"><div class="vce-simple-image-slider-list" data-slick-autoplay="on" data-slick-autoplay-delay="5000" data-slick-effect="slideIn" data-slick-dots="on" data-slick-arrows="off"><div class="slick-list"><div class="slick-track"><div class="vce-simple-image-slider-item vc-slick-item"><div style="background-image:url(https://images.ctfassets.net/hrltx12pl8hq/7JnR6tVVwDyUM8Cbci3GtJ/bf74366cff2ba271471725d0b0ef418c/shutterstock_376532611-og.jpg)" class="vce-simple-image-slider-img vce-simple-image-slider-img--scale"><img class="" src="https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547__340.jpg" style="opacity:0" alt=""></div></div></div></div></div><div class="vce-simple-image-slider-dots"></div></div></div></div></div></div></div></div></div></div>'
1414
);
1515

16-
const { container } = render(node);
16+
const { container } = render(<>{node}</>);
1717

1818
expect(container).toBeVisible();
1919
});
2020

2121
it("should return empty fragment for empty string", () => {
2222
const node = parse("");
2323

24-
expect(node).toStrictEqual(<></>);
24+
expect(node).toStrictEqual(<>{""}</>);
2525

26-
const { container } = render(node);
26+
const { container } = render(<>{node}</>);
2727

2828
expect(container).toBeVisible();
2929
});
@@ -35,22 +35,25 @@ describe("parse", () => {
3535
const node = parse(null, { onError });
3636

3737
expect(onError).toBeCalled();
38-
expect(onError).toBeCalledWith(new TypeError("HTML must be a string"));
39-
expect(node).toStrictEqual(<></>);
38+
expect(onError).toBeCalledWith(
39+
new TypeError("HTML must be a string"),
40+
null
41+
);
42+
expect(node).toStrictEqual(null);
4043
});
4144

4245
it("should return empty fragment and invoke onError if we got not a string after sanitize", () => {
43-
const onError = jest.fn();
46+
const onError = jest.fn(() => {});
4447
const sanitize = jest.fn((_: string) => null);
45-
4648
//@ts-ignore
4749
const node = parse("<div></div>", { onError, sanitize });
4850

4951
expect(onError).toBeCalled();
5052
expect(onError).toBeCalledWith(
51-
new TypeError("Sanitized HTML must be a string")
53+
new TypeError("Sanitized HTML must be a string"),
54+
null
5255
);
53-
expect(node).toStrictEqual(<></>);
56+
expect(node).toStrictEqual(null);
5457
});
5558

5659
it("should parse components", () => {
@@ -69,10 +72,11 @@ describe("parse", () => {
6972
attrsMap: {
7073
hello: "world",
7174
},
75+
onError: () => {},
7276
}
7377
);
7478

75-
const { getByText, container } = render(node);
79+
const { getByText, container } = render(<>{node}</>);
7680

7781
expect(container.innerHTML).toMatchInlineSnapshot(
7882
`"Hello,<b>!</b><div class="active" style="color: red; font-size-adjust: initial; font-weight: 600;" id="">, world<b>!</b><b></b></div><input type="text" value="Submit">"`

‎package.json‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-html5-parser",
33
"amdName": "ReactHTMLParser",
4-
"version": "0.1.4",
4+
"version": "0.2.0",
55
"sideEffects": false,
66
"description": "Fast, simple and small (2kb) HTML to React parser in browser",
77
"type": "module",
@@ -88,20 +88,20 @@
8888
{
8989
"path": "./dist/index.{module,modern}.mjs",
9090
"import": "{ parse }",
91-
"limit": "1.35 kB"
91+
"limit": "1.5 kB"
9292
},
9393
{
9494
"path": "./dist/index.{module,modern}.mjs",
9595
"import": "{ parse, parseAttrs, SVG_ATTRIBUTES }",
96-
"limit": "2.3 kB"
96+
"limit": "2.4 kB"
9797
},
9898
{
9999
"path": "./dist/index.cjs",
100-
"limit": "2.5 kB"
100+
"limit": "2.6 kB"
101101
},
102102
{
103103
"path": "./dist/index.umd.js",
104-
"limit": "2.6 kB"
104+
"limit": "2.7 kB"
105105
}
106106
]
107107
}

‎src/core/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ export type RenderOptions = {
3131
};
3232
export type ParseOptions = RenderOptions & {
3333
sanitize?: (html: string) => string;
34-
onError?: (error: unknown) => void;
34+
onError?: (error: unknown, html: unknown) => void;
3535
};

‎src/index.ts‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
1-
import { createElement, Fragment } from "react";
1+
import { createElement, Fragment, ReactNode } from "react";
22
import { SVG_ATTRIBUTES } from "./core/constants";
33
import { identity } from "./core/utils";
44
import { renderNode, renderNodes, getNodeList } from "./core/render";
55
import { parseAttrs } from "./core/attributes";
66
import { ParseOptions } from "./core/types";
77

8-
const emptyFragment = createElement(Fragment);
98
const typeErrorMessage = "HTML must be a string";
10-
119
function checkTypeError(html: unknown, message: string) {
1210
if (typeof html !== "string") {
1311
throw new TypeError(message);
1412
}
1513
}
1614

1715
function parse(html: string, options: ParseOptions = {}) {
16+
let node: ReactNode = null;
17+
1818
try {
1919
checkTypeError(html, typeErrorMessage);
2020

2121
html = (options.sanitize || identity)(html);
2222

2323
checkTypeError(html, "Sanitized " + typeErrorMessage);
2424

25-
if (html)
26-
return createElement(
27-
Fragment,
28-
{},
29-
renderNodes(getNodeList(html), options)
30-
);
25+
node = createElement(
26+
Fragment,
27+
{},
28+
html && renderNodes(getNodeList(html), options)
29+
);
3130
} catch (error) {
3231
const onError = options.onError;
3332

34-
if (onError?.call) onError(error);
33+
if (onError?.call) {
34+
onError(error, html);
35+
}
3536
}
3637

37-
return emptyFragment;
38+
return node;
3839
}
3940

4041
export * from "./core/types";

0 commit comments

Comments
 (0)