all files / src/vdom/ diff.js

97.04% Statements 131/135
95.97% Branches 119/124
100% Functions 7/7
96.77% Lines 120/124
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262    12× 12× 12× 12× 12×   12×   12×   12×   12×   12×   12×   12×     12×   12×   12×   12× 12×   3485× 3485× 4570×                     7599× 7599× 7599× 7599× 7599×     65215×   65215× 24×     65215× 3209× 22×     14×   3201×     62006×       62006× 4115×   57891×       57891×   57891×   57891× 5079× 52812× 10×   10×   10×       57891× 17064× 40827× 28757×     57891×   57891× 12×     57891×   57891×       28757×                         28757× 21938× 46297×   46297×   46296×         28757× 28744× 57616× 57616×             57616×               57616× 46281× 46283× 46283× 46276× 46276× 46276× 46276× 46276×           57616×   57616× 11342×         28757×             28757× 18×           7241× 11269× 11269× 11268×                     11280× 11280× 3160×   8120×   8120× 8120× 3164× 3164×     4956×     4956× 3157×           57891×     57891× 54859× 44×         57891× 37574× 54917× 1035×          
'use strict';
 
exports.__esModule = true;
exports.flushMounts = flushMounts;
exports.diff = diff;
exports.removeOrphanedChildren = removeOrphanedChildren;
exports.recollectNodeTree = recollectNodeTree;
 
var _constants = require('../constants');
 
var _util = require('../util');
 
var _index = require('./index');
 
var _functionalComponent = require('./functional-component');
 
var _component = require('./component');
 
var _domIndex = require('../dom/index');
 
var _domRecycler = require('../dom/recycler');
 
/** Diff recursion count, used to track the end of the diff cycle. */
var mounts = [];
 
exports.mounts = mounts;
/** Diff recursion count, used to track the end of the diff cycle. */
var diffLevel = 0;
 
exports.diffLevel = diffLevel;
var isSvgMode = false;
 
function flushMounts() {
	var c = undefined;
	while (c = mounts.pop()) {
		if (c.componentDidMount) c.componentDidMount();
	}
}
 
/** Apply differences in a given vnode (and it's deep children) to a real DOM Node.
 *	@param {Element} [dom=null]		A DOM node to mutate into the shape of the `vnode`
 *	@param {VNode} vnode			A VNode (with descendants forming a tree) representing the desired DOM structure
 *	@returns {Element} dom			The created/mutated element
 *	@private
 */
 
function diff(dom, vnode, context, mountAll, unmountChildrenOnly, parent) {
	exports.diffLevel = diffLevel += 1;
	var ret = idiff(dom, vnode, context, mountAll, unmountChildrenOnly);
	if (parent && ret.parentNode !== parent) parent.appendChild(ret);
	if (!(exports.diffLevel = diffLevel -= 1)) flushMounts();
	return ret;
}
 
function idiff(dom, vnode, context, mountAll, unmountChildrenOnly) {
	var originalAttributes = vnode.attributes;
 
	while (_functionalComponent.isFunctionalComponent(vnode)) {
		vnode = _functionalComponent.buildFunctionalComponent(vnode, context);
	}
 
	if (_util.isString(vnode)) {
		if (dom) {
			if (_domIndex.getNodeType(dom) === 3) {
				if (dom.nodeValue !== vnode) {
					dom.nodeValue = vnode;
				}
				return dom;
			}
			if (!unmountChildrenOnly) _domRecycler.collectNode(dom);
		}
		return document.createTextNode(vnode);
	}
 
	var out = dom,
	    nodeName = vnode.nodeName,
	    svgMode = undefined;
 
	if (_util.isFunction(nodeName)) {
		return _component.buildComponentFromVNode(dom, vnode, context, mountAll);
	}
	Iif (!_util.isString(nodeName)) {
		nodeName = String(nodeName);
	}
 
	svgMode = _util.toLowerCase(nodeName) === 'svg';
 
	if (svgMode) isSvgMode = true;
 
	if (!dom) {
		out = _domRecycler.createNode(nodeName, isSvgMode);
	} else if (!_index.isNamedNode(dom, nodeName)) {
		out = _domRecycler.createNode(nodeName, isSvgMode);
		// move children into the replacement node
		while (dom.firstChild) out.appendChild(dom.firstChild);
		// reclaim element nodes
		if (!unmountChildrenOnly) recollectNodeTree(dom);
	}
 
	// fast-path for elements containing a single TextNode:
	if (vnode.children && vnode.children.length === 1 && typeof vnode.children[0] === 'string' && out.childNodes.length === 1 && out.firstChild instanceof Text) {
		out.firstChild.nodeValue = vnode.children[0];
	} else if (vnode.children || out.firstChild) {
		innerDiffNode(out, vnode.children, context, mountAll);
	}
 
	diffAttributes(out, vnode.attributes);
 
	if (originalAttributes && originalAttributes.ref) {
		(out[_constants.ATTR_KEY].ref = originalAttributes.ref)(out);
	}
 
	if (svgMode) isSvgMode = false;
 
	return out;
}
 
