HTML Escape

HTML escaping turns &, <, >, a double quote, and an apostrophe into character references so a parser treats them as data instead of tags or the end of an attribute. Named forms such as lt and amp, decimal forms such as &#60;, and hex forms such as &#x3C; are the same code point written three ways. Escaping an existing reference again turns &lt; into &amp;lt;. A missing semicolon or a name outside amp, lt, gt, quot, apos, and nbsp is not the same as percent-encoding used in a URL.

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

  1. 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.
  2. 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.
  3. The usual replacements are &amp; for ampersand, &lt; for less-than, &gt; for greater-than, &quot; for the double quote, and &#39; for the apostrophe. The ampersand must be replaced first, or the new references are escaped again by accident.

How a named reference differs from &#60; and &#x3C;

  1. A named reference is & plus a name from the HTML list plus a semicolon. lt and amp are names, not descriptions of the character.
  2. &#60; is the decimal code point of less-than. &#x3C; is the same code point in hex. Case of the x and the hex digits does not change the code point.
  3. 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

  1. The text &lt; already means less-than. Escaping it again turns the leading ampersand into &amp;, so the characters on the page become &amp;lt;.
  2. One decode of &amp;lt; returns the four characters &lt;. A second decode returns less-than. Seeing amp in front of another name is the HTML form of double encoding.
  3. The same pile-up happens with &amp;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

  1. 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.
  2. 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.
  3. An unknown name such as copy is not one of amp, lt, gt, quot, apos, or nbsp. The numeric form &#169; still names that code point without a name table.

How a non-breaking space differs from a normal space and from URL encoding

  1. 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.
  2. 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.
  3. Percent-encoding writes UTF-8 bytes as %HH for a URI. HTML references name a code point for a markup parser. &lt; 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 &amp; 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 &lt; and a later layer escapes ampersands, the stored text is &amp;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.

Common questions

What is HTML escaping?
HTML escaping replaces characters that a parser would read as markup with character references, so those characters stay data. The usual set is ampersand, less-than, greater-than, double quote, and apostrophe, written as &amp;, &lt;, &gt;, &quot;, and &#39;. It does not hide the text. Anyone can turn the references back into characters.
What is the difference between &lt; and &amp;lt;?
&lt; is one reference and means the less-than character. &amp;lt; is that reference with its ampersand escaped again, so the text you see after one decode is the four characters ampersand, l, t, semicolon. A second decode yields less-than. Extra amp prefixes mean the string was escaped more than once.
Why does an ampersand have to be written as &amp;?
In HTML, an ampersand starts a character reference. A bare ampersand in text or in an attribute makes the following letters look like a name. Writing &amp; tells the parser that the data is an ampersand. If you escape the other characters first and the ampersand last, the ampersands you just inserted get escaped too, and you get a double-escaped string.
What is the difference between &#39; and apos?
Both can mean the apostrophe. &#39; is the decimal code point and does not need a name table. apos is a named reference. In attribute values, a numeric reference is the form that stays unambiguous when the surrounding quotes vary. Named apos is not the form produced by the common five-character escape, which uses &#39;.
Why does HTML entity decode fail when the semicolon is missing?
A reference is the ampersand, the name or number, and the semicolon that ends it. A strict decoder stops if the semicolon is absent, because it cannot know where the name ends. Browsers sometimes accept a known name without the semicolon. That leniency is not part of a strict decode, so the semicolon has to be restored before the reference will resolve.
Why will copy or other names not turn back into a character?
The HTML spec names hundreds of characters, and browsers know that list. A decoder limited to markup only accepts amp, lt, gt, quot, apos, and nbsp, plus decimal and hex numeric references. copy is outside that name set. The same code point is &#169; or &#xA9;, which does not need the name.
How is HTML escaping different from URL encoding?
HTML references tell a markup parser which code point to insert. Percent-encoding tells a URL parser which UTF-8 bytes were not safe as literal characters. Less-than is &lt; in HTML and %3C in a URI. Using one in place of the other leaves the wrong parser with the wrong spelling. Query and path bytes belong with URL encode and decode.
Does HTML escaping stop a script from running?
Escaping less-than in a text node stops that text from being parsed as a tag, which is why a visible sample of a script tag must be escaped. It is not a complete XSS policy. Attribute context, URL context, JavaScript strings, and CSS each have their own rules. Escaping the five markup characters is the text and attribute step, not a substitute for those other contexts.