|
| 1 | +/** |
| 2 | + * @author Titus Wormer |
| 3 | + * @copyright 2016 Titus Wormer |
| 4 | + * @license MIT |
| 5 | + * @module hastscript |
| 6 | + * @fileoverview Hyperscript compatible DSL for creating |
| 7 | + * virtual HAST trees. |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +/* eslint-env commonjs */ |
| 13 | + |
| 14 | +/* |
| 15 | + * Dependencies. |
| 16 | + */ |
| 17 | + |
| 18 | +var parseSelector = require('hast-util-parse-selector'); |
| 19 | +var camelcase = require('camelcase'); |
| 20 | +var propertyInformation = require('property-information'); |
| 21 | +var cssDeclarations = require('css-declarations').parse; |
| 22 | +var spaces = require('space-separated-tokens').parse; |
| 23 | +var commas = require('comma-separated-tokens').parse; |
| 24 | + |
| 25 | +/** |
| 26 | + * Parse a (list of) primitives. |
| 27 | + * |
| 28 | + * @param {Object} info - Information. |
| 29 | + * @param {string} name - Property name. |
| 30 | + * @param {*} value - Values to parse. |
| 31 | + * @return {*} - Parsed `value`. |
| 32 | + */ |
| 33 | +function parsePrimitive(info, name, value) { |
| 34 | + var result = value; |
| 35 | + var index; |
| 36 | + var length; |
| 37 | + |
| 38 | + if (typeof value === 'object' && 'length' in value) { |
| 39 | + length = value.length; |
| 40 | + index = -1; |
| 41 | + result = []; |
| 42 | + |
| 43 | + while (++index < length) { |
| 44 | + result[index] = parsePrimitive(info, name, value[index]); |
| 45 | + } |
| 46 | + |
| 47 | + return result; |
| 48 | + } |
| 49 | + |
| 50 | + if (info.boolean) { |
| 51 | + result = true; |
| 52 | + } else if (info.numeric || info.positiveNumeric) { |
| 53 | + result = Number(result); |
| 54 | + } else if (info.overloadedBoolean) { |
| 55 | + /* |
| 56 | + * Accept `boolean` and `string`. |
| 57 | + */ |
| 58 | + |
| 59 | + if ( |
| 60 | + typeof result === 'string' && |
| 61 | + (result === '' || value.toLowerCase() === name) |
| 62 | + ) { |
| 63 | + result = true; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return result; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Add `name` and its `value` to `properties`. |
| 72 | + * `properties` can be prefilled by `parseSelector`: |
| 73 | + * it can have `id` and `className` properties. |
| 74 | + * |
| 75 | + * @param {Object} properties - Attributes. |
| 76 | + * @param {string} name - Property name. |
| 77 | + * @param {*} value - Property value. |
| 78 | + */ |
| 79 | +function addProperty(properties, name, value) { |
| 80 | + var info = propertyInformation(name) || {}; |
| 81 | + var result = value; |
| 82 | + var key; |
| 83 | + |
| 84 | + /* |
| 85 | + * Ignore nully and NaN values. |
| 86 | + */ |
| 87 | + |
| 88 | + if (value === null || value === undefined || value !== value) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + /* |
| 93 | + * Handle values. |
| 94 | + */ |
| 95 | + |
| 96 | + if (name === 'style') { |
| 97 | + /* |
| 98 | + * Accept both `string` and `object`. |
| 99 | + */ |
| 100 | + |
| 101 | + if (typeof value === 'string') { |
| 102 | + result = cssDeclarations(result); |
| 103 | + } else { |
| 104 | + result = {}; |
| 105 | + |
| 106 | + for (key in value) { |
| 107 | + result[key] = value[key]; |
| 108 | + } |
| 109 | + } |
| 110 | + } else if (info.spaceSeparated) { |
| 111 | + /* |
| 112 | + * Accept both `string` and `Array`. |
| 113 | + */ |
| 114 | + |
| 115 | + result = typeof value === 'string' ? spaces(result) : result; |
| 116 | + |
| 117 | + /* |
| 118 | + * Class-names (which can be added both on |
| 119 | + * the `selector` and here). |
| 120 | + */ |
| 121 | + |
| 122 | + if (name === 'class' && properties.className) { |
| 123 | + result = properties.className.concat(result); |
| 124 | + } |
| 125 | + } else if (info.commaSeparated) { |
| 126 | + /* |
| 127 | + * Accept both `string` and `Array`. |
| 128 | + */ |
| 129 | + |
| 130 | + result = typeof value === 'string' ? commas(result) : result; |
| 131 | + } |
| 132 | + |
| 133 | + result = parsePrimitive(info, name, result); |
| 134 | + |
| 135 | + properties[info.propertyName || camelcase(name)] = result; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Add `value` as a child to `nodes`. |
| 140 | + * |
| 141 | + * @param {Array.<Node>} nodes - List of siblings. |
| 142 | + * @param {string|Node|Array.<string|Node>} value - List of |
| 143 | + * children or child to add. |
| 144 | + */ |
| 145 | +function addChild(nodes, value) { |
| 146 | + var index; |
| 147 | + var length; |
| 148 | + |
| 149 | + if (value === null || value === undefined) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + if (typeof value === 'string' || typeof value === 'number') { |
| 154 | + value = { |
| 155 | + 'type': 'text', |
| 156 | + 'value': String(value) |
| 157 | + }; |
| 158 | + } |
| 159 | + |
| 160 | + if (typeof value === 'object' && 'length' in value) { |
| 161 | + index = -1; |
| 162 | + length = value.length; |
| 163 | + |
| 164 | + while (++index < length) { |
| 165 | + addChild(nodes, value[index]); |
| 166 | + } |
| 167 | + |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + if (typeof value !== 'object' || !('type' in value)) { |
| 172 | + throw new Error( |
| 173 | + 'Expected node, nodes, or string, got `' + value + '`' |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + nodes.push(value); |
| 178 | +} |
| 179 | + |
| 180 | +/** |
| 181 | + * Hyperscript compatible DSL for creating virtual HAST |
| 182 | + * trees. |
| 183 | + * |
| 184 | + * @param {string?} selector - Simple CSS selector to parse. |
| 185 | + * @param {Object?} properties - HTML attributes to add. |
| 186 | + * @param {string|Array.<string|Node>} children - List of |
| 187 | + * children to add. |
| 188 | + * @return {Node} - HAST node. |
| 189 | + */ |
| 190 | +function h(selector, properties, children) { |
| 191 | + var node = parseSelector(selector); |
| 192 | + var property; |
| 193 | + |
| 194 | + if ( |
| 195 | + properties && |
| 196 | + !children && |
| 197 | + ( |
| 198 | + typeof properties === 'string' || |
| 199 | + 'length' in properties || |
| 200 | + // Only allow a node at the `properties` |
| 201 | + // position when it isn’t an `input`, as those |
| 202 | + // use HTML `type` and `value` attributes too |
| 203 | + // and are void. |
| 204 | + (node.tagName !== 'input' && 'type' in properties) |
| 205 | + ) |
| 206 | + ) { |
| 207 | + children = properties; |
| 208 | + properties = null; |
| 209 | + } |
| 210 | + |
| 211 | + if (properties) { |
| 212 | + for (property in properties) { |
| 213 | + addProperty(node.properties, property, properties[property]); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + addChild(node.children, children); |
| 218 | + |
| 219 | + return node; |
| 220 | +} |
| 221 | + |
| 222 | +/* |
| 223 | + * Expose. |
| 224 | + */ |
| 225 | + |
| 226 | +module.exports = h; |
0 commit comments