Expand description
The escaping of document text for HTML: the three functions that rewrite the characters a browser would otherwise read as markup, one for text between tags and two for attribute values.
Escaping means writing a character as a character reference — the
spelling &name; or &#number; that HTML reads back as that one character
— so that the character reaches the reader as itself instead of being taken
for syntax. HTML needs it in two places, and they are not the same place:
the text between tags, where < would start a tag, and the value of an
attribute, where the quotation mark closing the value ends it.
The three escaping functions here are the whole of the HTML backend’s
escaping. escape_text_into is what
HtmlSink::text writes through,
escape_attr_into what
TagBuilder::attr writes through, and
escape_attr_single_quoted_into what
TagBuilder::attr_json writes through; an
emitter calls the sink and never these functions, which are public so that
a test or a host can check exactly what the sink does. All three are total:
every string has a result, nothing is refused, and none of them ever reads
output back (the sink explains why that matters).
§What is escaped, and what is not
In text, three characters become references — &, < and > — and
eight space characters are written as numeric references (below).
Nothing else is rewritten: the output is UTF-8 and every other character,
accented or not, is written as itself. A quotation mark needs no escaping
between tags, and is written as itself.
In an attribute value written inside double quotation marks, four
characters become references — &, <, > and " — always, regardless
of what the value holds and where it came from. This is how almost every
attribute of this backend is written, so escaping the quotation mark keeps a
value from ending its attribute early. &, < and > are escaped there as
well, so that the value is valid regardless of what it holds, and because
this library never reads written output back to find out whether a value
was escaped already.
In an attribute value written inside single quotation marks, two
characters become references — & and ' — and nothing else is rewritten:
a double quotation mark, a < and a > are written as themselves, which HTML
allows inside a quoted attribute value. This is how an attribute whose
value is JSON is written, so that the raw page shows the JSON as
JSON — data-flm-source-span='["book/ch1.flm",0,8]' — instead of a line of
". It escapes as unconditionally as the other two: text a document
wrote cannot leave the attribute through it.
A further function, json_string_into, is not one of the three: it is not
HTML escaping and is not public, being used by this backend’s emitter
alone. It writes a string the way JSON writes one, for the only attribute
whose value is a JSON value rather than a plain word: the attribute
data-flm-source-span, which records where an element’s node is located
in its source. The two escapings then apply one after the other: the value
is built as JSON, and the sink escapes that value for HTML with
escape_attr_single_quoted_into, so a browser returns the JSON
intact.
Escaping is the same inside <pre> and <code> as everywhere else. Those
elements change how a browser lays text out, not how it reads markup: a <
inside them still starts a tag.
§The eight spaces
Eight Unicode space characters are written as numeric character
references (SPACE_REFERENCES) — the spelling &#x followed by the
character’s number in hexadecimal — because a space character that is not
the ordinary one cannot be told from an ordinary space by looking at the
output: a reviewer comparing two pages, or reading a diff, would see
nothing.
The spelling is numeric rather than a name such as because a
numeric reference is read the same way wherever the output is embedded,
XML included, while those names are defined by HTML alone and an XML parser
refuses every one of them. A numeric reference shows in a comparison of two
outputs as clearly as a named one, and that is the purpose of the
rewriting.
Constants§
- SPACE_
REFERENCES - The eight space characters written as numeric character references, each with the reference written for it: the no-break space, the hair space, the thin space, the punctuation space, the en space, the em space, the figure space and the narrow no-break space.
Functions§
- escape_
attr_ into - Appends
valuetooutescaped as the value of an attribute written inside double quotation marks:&,<,>and"as their character references, every other character as itself — the eight spaces included, since an attribute value is not read by a person the way text is. - escape_
attr_ single_ quoted_ into - Appends
valuetooutescaped as the value of an attribute written inside single quotation marks:&as&and'as', every other character as itself — a double quotation mark, a<and a>included, which HTML allows inside a quoted attribute value. - escape_
text_ into - Appends
texttooutescaped as document text:&,<and>as their character references, the eight spaces ofSPACE_REFERENCESas their numeric references, every other character as itself.