
What safe HTML escaping actually means
A safe HTML escape encodes syntax characters for the exact context that will consume them and keeps the result as data rather than executable markup. This is a context rule, not a claim that one fixed transformation makes every HTML insertion safe.
In an HTML text node, a literal less-than sign (<) can start markup, while an ampersand (&) can start a character reference. Basic encoding maps the ampersand to &, less-than to <, greater-than to >, double quote to ", and apostrophe to '. The encoder handles the ampersand first, so a reference created during the same operation is not encoded a second time.
The important boundary is the receiving context. HTML syntax characters can have different meanings in a text node, an attribute value, a JavaScript string, a URL, CSS, SQL, or an HTTP header. A reference that is harmless in one of those locations can be invalid or unsafe in another.
The HTML Entity Encoder / Decoder performs both directions entirely in the browser. The page does not save history, fetch a remote table, or transmit the input. For a quick lookup of the five fixed encoding characters, the HTML escape cheat sheet for reserved characters provides a useful companion.
Choose the operation and encoding mode
Choose the direction before pasting content: encoding turns literal characters into HTML references, while decoding resolves references back into Unicode characters.
| Operation | Verified behavior | Best fit |
|---|---|---|
| Basic encoding | Replaces a fixed set of HTML syntax characters with references. | Readable HTML that retains ordinary ASCII text. |
| Non-ASCII encoding | Applies the same syntax protection and converts every code point above ASCII 126 to an uppercase hexadecimal numeric reference. | Legacy, transport, teaching, or comparison workflows that specifically require numeric references. |
| Decode references | Resolves current named, decimal, and hexadecimal references to Unicode characters. | Reading reference-encoded text or inspecting what a reference will produce. |
Basic mode deliberately leaves ordinary ASCII letters, numbers, spaces, tabs, and line breaks readable. Non-ASCII mode performs the same syntax protection and then handles each Unicode code point rather than each UTF-16 code unit. As a result, an emoji becomes one numeric reference rather than two invalid surrogate references.
Use basic mode when readable source matters. Choose non-ASCII mode only when a numeric-reference representation is genuinely required. Modern UTF-8 HTML can contain Unicode directly, and MDN’s character reference glossary recommends avoiding unnecessary references.
Decode mode is broader than the small table used by the encoder. It uses the browser’s current HTML parser, including the full named-reference table rather than only the five basic encode characters.
Run a checked HTML escape workflow
- Identify the destination. Confirm whether the output will become an HTML text node, a quoted attribute value, visible code, or part of a non-HTML context. For JavaScript, URLs, CSS, SQL, or HTTP headers, use a context-specific mechanism instead of generic HTML escaping.
- Select Encode characters. Start with basic mode for readable HTML. Choose non-ASCII mode only if the receiving workflow specifically requires numeric references for characters above ASCII 126.
- Paste a small representative sample. The input limit is 500,000 JavaScript characters, but starting with a short sample makes unexpected ampersands, angle brackets, and quote handling easier to inspect.
- Run the conversion. Select the conversion button and wait for the output to appear.
- Inspect the reserved characters. Check ampersands and angle brackets first, then confirm that quote characters and apostrophes have the expected fixed references. For non-ASCII mode, verify that the intended Unicode characters now have numeric references.
- Copy only after confirming the context. Place the result where the expected parser will treat it as data. If an application or framework provides output escaping, prefer that boundary rather than relying on a value prepared in an unrelated context.
Match the output to its receiving context
The same characters do not represent the same thing everywhere. Use the following comparison to identify what still needs checking before an escaped value is inserted.
| Destination | Required context check |
|---|---|
| Plain HTML text | Use text-node escaping so characters that could otherwise resemble tags or references remain visible text. |
| Quoted HTML attribute | Confirm the attribute delimiter and insertion method; a matching quote must not be able to terminate the value. |
| Visible code or examples | Encode the example as element text so its tags display literally rather than becoming active child elements. |
| JavaScript string | HTML references do not replace JavaScript-string escaping or protect the surrounding script context. |
| URL | Apply the URL component’s own encoding rules; HTML references do not encode URL delimiters or reserved characters. |
| CSS, SQL, or HTTP header | Use the mechanism required by that system. General HTML escaping does not satisfy these syntax or security requirements. |
Framework auto-escaping or a trusted templating engine should normally handle text at the final output boundary. The converter is not a substitute for framework auto-escaping, a trusted templating engine, a sanitizer, or Content Security Policy. A sanitizer addresses a different task: deciding which markup to allow or remove rather than turning a string of text into references.
Decode references without executing them
Select Decode references when the source already contains character references. The decoder asks the browser’s HTML parser to apply the complete current table, including named references, decimal references, hexadecimal references, legacy aliases, and references that map to more than one code point. The WHATWG HTML Living Standard’s named character reference table defines that behavior.
Decoded content is not inserted into the visible page as markup or executed as a script. The result is placed in a plain read-only text area. For example, decoding the references that spell a script tag reveals literal markup-looking characters, but the converter itself does not turn them into a script element.
Treat that decoded text as untrusted data. Copying it into an unsafe innerHTML sink could still create a vulnerability even though the decoder displayed it as plain text. Apply the correct escaping again at the final output boundary. Also note that the browser may preserve or normalize some legacy parsing details according to the HTML standard it implements.
Limits that affect encoding decisions
- Input size: Each operation accepts up to 500,000 JavaScript characters. A small test remains the safer first step even when the larger block fits.
- Non-ASCII conversion is optional: Converting every code point above ASCII 126 creates valid numeric references, but it can make ordinary modern UTF-8 content unnecessarily difficult to read.
- HTML and XML are different: XML has a much smaller predefined entity set and different parsing rules, so output intended for an XML parser should not be assumed to behave like this HTML-specific conversion.
- Decoding reflects browser parsing: Named-reference resolution is broad and current, but legacy parsing details can vary with the browser implementation.
- Local processing is not sanitization: The conversion stays in the browser and the input is not transmitted, but that processing model does not make an unsafe destination safe.
Verify the result before copying
- Confirm that the selected operation and mode match the intended workflow.
- Check that every ampersand begins a valid intended reference rather than an accidental or duplicated sequence.
- Inspect less-than and greater-than characters to confirm that markup-looking input remains text where required.
- Verify double quotes and apostrophes when they appear in attribute-related content.
- For non-ASCII mode, confirm that the changed characters were expected to become numeric references.
- Test the result in a small representative destination before processing and copying a larger block.
Once those checks pass, copy the output only into the verified HTML context. The goal is not merely to change recognizable symbols; it is to preserve the intended text while ensuring the receiving parser treats it according to that context’s rules.
For a deeper look, see Is HTML Entity Encoder / Decoder Safe to Use Online?.