I had issue with text nodes, and it was showing wrong index. Here is version to fix it.
function getChildNodeIndex(elem)
{
let position = 0;
while ((elem = elem.previousSibling) != null)
{
if(elem.nodeType != Node.TEXT_NODE)
position++;
}
return position;
}