Applying tree traversal algorithms to DOM