'use strict';
exports.__esModule = true;
exports.h = h;
var _vnode = require('./vnode');
var _hooks = require('./hooks');
var _util = require('./util');
var SHARED_TEMP_ARRAY = [];
/** JSX/hyperscript reviver
* @see http://jasonformat.com/wtf-is-jsx
* @public
* @example
* /** @jsx h *\/
* import { render, h } from 'preact';
* render(<span>foo</span>, document.body);
*/
function h(nodeName, attributes, firstChild) {
var len = arguments.length,
children = undefined,
arr = undefined,
lastSimple = undefined;
if (len > 2) {
var type = typeof firstChild;
if (len === 3 && type !== 'object' && type !== 'function') {
if (!_util.falsey(firstChild)) {
children = [String(firstChild)];
}
} else {
children = [];
for (var i = 2; i < len; i++) {
var _p = arguments[i];
if (_util.falsey(_p)) continue;
if (_p.join) arr = _p;else (arr = SHARED_TEMP_ARRAY)[0] = _p;
for (var j = 0; j < arr.length; j++) {
var child = arr[j],
simple = !(_util.falsey(child) || _util.isFunction(child) || child instanceof _vnode.VNode);
if (simple && !_util.isString(child)) child = String(child);
if (simple && lastSimple) {
children[children.length - 1] += child;
} else Eif (!_util.falsey(child)) {
children.push(child);
}
lastSimple = simple;
}
}
}
} else if (attributes) {
if (attributes.children) {
return h(nodeName, attributes, attributes.children);
}
if (!_util.isFunction(nodeName)) {
// normalize className to class.
if ('className' in attributes) {
attributes['class'] = attributes.className;
delete attributes.className;
}
lastSimple = attributes['class'];
if (lastSimple && !_util.isString(lastSimple)) {
attributes['class'] = _util.hashToClassName(lastSimple);
}
lastSimple = attributes.style;
if (lastSimple && !_util.isString(lastSimple)) {
attributes.style = _util.styleObjToCss(lastSimple);
}
}
}
var p = new _vnode.VNode(nodeName, attributes || undefined, children);
_hooks.optionsHook('vnode', p);
return p;
}
|