Skip to content

Commit c33f6c4

Browse files
Merge pull request #780 from remarkablemark/fix/domhandler
fix(index): export domhandler classes
2 parents 4c3f41b + 7a9c207 commit c33f6c4

File tree

7 files changed

+57
-21
lines changed

7 files changed

+57
-21
lines changed

.size-limit.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
33
"path": "dist/html-react-parser.min.js",
4-
"limit": "10.531 KB"
4+
"limit": "10.524 KB"
55
}
66
]

index.d.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
// TypeScript Version: 4.7
2+
/* eslint-disable no-undef, no-unused-vars */
23

3-
import { ParserOptions } from 'htmlparser2';
44
import {
55
Comment,
6-
DomHandlerOptions,
76
Element,
87
Node,
98
ProcessingInstruction,
10-
Text
9+
Text,
10+
type DomHandlerOptions
1111
} from 'domhandler';
1212
import htmlToDOM from 'html-dom-parser';
13+
import { ParserOptions } from 'htmlparser2';
1314

1415
import attributesToProps from './lib/attributes-to-props';
1516
import domToReact from './lib/dom-to-react';
1617

1718
export { attributesToProps, domToReact, htmlToDOM };
1819
export type HTMLParser2Options = ParserOptions & DomHandlerOptions;
19-
export { Comment, Element, Node, ProcessingInstruction, Text };
20+
export { Comment, Element, ProcessingInstruction, Text };
2021
export type DOMNode = Comment | Element | Node | ProcessingInstruction | Text;
2122

2223
export interface HTMLReactParserOptions {

index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
var domToReact = require('./lib/dom-to-react');
2-
var attributesToProps = require('./lib/attributes-to-props');
1+
var domhandler = require('domhandler');
32
var htmlToDOM = require('html-dom-parser');
43

4+
var attributesToProps = require('./lib/attributes-to-props');
5+
var domToReact = require('./lib/dom-to-react');
6+
57
// support backwards compatibility for ES Module
68
htmlToDOM =
79
/* istanbul ignore next */
@@ -36,7 +38,12 @@ function HTMLReactParser(html, options) {
3638
HTMLReactParser.domToReact = domToReact;
3739
HTMLReactParser.htmlToDOM = htmlToDOM;
3840
HTMLReactParser.attributesToProps = attributesToProps;
39-
HTMLReactParser.Element = require('domhandler').Element;
41+
42+
// domhandler
43+
HTMLReactParser.Comment = domhandler.Comment;
44+
HTMLReactParser.Element = domhandler.Element;
45+
HTMLReactParser.ProcessingInstruction = domhandler.ProcessingInstruction;
46+
HTMLReactParser.Text = domhandler.Text;
4047

4148
// support CommonJS and ES Modules
4249
module.exports = HTMLReactParser;

index.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import HTMLReactParser from './index.js';
33
export var domToReact = HTMLReactParser.domToReact;
44
export var htmlToDOM = HTMLReactParser.htmlToDOM;
55
export var attributesToProps = HTMLReactParser.attributesToProps;
6+
7+
// domhandler
8+
export var Comment = HTMLReactParser.Comment;
69
export var Element = HTMLReactParser.Element;
10+
export var ProcessingInstruction = HTMLReactParser.ProcessingInstruction;
11+
export var Text = HTMLReactParser.Text;
712

813
export default HTMLReactParser;

test/index.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const React = require('react');
2+
const domhandler = require('domhandler');
23
const parse = require('..');
34
const { html, svg } = require('./data');
45
const { render } = require('./helpers');
@@ -24,6 +25,16 @@ describe('module', () => {
2425
expect(parse.attributesToProps).toBe(require('../lib/attributes-to-props'));
2526
expect(parse.attributesToProps).toBeInstanceOf(Function);
2627
});
28+
29+
describe('domhandler', () => {
30+
it.each(['Comment', 'Element', 'ProcessingInstruction', 'Text'])(
31+
'exports %s',
32+
name => {
33+
expect(parse[name]).toBeInstanceOf(Function);
34+
expect(parse[name]).toBe(domhandler[name]);
35+
}
36+
);
37+
});
2738
});
2839

2940
describe('HTMLReactParser', () => {

test/integration/index.test.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
describe.each([
2-
['unminified', '../../dist/html-react-parser'],
3-
['minified', '../../dist/html-react-parser.min']
2+
['minified', '../../dist/html-react-parser.min'],
3+
['unminified', '../../dist/html-react-parser']
44
])('UMD %s', (_type, path) => {
55
const parse = require(path);
66

7-
it('exports the parser', () => {
7+
it('exports parser', () => {
88
expect(parse).toBeInstanceOf(Function);
99
});
1010

1111
it('exports default', () => {
1212
expect(parse.default).toBeInstanceOf(Function);
1313
});
1414

15-
it('exports domToReact', () => {
16-
expect(parse.domToReact).toBeInstanceOf(Function);
15+
describe('internal', () => {
16+
it.each(['attributesToProps', 'domToReact', 'htmlToDOM'])(
17+
'exports %s',
18+
name => {
19+
expect(parse[name]).toBeInstanceOf(Function);
20+
}
21+
);
1722
});
1823

19-
it('exports htmlToDOM', () => {
20-
expect(parse.htmlToDOM).toBeInstanceOf(Function);
21-
});
22-
23-
it('exports attributesToProps', () => {
24-
expect(parse.attributesToProps).toBeInstanceOf(Function);
24+
describe('domhandler', () => {
25+
it.each(['Comment', 'Element', 'ProcessingInstruction', 'Text'])(
26+
'exports %s',
27+
name => {
28+
expect(parse[name]).toBeInstanceOf(Function);
29+
}
30+
);
2531
});
2632
});

test/module/index.mjs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import assert from 'assert';
22
import parse, {
3+
attributesToProps,
34
domToReact,
4-
htmlToDOM,
5-
attributesToProps
5+
htmlToDOM
66
} from '../../index.mjs';
7+
import { Comment, Element, ProcessingInstruction, Text } from '../../index.mjs';
78

89
[parse, domToReact, htmlToDOM, attributesToProps].forEach(func => {
910
assert.strictEqual(typeof func, 'function');
1011
});
12+
13+
// domhandler
14+
[Comment, Element, ProcessingInstruction, Text].forEach(func => {
15+
assert.strictEqual(typeof func, 'function');
16+
});

0 commit comments

Comments
 (0)