Fix for 4948: changing tags (based on master)

This is a followup to #4968 and a fix for #4948. With this change, tags can change from one tag to another, including void elements like `<img>`.

To @njx to finish the review