I implemented a general isAfter function, which considers the depth and DOM tree and can correctly determine if a is after b even if they are in 2 different subtrees:
you can find it in my GitHub Repository. Would appreciate your feedback on the code