correctly handle tag name replacement when replaced tags are not closed.

Added tests.