-
Notifications
You must be signed in to change notification settings - Fork 4
/
render-to-string.js
190 lines (163 loc) · 4.53 KB
/
render-to-string.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
import stringifyStyles from './stringify-styles'
import escapeString from './escape-string'
import Fragment from './fragment'
import Suspense from './suspense'
import dispatcher from './dispatcher'
const ATTR_ALIASES = {
acceptCharset: 'acceptcharset',
accessKey: 'accesskey',
allowFullScreen: 'allowfullscreen',
autoCapitalize: 'autocapitalize',
autoComplete: 'autocomplete',
autoCorrect: 'autocorrect',
autoFocus: 'autofocus',
autoPlay: 'autoplay',
charSet: 'charset',
className: 'class',
colSpan: 'colspan',
contentEditable: 'contenteditable',
crossOrigin: 'crossorigin',
dateTime: 'datetime',
defaultChecked: 'checked',
defaultSelected: 'selected',
defaultValue: 'value',
htmlFor: 'for',
httpEquiv: 'http-equiv',
longDesc: 'longdesc',
maxLength: 'maxlength',
minLength: 'minlength',
noModule: 'nomodule',
noValidate: 'novalidate',
readOnly: 'readonly',
referrerPolicy: 'referrerpolicy',
rowSpan: 'rowspan',
spellCheck: 'spellcheck',
tabIndex: 'tabindex',
useMap: 'usemap'
}
// <https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes>
const BOOLEAN_ATTRS = new Set([
'async',
'allowfullscreen',
'allowpaymentrequest',
'autofocus',
'autoplay',
'checked',
'controls',
'default',
'defer',
'disabled',
'formnovalidate',
'hidden',
'ismap',
'multiple',
'muted',
'novalidate',
'nowrap',
'open',
'readonly',
'required',
'reversed',
'selected'
])
// https://www.w3.org/TR/html/syntax.html#void-elements
const VOID_ELEMENTS = new Set([
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'link',
'meta',
'param',
'source',
'track',
'wbr'
])
const EMPTY_OBJECT = Object.freeze({})
function renderToString(element, context = {}) {
// TODO nullify and only allow in functional components
dispatcher.context = context
if (typeof element === 'string') {
return escapeString(element)
} else if (typeof element === 'number') {
return String(element)
} else if (typeof element === 'boolean' || element == null) {
return ''
} else if (Array.isArray(element)) {
let html = ''
for (let i = 0, len = element.length; i < len; i++) {
html += renderToString(element[i], context)
}
return html
}
const type = element.type
if (type) {
const props = element.props || EMPTY_OBJECT
if (type.contextRef) {
context = Object.assign({}, context, { [type.contextRef.id]: props.value })
return renderToString(type(props), context)
}
if (type.prototype && type.prototype.render) {
const instance = new type(props)
if (type.contextType) {
instance.context = type.contextType.getChildContext(context)
}
return renderToString(instance.render(), context)
}
if (typeof type === 'function') {
return renderToString(type(props), context)
}
if (type === Fragment) {
return renderToString(props.children, context)
}
if (type === Suspense) {
return renderToString(props.fallback, context)
}
if (typeof type === 'string') {
let html = `<${type}`
let innerHTML
for (const prop in props) {
const value = props[prop]
if (prop === 'children' || prop === 'key' || prop === 'ref') {
// Why not use a continue statement? It's slower ¯\_(ツ)_/¯
} else if (prop === 'class' || prop === 'className') {
// This condition is here because it is the most common attribute
// and short-circuiting results in a ~5% performance boost.
html += value ? ` class="${escapeString(value)}"` : ''
} else if (prop === 'style') {
html += ` style="${stringifyStyles(value)}"`
} else if (prop === 'dangerouslySetInnerHTML') {
innerHTML = value.__html
} else {
const name = ATTR_ALIASES[prop] || prop
if (BOOLEAN_ATTRS.has(name)) {
html += value ? ` ${name}` : ''
} else if (typeof value === 'string') {
html += ` ${name}="${escapeString(value)}"`
} else if (typeof value === 'number') {
html += ` ${name}="${String(value)}"`
} else if (typeof value === 'boolean') {
html += ` ${name}="${value}"`
}
}
}
if (VOID_ELEMENTS.has(type)) {
html += '/>'
} else {
html += '>'
if (innerHTML) {
html += innerHTML
} else {
html += renderToString(props.children, context)
}
html += `</${type}>`
}
return html
}
}
}
export default renderToString