Skip to content

Commit 00fbef2

Browse files
cbbfcdhuangteng02
authored and
huangteng02
committed
feat: export attributesToProps
1 parent e0e0791 commit 00fbef2

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

index.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { DomElement, ParserOptions } from 'htmlparser2';
44
import domToReact from './lib/dom-to-react';
5+
import attributesToProps from './lib/attributes-to-props';
56
import htmlToDOM from 'html-dom-parser';
67

78
export interface HTMLReactParserOptions {
@@ -37,6 +38,6 @@ declare function HTMLReactParser(
3738
options?: HTMLReactParserOptions
3839
): ReturnType<typeof domToReact>;
3940

40-
export { DomElement, ParserOptions, domToReact, htmlToDOM };
41+
export { DomElement, ParserOptions, domToReact, htmlToDOM, attributesToProps };
4142

4243
export default HTMLReactParser;

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var domToReact = require('./lib/dom-to-react');
2+
var attributesToProps = require('./lib/attributes-to-props');
23
var htmlToDOM = require('html-dom-parser');
34

45
// decode HTML entities by default for `htmlparser2`
@@ -30,6 +31,7 @@ function HTMLReactParser(html, options) {
3031

3132
HTMLReactParser.domToReact = domToReact;
3233
HTMLReactParser.htmlToDOM = htmlToDOM;
34+
HTMLReactParser.attributesToProps = attributesToProps;
3335

3436
// support CommonJS and ES Modules
3537
module.exports = HTMLReactParser;

lib/attributes-to-props.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// TypeScript Version: 3.3
2+
3+
/**
4+
* Converts HTML/SVG DOM attributes to React props.
5+
*
6+
* @param attributes - The HTML/SVG DOM attributes.
7+
* @returns - The React props.
8+
*/
9+
export default function attributesToProps(
10+
attributes: { [key: string]: string }
11+
): { [key: string]: string };

test/helpers/data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports.html = {
66
multiple: '<p>foo</p><p>bar</p>',
77
nested: '<div><p>foo <em>bar</em></p></div>',
88
attributes:
9-
'<hr id="foo" class="bar baz" style="background: #fff; text-align: center;" data-foo="bar" />',
9+
'<hr id="foo" class="bar baz" style="background:#fff;text-align:center" data-foo="bar"/>',
1010
complex:
1111
'<html><head><meta charSet="utf-8"/><title>Title</title><link rel="stylesheet" href="style.css"/></head><body><header id="header">Header</header><h1 style="color:#000;font-size:42px">Heading</h1><hr/><p>Paragraph</p><img src="image.jpg"/><div class="class1 class2">Some <em>text</em>.</div><script>alert();</script></body></html>',
1212
textarea: '<textarea>foo</textarea>',

test/html-to-react.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,36 @@ describe('HTML to React', () => {
129129

130130
expect(render(reactElement)).toBe(html);
131131
});
132+
133+
it('use attributesToProps to converts attributes to React props', () => {
134+
const { attributesToProps } = parse;
135+
const html = data.html.attributes;
136+
137+
let props;
138+
const options = {
139+
replace: node => {
140+
if (node.attribs && node.name === 'hr') {
141+
props = attributesToProps(node.attribs);
142+
return {
143+
type: 'hr',
144+
props
145+
};
146+
}
147+
}
148+
};
149+
const reactElement = parse(html, options);
150+
151+
expect(props).toEqual({
152+
id: 'foo',
153+
className: 'bar baz',
154+
style: {
155+
background: '#fff',
156+
textAlign: 'center'
157+
},
158+
['data-foo']: 'bar'
159+
});
160+
expect(render(reactElement)).toBe(html);
161+
});
132162
});
133163

134164
describe('library', () => {

0 commit comments

Comments
 (0)