Skip to content

Commit

Permalink
feat: export attributesToProps
Browse files Browse the repository at this point in the history
  • Loading branch information
cbbfcd authored and huangteng02 committed Sep 10, 2020
1 parent e0e0791 commit 00fbef2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

export interface HTMLReactParserOptions {
Expand Down Expand Up @@ -37,6 +38,6 @@ declare function HTMLReactParser(
options?: HTMLReactParserOptions
): ReturnType<typeof domToReact>;

export { DomElement, ParserOptions, domToReact, htmlToDOM };
export { DomElement, ParserOptions, domToReact, htmlToDOM, attributesToProps };

export default HTMLReactParser;
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var domToReact = require('./lib/dom-to-react');
var attributesToProps = require('./lib/attributes-to-props');
var htmlToDOM = require('html-dom-parser');

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

HTMLReactParser.domToReact = domToReact;
HTMLReactParser.htmlToDOM = htmlToDOM;
HTMLReactParser.attributesToProps = attributesToProps;

// support CommonJS and ES Modules
module.exports = HTMLReactParser;
Expand Down
11 changes: 11 additions & 0 deletions lib/attributes-to-props.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// TypeScript Version: 3.3

/**
* Converts HTML/SVG DOM attributes to React props.
*
* @param attributes - The HTML/SVG DOM attributes.
* @returns - The React props.
*/
export default function attributesToProps(
attributes: { [key: string]: string }
): { [key: string]: string };
2 changes: 1 addition & 1 deletion test/helpers/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports.html = {
multiple: '<p>foo</p><p>bar</p>',
nested: '<div><p>foo <em>bar</em></p></div>',
attributes:
'<hr id="foo" class="bar baz" style="background: #fff; text-align: center;" data-foo="bar" />',
'<hr id="foo" class="bar baz" style="background:#fff;text-align:center" data-foo="bar"/>',
complex:
'<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>',
textarea: '<textarea>foo</textarea>',
Expand Down
30 changes: 30 additions & 0 deletions test/html-to-react.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,36 @@ describe('HTML to React', () => {

expect(render(reactElement)).toBe(html);
});

it('use attributesToProps to converts attributes to React props', () => {
const { attributesToProps } = parse;
const html = data.html.attributes;

let props;
const options = {
replace: node => {
if (node.attribs && node.name === 'hr') {
props = attributesToProps(node.attribs);
return {
type: 'hr',
props
};
}
}
};
const reactElement = parse(html, options);

expect(props).toEqual({
id: 'foo',
className: 'bar baz',
style: {
background: '#fff',
textAlign: 'center'
},
['data-foo']: 'bar'
});
expect(render(reactElement)).toBe(html);
});
});

describe('library', () => {
Expand Down

0 comments on commit 00fbef2

Please sign in to comment.