all files / src/dom/ index.js

96.97% Statements 64/66
91.38% Branches 53/58
100% Functions 9/9
98.18% Lines 54/55
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118    11× 11× 11× 11× 11× 11× 11×   11×   11×   11×   2942×     60× 46×                             1093× 1093×                       2791×   2791×   675× 667× 10× 657× 654× 32× 32× 622× 549×   549× 549× 73× 29× 44× 39×             32× 32×                         11×             21×     21× 21×    
'use strict';
 
exports.__esModule = true;
exports.ensureNodeData = ensureNodeData;
exports.getNodeType = getNodeType;
exports.appendChildren = appendChildren;
exports.removeNode = removeNode;
exports.setAccessor = setAccessor;
exports.getRawNodeAttributes = getRawNodeAttributes;
 
var _constants = require('../constants');
 
var _util = require('../util');
 
var _hooks = require('../hooks');
 
function ensureNodeData(node, data) {
	return node[_constants.ATTR_KEY] || (node[_constants.ATTR_KEY] = data || {});
}
 
function getNodeType(node) {
	if (node instanceof Text) return 3;
	Eif (node instanceof Element) return 1;
	return 0;
}
 
/** Append multiple children to a Node.
 *	Uses a Document Fragment to batch when appending 2 or more children
 *	@private
 */
 
function appendChildren(parent, children) {
	var len = children.length,
	    many = len > 2,
	    into = many ? document.createDocumentFragment() : parent;
	for (var i = 0; i < len; i++) {
		into.appendChild(children[i]);
	}Iif (many) parent.appendChild(into);
}
 
/** Removes a given DOM Node from its parent. */
 
function removeNode(node) {
	var p = node.parentNode;
	if (p) p.removeChild(node);
}
 
/** Set a named attribute on the given Node, with special behavior for some names and event handlers.
 *	If `value` is `null`, the attribute/handler will be removed.
 *	@param {Element} node	An element to mutate
 *	@param {string} name	The name/key to set, such as an event or attribute name
 *	@param {any} value		An attribute value, such as a function to be used as an event handler
 *	@param {any} previousValue	The last value that was set for this name/node pair
 *	@private
 */
 
function setAccessor(node, name, value) {
	ensureNodeData(node)[name] = value;
 
	if (name === 'key' || name === 'children') return;
 
	if (name === 'class') {
		node.className = value || '';
	} else if (name === 'style') {
		node.style.cssText = value || '';
	} else if (name === 'dangerouslySetInnerHTML') {
		if (value && value.__html) node.innerHTML = value.__html;
	} else if (name !== 'type' && name in node) {
		setProperty(node, name, _util.empty(value) ? '' : value);
		if (_util.falsey(value)) node.removeAttribute(name);
	} else if (name[0] === 'o' && name[1] === 'n') {
		var type = normalizeEventName(name),
		    l = node._listeners || (node._listeners = {});
		if (!l[type]) node.addEventListener(type, eventProxy);else if (!value) node.removeEventListener(type, eventProxy);
		l[type] = value;
	} else if (_util.falsey(value)) {
		node.removeAttribute(name);
	} else if (typeof value !== 'object' && !_util.isFunction(value)) {
		node.setAttribute(name, value);
	}
}
 
/** Attempt to set a DOM property to the given value.
 *	IE & FF throw for certain property-value combinations.
 */
function setProperty(node, name, value) {
	try {
		node[name] = value;
	} catch (e) {}
}
 
/** Proxy an event to hooked event handlers
 *	@private
 */
function eventProxy(e) {
	return this._listeners[normalizeEventName(e.type)](_hooks.optionsHook('event', e) || e);
}
 
/** Convert an Event name/type to lowercase and strip any "on*" prefix.
 *	@function
 *	@private
 */
var normalizeEventName = _util.memoize(function (t) {
	return _util.toLowerCase(t.replace(/^on/i, ''));
});
 
/** Get a node's attributes as a hashmap.
 *	@private
 */
 
function getRawNodeAttributes(node) {
	var list = node.attributes,
	    attrs = {},
	    i = list.length;
	while (i--) attrs[list[i].name] = list[i].value;
	return attrs;
}