Last active
September 18, 2017 15:50
-
-
Save dmail/0dc7791b134e631136592afb15f8aacf to your computer and use it in GitHub Desktop.
Node iteration mess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function readNodeAt(rootNode, path) { | |
var i = 0; | |
var j = path.length; | |
var index; | |
var found = rootNode; | |
for (;i < j; i++) { | |
index = Number(path[i]); | |
found = found.childNodes[index]; | |
if (found === null) { | |
break; | |
} | |
} | |
return found; | |
} | |
function getNodePath(node, rootNode) { | |
var path = []; | |
var selfOrAncestorNode = node; | |
while (selfOrAncestorNode && selfOrAncestorNode !== rootNode) { | |
var parentNode = selfOrAncestorNode.parentNode; | |
var index = Array.prototype.indexOf.call(parentNode.childNodes, selfOrAncestorNode); | |
path.unshift(index); | |
selfOrAncestorNode = parentNode; | |
} | |
return path; | |
} | |
function findNodeClone(node, rootNode, clonedRootNode) { | |
return readNodeAt(clonedRootNode, getNodePath(node, rootNode)); | |
} | |
function traverseDepthFirst(node, fn) { | |
var firstChild = node.firstChild; | |
var childNode = firstChild; | |
while (childNode) { | |
fn(childNode); | |
traverseDepthFirst(childNode, fn); | |
childNode = childNode.nextSibling; | |
} | |
} | |
function next(node, rootNode, skipChild) { | |
var nextNode = null; | |
if (!skipChild) { | |
var firstChild = node.firstChild; | |
if (firstChild) { | |
nextNode = firstChild; | |
} | |
} | |
if (!nextNode) { | |
var nextSibling = node.nextSibling; | |
if (nextSibling) { | |
nextNode = nextSibling; | |
} | |
} | |
if (!nextNode) { | |
if (node !== rootNode) { | |
var parentNode = node.parentNode; | |
if (parentNode) { | |
if (parentNode !== rootNode) { | |
nextNode = next(parentNode, rootNode, true); | |
} | |
} | |
} | |
} | |
return nextNode; | |
} | |
function createNextIterator(rootNode) { | |
var current = rootNode; | |
return { | |
next: function(skipChild) { | |
current = next(current, rootNode, skipChild); | |
return current; | |
} | |
}; | |
} | |
function getDeepestChild(node) { | |
var deepestChild = node.lastChild; | |
while (deepestChild) { | |
var lastChild = deepestChild.lastChild; | |
if (lastChild) { | |
deepestChild = lastChild; | |
} else { | |
break; | |
} | |
} | |
return deepestChild; | |
} | |
function previous(node, rootNode) { | |
var previousNode = null; | |
if (node === rootNode) { | |
previousNode = getDeepestChild(node); | |
} else { | |
var previousSibling = node.previousSibling; | |
if (previousSibling) { | |
var deepestChild = getDeepestChild(previousSibling); | |
if (deepestChild) { | |
previousNode = deepestChild; | |
} else { | |
previousNode = previousSibling; | |
} | |
} else if (node !== rootNode) { | |
var parentNode = node.parentNode; | |
if (parentNode && parentNode !== rootNode) { | |
previousNode = parentNode; | |
} | |
} | |
} | |
return previousNode; | |
} | |
function createPreviousIterator(rootNode) { | |
var current = rootNode; | |
return { | |
next: function() { | |
current = previous(current, rootNode); | |
return current; | |
} | |
}; | |
} |
johansatge
commented
Sep 18, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment