Corrected logic calculating tag depth in html_encode()