all files / src/ h.js

100% Statements 49/49
96% Branches 48/50
100% Functions 2/2
100% Lines 43/43
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    12× 12×   12×   12×   12×   12×   12×   12×                     4399×         4399× 2278× 2278× 747× 742×     1531× 1531× 3128× 3128× 3120× 3120× 3128×   3128× 3128× 454× 2674× 2671× 2671×         2121× 439×     3960× 2285× 441×     2285×   1548×     1548× 1548×                   3960× 3960× 3960×    
'use strict';
 
exports.__esModule = true;
exports.h = h;
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
 
var _vnode = require('./vnode');
 
var _options = require('./options');
 
var _options2 = _interopRequireDefault(_options);
 
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 if (!_util.falsey(child)) {
						children.push(child);
						lastSimple = simple;
					}
				}
			}
		}
	} else if (attributes && attributes.children) {
		return h(nodeName, attributes, attributes.children);
	}
 
	if (attributes) {
		if (attributes.children) {
			delete 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 && !isString(lastSimple)) {
			// 	attributes.style = styleObjToCss(lastSimple);
			// }
		}
	}
 
	var p = new _vnode.VNode(nodeName, attributes || undefined, children);
	Eif (_options2['default'].vnode) _options2['default'].vnode(p);
	return p;
}