MCPcopy Index your code
hub / github.com/HelloZeroNet/ZeroNet / morphdom

Function morphdom

plugins/Sidebar/media/all.js:1375–1616  ·  view source on GitHub ↗
(fromNode, toNode, options)

Source from the content-addressed store, hash-verified

1373}
1374
1375function morphdom(fromNode, toNode, options) {
1376 if (!options) {
1377 options = {};
1378 }
1379
1380 if (typeof toNode === 'string') {
1381 var newBodyEl = document.createElement('body');
1382 newBodyEl.innerHTML = toNode;
1383 toNode = newBodyEl.childNodes[0];
1384 }
1385
1386 var savedEls = {}; // Used to save off DOM elements with IDs
1387 var unmatchedEls = {};
1388 var onNodeDiscarded = options.onNodeDiscarded || noop;
1389 var onBeforeMorphEl = options.onBeforeMorphEl || noop;
1390 var onBeforeMorphElChildren = options.onBeforeMorphElChildren || noop;
1391
1392 function removeNodeHelper(node, nestedInSavedEl) {
1393 var id = node.id;
1394 // If the node has an ID then save it off since we will want
1395 // to reuse it in case the target DOM tree has a DOM element
1396 // with the same ID
1397 if (id) {
1398 savedEls[id] = node;
1399 } else if (!nestedInSavedEl) {
1400 // If we are not nested in a saved element then we know that this node has been
1401 // completely discarded and will not exist in the final DOM.
1402 onNodeDiscarded(node);
1403 }
1404
1405 if (node.nodeType === 1) {
1406 var curChild = node.firstChild;
1407 while(curChild) {
1408 removeNodeHelper(curChild, nestedInSavedEl || id);
1409 curChild = curChild.nextSibling;
1410 }
1411 }
1412 }
1413
1414 function walkDiscardedChildNodes(node) {
1415 if (node.nodeType === 1) {
1416 var curChild = node.firstChild;
1417 while(curChild) {
1418
1419
1420 if (!curChild.id) {
1421 // We only want to handle nodes that don't have an ID to avoid double
1422 // walking the same saved element.
1423
1424 onNodeDiscarded(curChild);
1425
1426 // Walk recursively
1427 walkDiscardedChildNodes(curChild);
1428 }
1429
1430 curChild = curChild.nextSibling;
1431 }
1432 }

Callers 1

all.jsFile · 0.70

Calls 3

moveChildrenFunction · 0.70
morphElFunction · 0.70
walkDiscardedChildNodesFunction · 0.70

Tested by

no test coverage detected