|
2 | 2 |
|
3 | 3 | module.exports = x |
4 | 4 |
|
5 | | -var slice = [].slice |
6 | | - |
7 | 5 | // Creating xast elements. |
8 | 6 | function x(name, attributes) { |
9 | | - var attrs = {} |
10 | | - var childrenIndex = 2 |
11 | | - var attribute |
12 | | - var value |
13 | | - var node |
| 7 | + var node = {type: 'element', name: name, attributes: {}, children: []} |
| 8 | + var index = 1 |
| 9 | + var key |
14 | 10 |
|
15 | | - if (typeof name !== 'string' || name === '') { |
| 11 | + if (typeof name !== 'string' || !name) { |
16 | 12 | throw new Error('Expected element name, got `' + name + '`') |
17 | 13 | } |
18 | 14 |
|
19 | | - node = {type: 'element', name: name, attributes: attrs, children: []} |
20 | | - |
21 | | - // Note that we do not accept a node instead of attributes. |
22 | | - if ( |
23 | | - typeof attributes === 'number' || |
24 | | - typeof attributes === 'string' || |
25 | | - (attributes && 'length' in attributes) |
26 | | - ) { |
27 | | - childrenIndex = 1 |
28 | | - attributes = null |
29 | | - } |
30 | | - |
31 | | - if (attributes !== null && attributes !== undefined) { |
32 | | - for (attribute in attributes) { |
33 | | - value = attributes[attribute] |
34 | | - |
35 | | - // Ignore nullish and NaN values. |
36 | | - if (value !== null && value !== undefined && value === value) { |
37 | | - attrs[attribute] = String(value) |
| 15 | + // Handle props. |
| 16 | + if (attributes) { |
| 17 | + if ( |
| 18 | + typeof attributes === 'string' || |
| 19 | + typeof attributes === 'number' || |
| 20 | + 'length' in attributes |
| 21 | + ) { |
| 22 | + // Nope, it’s something for `children`. |
| 23 | + index-- |
| 24 | + } else { |
| 25 | + for (key in attributes) { |
| 26 | + // Ignore nullish and NaN values. |
| 27 | + if (attributes[key] != null && attributes[key] === attributes[key]) { |
| 28 | + node.attributes[key] = String(attributes[key]) |
| 29 | + } |
38 | 30 | } |
39 | 31 | } |
40 | 32 | } |
41 | 33 |
|
42 | | - add(node.children, slice.call(arguments, childrenIndex)) |
| 34 | + // Handle children. |
| 35 | + while (++index < arguments.length) { |
| 36 | + addChild(node.children, arguments[index]) |
| 37 | + } |
43 | 38 |
|
44 | 39 | return node |
45 | 40 | } |
46 | 41 |
|
47 | | -function add(siblings, value) { |
48 | | - var index |
49 | | - var length |
| 42 | +function addChild(nodes, value) { |
| 43 | + var index = -1 |
50 | 44 |
|
51 | | - if (value === null || value === undefined) { |
| 45 | + if (value == null) { |
52 | 46 | // Empty. |
53 | 47 | } else if (typeof value === 'string' || typeof value === 'number') { |
54 | | - siblings.push({type: 'text', value: String(value)}) |
| 48 | + nodes.push({type: 'text', value: String(value)}) |
55 | 49 | } else if (typeof value === 'object' && 'length' in value) { |
56 | | - index = -1 |
57 | | - length = value.length |
58 | | - |
59 | | - while (++index < length) { |
60 | | - add(siblings, value[index]) |
| 50 | + while (++index < value.length) { |
| 51 | + addChild(nodes, value[index]) |
61 | 52 | } |
62 | | - } else if (typeof value === 'object' && typeof value.type === 'string') { |
63 | | - siblings.push(value) |
| 53 | + } else if (typeof value === 'object' && value.type) { |
| 54 | + nodes.push(value) |
64 | 55 | } else { |
65 | 56 | throw new TypeError('Expected node, nodes, string, got `' + value + '`') |
66 | 57 | } |
|
0 commit comments