-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
192 lines (168 loc) · 4.83 KB
/
index.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
/** @jsx h */
const { h, options, resetCursor } = require('./fre-cjs.js')
options.currentFiber = null
const VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/
function attributeHook (name, value, isComponent) {
let type = typeof value
if (name === 'dangerouslySetInnerHTML') return false
if (value == null || type === 'function') return ''
if (
!isComponent &&
(value === false ||
((name === 'class' || name === 'style') && value === ''))
) {
return ''
}
let indentChar = '\t'
if (type !== 'string') {
return indent(`\n${name}={${value}}`, indentChar)
}
return `\n${indentChar}${name}="${encodeEntities(value)}"`
}
async function renderToString (vnode, isSvgMode, selectValue) {
if (vnode == null || typeof vnode === 'boolean') return ''
let nodeName = vnode.type
let props = vnode.props
let isComponent = false
if (nodeName === 'text' && props.nodeValue) {
return encodeEntities(props.nodeValue)
}
if (typeof nodeName === 'function') {
isComponent = true
resetCursor()
options.currentFiber = vnode
let tempVnode = nodeName.call(vnode, props)
if (vnode.action.length) {
resetCursor()
const cleanups = await Promise.all(
vnode.action.map(e => Promise.resolve(e()))
)
vnode.action = []
tempVnode = nodeName.call(vnode, props)
cleanups.filter(c => !!c).map(c => c())
}
delete vnode.action
options.currentFiber = null
return await renderToString(tempVnode, isSvgMode, selectValue)
}
let s = ''
let html
if (props) {
let attrs = Object.keys(props)
for (let i = 0; i < attrs.length; i++) {
let name = attrs[i]
let v = props[name]
if (name === 'children') continue
if (name.match(/[\s\n\\/='"\0<>]/)) continue
if (name === 'key' || name === 'ref') continue
if (name === 'className') {
name = 'class'
} else if (isSvgMode && name.match(/^xlink:?./)) {
name = name.toLowerCase().replace(/^xlink:?/, 'xlink:')
}
if (name === 'style' && v && typeof v === 'object') {
v = styleObjToCss(v)
}
let hooked = attributeHook(name, v, 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
s += ' ' + name
continue
}
if (name === 'value') {
if (nodeName === 'select') {
selectValue = v
continue
} else if (nodeName === 'option' && selectValue == v) {
s += ` selected`
}
}
s += ` ${name}="${encodeEntities(v)}"`
}
}
}
s = `<${nodeName}${s}>`
if (String(nodeName).match(/[\s\n\\/='"\0<>]/)) throw s
let isVoid = String(nodeName).match(VOID_ELEMENTS)
if (isVoid) s = s.replace(/>$/, ' />')
let pieces = []
let children
if (html) {
s += html
} else if (props && getChildren((children = []), props.children).length) {
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
let ret = await renderToString(child, childSvgMode, selectValue)
if (ret) {
pieces.push(ret)
}
}
}
}
if (pieces.length) {
s += pieces.join('')
}
if (!isVoid) {
s += `</${nodeName}>`
}
return s
}
let encodeEntities = s =>
String(s)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
const JS_TO_CSS = {}
// Convert an Object style to a CSSText string
function styleObjToCss (s) {
let str = ''
for (let prop in s) {
let val = s[prop]
if (val != null) {
if (str) str += ' '
// str += jsToCss(prop);
str +=
JS_TO_CSS[prop] ||
(JS_TO_CSS[prop] = prop.replace(/([A-Z])/g, '-$1').toLowerCase())
str += ': '
str += val
if (typeof val === 'number' && IS_NON_DIMENSIONAL.test(prop) === false) {
str += 'px'
}
str += ';'
}
}
return str || undefined
}
let indent = (s, char) => String(s).replace(/(\n+)/g, '$1' + (char || '\t'))
function getChildren (accumulator, children) {
if (Array.isArray(children)) {
children.reduce(getChildren, accumulator)
} else if (children != null && children !== false) {
accumulator.push(children)
}
return accumulator
}
function useAction (fn) {
options.currentFiber.action = options.currentFiber.action || []
options.currentFiber.action.push(fn)
}
module.exports = {
renderToString,
useAction
}