Pasting text into HTML looks trivial until a stray < or & breaks your page or opens an XSS hole. Here are the entity and escaping mistakes I see most often.
1. Escaping in the wrong context
HTML text, HTML attributes, URLs, JavaScript strings and CSS each need different escaping. Escaping for HTML body does not make a value safe inside onclick="..." or a <script> block.
2. Double escaping
If your template engine already escapes, escaping again gives you &amp; on screen. Escape once, at output time, not when storing data.
3. Forgetting quotes in attributes
&, < and > are not enough inside attributes. You must also escape " (and ' if you use single quotes), or a value like " onmouseover="alert(1) escapes the attribute.
4. Named vs numeric entities
©, © and © render the same. Named entities are readable; numeric ones are universal. Always include the trailing semicolon.
5. Non-breaking spaces
is not a regular space: it does not collapse and breaks naive string comparisons (U+00A0). Normalize before comparing.
6. Using innerHTML when textContent is enough
element.textContent = userInput escapes for you. innerHTML does not.
Quick way to check
When I need to encode or decode a snippet while debugging, I use the free, browser-only HTML Entities tool. Nothing is uploaded, so it is safe to paste real data.
I'm building a collection of 550+ free client-side developer tools at webdevtoolbox.com. Feedback welcome.
Top comments (0)