We maintain a stack of open element nodes and non-node elements and use it to determine when to close them.