Skip to content

Commit 9ed889c

Browse files
style: lint and reindent files based on prettier rules
The major change is reindenting from 4 to 2 spaces.
1 parent afc143b commit 9ed889c

11 files changed

+692
-696
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ var domParserOptions = { decodeEntities: true, lowerCaseAttributeNames: false };
1313
* @return {ReactElement|Array}
1414
*/
1515
function HTMLReactParser(html, options) {
16-
if (typeof html !== 'string') {
17-
throw new TypeError('First argument must be a string');
18-
}
19-
return domToReact(htmlToDOM(html, domParserOptions), options);
16+
if (typeof html !== 'string') {
17+
throw new TypeError('First argument must be a string');
18+
}
19+
return domToReact(htmlToDOM(html, domParserOptions), options);
2020
}
2121

2222
/**

lib/attributes-to-props.js

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ var utilities = require('./utilities');
55

66
var config = propertyConfig.config;
77
var isCustomAttribute = propertyConfig.HTMLDOMPropertyConfig.isCustomAttribute;
8-
DOMProperty.injection.injectDOMPropertyConfig(propertyConfig.HTMLDOMPropertyConfig);
8+
DOMProperty.injection.injectDOMPropertyConfig(
9+
propertyConfig.HTMLDOMPropertyConfig
10+
);
911

1012
/**
1113
* Makes attributes compatible with React props.
@@ -14,44 +16,47 @@ DOMProperty.injection.injectDOMPropertyConfig(propertyConfig.HTMLDOMPropertyConf
1416
* @return {Object} - The props.
1517
*/
1618
function attributesToProps(attributes) {
17-
attributes = attributes || {};
18-
var props = {};
19-
var propertyName;
20-
var propertyValue;
21-
var reactProperty;
19+
attributes = attributes || {};
20+
var props = {};
21+
var propertyName;
22+
var propertyValue;
23+
var reactProperty;
2224

23-
for (propertyName in attributes) {
24-
propertyValue = attributes[propertyName];
25+
for (propertyName in attributes) {
26+
propertyValue = attributes[propertyName];
2527

26-
// custom attributes (`data-` and `aria-`)
27-
if (isCustomAttribute(propertyName)) {
28-
props[propertyName] = propertyValue;
29-
continue;
30-
}
31-
32-
// make HTML DOM attribute/property consistent with React attribute/property
33-
reactProperty = config.html[propertyName.toLowerCase()];
34-
if (reactProperty) {
35-
if (DOMProperty.properties.hasOwnProperty(reactProperty) && DOMProperty.properties[reactProperty].hasBooleanValue) {
36-
props[reactProperty] = true;
37-
} else {
38-
props[reactProperty] = propertyValue;
39-
}
40-
continue;
41-
}
28+
// custom attributes (`data-` and `aria-`)
29+
if (isCustomAttribute(propertyName)) {
30+
props[propertyName] = propertyValue;
31+
continue;
32+
}
4233

43-
// make SVG DOM attribute/property consistent with React attribute/property
44-
reactProperty = config.svg[propertyName];
45-
if (reactProperty) {
46-
props[reactProperty] = propertyValue;
47-
}
34+
// make HTML DOM attribute/property consistent with React attribute/property
35+
reactProperty = config.html[propertyName.toLowerCase()];
36+
if (reactProperty) {
37+
if (
38+
DOMProperty.properties.hasOwnProperty(reactProperty) &&
39+
DOMProperty.properties[reactProperty].hasBooleanValue
40+
) {
41+
props[reactProperty] = true;
42+
} else {
43+
props[reactProperty] = propertyValue;
44+
}
45+
continue;
4846
}
4947

50-
// convert inline style to object
51-
if (attributes.style != null) {
52-
props.style = cssToJs(attributes.style);
48+
// make SVG DOM attribute/property consistent with React attribute/property
49+
reactProperty = config.svg[propertyName];
50+
if (reactProperty) {
51+
props[reactProperty] = propertyValue;
5352
}
54-
return props;
53+
}
54+
55+
// convert inline style to object
56+
if (attributes.style != null) {
57+
props.style = cssToJs(attributes.style);
58+
}
59+
return props;
5560
}
5661

5762
/**
@@ -61,18 +66,18 @@ function attributesToProps(attributes) {
6166
* @return {Object} - The JS style object.
6267
*/
6368
function cssToJs(style) {
64-
if (typeof style !== 'string') {
65-
throw new TypeError('First argument must be a string.');
66-
}
67-
var styleObj = {};
69+
if (typeof style !== 'string') {
70+
throw new TypeError('First argument must be a string.');
71+
}
72+
var styleObj = {};
6873

69-
styleToObject(style, function(propName, propValue) {
70-
// Check if it's not a comment node
71-
if (propName && propValue) {
72-
styleObj[utilities.camelCase(propName)] = propValue;
73-
}
74-
});
75-
return styleObj;
74+
styleToObject(style, function(propName, propValue) {
75+
// Check if it's not a comment node
76+
if (propName && propValue) {
77+
styleObj[utilities.camelCase(propName)] = propValue;
78+
}
79+
});
80+
return styleObj;
7681
}
7782

7883
module.exports = attributesToProps;

lib/dom-to-react.js

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,80 +10,77 @@ var attributesToProps = require('./attributes-to-props');
1010
* @return {ReactElement|Array}
1111
*/
1212
function domToReact(nodes, options) {
13-
options = options || {};
14-
var result = [];
15-
var node;
16-
var isReplacePresent = typeof options.replace === 'function';
17-
var replacement;
18-
var props;
19-
var children;
13+
options = options || {};
14+
var result = [];
15+
var node;
16+
var isReplacePresent = typeof options.replace === 'function';
17+
var replacement;
18+
var props;
19+
var children;
2020

21-
for (var i = 0, len = nodes.length; i < len; i++) {
22-
node = nodes[i];
21+
for (var i = 0, len = nodes.length; i < len; i++) {
22+
node = nodes[i];
2323

24-
// replace with custom React element (if applicable)
25-
if (isReplacePresent) {
26-
replacement = options.replace(node);
24+
// replace with custom React element (if applicable)
25+
if (isReplacePresent) {
26+
replacement = options.replace(node);
2727

28-
if (React.isValidElement(replacement)) {
29-
// specify a "key" prop if element has siblings
30-
// https://fb.me/react-warning-keys
31-
if (len > 1) {
32-
replacement = React.cloneElement(replacement, { key: i });
33-
}
34-
result.push(replacement);
35-
continue;
36-
}
37-
}
38-
39-
if (node.type === 'text') {
40-
result.push(node.data);
41-
continue;
28+
if (React.isValidElement(replacement)) {
29+
// specify a "key" prop if element has siblings
30+
// https://fb.me/react-warning-keys
31+
if (len > 1) {
32+
replacement = React.cloneElement(replacement, { key: i });
4233
}
34+
result.push(replacement);
35+
continue;
36+
}
37+
}
4338

44-
// update values
45-
props = attributesToProps(node.attribs);
46-
children = null;
39+
if (node.type === 'text') {
40+
result.push(node.data);
41+
continue;
42+
}
4743

48-
// node type for <script> is "script"
49-
// node type for <style> is "style"
50-
if (node.type === 'script' || node.type === 'style') {
51-
// prevent text in <script> or <style> from being escaped
52-
// https://facebook.github.io/react/tips/dangerously-set-inner-html.html
53-
if (node.children[0]) {
54-
props.dangerouslySetInnerHTML = {
55-
__html: node.children[0].data
56-
};
57-
}
44+
// update values
45+
props = attributesToProps(node.attribs);
46+
children = null;
5847

59-
} else if (node.type === 'tag') {
60-
// setting textarea value in children is an antipattern in React
61-
// https://reactjs.org/docs/forms.html#the-textarea-tag
62-
if (node.name === 'textarea' && node.children[0]) {
63-
props.defaultValue = node.children[0].data;
48+
// node type for <script> is "script"
49+
// node type for <style> is "style"
50+
if (node.type === 'script' || node.type === 'style') {
51+
// prevent text in <script> or <style> from being escaped
52+
// https://facebook.github.io/react/tips/dangerously-set-inner-html.html
53+
if (node.children[0]) {
54+
props.dangerouslySetInnerHTML = {
55+
__html: node.children[0].data
56+
};
57+
}
58+
} else if (node.type === 'tag') {
59+
// setting textarea value in children is an antipattern in React
60+
// https://reactjs.org/docs/forms.html#the-textarea-tag
61+
if (node.name === 'textarea' && node.children[0]) {
62+
props.defaultValue = node.children[0].data;
6463

65-
// continue recursion of creating React elements (if applicable)
66-
} else if (node.children && node.children.length) {
67-
children = domToReact(node.children, options);
68-
}
64+
// continue recursion of creating React elements (if applicable)
65+
} else if (node.children && node.children.length) {
66+
children = domToReact(node.children, options);
67+
}
6968

70-
// skip all other cases (e.g., comment)
71-
} else {
72-
continue;
73-
}
74-
75-
// specify a "key" prop if element has siblings
76-
// https://fb.me/react-warning-keys
77-
if (len > 1) {
78-
props.key = i;
79-
}
69+
// skip all other cases (e.g., comment)
70+
} else {
71+
continue;
72+
}
8073

81-
result.push(
82-
React.createElement(node.name, props, children)
83-
);
74+
// specify a "key" prop if element has siblings
75+
// https://fb.me/react-warning-keys
76+
if (len > 1) {
77+
props.key = i;
8478
}
8579

86-
return result.length === 1 ? result[0] : result;
80+
result.push(React.createElement(node.name, props, children));
81+
}
82+
83+
return result.length === 1 ? result[0] : result;
8784
}
8885

8986
module.exports = domToReact;

lib/property-config.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ var SVGDOMPropertyConfig = require('react-dom-core/lib/SVGDOMPropertyConfig');
33
var utilities = require('./utilities');
44

55
var config = {
6-
html: {},
7-
svg: {}
6+
html: {},
7+
svg: {}
88
};
99

1010
var propertyName;
@@ -17,15 +17,13 @@ var propertyName;
1717

1818
// first map out the HTML attribute names
1919
// e.g., { className: 'class' } => { 'class': 'className' }
20-
config.html = utilities.invertObject(
21-
HTMLDOMPropertyConfig.DOMAttributeNames
22-
);
20+
config.html = utilities.invertObject(HTMLDOMPropertyConfig.DOMAttributeNames);
2321

2422
// then map out the rest of the HTML properties
2523
// e.g., { readOnly: 0 } => { readonly: 'readOnly' }
2624
for (propertyName in HTMLDOMPropertyConfig.Properties) {
27-
// lowercase to make matching property names easier
28-
config.html[propertyName.toLowerCase()] = propertyName;
25+
// lowercase to make matching property names easier
26+
config.html[propertyName.toLowerCase()] = propertyName;
2927
}
3028

3129
/**
@@ -36,19 +34,17 @@ for (propertyName in HTMLDOMPropertyConfig.Properties) {
3634

3735
// first map out the SVG attribute names
3836
// e.g., { fontSize: 'font-size' } => { 'font-size': 'fontSize' }
39-
config.svg = utilities.invertObject(
40-
SVGDOMPropertyConfig.DOMAttributeNames
41-
);
37+
config.svg = utilities.invertObject(SVGDOMPropertyConfig.DOMAttributeNames);
4238

4339
// then map out the rest of the SVG properties
4440
// e.g., { fillRule: 0 } => { fillRule: 'fillRule' }
4541
for (propertyName in SVGDOMPropertyConfig.Properties) {
46-
// do not lowercase as some svg properties are camel cased
47-
config.html[propertyName] = propertyName;
42+
// do not lowercase as some svg properties are camel cased
43+
config.html[propertyName] = propertyName;
4844
}
4945

5046
module.exports = {
51-
config: config,
52-
HTMLDOMPropertyConfig: HTMLDOMPropertyConfig,
53-
SVGDOMPropertyConfig: SVGDOMPropertyConfig
47+
config: config,
48+
HTMLDOMPropertyConfig: HTMLDOMPropertyConfig,
49+
SVGDOMPropertyConfig: SVGDOMPropertyConfig
5450
};

0 commit comments

Comments
 (0)