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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 1× 23× 23× 1× 22143× 22143× 11× 22143× 35× 22108× 23× 22085× 22085× 90× 21995× 5× 5× 5× 22085× 22085× 10× 22085× 1× 22085× 22085× 7688× 14397× 9933× 22085× 1× 20863× 20863× 1× 3× 3× 1× 89× 1× 9933× 9933× 9888× 9888× 20866× 20866× 4× 4× 4× 20862× 9933× 9928× 20921× 20921× 1× 1× 20921× 20857× 20857× 20857× 20856× 20856× 20856× 20856× 20856× 20921× 20921× 65× 65× 65× 1× 64× 65× 9933× 4× 4× 4× 9933× 8× 1× 25× 18× 18× 18× 1× 24× 24× 24× 24× 7× 17× 16× 6× 6× 10× 11× 11× 7× 1× 22085× 22085× 22085× 14325× 20943× 20943× 88× 1× 22085× 22085× 20886× 6× | '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'); function buildTextNode(dom, str) { Iif (dom) { var type = _dom.getNodeType(dom); if (type === 3) { if (dom.nodeValue !== str) { dom.nodeValue = str; } return dom; } if (type === 1) _domRecycler.collectNode(dom); } return document.createTextNode(str); } /** 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) { 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)) { return buildTextNode(dom, vnode); } var out = dom, nodeName = vnode.nodeName || _constants.UNDEFINED_ELEMENT; 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.getChildren(dom))); // reclaim element nodes recollectNodeTree(dom); } diffNode(out, vnode, context); if (originalAttributes && originalAttributes.ref) { // (getNodeData(out).ref = originalAttributes.ref)(out); (out[_constants.ATTR_KEY].ref = originalAttributes.ref)(out); } return out; } // let types = window.types = []; /** Morph a DOM node to look like the given VNode. Creates DOM if it doesn't exist. */ function diffNode(dom, vnode, context) { // let key = [].slice.call(arguments).map( s => Object.prototype.toString.call(s) ).join(', '); // if (types.indexOf(key)===-1) types.push(key); 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); } // let vchildren = vnode.children, // children = getChildren(out); // if (vchildren && vchildren.length===1 && children.length===1 && typeof vchildren[0]==='string' && children[0] instanceof Text) { // children[0].nodeValue = vchildren[0]; // } // else if (vchildren || children.length) { // innerDiffNode(out, vnode, context, children); // } // let vchildren = vnode.children, // children = getChildren(out), // len = children.length; // if (vchildren && vchildren.length===1 && isString(vchildren[0]) && children.length===1 && children[0] instanceof Text) { // children[0].nodeValue = vchildren[0]; // } // else if (len || vchildren) { // innerDiffNode(out, vnode, context, children); // } // let vchildren = vnode.children, // children = getChildren(out), // len = children.length; // if (vchildren && vchildren.length===1 && isString(vchildren[0])) { // let fc = out.firstChild; // if (getNodeType(fc)===3) fc.nodeValue = vchildren[0]; // else out.textContent = vchildren[0]; // } // else if (len || vchildren) { // innerDiffNode(out, vnode, context, children); // } // let children = getChildren(out); // if (vnode.children || children) { // innerDiffNode(out, vnode, context, children); // } diffAttributes(dom, vnode); } function getKey(child) { // let component = getComponentForNode(child), // key; // if (component!==undefined) { // key = getKeyForComponent(component); // } // else { // key = getAccessor(child, 'key'); // } // let component = getComponentForNode(child), // key = component ? component.__key : getAccessor(child, 'key'); var data = _dom.getNodeData(child), key = data !== undefined && data.key; return _util.empty(key) ? '' : key; } function getKeyForComponent(component) { Iif (component.__key !== undefined && component.__key !== null) return String(component.__key); return ''; } function getComponentForNode(node) { // try { return node._component; } catch (err) {} if (node._component !== undefined && node._component !== null) return node._component; } /** Apply child and attribute changes between a VNode and a DOM Node to the DOM. */ function innerDiffNode(dom, vchildren, context) { var originalChildren = _dom.getChildren(dom), 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 = !_util.empty(child._component) ? getKeyForComponent(child._component) : getKey(child); if (!_util.empty(key) && key !== '') { Eif (!keyed) keyed = {}; 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.attributes.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--; Eif (j === min) min++; break; } } } // morph the matched/found/created DOM child to match vchild (deep) child = diff(child, vchild, context); if (originalChildren[i] !== child) { // let c = !child.parentNode && getComponentForNode(child), var c = child.parentNode !== dom && getComponentForNode(child), next = originalChildren[i + 1]; if (c) _hooks.deepHook(c, 'componentWillMount'); 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]; Eif (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; // let attrs = getNodeData(node); var attrs = node[_constants.ATTR_KEY]; if (attrs) _hooks.hook(attrs, 'ref', null); var component = getComponentForNode(node); 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.getNodeAttributes(dom), attrs = vnode.attributes; removeAttributes(dom, old, attrs || _constants.EMPTY); // new & updated if (attrs) { for (var _name in attrs) { // let value = attrs[name]; var value = attrs[_name] === undefined ? null : attrs[_name]; // if (value===undefined) value = null; // if (!empty(value) && value!=getAccessor(dom, name)) { // if (!empty(value) && value!=old[name]) { if (value != old[_name]) { _dom.setAccessor(dom, _name, value); } } } } function removeAttributes(dom, prev, next) { try { for (var _name2 in prev) { // if (empty(next[name])) { if (!next.hasOwnProperty(_name2)) { _dom.setAccessor(dom, _name2, null); } } } catch (e) {} } |