Simplify getNodeIndex
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Thu, 07 Jul 2011 14:09:34 -0600
changeset 370 4a50bcd41e11
parent 369 5434609f3f5d
child 371 e9b08040054c
Simplify getNodeIndex

This also happens to better work around the IE bug noted in the comment,
in that it still works in IE10PP2 while the old workaround broke. Of
course, it might break in other ways if IE has more bizarre DOM bugs,
but hopefully not in the form of an infinite loop this time.
implementation.js
--- a/implementation.js	Thu Jul 07 13:46:02 2011 -0600
+++ b/implementation.js	Thu Jul 07 14:09:34 2011 -0600
@@ -263,29 +263,10 @@
 //@{
 
 function getNodeIndex(node) {
-	if (!node.parentNode) {
-		// No preceding siblings, so . . .
-		return 0;
-	}
-
 	var ret = 0;
-	// These are no-ops to avoid a completely ridiculous bug in IE where
-	// sometimes a node is not actually equal to any of its parents' children.
-	// Somehow this makes it go away.  Sigh.
-	if (node.nextSibling) {
-		node = node.nextSibling.previousSibling;
-	} else if (node.previousSibling) {
-		node = node.previousSibling.nextSibling;
-	} else {
-		node = node.parentNode.firstChild;
-	}
-	while (ret < node.parentNode.childNodes.length && node != node.parentNode.childNodes[ret]) {
+	while (node.previousSibling) {
 		ret++;
-	}
-	if (ret >= node.parentNode.childNodes.length) {
-		// This actually happens in IE sometimes (although hopefully not with
-		// my workaround in place).
-		throw "node is not equal to any of its parents' children";
+		node = node.previousSibling;
 	}
 	return ret;
 }