File tree Expand file tree Collapse file tree 2 files changed +77
-7
lines changed Expand file tree Collapse file tree 2 files changed +77
-7
lines changed Original file line number Diff line number Diff line change 4
4
* Module dependencies.
5
5
*/
6
6
var utilities = require ( './utilities' ) ;
7
+ var propertyConfig = require ( './property-config' ) ;
7
8
8
9
/**
9
10
* Make attributes compatible with React props.
10
11
*
11
- * @param {Object } attr - The attributes.
12
- * @return {Object } - The props.
12
+ * @param {Object } attributes - The attributes.
13
+ * @return {Object } - The props.
13
14
*/
14
- function attributesToProps ( attr ) {
15
- attr = attr || { } ;
15
+ function attributesToProps ( attributes ) {
16
+ attributes = attributes || { } ;
17
+ var props = { } ;
18
+ var propertyName ;
19
+ var reactProperty ;
16
20
17
- if ( attr . style ) {
18
- attr . style = cssToJs ( attr . style ) ;
21
+ // convert DOM attribute/property to React attribute/property
22
+ for ( propertyName in attributes ) {
23
+ propertyName = propertyName . toLowerCase ( ) ;
24
+ reactProperty = propertyConfig [ propertyName ]
25
+ if ( reactProperty ) {
26
+ props [ reactProperty ] = attributes [ propertyName ] ;
27
+ }
28
+ }
29
+
30
+ // convert inline style to object
31
+ if ( attributes . style ) {
32
+ props . style = cssToJs ( attributes . style ) ;
19
33
}
20
34
21
- return attr ;
35
+ return props ;
22
36
}
23
37
24
38
/**
Original file line number Diff line number Diff line change @@ -11,6 +11,62 @@ var attributesToProps = require('../lib/attributes-to-props');
11
11
*/
12
12
describe ( 'attributes to props helper' , function ( ) {
13
13
14
+ it ( 'converts DOM attributes to React props' , function ( ) {
15
+ assert . deepEqual (
16
+ attributesToProps ( {
17
+ 'class' : 'ic' ,
18
+ 'for' : 'tran' ,
19
+ 'http-equiv' : 'refresh'
20
+ } ) ,
21
+ {
22
+ className : 'ic' ,
23
+ htmlFor : 'tran' ,
24
+ httpEquiv : 'refresh' ,
25
+ }
26
+ ) ;
27
+ } ) ;
28
+
29
+ it ( 'converts DOM standard properties to React props' , function ( ) {
30
+ assert . deepEqual (
31
+ attributesToProps ( {
32
+ allowfullscreen : true ,
33
+ charset : 'utf-8' ,
34
+ tabindex : 1
35
+ } ) ,
36
+ {
37
+ allowFullScreen : true ,
38
+ charSet : 'utf-8' ,
39
+ tabIndex : 1
40
+ }
41
+ ) ;
42
+ } ) ;
43
+
44
+ it ( 'converts DOM RDFa properties to React props' , function ( ) {
45
+ assert . deepEqual (
46
+ attributesToProps ( {
47
+ property : 'foo' ,
48
+ 'typeof' : 'bar'
49
+ } ) ,
50
+ {
51
+ property : 'foo' ,
52
+ 'typeof' : 'bar'
53
+ }
54
+ ) ;
55
+ } ) ;
56
+
57
+ it ( 'converts DOM non-standard properties to React props' , function ( ) {
58
+ assert . deepEqual (
59
+ attributesToProps ( {
60
+ itemscope : true ,
61
+ itemid : 1337
62
+ } ) ,
63
+ {
64
+ itemScope : true ,
65
+ itemID : 1337
66
+ }
67
+ ) ;
68
+ } ) ;
69
+
14
70
it ( 'converts CSS style string to JS style object' , function ( ) {
15
71
// proper css
16
72
assert . deepEqual (
You can’t perform that action at this time.
0 commit comments