-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfactory.js
193 lines (157 loc) · 4.57 KB
/
factory.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
'use strict'
var find = require('property-information/find')
var normalize = require('property-information/normalize')
var parseSelector = require('hast-util-parse-selector')
var spaceSeparated = require('space-separated-tokens')
var commaSeparated = require('comma-separated-tokens')
module.exports = factory
var own = {}.hasOwnProperty
function factory(schema, defaultTagName, caseSensitive) {
var adjust = caseSensitive && createAdjustMap(caseSensitive)
return h
// Hyperscript compatible DSL for creating virtual hast trees.
function h(selector, properties) {
var node =
selector == null
? {type: 'root', children: []}
: parseSelector(selector, defaultTagName)
var name = selector == null ? null : node.tagName.toLowerCase()
var index = 1
var property
// Normalize the name.
if (name != null) {
node.tagName = adjust && own.call(adjust, name) ? adjust[name] : name
}
// Handle props.
if (properties) {
if (
name == null ||
typeof properties === 'string' ||
'length' in properties ||
isNode(name, properties)
) {
// Nope, it’s something for `children`.
index--
} else {
for (property in properties) {
addProperty(schema, node.properties, property, properties[property])
}
}
}
// Handle children.
while (++index < arguments.length) {
addChild(node.children, arguments[index])
}
if (name === 'template') {
node.content = {type: 'root', children: node.children}
node.children = []
}
return node
}
}
function isNode(name, value) {
var type = value.type
if (name === 'input' || !type || typeof type !== 'string') {
return false
}
if (typeof value.children === 'object' && 'length' in value.children) {
return true
}
type = type.toLowerCase()
if (name === 'button') {
return (
type !== 'menu' &&
type !== 'submit' &&
type !== 'reset' &&
type !== 'button'
)
}
return 'value' in value
}
function addProperty(schema, properties, key, value) {
var info = find(schema, key)
var result = value
var index = -1
var finalResult
// Ignore nullish and NaN values.
if (result == null || result !== result) {
return
}
// Handle list values.
if (typeof result === 'string') {
if (info.spaceSeparated) {
result = spaceSeparated.parse(result)
} else if (info.commaSeparated) {
result = commaSeparated.parse(result)
} else if (info.commaOrSpaceSeparated) {
result = spaceSeparated.parse(commaSeparated.parse(result).join(' '))
}
}
// Accept `object` on style.
if (info.property === 'style' && typeof result !== 'string') {
result = style(result)
}
// Class names (which can be added both on the `selector` and here).
if (info.property === 'className' && properties.className) {
result = properties.className.concat(result)
}
if (typeof result === 'object' && 'length' in result) {
finalResult = []
while (++index < result.length) {
finalResult[index] = parsePrimitive(info, info.property, result[index])
}
} else {
finalResult = parsePrimitive(info, info.property, result)
}
properties[info.property] = finalResult
}
function addChild(nodes, value) {
var index = -1
if (typeof value === 'string' || typeof value === 'number') {
nodes.push({type: 'text', value: String(value)})
} else if (typeof value === 'object' && 'length' in value) {
while (++index < value.length) {
addChild(nodes, value[index])
}
} else if (typeof value === 'object' && 'type' in value) {
if (value.type === 'root') {
addChild(nodes, value.children)
} else {
nodes.push(value)
}
} else {
throw new Error('Expected node, nodes, or string, got `' + value + '`')
}
}
// Parse a single primitives.
function parsePrimitive(info, name, value) {
var result = value
if ((info.number || info.positiveNumber) && !isNaN(result) && result !== '') {
result = Number(result)
}
// Accept `boolean` and `string`.
if (
(info.boolean || info.overloadedBoolean) &&
typeof result === 'string' &&
(result === '' || normalize(value) === normalize(name))
) {
result = true
}
return result
}
function style(value) {
var result = []
var key
for (key in value) {
result.push([key, value[key]].join(': '))
}
return result.join('; ')
}
function createAdjustMap(values) {
var result = {}
var index = -1
while (++index < values.length) {
result[values[index].toLowerCase()] = values[index]
}
return result
}