/** Apply child and attribute changes between a VNode and a DOM Node to the DOM. */
function innerDiffNode(dom, vchildren, context, mountAll) {
	var originalChildren = dom.childNodes,
	    children = [],
	    keyed = {},
	    keyedLen = 0,
	    min = 0,
	    len = originalChildren.length,
	    childrenLen = 0,
	    vlen = vchildren && vchildren.length,
	    j = undefined,
	    c = undefined,
	    vchild = undefined,
	    child = undefined;
 
	if (len) {
		for (var i = 0; i < len; i++) {
			var _child = originalChildren[i],
			    key = vlen ? (c = _child._component) ? c.__key : (c = _child[_constants.ATTR_KEY]) ? c.key : null : null;
			if (key || key === 0) {
				keyedLen++;
				keyed[key] = _child;
			} else {
				children[childrenLen++] = _child;
			}
		}
	}
 
	if (vlen) {
		for (var i = 0; i < vlen; i++) {
			vchild = vchildren[i];
			child = null;
 
			// if (isFunctionalComponent(vchild)) {
			// 	vchild = buildFunctionalComponent(vchild);
			// }
 
			// attempt to find a node based on key matching
			if (keyedLen && vchild.attributes) {
				var key = vchild.key;
				Iif (!_util.empty(key) && key in keyed) {
					child = keyed[key];
					keyed[key] = undefined;
					keyedLen--;
				}
			}
 
			// attempt to pluck a node of the same type from the existing children
			if (!child && min < childrenLen) {
				for (j = min; j < childrenLen; j++) {
					c = children[j];
					if (c && _index.isSameNodeType(c, vchild)) {
						child = c;
						children[j] = undefined;
						if (j === childrenLen - 1) childrenLen--;
						if (j === min) min++;
						break;
					}
				}
			}
 
			// morph the matched/found/created DOM child to match vchild (deep)
			child = idiff(child, vchild, context, mountAll);
 
			if (child !== originalChildren[i]) {
				dom.insertBefore(child, originalChildren[i] || null);
			}
		}
	}
 
	if (keyedLen) {
		/*eslint guard-for-in:0*/
		for (var i in keyed) {
			Eif (keyed[i]) {
				children[min = childrenLen++] = keyed[i];
			}
		}
	}
 
	// remove orphaned children
	if (min < childrenLen) {
		removeOrphanedChildren(children);
	}
}
 
/** Reclaim children that were unreferenced in the desired VTree */
 
function removeOrphanedChildren(children, unmountOnly) {
	for (var i = children.length; i--;) {
		var child = children[i];
		if (child) {
			recollectNodeTree(child, unmountOnly);
		}
	}
}
 
/** Reclaim an entire tree of nodes, starting at the root. */
 
function recollectNodeTree(node, unmountOnly) {
	// @TODO: Need to make a call on whether Preact should remove nodes not created by itself.
	// Currently it *does* remove them. Discussion: https://github.com/developit/preact/issues/39
	//if (!node[ATTR_KEY]) return;
 
	var component = node._component;
	if (component) {
		_component.unmountComponent(component, !unmountOnly);
	} else {
		if (node[_constants.ATTR_KEY] && node[_constants.ATTR_KEY].ref) node[_constants.ATTR_KEY].ref(null);
 
		Eif (!unmountOnly) {
			if (_domIndex.getNodeType(node) !== 1) {
				_domIndex.removeNode(node);
				return;
			}
 
			_domRecycler.collectNode(node);
		}
 
		if (node.childNodes.length) {
			removeOrphanedChildren(node.childNodes, unmountOnly);
		}
	}
}
 
/** Apply differences in attributes from a VNode to the given DOM Node. */
function diffAttributes(dom, attrs) {
	var old = dom[_constants.ATTR_KEY] || _domIndex.getRawNodeAttributes(dom);
 
	// removeAttributes(dom, old, attrs || EMPTY);
	for (var _name in old) {
		if (!attrs || !(_name in attrs)) {
			_domIndex.setAccessor(dom, _name, null, isSvgMode);
		}
	}
 
	// new & updated
	if (attrs) {
		for (var _name2 in attrs) {
			if (!(_name2 in old) || attrs[_name2] != (_name2 === 'value' || _name2 === 'selected' || _name2 === 'checked' ? dom[_name2] : old[_name2])) {
				_domIndex.setAccessor(dom, _name2, attrs[_name2], old[_name2], isSvgMode);
			}
		}
	}
}