all files / src/vdom/ diff.js

96.85% Statements 123/127
94.64% Branches 106/112
100% Functions 7/7
96.67% Lines 116/120
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    11× 11× 11× 11×   11×   11×   11×   11×   11×   11×   11×   11×                 32601×   32601× 11×     32601× 2465×     30136× 31×         30×         30105×     30105× 125× 29980×         30105×   30105× 10×     30105×         30105×   30105× 10481× 19624× 13283×     30105×     26301× 26301×       13283×                 13283× 13214× 13214× 28182×   28182×   28181×         13283× 13277× 28273×               28273×               28273× 28174× 28175× 28175× 28172× 28172× 28172× 28172× 28172×           28273×   28273×   28273×   28273× 102× 102×   101×       28273×       13283×             13283×           6753× 11051× 11051× 11050×                     11056× 11056×   11056× 11056× 3237×   7819× 17×     12×     7814× 7814× 2698×           30105×       30105× 28470×         30105× 19530× 28552× 28552× 380×          
'use strict';
 
exports.__esModule = true;
exports['default'] = diff;
exports.removeOrphanedChildren = removeOrphanedChildren;
exports.recollectNodeTree = recollectNodeTree;
 
var _constants = require('../constants');
 
var _util = require('../util');
 
var _hooks = require('../hooks');
 
var _ = require('.');
 
var _functionalComponent = require('./functional-component');
 
var _component = require('./component');
 
var _dom = require('../dom');
 
var _domRecycler = require('../dom/recycler');
 
/** 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) {
	var originalAttributes = vnode.attributes;
 
	while (_functionalComponent.isFunctionalComponent(vnode)) {
		vnode = _functionalComponent.buildFunctionalComponent(vnode, context);
	}
 
	if (_util.isFunction(vnode.nodeName)) {
		return _component.buildComponentFromVNode(dom, vnode, context);
	}
 
	if (_util.isString(vnode)) {
		if (dom) {
			Eif (_dom.getNodeType(dom) === 3) {
				Eif (dom.nodeValue !== vnode) {
					dom.nodeValue = vnode;
				}
				return dom;
			}
			_domRecycler.collectNode(dom);
		}
		return document.createTextNode(vnode);
	}
 
	// return diffNode(dom, vnode, context);
	// }
	var out = dom,
	    nodeName = String(vnode.nodeName);
 
	if (!dom) {
		out = _domRecycler.createNode(nodeName);
	} else if (!_.isNamedNode(dom, nodeName)) {
		out = _domRecycler.createNode(nodeName);
		// move children into the replacement node
		_dom.appendChildren(out, _util.toArray(dom.childNodes));
		// reclaim element nodes
		recollectNodeTree(dom);
	}
 
	diffNode(out, vnode, context, mountAll);
 
	if (originalAttributes && originalAttributes.ref) {
		(out[_constants.ATTR_KEY].ref = originalAttributes.ref)(out);
	}
 
	return out;
}
 
/** Morph a DOM node to look like the given VNode. Creates DOM if it doesn't exist. */
function diffNode(dom, vnode, context, mountAll) {
 
	var vchildren = vnode.children,
	    firstChild = dom.firstChild;
	if (vchildren && vchildren.length === 1 && typeof vchildren[0] === 'string' && firstChild instanceof Text && dom.childNodes.length === 1) {
		firstChild.nodeValue = vchildren[0];
	} else if (vchildren || firstChild) {
		innerDiffNode(dom, vchildren, context, mountAll);
	}
 
	diffAttributes(dom, vnode);
}
 
function getKey(child) {
	var data = _dom.getNodeData(child);
	if (data && !_util.empty(data.key)) return data.key;
}
 
/** 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 = undefined,
	    keyed = undefined,
	    keyedLen = 0,
	    min = 0,
	    vlen = vchildren && vchildren.length,
	    len = originalChildren.length,
	    childrenLen = 0;
 
	if (len) {
		children = [];
		for (var i = 0; i < len; i++) {
			var child = originalChildren[i],
			    key = child._component ? child._component.__key : getKey(child);
			if (key || key === 0) {
				Eif (!keyed) keyed = _util.createObject();
				keyed[key] = child;
				keyedLen++;
			} else {
				children[childrenLen++] = child;
			}
		}
	}
 
	if (vlen) {
		for (var i = 0; i < vlen; i++) {
			var vchild = vchildren[i],
			    child = undefined;
 
			// if (isFunctionalComponent(vchild)) {
			// 	vchild = buildFunctionalComponent(vchild);
			// }
 
			// attempt to find a node based on key matching
			if (keyedLen !== 0 && vchild.attributes) {
				var key = vchild.key;
				Iif (!_util.empty(key) && _util.hasOwnProperty.call(keyed, key)) {
					child = keyed[key];
					keyed[key] = null;
					keyedLen--;
				}
			}
 
			// attempt to pluck a node of the same type from the existing children
			if (!child && min < childrenLen) {
				for (var j = min; j < childrenLen; j++) {
					var _c = children[j];
					if (_c && _.isSameNodeType(_c, vchild)) {
						child = _c;
						children[j] = null;
						if (j === childrenLen - 1) childrenLen--;
						if (j === min) min++;
						break;
					}
				}
			}
 
			// morph the matched/found/created DOM child to match vchild (deep)
			child = diff(child, vchild, context, mountAll);
 
			var c = (mountAll || child.parentNode !== dom) && child._component;
 
			if (c) _hooks.deepHook(c, 'componentWillMount');
 
			if (originalChildren[i] !== child) {
				var next = originalChildren[i + 1];
				if (next) {
					dom.insertBefore(child, next);
				} else {
					dom.appendChild(child);
				}
			}
 
			if (c) _hooks.deepHook(c, 'componentDidMount');
		}
	}
 
	if (keyedLen) {
		/*eslint guard-for-in:0*/
		for (var i in keyed) {
			Eif (_util.hasOwnProperty.call(keyed, i) && 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 attrs = node[_constants.ATTR_KEY];
	if (attrs) _hooks.hook(attrs, 'ref', null);
 
	var component = node._component;
	if (component) {
		_component.unmountComponent(component, !unmountOnly);
	} else {
		if (!unmountOnly) {
			if (_dom.getNodeType(node) !== 1) {
				_dom.removeNode(node);
				return;
			}
 
			_domRecycler.collectNode(node);
		}
 
		var c = node.childNodes;
		if (c && c.length) {
			removeOrphanedChildren(c, unmountOnly);
		}
	}
}
 
/** Apply differences in attributes from a VNode to the given DOM Node. */
function diffAttributes(dom, vnode) {
	var old = _dom.getNodeData(dom) || _dom.getRawNodeAttributes(dom),
	    attrs = vnode.attributes;
 
	// removeAttributes(dom, old, attrs || EMPTY);
	for (var _name in old) {
		if (!attrs || !_util.hasOwnProperty.call(attrs, _name)) {
			_dom.setAccessor(dom, _name, null);
		}
	}
 
	// new & updated
	if (attrs) {
		for (var _name2 in attrs) {
			var value = attrs[_name2] === undefined ? null : attrs[_name2];
			if (value != old[_name2]) {
				_dom.setAccessor(dom, _name2, value);
			}
		}
	}
}