1
- var DOMProperty = require ( 'react-dom-core/lib/DOMProperty' ) ;
2
- var propertyConfig = require ( './property-config' ) ;
1
+ var reactProperty = require ( 'react-property' ) ;
3
2
var styleToObject = require ( 'style-to-object' ) ;
4
3
var utilities = require ( './utilities' ) ;
4
+
5
5
var camelCase = utilities . camelCase ;
6
6
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 ;
12
12
13
13
/**
14
- * Makes attributes compatible with React props.
14
+ * Converts HTML/SVG DOM attributes to React props.
15
15
*
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.
18
18
*/
19
19
function attributesToProps ( attributes ) {
20
20
attributes = attributes || { } ;
21
+
22
+ var attributeName ;
23
+ var attributeNameLowerCased ;
24
+ var attributeValue ;
25
+ var property ;
21
26
var props = { } ;
22
- var propertyName ;
23
- var propertyValue ;
24
- var reactProperty ;
25
27
26
- for ( propertyName in attributes ) {
27
- propertyValue = attributes [ propertyName ] ;
28
+ for ( attributeName in attributes ) {
29
+ attributeValue = attributes [ attributeName ] ;
28
30
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 ;
32
34
continue ;
33
35
}
34
36
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 ;
49
46
continue ;
50
47
}
51
48
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 ;
58
59
}
59
60
}
60
61
61
62
// convert inline style to object
62
63
if ( attributes . style != null ) {
63
64
props . style = cssToJs ( attributes . style ) ;
64
65
}
66
+
65
67
return props ;
66
68
}
67
69
@@ -75,14 +77,16 @@ function cssToJs(style) {
75
77
if ( typeof style !== 'string' ) {
76
78
throw new TypeError ( 'First argument must be a string.' ) ;
77
79
}
80
+
78
81
var styleObj = { } ;
79
82
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 ;
84
87
}
85
88
} ) ;
89
+
86
90
return styleObj ;
87
91
}
88
92
0 commit comments