|
1 |
| -'use strict'; |
| 1 | +'use strict' |
2 | 2 |
|
3 |
| -var parseSelector = require('hast-util-parse-selector'); |
4 |
| -var camelcase = require('camelcase'); |
5 |
| -var propertyInformation = require('property-information'); |
6 |
| -var spaces = require('space-separated-tokens').parse; |
7 |
| -var commas = require('comma-separated-tokens').parse; |
| 3 | +var parseSelector = require('hast-util-parse-selector') |
| 4 | +var camelcase = require('camelcase') |
| 5 | +var propertyInformation = require('property-information') |
| 6 | +var spaces = require('space-separated-tokens').parse |
| 7 | +var commas = require('comma-separated-tokens').parse |
8 | 8 |
|
9 |
| -module.exports = h; |
| 9 | +module.exports = h |
10 | 10 |
|
11 | 11 | /* Hyperscript compatible DSL for creating virtual HAST
|
12 | 12 | * trees. */
|
13 | 13 | function h(selector, properties, children) {
|
14 |
| - var node = parseSelector(selector); |
15 |
| - var property; |
| 14 | + var node = parseSelector(selector) |
| 15 | + var property |
16 | 16 |
|
17 | 17 | if (
|
18 | 18 | properties &&
|
19 | 19 | !children &&
|
20 |
| - ( |
21 |
| - typeof properties === 'string' || |
| 20 | + (typeof properties === 'string' || |
22 | 21 | 'length' in properties ||
|
23 |
| - isNode(node.tagName, properties) |
24 |
| - ) |
| 22 | + isNode(node.tagName, properties)) |
25 | 23 | ) {
|
26 |
| - children = properties; |
27 |
| - properties = null; |
| 24 | + children = properties |
| 25 | + properties = null |
28 | 26 | }
|
29 | 27 |
|
30 | 28 | if (properties) {
|
31 | 29 | for (property in properties) {
|
32 |
| - addProperty(node.properties, property, properties[property]); |
| 30 | + addProperty(node.properties, property, properties[property]) |
33 | 31 | }
|
34 | 32 | }
|
35 | 33 |
|
36 |
| - addChild(node.children, children); |
| 34 | + addChild(node.children, children) |
37 | 35 |
|
38 | 36 | if (node.tagName === 'template') {
|
39 |
| - node.content = {type: 'root', children: node.children}; |
40 |
| - node.children = []; |
| 37 | + node.content = {type: 'root', children: node.children} |
| 38 | + node.children = [] |
41 | 39 | }
|
42 | 40 |
|
43 |
| - return node; |
| 41 | + return node |
44 | 42 | }
|
45 | 43 |
|
46 | 44 | /* Check if `value` is a valid child node of `tagName`. */
|
47 | 45 | function isNode(tagName, value) {
|
48 |
| - var type = value.type; |
| 46 | + var type = value.type |
49 | 47 |
|
50 | 48 | if (typeof type === 'string') {
|
51 |
| - type = type.toLowerCase(); |
| 49 | + type = type.toLowerCase() |
52 | 50 | }
|
53 | 51 |
|
54 | 52 | if (tagName === 'input' || !type || typeof type !== 'string') {
|
55 |
| - return false; |
| 53 | + return false |
56 | 54 | }
|
57 | 55 |
|
58 | 56 | if (typeof value.children === 'object' && 'length' in value.children) {
|
59 |
| - return true; |
| 57 | + return true |
60 | 58 | }
|
61 | 59 |
|
62 | 60 | if (tagName === 'button') {
|
63 |
| - return type !== 'menu' && |
| 61 | + return ( |
| 62 | + type !== 'menu' && |
64 | 63 | type !== 'submit' &&
|
65 | 64 | type !== 'reset' &&
|
66 |
| - type !== 'button'; |
| 65 | + type !== 'button' |
| 66 | + ) |
67 | 67 | }
|
68 | 68 |
|
69 |
| - return 'value' in value; |
| 69 | + return 'value' in value |
70 | 70 | }
|
71 | 71 |
|
72 | 72 | /* Add `value` as a child to `nodes`. */
|
73 | 73 | function addChild(nodes, value) {
|
74 |
| - var index; |
75 |
| - var length; |
| 74 | + var index |
| 75 | + var length |
76 | 76 |
|
77 | 77 | if (value === null || value === undefined) {
|
78 |
| - return; |
| 78 | + return |
79 | 79 | }
|
80 | 80 |
|
81 | 81 | if (typeof value === 'string' || typeof value === 'number') {
|
82 |
| - value = {type: 'text', value: String(value)}; |
| 82 | + value = {type: 'text', value: String(value)} |
83 | 83 | }
|
84 | 84 |
|
85 | 85 | if (typeof value === 'object' && 'length' in value) {
|
86 |
| - index = -1; |
87 |
| - length = value.length; |
| 86 | + index = -1 |
| 87 | + length = value.length |
88 | 88 |
|
89 | 89 | while (++index < length) {
|
90 |
| - addChild(nodes, value[index]); |
| 90 | + addChild(nodes, value[index]) |
91 | 91 | }
|
92 | 92 |
|
93 |
| - return; |
| 93 | + return |
94 | 94 | }
|
95 | 95 |
|
96 | 96 | if (typeof value !== 'object' || !('type' in value)) {
|
97 |
| - throw new Error('Expected node, nodes, or string, got `' + value + '`'); |
| 97 | + throw new Error('Expected node, nodes, or string, got `' + value + '`') |
98 | 98 | }
|
99 | 99 |
|
100 |
| - nodes.push(value); |
| 100 | + nodes.push(value) |
101 | 101 | }
|
102 | 102 |
|
103 | 103 | /* Add `name` and its `value` to `properties`. `properties` can
|
104 | 104 | * be prefilled by `parseSelector`: it can have `id` and `className`
|
105 | 105 | * properties. */
|
106 | 106 | function addProperty(properties, name, value) {
|
107 |
| - var info = propertyInformation(name) || {}; |
108 |
| - var result = value; |
109 |
| - var key; |
| 107 | + var info = propertyInformation(name) || {} |
| 108 | + var result = value |
| 109 | + var key |
110 | 110 |
|
111 | 111 | /* Ignore nully and NaN values. */
|
112 | 112 | if (value === null || value === undefined || value !== value) {
|
113 |
| - return; |
| 113 | + return |
114 | 114 | }
|
115 | 115 |
|
116 | 116 | /* Handle values. */
|
117 | 117 | if (name === 'style') {
|
118 | 118 | /* Accept `object`. */
|
119 | 119 | if (typeof value !== 'string') {
|
120 |
| - result = []; |
| 120 | + result = [] |
121 | 121 |
|
122 | 122 | for (key in value) {
|
123 |
| - result.push([key, value[key]].join(': ')); |
| 123 | + result.push([key, value[key]].join(': ')) |
124 | 124 | }
|
125 | 125 |
|
126 |
| - result = result.join('; '); |
| 126 | + result = result.join('; ') |
127 | 127 | }
|
128 | 128 | } else if (info.spaceSeparated) {
|
129 | 129 | /* Accept both `string` and `Array`. */
|
130 |
| - result = typeof value === 'string' ? spaces(result) : result; |
| 130 | + result = typeof value === 'string' ? spaces(result) : result |
131 | 131 |
|
132 | 132 | /* Class-names (which can be added both on
|
133 | 133 | * the `selector` and here). */
|
134 | 134 | if (name === 'class' && properties.className) {
|
135 |
| - result = properties.className.concat(result); |
| 135 | + result = properties.className.concat(result) |
136 | 136 | }
|
137 | 137 | } else if (info.commaSeparated) {
|
138 | 138 | /* Accept both `string` and `Array`. */
|
139 |
| - result = typeof value === 'string' ? commas(result) : result; |
| 139 | + result = typeof value === 'string' ? commas(result) : result |
140 | 140 | }
|
141 | 141 |
|
142 |
| - result = parsePrimitive(info, name, result); |
| 142 | + result = parsePrimitive(info, name, result) |
143 | 143 |
|
144 |
| - properties[info.propertyName || camelcase(name)] = result; |
| 144 | + properties[info.propertyName || camelcase(name)] = result |
145 | 145 | }
|
146 | 146 |
|
147 | 147 | /* Parse a (list of) primitives. */
|
148 | 148 | function parsePrimitive(info, name, value) {
|
149 |
| - var result = value; |
150 |
| - var index; |
151 |
| - var length; |
| 149 | + var result = value |
| 150 | + var index |
| 151 | + var length |
152 | 152 |
|
153 | 153 | if (typeof value === 'object' && 'length' in value) {
|
154 |
| - length = value.length; |
155 |
| - index = -1; |
156 |
| - result = []; |
| 154 | + length = value.length |
| 155 | + index = -1 |
| 156 | + result = [] |
157 | 157 |
|
158 | 158 | while (++index < length) {
|
159 |
| - result[index] = parsePrimitive(info, name, value[index]); |
| 159 | + result[index] = parsePrimitive(info, name, value[index]) |
160 | 160 | }
|
161 | 161 |
|
162 |
| - return result; |
| 162 | + return result |
163 | 163 | }
|
164 | 164 |
|
165 | 165 | if (info.numeric || info.positiveNumeric) {
|
166 | 166 | if (!isNaN(result) && result !== '') {
|
167 |
| - result = Number(result); |
| 167 | + result = Number(result) |
168 | 168 | }
|
169 | 169 | } else if (info.boolean || info.overloadedBoolean) {
|
170 | 170 | /* Accept `boolean` and `string`. */
|
171 | 171 | if (
|
172 | 172 | typeof result === 'string' &&
|
173 | 173 | (result === '' || value.toLowerCase() === name)
|
174 | 174 | ) {
|
175 |
| - result = true; |
| 175 | + result = true |
176 | 176 | }
|
177 | 177 | }
|
178 | 178 |
|
179 |
| - return result; |
| 179 | + return result |
180 | 180 | }
|
0 commit comments