Skip to content

Commit 6278ec1

Browse files
Merge pull request #117 from remarkablemark/feat/react-property
feat: replace `react-dom-core` with `react-property`
2 parents c963bc0 + 26ebef9 commit 6278ec1

File tree

4 files changed

+128
-138
lines changed

4 files changed

+128
-138
lines changed

lib/attributes-to-props.js

+46-42
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,69 @@
1-
var DOMProperty = require('react-dom-core/lib/DOMProperty');
2-
var propertyConfig = require('./property-config');
1+
var reactProperty = require('react-property');
32
var styleToObject = require('style-to-object');
43
var utilities = require('./utilities');
4+
55
var camelCase = utilities.camelCase;
66

7-
var config = propertyConfig.config;
8-
var isCustomAttribute = propertyConfig.HTMLDOMPropertyConfig.isCustomAttribute;
9-
DOMProperty.injection.injectDOMPropertyConfig(
10-
propertyConfig.HTMLDOMPropertyConfig
11-
);
7+
var htmlProperties = reactProperty.html;
8+
var svgProperties = reactProperty.svg;
9+
var isCustomAttribute = reactProperty.isCustomAttribute;
10+
11+
var hasOwnProperty = Object.prototype.hasOwnProperty;
1212

1313
/**
14-
* Makes attributes compatible with React props.
14+
* Converts HTML/SVG DOM attributes to React props.
1515
*
16-
* @param {Object} [attributes={}] - The attributes.
17-
* @return {Object} - The props.
16+
* @param {Object} [attributes={}] - The HTML/SVG DOM attributes.
17+
* @return {Object} - The React props.
1818
*/
1919
function attributesToProps(attributes) {
2020
attributes = attributes || {};
21+
22+
var attributeName;
23+
var attributeNameLowerCased;
24+
var attributeValue;
25+
var property;
2126
var props = {};
22-
var propertyName;
23-
var propertyValue;
24-
var reactProperty;
2527

26-
for (propertyName in attributes) {
27-
propertyValue = attributes[propertyName];
28+
for (attributeName in attributes) {
29+
attributeValue = attributes[attributeName];
2830

29-
// custom attributes (`data-` and `aria-`)
30-
if (isCustomAttribute(propertyName)) {
31-
props[propertyName] = propertyValue;
31+
// ARIA (aria-*) or custom data (data-*) attribute
32+
if (isCustomAttribute(attributeName)) {
33+
props[attributeName] = attributeValue;
3234
continue;
3335
}
3436

35-
// make HTML DOM attribute/property consistent with React attribute/property
36-
reactProperty = config.html[propertyName.toLowerCase()];
37-
if (reactProperty) {
38-
if (
39-
Object.prototype.hasOwnProperty.call(
40-
DOMProperty.properties,
41-
reactProperty
42-
) &&
43-
DOMProperty.properties[reactProperty].hasBooleanValue
44-
) {
45-
props[reactProperty] = true;
46-
} else {
47-
props[reactProperty] = propertyValue;
48-
}
37+
// convert HTML attribute to React prop
38+
attributeNameLowerCased = attributeName.toLowerCase();
39+
if (hasOwnProperty.call(htmlProperties, attributeNameLowerCased)) {
40+
property = htmlProperties[attributeNameLowerCased];
41+
props[property.propertyName] =
42+
property.hasBooleanValue ||
43+
(property.hasOverloadedBooleanValue && !attributeValue)
44+
? true
45+
: attributeValue;
4946
continue;
5047
}
5148

52-
// make SVG DOM attribute/property consistent with React attribute/property
53-
reactProperty = config.svg[propertyName];
54-
if (reactProperty) {
55-
props[reactProperty] = propertyValue;
56-
} else if (utilities.PRESERVE_CUSTOM_ATTRIBUTES) {
57-
props[propertyName] = propertyValue;
49+
// convert SVG attribute to React prop
50+
if (hasOwnProperty.call(svgProperties, attributeName)) {
51+
property = svgProperties[attributeName];
52+
props[property.propertyName] = attributeValue;
53+
continue;
54+
}
55+
56+
// preserve custom attribute if React >=16
57+
if (utilities.PRESERVE_CUSTOM_ATTRIBUTES) {
58+
props[attributeName] = attributeValue;
5859
}
5960
}
6061

6162
// convert inline style to object
6263
if (attributes.style != null) {
6364
props.style = cssToJs(attributes.style);
6465
}
66+
6567
return props;
6668
}
6769

@@ -75,14 +77,16 @@ function cssToJs(style) {
7577
if (typeof style !== 'string') {
7678
throw new TypeError('First argument must be a string.');
7779
}
80+
7881
var styleObj = {};
7982

80-
styleToObject(style, function(propName, propValue) {
81-
// Check if it's not a comment node
82-
if (propName && propValue) {
83-
styleObj[camelCase(propName)] = propValue;
83+
styleToObject(style, function(property, value) {
84+
// skip if it's a comment node
85+
if (property && value) {
86+
styleObj[camelCase(property)] = value;
8487
}
8588
});
89+
8690
return styleObj;
8791
}
8892

lib/property-config.js

-50
This file was deleted.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"dependencies": {
3838
"@types/domhandler": "2.4.1",
3939
"html-dom-parser": "0.2.2",
40-
"react-dom-core": "0.1.1",
40+
"react-property": "1.0.1",
4141
"style-to-object": "0.2.3"
4242
},
4343
"devDependencies": {

0 commit comments

Comments
 (0)