Short answer: pick a direction, paste the text, and the result appears as you type. Use the minimal mode for HTML you are about to paste, and decode when a value from a database or an email shows up full of &.
Why double-escaping happens
A URL parameter, a JSON field and an HTML attribute are three different layers. Escape the same value twice — once for JSON and again for HTML — and & becomes&, which renders as literal &. Decoding once is correct; decoding until it stops changing is how text like AT&T turns into AT&T with a broken ampersand.
Where escaping is the wrong tool
- Inside a script or style block. HTML entities are not decoded there, so an escape changes the program rather than the text.
- Inside a URL. Percent-encoding is the correct layer; theURL encoder handles it.
- As an XSS defence on its own. Escaping must match the context it is inserted into;
javascript:in an href survives entity escaping.