What HTML escaping changes in text and in attributes
HTML escaping replaces characters that the parser would treat as markup with character references, so the characters survive as data. The five that matter for ordinary text and attributes are ampersand, less-than, greater-than, double quote, and apostrophe. Less-than starts a tag, ampersand starts a character reference, and a quote can end an attribute value. The replacement is reversible text, not encryption and not percent-encoding.
The same code point can be written three ways. A named reference uses a word from the HTML named list, such as lt or amp. A decimal numeric reference uses &# and the code point in base 10. A hexadecimal numeric reference uses &#x and hex digits. Browsers accept a long named list. A decoder that only implements the markup set recognizes amp, lt, gt, quot, apos, and nbsp, plus numeric references. Anything else in that decoder needs the numeric form.
Escaping markup is not the same job as formatting a document or putting a character into a URL. Pretty-printing tags belongs in the HTML formatter. Bytes that must survive in a path or query use percent-encoding, covered by URL encode and decode.
How named references, numeric references, and double escaping relate
Why ampersand, less-than, and quotes cannot stay literal
- In element text, less-than opens a tag and ampersand opens a character reference. Leaving them literal makes the following characters part of markup, not content.
- In a double-quoted attribute, a raw double quote ends the value. In a single-quoted attribute, a raw apostrophe does the same. Greater-than is less dangerous in text but is still escaped so the string is safe in either context.
- The usual replacements are & for ampersand, < for less-than, > for greater-than, " for the double quote, and ' for the apostrophe. The ampersand must be replaced first, or the new references are escaped again by accident.
How a named reference differs from < and <
- A named reference is & plus a name from the HTML list plus a semicolon. lt and amp are names, not descriptions of the character.
- < is the decimal code point of less-than. < is the same code point in hex. Case of the x and the hex digits does not change the code point.
- Numeric references do not depend on a name table, so they can represent any scalar value. Named references only work when the consumer knows that name.
What a second escape does to an existing reference
- The text < already means less-than. Escaping it again turns the leading ampersand into &, so the characters on the page become &lt;.
- One decode of &lt; returns the four characters <. A second decode returns less-than. Seeing amp in front of another name is the HTML form of double encoding.
- The same pile-up happens with &amp; when an ampersand that was already escaped is escaped again. Count the amp prefixes before deciding the value is wrong.
Why a missing semicolon or an unknown name fails
- A character reference runs from the ampersand to the semicolon. Without the semicolon, a strict decoder cannot tell where the name ends and rejects the string.
- HTML parsers in browsers are more forgiving and may consume a known name even when the semicolon is missing. A strict decoder does not copy that forgiveness.
- An unknown name such as copy is not one of amp, lt, gt, quot, apos, or nbsp. The numeric form © still names that code point without a name table.
How a non-breaking space differs from a normal space and from URL encoding
- A normal space is U+0020 and usually stays a space in HTML text. A non-breaking space is U+00A0, and its named reference is nbsp.
- nbsp is not produced by escaping only the five markup characters. It shows up when the source contained U+00A0 or the named reference itself.
- Percent-encoding writes UTF-8 bytes as %HH for a URI. HTML references name a code point for a markup parser. < and %3C are not substitutes for each other.
Where an entity string is not the same as HTML source
Text that must be shown, including a tag-shaped sample
A sample such as a script tag is data if the reader should see the characters. Escaping less-than and greater-than keeps the sample from being parsed as an element. Escaping is the text-level step. It does not fix a document that is already broken, which is what the HTML formatter is for after the text is real markup again.
Attribute values that contain a quote or an ampersand
A title or href value that contains a double quote will end the attribute early if the attribute uses double quotes and the quote is not a reference. An ampersand in a query inside href is also the start of a character reference, so it has to be & in the HTML source even when the URL itself uses percent-encoding for a different reason. URL bytes still follow URL encode and decode.
A string that was escaped once and then escaped again in a template
Templates, logs, and JSON strings each escape once. If the value already contains < and a later layer escapes ampersands, the stored text is &lt;. Rendering that without a decode shows the entity, not the character. Decode until the markup characters are real, then escape once at the last boundary that writes HTML.