Skip to content

Commit 2afcf15

Browse files
Merge pull request #192 from remarkablemark/refactor/dom-to-react
refactor(dom-to-react): rename function, tidy module, add tests
2 parents 42317c7 + e35be8e commit 2afcf15

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

lib/dom-to-react.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1+
var React = require('react');
12
var attributesToProps = require('./attributes-to-props');
23
var utilities = require('./utilities');
34

45
/**
56
* Converts DOM nodes to React elements.
67
*
7-
* @param {DomElement[]} nodes - The DOM nodes.
8-
* @param {Object} [options={}] - The additional options.
9-
* @param {Function} [options.replace] - The replacer.
10-
* @param {Object} [options.library] - The library (React, Preact, etc.).
11-
* @return {String|ReactElement|ReactElement[]}
8+
* @param {DomElement[]} nodes - DOM nodes.
9+
* @param {object} [options={}] - Options.
10+
* @param {Function} [options.replace] - Replacer.
11+
* @param {object} [options.library] - Library (React/Preact/etc.).
12+
* @return {string|ReactElement|ReactElement[]}
1213
*/
1314
function domToReact(nodes, options) {
1415
options = options || {};
1516

16-
var React = options.library || require('react');
17-
var cloneElement = React.cloneElement;
18-
var createElement = React.createElement;
19-
var isValidElement = React.isValidElement;
17+
var library = options.library || React;
18+
var cloneElement = library.cloneElement;
19+
var createElement = library.createElement;
20+
var isValidElement = library.isValidElement;
2021

2122
var result = [];
2223
var node;
@@ -61,7 +62,7 @@ function domToReact(nodes, options) {
6162
}
6263

6364
props = node.attribs;
64-
if (!shouldPassAttributesUnaltered(node)) {
65+
if (!skipAttributesToProps(node)) {
6566
props = attributesToProps(node.attribs);
6667
}
6768

@@ -108,12 +109,13 @@ function domToReact(nodes, options) {
108109
}
109110

110111
/**
111-
* Determines whether attributes should be altered or not.
112+
* Determines whether DOM element attributes should be transformed to props.
113+
* Web Components (custom elements) should not have their attributes transformed.
112114
*
113-
* @param {React.ReactElement} node
114-
* @return {Boolean}
115+
* @param {DomElement} node
116+
* @return {boolean}
115117
*/
116-
function shouldPassAttributesUnaltered(node) {
118+
function skipAttributesToProps(node) {
117119
return (
118120
utilities.PRESERVE_CUSTOM_ATTRIBUTES &&
119121
node.type === 'tag' &&

test/dom-to-react.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ const { render } = require('./helpers');
88
const { html, svg } = require('./data');
99

1010
describe('domToReact', () => {
11+
it.each([
12+
['comment', html.comment],
13+
['doctype', html.doctype]
14+
])('skips %s', (type, value) => {
15+
expect(domToReact(htmlToDOM(value))).toEqual([]);
16+
});
17+
18+
it('converts "text" to "text"', () => {
19+
const text = 'text';
20+
expect(domToReact(htmlToDOM(text))).toBe(text);
21+
});
22+
1123
it('converts single DOM node to React', () => {
1224
const reactElement = domToReact(htmlToDOM(html.single));
1325
expect(reactElement).toMatchSnapshot();

0 commit comments

Comments
 (0)