1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//! 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`](super::HtmlSink::text) writes through,
//! [`escape_attr_into`] what
//! [`TagBuilder::attr`](super::TagBuilder::attr) writes through, and
//! [`escape_attr_single_quoted_into`] what
//! [`TagBuilder::attr_json`](super::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](super::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.
/// 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.
///
/// The table is public so that a test or a host can refer to the same eight
/// characters without repeating them. It is used by [`escape_text_into`]
/// alone: an attribute value is not rewritten this way, since a reader does
/// not compare the spaces inside one.
pub const SPACE_REFERENCES: = ;
/// Returns the numeric character reference of one of the eight spaces
/// ([`SPACE_REFERENCES`]), or `None` for every other character.
/// Appends `text` to `out` escaped as document text: `&`, `<` and `>` as their
/// character references, the eight spaces of [`SPACE_REFERENCES`] as their
/// numeric references, every other character as itself.
///
/// This is what [`HtmlSink::text`](super::HtmlSink::text) writes through; see
/// [the module documentation](self) for what is escaped where and why.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::html::escape::escape_text_into;
///
/// let mut out = String::new();
/// escape_text_into(&mut out, "a < b & \"c\"");
/// assert_eq!(out, "a < b & \"c\"");
///
/// // A string that already holds a character reference is escaped again: the
/// // text a document wrote is text, never markup.
/// let mut out = String::new();
/// escape_text_into(&mut out, "&");
/// assert_eq!(out, "&amp;");
///
/// // A no-break space, which nobody could see in the output otherwise.
/// let mut out = String::new();
/// escape_text_into(&mut out, "10\u{00A0}km");
/// assert_eq!(out, "10 km");
/// ```
/// Appends `value` to `out` escaped 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.
///
/// This is what [`TagBuilder::attr`](super::TagBuilder::attr) writes through;
/// see [the module documentation](self) for why the escaping is unconditional.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::html::escape::escape_attr_into;
///
/// let mut out = String::new();
/// escape_attr_into(&mut out, "a \"quoted\" <value>");
/// assert_eq!(out, "a "quoted" <value>");
///
/// // An address keeps every character it needs; only the four are rewritten.
/// let mut out = String::new();
/// escape_attr_into(&mut out, "https://example.org/p?a=1&b=2");
/// assert_eq!(out, "https://example.org/p?a=1&b=2");
/// ```
/// Appends `value` to `out` escaped 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.
///
/// This is what [`TagBuilder::attr_json`](super::TagBuilder::attr_json) writes
/// through, and it is meant for an attribute whose value is a JSON value: a
/// JSON value is full of double quotation marks, and writing it between single
/// quotation marks leaves them as themselves, so the raw page shows the value
/// as JSON. See [the module documentation](self) for what is escaped where.
///
/// Escaping the two characters keeps a value from ending its attribute early.
/// The escaping is unconditional: regardless of what the value holds and where
/// it came from, the result cannot leave the attribute.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::html::escape::escape_attr_single_quoted_into;
///
/// let mut out = String::new();
/// escape_attr_single_quoted_into(&mut out, "[\"book/ch1.flm\",0,8]");
/// assert_eq!(out, "[\"book/ch1.flm\",0,8]");
///
/// // The two characters that are rewritten, and only those.
/// let mut out = String::new();
/// escape_attr_single_quoted_into(&mut out, "it's <a & b>");
/// assert_eq!(out, "it's <a & b>");
/// ```
/// Appends `value` to `out` as JSON spells a string: the quotation marks
/// around it, a backslash before every quotation mark and every backslash
/// inside it, and every control character (below `U+0020`) as the short escape
/// JSON gives it or as `\u00XX`.
///
/// Every other character is written as itself, which is valid JSON: a JSON
/// document is Unicode text, and this library's output is UTF-8.
///
/// This is not HTML escaping and does not replace it. The result is JSON text;
/// written as an attribute value it goes through
/// [`escape_attr_single_quoted_into`] afterwards, which leaves its quotation
/// marks as themselves and writes the value between single quotation marks.
/// See the module documentation.
///
/// It is not public: the only attribute whose value is JSON is written by this
/// backend's emitter, and nothing outside the backend has a use for it.
pub