1- var DOMProperty = require ( 'react-dom-core/lib/DOMProperty' ) ;
2- var propertyConfig = require ( './property-config' ) ;
1+ var reactProperty = require ( 'react-property' ) ;
32var styleToObject = require ( 'style-to-object' ) ;
43var utilities = require ( './utilities' ) ;
4+
55var 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 */
1919function 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
0 commit comments