-
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathssr.js
More file actions
155 lines (130 loc) 路 3.93 KB
/
Copy pathssr.js
File metadata and controls
155 lines (130 loc) 路 3.93 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { encodeEntities, styleObjToCss, assign } from './util';
import { options } from 'preact';
const VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
const UNSAFE_NAME = /[\s\n\\/='"\0<>]/;
/**
* Render Preact JSX + Components to an HTML string.
* @param {import('preact').VNode|string|number|null|boolean} vnode any renderable value
* @param {object} context context object, forks throughout the tree
* @param {any} selectValue the current select value, passed down through the tree to set <options selected>
*/
export default function renderToString(vnode, context, selectValue) {
if (vnode == null || vnode === false || vnode === true) {
return '';
}
if (typeof vnode !== 'object') {
return encodeEntities(vnode);
}
context = context || {};
if (Array.isArray(vnode)) {
let s = '';
for (let i=0; i<vnode.length; i++) {
s += renderToString(vnode[i], context, selectValue);
}
return s;
}
let nodeName = vnode.type,
props = vnode.props;
// components
if (typeof nodeName === 'function') {
let rendered;
let c = vnode.__c = { __v: vnode, context, props: vnode.props, __h: [] };
// options.render
if (options.__r) options.__r(vnode);
// Necessary for createContext api. Setting this property will pass
// the context value as `this.context` just for this component.
let cxType = nodeName.contextType;
let provider = cxType && context[cxType.__c];
let cctx = cxType != null ? (provider ? provider.props.value : cxType.__) : context;
if (nodeName.prototype && nodeName.prototype.render) {
c = vnode.__c = new nodeName(props, cctx);
c.__v = vnode;
// turn off stateful re-rendering:
c._dirty = c.__d = true;
c.props = props;
if (c.state==null) c.state = {};
if (c._nextState==null && c.__s==null) {
c._nextState = c.__s = c.state;
}
c.context = cctx;
if (nodeName.getDerivedStateFromProps) {
c.state = assign(assign({}, c.state), nodeName.getDerivedStateFromProps(c.props, c.state));
}
else if (c.componentWillMount) {
c.componentWillMount();
}
// If the user called setState in cWM we need to flush pending,
// state updates. This is the same behaviour in React.
c.state = c._nextState !== c.state
? c._nextState : c.__s!==c.state
? c.__s : c.state;
rendered = c.render(c.props, c.state, c.context);
}
else {
// stateless functional components
rendered = nodeName.call(c, props, cctx);
}
if (c.getChildContext) {
context = assign(assign({}, context), c.getChildContext());
}
return renderToString(rendered, context, selectValue);
}
if (UNSAFE_NAME.test(nodeName)) return '';
let html = '';
let s = '<';
s += nodeName;
if (props) {
for (let name in props) {
let v = props[name];
let type;
if (name === 'children' || name === 'key' || name === 'ref' || UNSAFE_NAME.test(name)) {
// skip
}
else if (nodeName === 'option' && name === 'value' && selectValue === v) {
s += ' selected';
selectValue = undefined;
}
else if (nodeName === 'select' && name === 'value') {
selectValue = v;
}
else if (name === 'dangerouslySetInnerHTML') {
html = v && v.__html;
}
else if ((v || v===0) && (type = typeof v) !== 'function') {
if (name === 'style' && type === 'object') {
v = styleObjToCss(v);
}
else if (name.substring(0,5) === 'xlink') {
// this doesn't actually need to be limited to SVG, since attributes "xlinkHref" are invalid anyway
name = name.replace(/^xlink([A-Z])/, 'xlink:$1');
}
s += ' ';
s += name;
if (v !== true && v !== '') {
s += '="';
s += encodeEntities(v);
s += '"';
}
}
}
}
let isVoid = VOID_ELEMENTS.test(nodeName);
if (isVoid) {
s += ' />';
}
else {
s += '>';
}
if (html) {
s += html;
}
else if (props && props.children) {
s += renderToString(props.children, context, selectValue);
}
if (!isVoid) {
s += '</';
s += nodeName;
s += '>';
}
return s;
}