1
1
import React from 'react' ;
2
2
import collapse from 'collapse-white-space' ;
3
3
import { isElement } from 'react-addons-test-utils' ;
4
+ import isPlainObject from 'is-plain-object' ;
4
5
import stringify from 'stringify-object' ;
5
6
import sortobject from 'sortobject' ;
7
+ import traverse from 'traverse' ;
6
8
7
9
export default function reactElementToJSXString ( ReactElement ) {
8
10
return toJSXString ( { ReactElement} ) ;
@@ -83,6 +85,8 @@ function formatProps(props) {
83
85
return {
84
86
name : propName ,
85
87
value : formatPropValue ( props [ propName ] )
88
+ . replace ( / ' ? < _ _ r e a c t E l e m e n t T o J S X S t r i n g _ _ R e a c t E l e m e n t _ _ > / g, '' )
89
+ . replace ( / < \/ _ _ r e a c t E l e m e n t T o J S X S t r i n g _ _ R e a c t E l e m e n t _ _ > ' ? / g, '' )
86
90
} ;
87
91
} ) ;
88
92
}
@@ -99,8 +103,17 @@ function formatValue(value) {
99
103
if ( typeof value === 'function' ) {
100
104
return function noRefCheck ( ) { } ;
101
105
} else if ( isElement ( value ) ) {
102
- return toJSXString ( { ReactElement : value , inline : true } ) ;
103
- } else if ( value && Object . keys ( value ) . length > 0 ) {
106
+ // we use this delimiter hack in cases where the react element is a property
107
+ // of an object from a root prop
108
+ // i.e.
109
+ // reactElementToJSXString(<div a={{b: <div />}} />
110
+ // // <div a={{b: <div />}} />
111
+ // we then remove the whole wrapping
112
+ // otherwise, the element would be surrounded by quotes: <div a={{b: '<div />'}} />
113
+ return '<__reactElementToJSXString__ReactElement__>' +
114
+ toJSXString ( { ReactElement : value , inline : true } ) +
115
+ '</__reactElementToJSXString__ReactElement__>' ;
116
+ } else if ( isPlainObject ( value ) ) {
104
117
return stringifyObject ( value ) ;
105
118
}
106
119
@@ -114,10 +127,13 @@ function recurse({lvl, inline}) {
114
127
}
115
128
116
129
function stringifyObject ( obj ) {
117
- // sortobject fails on some types, like regex
118
- if ( obj && Object . keys ( obj ) . length > 0 ) {
119
- obj = sortobject ( obj ) ;
120
- }
130
+ obj = traverse ( obj ) . map ( function ( value ) {
131
+ if ( isElement ( value ) || this . isLeaf ) {
132
+ this . update ( formatValue ( value ) ) ;
133
+ }
134
+ } ) ;
135
+
136
+ obj = sortobject ( obj ) ;
121
137
122
138
return collapse ( stringify ( obj ) )
123
139
. replace ( / { / g, '{' )
0 commit comments