recursive flattening of the DOM tree