Why don't we always insert our attribute right after the tag name? Then we don't need to search ">" and no need to worry about ">" in attribute values. We also no need to have  the code below that checks for "/" in empty tags.

The suggested code is ``var insertIndex = tag.offset + tag.name + 1 + insertCount;``