Encoding the Same Text Twice Turns an Ampersand Into &
Published 11/25/2026 · 3 min read · Developer tools
Daniel Okonkwo — Front-end developer and tech writer at OneKitly
Web performance · File formats
Checked against 2 sources
Escaping is not idempotent, because the escape sequence itself contains a character that needs escaping. Run "Café & <crème>" through the encoder once and you get "Café & <crème>". Run the result through again and the ampersands of those escapes are themselves escaped, giving "Café &amp; &lt;crème&gt;", which a browser renders as the literal text "Café & <crème>". Nothing is corrupted and nothing throws an error — the page simply shows its own plumbing, which is why double encoding is usually found by a reader rather than by a test.
The first pass turns & into &. The second turns that & into & again, and the page displays the escape instead of the character.
The minimum depends on where the text lands
In ordinary text content, escaping the ampersand and the less-than sign is enough; a greater-than sign is harmless there. Inside a double-quoted attribute the double quote must go, inside a single-quoted attribute the apostrophe must, and inside a script or a style block HTML escaping does nothing useful at all because those elements have their own parsing rules. The safe mode escapes all five characters precisely so the same output can be dropped anywhere, at the cost of noisier source.
How double encoding gets into a system
Almost always because two layers each believe they are the one responsible. A form library escapes on the way in, the template engine escapes again on the way out, and the value stored in the database is already escaped so a later export carries the escapes into a spreadsheet. The rule that fixes it is to store text raw and escape once, at the moment of rendering, for the context being rendered into — the decode direction of this tool exists mostly to clean up databases where that rule was broken.
| Mode | What it escapes | Result fragment |
|---|---|---|
| Minimal | & and < only | <crème> |
| Safe | & < > " ' | l'unité |
| Numeric | everything above ASCII too | Café |
| Safe, applied twice | the escapes as well | &amp; |
Frequently asked questions
- Do accented characters need escaping?
- Not on a page declared as UTF-8, which is every page produced this century. Writing é as é makes the source larger and harder to search without changing what a reader sees, and it is only worth doing when the file must survive a pipeline that mangles bytes above 127 — an old email gateway, a legacy CMS field, a system whose charset you cannot control. The numeric mode exists for those, not for everyday markup.
- Is escaping enough to prevent injection?
- Only when it matches the context, which is the part that goes wrong. HTML escaping placed into a URL attribute, a JavaScript string or a CSS value protects nothing, because each of those has a different grammar and different dangerous characters. The safe answer is to let a template engine or framework escape automatically for the context it is inserting into, and to treat manual escaping as something you do to inspect a value here, not as the security boundary itself.
Articles you may find interesting
All guides →Related tools
Sources
Spotted a mistake in this article?