Understanding HTML Entities
HTML entities are special codes used to represent characters that have a reserved meaning in HTML or characters that are not easily typed from a keyboard. They ensure that these characters display correctly in a web page.
They start with an ampersand (&) and end with a semicolon (;).
Some represent reserved HTML characters, like <, >, &, which otherwise would be interpreted as HTML tags or syntax.
Others represent special symbols, accented letters, or Unicode characters.
< → < (less than)
> → > (greater than)
& → & (ampersand)
" → " (double quote)
' → ' (single quote)
→ non-breaking space
In short: HTML entities allow you to safely display reserved characters, special symbols, or accented letters in your web pages.
You're building a comment section where users can post text. A user submits 'I love <script>alert(1)</script> and AT&T'. What happens if you render this directly into innerHTML, and how would you fix it using HTML entities?
A designer sends you copy containing 'Price: $19.99 — 50% off!' with an em dash and special quotes. The page shows garbled characters. What entities would you use to display this correctly, and how would you verify the fix?
Your team migrates a legacy PHP template to a React app. The old code used htmlspecialchars() everywhere. In React you see dangerouslySetInnerHTML being used with user data. Why is this dangerous, and how would you refactor it to leverage React's built-in escaping?
A bug report: user-submitted emoji (🎉) renders as in the admin dashboard but works fine on the public site. Both use the same database. Walk me through how you'd debug whether this is an entity encoding issue, a charset mismatch, or something else.
You're designing a CMS that stores rich text from a WYSIWYG editor. The editor outputs HTML with entities already encoded (e.g., <b>bold</b>). Should you double-encode on save, decode on render, or store raw? What are the security and maintenance tradeoffs of each approach?
A high-traffic page renders 10k user comments server-side. Profiling shows HTML entity encoding takes 15% of response time. How would you optimize this without losing XSS protection? Consider caching, streaming, or client-side strategies.
Your org has 50+ micro-frontends owned by different teams, each with their own encoding utilities. A security audit finds inconsistent entity handling leading to stored XSS in three apps. How would you establish a cross-team encoding standard, enforce it, and handle legacy code that can't be rewritten immediately?
You're leading a migration from a 15-year-old ISO-8859-1 codebase to UTF-8. The database contains mixed entity representations: some characters stored as named entities (é), some as numeric (é), some as raw bytes. Design a migration strategy that guarantees no data corruption and zero downtime.