Skip to content

Commit 21190a3

Browse files
refactor(dom-to-react): replace if conditional with switch case
1 parent 507ad1c commit 21190a3

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

lib/dom-to-react.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,32 @@ function domToReact(nodes, options) {
6767

6868
children = null;
6969

70-
if (node.type === 'script' || node.type === 'style') {
71-
// prevent text in <script> or <style> from being escaped
72-
// https://facebook.github.io/react/tips/dangerously-set-inner-html.html
73-
if (node.children[0]) {
74-
props.dangerouslySetInnerHTML = {
75-
__html: node.children[0].data
76-
};
77-
}
78-
} else if (node.type === 'tag') {
79-
// setting textarea value in children is an antipattern in React
80-
// https://reactjs.org/docs/forms.html#the-textarea-tag
81-
if (node.name === 'textarea' && node.children[0]) {
82-
props.defaultValue = node.children[0].data;
83-
84-
// continue recursion of creating React elements (if applicable)
85-
} else if (node.children && node.children.length) {
86-
children = domToReact(node.children, options);
87-
}
70+
switch (node.type) {
71+
case 'script':
72+
case 'style':
73+
// prevent text in <script> or <style> from being escaped
74+
// https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml
75+
if (node.children[0]) {
76+
props.dangerouslySetInnerHTML = {
77+
__html: node.children[0].data
78+
};
79+
}
80+
break;
81+
82+
case 'tag':
83+
// setting textarea value in children is an antipattern in React
84+
// https://reactjs.org/docs/forms.html#the-textarea-tag
85+
if (node.name === 'textarea' && node.children[0]) {
86+
props.defaultValue = node.children[0].data;
87+
} else if (node.children && node.children.length) {
88+
// continue recursion of creating React elements (if applicable)
89+
children = domToReact(node.children, options);
90+
}
91+
break;
8892

8993
// skip all other cases (e.g., comment)
90-
} else {
91-
continue;
94+
default:
95+
continue;
9296
}
9397

9498
// set "key" prop for sibling elements

0 commit comments

Comments
 (0)