-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
155 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
'use strict'; | ||
|
||
const printString = require('../printString'); | ||
|
||
const reactElement = Symbol.for('react.element'); | ||
|
||
function traverseChildren(opaqueChildren, cb) { | ||
if (Array.isArray(opaqueChildren)) { | ||
opaqueChildren.forEach(child => traverseChildren(child, cb)); | ||
} else if (opaqueChildren != null && opaqueChildren !== false) { | ||
cb(opaqueChildren); | ||
} | ||
} | ||
|
||
function printChildren(flatChildren, print, indent) { | ||
return flatChildren.map(node => { | ||
if (typeof node === 'object') { | ||
return printElement(node, print, indent); | ||
} else if (typeof node === 'string') { | ||
return printString(node); | ||
} else { | ||
return print(node); | ||
} | ||
}).join('\n'); | ||
} | ||
|
||
function printProps(props, print, indent) { | ||
return Object.keys(props).sort().map(name => { | ||
if (name === 'children') { | ||
return ''; | ||
} | ||
|
||
const prop = props[name]; | ||
let printed = print(prop); | ||
|
||
if (typeof prop !== 'string') { | ||
if (printed.indexOf('\n') !== -1) { | ||
printed = '{\n' + indent(indent(printed) + '\n}'); | ||
} else { | ||
printed = '{' + printed + '}'; | ||
} | ||
} | ||
|
||
return '\n' + indent(name + '=') + printed; | ||
}).join(''); | ||
} | ||
|
||
function printElement(element, print, indent) { | ||
let result = '<' + element.type; | ||
result += printProps(element.props, print, indent); | ||
|
||
const opaqueChildren = element.props.children; | ||
if (opaqueChildren) { | ||
let flatChildren = []; | ||
traverseChildren(opaqueChildren, child => { | ||
flatChildren.push(child); | ||
}); | ||
const children = printChildren(flatChildren, print, indent); | ||
result += '>\n' + indent(children) + '\n</' + element.type + '>'; | ||
} else { | ||
result += ' />'; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
module.exports = { | ||
test(object) { | ||
return object && object.$$typeof === reactElement; | ||
}, | ||
print(val, print, indent) { | ||
return printElement(val, print, indent); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
824b7ca
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add it to the documentation? This is a nice plugin, I actually wrote a very similar one some time ago: https://github.com/algolia/react-element-to-jsx-string
Nice to see you took the same approach :p