-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathserialize.js
426 lines (366 loc) · 10.3 KB
/
serialize.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import { encodeEntities, styleObjToCss, assign, getChildren } from './util';
import { options, Fragment } from 'preact';
const EMPTY_ARR = [];
export function serialize(vnode, format) {
// Performance optimization: `renderToString` is synchronous and we
// therefore don't execute any effects. To do that we pass an empty
// array to `options._commit` (`__c`). But we can go one step further
// and avoid a lot of dirty checks and allocations by setting
// `options._skipEffects` (`__s`) too.
const previousSkipEffects = options.__s;
options.__s = true;
let res;
try {
const serializeFormat = (vnode, context, a0, a1, a2, a3, a4) =>
_serialize(serializeFormat, format, vnode, context, a0, a1, a2, a3, a4);
res = serializeFormat(vnode, {});
} finally {
// options._commit, we don't schedule any effects in this library right now,
// so we can pass an empty queue to this hook.
if (options.__c) options.__c(vnode, EMPTY_ARR);
EMPTY_ARR.length = 0;
options.__s = previousSkipEffects;
}
return format.result(res);
}
export function serializeToString(vnode) {
return serialize(vnode, new StringFormat());
}
const noop = () => {};
/**
* @private
* @param {Format} format
* @param {VNode} vnode
* @param {Object} context
* @param {?} a0
* @param {?} a1
* @param {?} a2
* @param {?} a3
* @param {?} a4
* @return {?}
*/
function _serialize(serialize, format, vnode, context, a0, a1, a2, a3, a4) {
if (vnode == null || typeof vnode === 'boolean') {
return vnode;
}
// #text nodes
if (typeof vnode !== 'object') {
return format.text(serialize, vnode, context);
}
if (Array.isArray(vnode)) {
return format.array(serialize, vnode, context, a0, a1, a2, a3, a4);
}
let nodeName = vnode.type,
props = vnode.props;
// isComponent = false;
// components
if (typeof nodeName === 'function') {
// isComponent = true;
if (nodeName === Fragment) {
const children = [];
getChildren(children, vnode.props.children);
return serialize(children, context, a0, a1, a2, a3, a4);
// opts.shallowHighOrder !== false,
}
let rendered;
let c = (vnode.__c = {
__v: vnode,
context,
props: vnode.props,
// silently drop state updates
setState: noop,
forceUpdate: noop,
// hooks
__h: []
});
// options._diff
if (options.__b) options.__b(vnode);
// 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());
}
if (options.diffed) options.diffed(vnode);
return serialize(rendered, context, a0, a1, a2, a3, a4);
}
if (typeof vnode.type === 'object') {
return format.object(serialize, vnode, context, a0, a1, a2, a3, a4);
}
return format.element(serialize, vnode, context, a0, a1, a2, a3, a4);
}
const UNSAFE_NAME = /[\s\n\\/='"\0<>]/;
const VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
/** @implements {Format} */
export class StringFormat {
constructor() {
this._str = '';
}
/** @param {string} s */
push(s) {
this._str += s;
}
/** @return {string} */
result() {
return this._str;
}
/**
* @param {SerializeFunc} serialize
* @param {String} str
*/
text(serialize, str) {
this.push(encodeEntities(str));
}
/**
* @param {SerializeFunc} serialize
* @param {Array<VNode>} array
* @param {Object} context
* @param {boolean} isSvgMode
* @param {string} selectValue
*/
array(serialize, array, context, isSvgMode, selectValue) {
for (let i = 0; i < array.length; i++) {
// if (pretty && i > 0) rendered += '\n';
serialize(
array[i],
context,
isSvgMode,
selectValue
// inner,
);
}
}
/**
* @param {SerializeFunc} serialize
* @param {VNode} vnode
* @param {Object} context
* @param {boolean} isSvgMode
* @param {string} selectValue
*/
element(serialize, vnode, context, isSvgMode, selectValue) {
const { type: nodeName, props } = vnode;
// render JSX to HTML
this.push('<' + nodeName);
let propChildren, html;
if (props) {
const 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') {
propChildren = v;
continue;
}
if (UNSAFE_NAME.test(name)) {
continue;
}
if (
// !(opts && opts.allAttributes) &&
name === 'key' ||
name === 'ref' ||
name === '__self' ||
name === '__source' ||
name === 'defaultValue'
) {
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);
}
// always use string values instead of booleans for aria attributes
// also see https://github.com/preactjs/preact/pull/2347/files
if (name[0] === 'a' && name['1'] === 'r' && typeof v === 'boolean') {
v = String(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 (nodeName === 'textarea' && name === 'value') {
// <textarea value="a&b"> --> <textarea>a&b</textarea>
propChildren = v;
} else if ((v || v === 0 || v === '') && typeof v !== 'function') {
if (v === true || v === '') {
v = name;
this.push(' ' + name);
continue;
}
if (name === 'value') {
if (nodeName === 'select') {
selectValue = v;
continue;
} else if (nodeName === 'option' && selectValue == v) {
this.push(' selected');
}
}
this.push(` ${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';
// }
this.push('>');
if (UNSAFE_NAME.test(nodeName))
throw new Error(
`${nodeName} is not a valid HTML tag name in ${this._str}`
);
let isVoid = VOID_ELEMENTS.test(nodeName);
// || (opts.voidElements && opts.voidElements.test(nodeName));
// let pieces = [];
let children;
if (html) {
// if multiline, indent.
// if (pretty && isLargeString(html)) {
// html = '\n' + indentChar + indent(html, indentChar);
// }
this.push(html);
} else if (
propChildren != null &&
getChildren((children = []), propChildren).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;
serialize(
child,
context,
childSvgMode,
selectValue
// true,
);
// 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 + indent(pieces[i], indentChar);
// }
// }
}
// if (pieces.length || html) {
// s += pieces.join('');
// } else if (opts && opts.xml) {
// return s.substring(0, s.length - 1) + ' />';
// }
if (isVoid && !children && !html) {
// QQQQQ: do some other way!?
this._str = this._str.replace(/>$/, ' />');
} else {
// if (pretty && ~s.indexOf('\n')) s += '\n';
this.push(`</${nodeName}>`);
}
}
/**
* @param {SerializeFunc} serialize
* @param {VNode} vnode
* @param {Object} context
* @param {boolean} isSvgMode
* @param {string} selectValue
*/
object(serialize, vnode, context, isSvgMode, selectValue) {
return this.element(serialize, vnode, context, isSvgMode, selectValue);
}
}