You should keep the new line at the end:
echo str_replace('/>', '>', $element)."\n";
That will solve the space issue. Minifying HTML should only collapse all whitespace to one space character, not remove it entirely.
Edit: also the output_raw function needs changing too, and any blank lines could be removed.