-
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathindex.js
More file actions
325 lines (268 loc) 路 9.13 KB
/
Copy pathindex.js
File metadata and controls
325 lines (268 loc) 路 9.13 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import { encodeEntities, indent, isLargeString, styleObjToCss, assign, getChildren } from './util';
import { options, Fragment, createElement } from 'preact';
const SHALLOW = { shallow: true };
// components without names, kept as a hash for later comparison to return consistent UnnamedComponentXX names.
const UNNAMED = [];
const VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
const noop = () => {};
/** Render Preact JSX + Components to an HTML string.
* @name render
* @function
* @param {VNode} vnode JSX VNode to render.
* @param {Object} [context={}] Optionally pass an initial context object through the render path.
* @param {Object} [options={}] Rendering options
* @param {Boolean} [options.shallow=false] If `true`, renders nested Components as HTML elements (`<Foo a="b" />`).
* @param {Boolean} [options.xml=false] If `true`, uses self-closing tags for elements without children.
* @param {Boolean} [options.pretty=false] If `true`, adds whitespace for readability
* @param {Boolean} [options.preserveWhitespace=['pre']] Array of HTML tags for which to preserve indentation.
*/
renderToString.render = renderToString;
/** Only render elements, leaving Components inline as `<ComponentName ... />`.
* This method is just a convenience alias for `render(vnode, context, { shallow:true })`
* @name shallow
* @function
* @param {VNode} vnode JSX VNode to render.
* @param {Object} [context={}] Optionally pass an initial context object through the render path.
*/
let shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW);
/** The default export is an alias of `render()`. */
function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
if (vnode==null || typeof vnode==='boolean') {
return '';
}
// wrap array nodes in Fragment
if (Array.isArray(vnode)) {
vnode = createElement(Fragment, null, vnode);
}
let nodeName = vnode.type,
props = vnode.props,
isComponent = false;
context = context || {};
opts = opts || {};
let pretty = (opts.preserveWhitespace || ['pre']).indexOf(nodeName) === -1 && opts.pretty,
indentChar = pretty && typeof pretty==='string' ? pretty : '\t';
// #text nodes
if (typeof vnode!=='object' && !nodeName) {
return encodeEntities(vnode);
}
// components
if (typeof nodeName==='function') {
isComponent = true;
if (opts.shallow && (inner || opts.renderRootComponent===false)) {
nodeName = getComponentName(nodeName);
}
else if (nodeName===Fragment) {
let rendered = '';
let children = [];
getChildren(children, vnode.props.children);
for (let i = 0; i < children.length; i++) {
rendered += (i > 0 && pretty ? '\n' : '') + renderToString(children[i], context, opts, opts.shallowHighOrder!==false, isSvgMode, selectValue);
}
return pretty ? indent(rendered, indentChar) : rendered;
}
else {
let rendered;
let c = vnode.__c = {
__v: vnode,
context,
props: vnode.props,
// silently drop state updates
setState: noop,
forceUpdate: noop,
// hooks
__h: []
};
// options.render
if (options.__r) options.__r(vnode);
if (!nodeName.prototype || typeof nodeName.prototype.render!=='function') {
// 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;
// stateless functional components
rendered = nodeName.call(vnode.__c, props, cctx);
}
else {
// class-based components
let cxType = nodeName.contextType;
let provider = cxType && context[cxType.__c];
let cctx = cxType != null ? (provider ? provider.props.value : cxType.__) : context;
// c = new nodeName(props, context);
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);
}
if (c.getChildContext) {
context = assign(assign({}, context), c.getChildContext());
}
return renderToString(rendered, context, opts, opts.shallowHighOrder!==false, isSvgMode, selectValue);
}
}
// render JSX to HTML
let s = '', html;
if (props) {
let attrs = Object.keys(props);
// allow sorting lexicographically for more determinism (useful for tests, such as via preact-jsx-chai)
if (opts && opts.sortAttributes===true) attrs.sort();
for (let i=0; i<attrs.length; i++) {
let name = attrs[i],
v = props[name];
if (name==='children') continue;
if (name.match(/[\s\n\\/='"\0<>]/)) continue;
if (!(opts && opts.allAttributes) && (name==='key' || name==='ref' || name==='__self' || name==='__source')) continue;
if (name==='className') {
if (props.class) continue;
name = 'class';
}
else if (isSvgMode && name.match(/^xlink:?./)) {
name = name.toLowerCase().replace(/^xlink:?/, 'xlink:');
}
if (name==='htmlFor') {
if (props.for) continue;
name = 'for';
}
if (name==='style' && v && typeof v==='object') {
v = styleObjToCss(v);
}
let hooked = opts.attributeHook && opts.attributeHook(name, v, context, opts, isComponent);
if (hooked || hooked==='') {
s += hooked;
continue;
}
if (name==='dangerouslySetInnerHTML') {
html = v && v.__html;
}
else if ((v || v===0 || v==='') && typeof v!=='function') {
if (v===true || v==='') {
v = name;
// in non-xml mode, allow boolean attributes
if (!opts || !opts.xml) {
s += ' ' + name;
continue;
}
}
if (name==='value') {
if (nodeName==='select') {
selectValue = v;
continue;
}
else if (nodeName==='option' && selectValue==v) {
s += ` selected`;
}
}
s += ` ${name}="${encodeEntities(v)}"`;
}
}
}
// account for >1 multiline attribute
if (pretty) {
let sub = s.replace(/^\n\s*/, ' ');
if (sub!==s && !~sub.indexOf('\n')) s = sub;
else if (pretty && ~s.indexOf('\n')) s += '\n';
}
s = `<${nodeName}${s}>`;
if (String(nodeName).match(/[\s\n\\/='"\0<>]/)) throw new Error(`${nodeName} is not a valid HTML tag name in ${s}`);
let isVoid = String(nodeName).match(VOID_ELEMENTS);
if (isVoid) s = s.replace(/>$/, ' />');
let pieces = [];
let children;
if (html) {
// if multiline, indent.
if (pretty && isLargeString(html)) {
html = '\n' + indentChar + indent(html, indentChar);
}
s += html;
}
else if (props && getChildren(children = [], props.children).length) {
let hasLarge = pretty && ~s.indexOf('\n');
let lastWasText = false;
for (let i=0; i<children.length; i++) {
let child = children[i];
if (child!=null && child!==false) {
let childSvgMode = nodeName==='svg' ? true : nodeName==='foreignObject' ? false : isSvgMode,
ret = renderToString(child, context, opts, true, childSvgMode, selectValue);
if (pretty && !hasLarge && isLargeString(ret)) hasLarge = true;
// Skip if we received an empty string
if (ret) {
if (pretty) {
let isText = ret.length > 0 && ret[0]!='<';
// We merge adjacent text nodes, otherwise each piece would be printed
// on a new line.
if (lastWasText && isText) {
pieces[pieces.length -1] += ret;
}
else {
pieces.push(ret);
}
lastWasText = isText;
}
else {
pieces.push(ret);
}
}
}
}
if (pretty && hasLarge) {
for (let i=pieces.length; i--; ) {
pieces[i] = '\n' + indentChar + pieces[i];
}
}
}
if (pieces.length) {
s += pieces.join('');
}
else if (opts && opts.xml) {
return s.substring(0, s.length-1) + ' />';
}
if (!isVoid) {
if (pretty && ~s.indexOf('\n')) s += '\n';
s += `</${nodeName}>`;
}
return s;
}
function getComponentName(component) {
return component.displayName || component!==Function && component.name || getFallbackComponentName(component);
}
function getFallbackComponentName(component) {
let str = Function.prototype.toString.call(component),
name = (str.match(/^\s*function\s+([^( ]+)/) || '')[1];
if (!name) {
// search for an existing indexed name for the given component:
let index = -1;
for (let i=UNNAMED.length; i--; ) {
if (UNNAMED[i]===component) {
index = i;
break;
}
}
// not found, create a new indexed name:
if (index<0) {
index = UNNAMED.push(component) - 1;
}
name = `UnnamedComponent${index}`;
}
return name;
}
renderToString.shallowRender = shallowRender;
export default renderToString;
export {
renderToString as render,
renderToString,
shallowRender
};