-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathjsx.js
76 lines (66 loc) · 1.96 KB
/
jsx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import './polyfills';
import renderToString from './index';
import { indent, encodeEntities, assign } from './util';
import prettyFormat from 'pretty-format';
// we have to patch in Array support, Possible issue in npm.im/pretty-format
let preactPlugin = {
test(object) {
return (
object &&
typeof object === 'object' &&
'type' in object &&
'props' in object &&
'key' in object
);
},
print(val, print, indent) {
return renderToString(val, preactPlugin.context, preactPlugin.opts, true);
}
};
let prettyFormatOpts = {
plugins: [preactPlugin]
};
function attributeHook(name, value, context, opts, isComponent) {
let type = typeof value;
// Use render-to-string's built-in handling for these properties
if (name === 'dangerouslySetInnerHTML') return false;
// always skip null & undefined values, skip false DOM attributes, skip functions if told to
if (value == null || (type === 'function' && !opts.functions)) return '';
if (
opts.skipFalseAttributes &&
!isComponent &&
(value === false ||
((name === 'class' || name === 'style') && value === ''))
)
return '';
let indentChar = typeof opts.pretty === 'string' ? opts.pretty : '\t';
if (type !== 'string') {
if (type === 'function' && !opts.functionNames) {
value = 'Function';
} else {
preactPlugin.context = context;
preactPlugin.opts = opts;
value = prettyFormat(value, prettyFormatOpts);
if (~value.indexOf('\n')) {
value = `${indent('\n' + value, indentChar)}\n`;
}
}
return indent(`\n${name}={${value}}`, indentChar);
}
return `\n${indentChar}${name}="${encodeEntities(value)}"`;
}
let defaultOpts = {
attributeHook,
jsx: true,
xml: false,
functions: true,
functionNames: true,
skipFalseAttributes: true,
pretty: ' '
};
function renderToJsxString(vnode, context, opts, inner) {
opts = assign(assign({}, defaultOpts), opts || {});
return renderToString(vnode, context, opts, inner);
}
export default renderToJsxString;
export { renderToJsxString as render };