class Sanitize
def traverse(node, &block)
Performs top-down traversal of the given node, operating first on the node
def traverse(node, &block) block.call(node) child = node.child while child do prev = child.previous_sibling traverse(child, &block) if child.parent != node # The child was unlinked or reparented, so traverse the previous node's # next sibling, or the parent's first child if there is no previous # node. child = prev ? prev.next_sibling : node.child else child = child.next_sibling end end end