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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! The naming functions of the HTML contract: how a presentation element's
//! name, a role and a payload field become the class names, attribute names
//! and custom properties the output carries, and the checks a name passes
//! before it is written.
//!
//! The **HTML contract** is the set of class names and attributes a stylesheet
//! or a script may rely on in the HTML backend's output, stated as data in
//! [`CONVENTIONS`](super::CONVENTIONS). Its names are not written down one by
//! one: the four functions below derive them from the names the presentation
//! vocabulary already has, so that a class cannot drift from the element it
//! represents.
//!
//! - [`class_of_element`] — the class every node of a presentation element
//! carries: the element's name with every hyphen doubled and every dot turned
//! into one hyphen, so `flm.pres.block-heading` is written
//! `flm-pres-block--heading`.
//! - [`class_of_role`] — the second class, for the payload's **role**: the
//! dotted, feature-owned name saying what a container is
//! (`flm.heading.number`; see [decorations, roles and
//! kinds](crate::guide::concepts_overview#decorations-roles-and-kinds)),
//! rewritten the same way (`flm-heading-number`).
//! - [`data_attribute`] — the attribute one other payload field is written
//! under: [`DATA_PREFIX`] followed by the field's name with `_` and `.`
//! turned into hyphens, so the field `source_span` is written
//! `data-flm-source-span`.
//! - [`style_property`] — the custom property a payload's **length** field is
//! written as, in the element's `style` attribute: [`STYLE_PROPERTY_PREFIX`]
//! followed by the field's name turned by the same rule, so the field
//! `indent_left` is written `--flm-element-indent-left`.
//!
//! The [conventions table](super::conventions), not these functions, decides
//! which field becomes which attribute and which elements carry a role: these
//! functions only say how a name is written once the table has chosen it.
//!
//! [`is_valid_token`] is the check a role and a kind token pass before they
//! are written: the `class` attribute and `data-flm-kind` both hold a list of
//! tokens separated by whitespace, so a name holding whitespace would be read
//! as several of them.
//!
//! The two remaining functions, [`is_valid_tag_name`] and
//! [`is_valid_attribute_name`], are the check applied to a tag or attribute
//! name that comes from a [construct node](super::construct) (a node that a
//! lowering rule stages, that is, builds into the lowered tree, to write one
//! HTML tag of its own; see [`Lowerer`](crate::core::lowering::Lowerer)) or
//! from the render options. Such a name is chosen by the rule's author or the host, so it is
//! checked before it is written; the names this module derives pass the
//! check by construction.
/// The prefix of every attribute this backend writes for a payload field that
/// is not one of HTML's own attributes: `data-flm-`.
///
/// `data-*` is HTML's own space for attributes an application defines, so an
/// attribute written under this prefix is valid HTML and collides with nothing
/// a browser or another library defines. See [`data_attribute`].
pub const DATA_PREFIX: &str = "data-flm-";
/// The prefix of every custom property this backend writes in an element's
/// `style` attribute for a payload's length field: `--flm-element-`.
///
/// A custom property is a CSS variable: it sets nothing by itself, and a
/// stylesheet rule applies it (`padding-left:
/// var(--flm-element-indent-left, 0)`), so a page's own rules remain free to
/// apply it differently. See [`style_property`].
pub const STYLE_PROPERTY_PREFIX: &str = "--flm-element-";
/// A dotted name as one class name: every `-` becomes `--`, and every `.`
/// becomes `-`.
///
/// The two replacements happen together, in one pass over the characters, so
/// that a hyphen the name already held is doubled and a hyphen written for a
/// dot is not.
///
/// **A class produced here reads back to exactly one name**: reading the class
/// from left to right, a single hyphen was a dot and a double hyphen was a
/// hyphen, so `flm-pres-block--heading` can only have come from
/// `flm.pres.block-heading`. That property holds for a **well-formed dotted
/// name**, which is one whose segments — the parts between the dots — are
/// non-empty and neither begin nor end with a hyphen. A name that breaks the
/// condition can share its class with another one: `flm.pres.-x` and
/// `flm.pres-.x` are both written `flm-pres---x`, and `flm.pres..x` is written
/// `flm-pres--x`, the class of the well-formed name `flm.pres-x`. **Nothing
/// checks the condition**: the names of this library's own vocabulary and the
/// dotted names features declare satisfy it by the way dotted names are
/// written, and a check at every class would cost more than it is worth.
/// Returns the class a node of the presentation element `element` carries: the
/// element's name with every hyphen doubled and every dot turned into one
/// hyphen.
///
/// The doubling keeps the class readable backwards: a single hyphen in the
/// class represents a dot in the name and a double hyphen a hyphen, so
/// one class can come from one element name only. That holds for a
/// **well-formed dotted name** — one whose segments, the parts between the
/// dots, are non-empty and neither begin nor end with a hyphen — and **nothing
/// checks the condition**, neither here nor at the registry: a name that
/// breaks it can share its class with another one (`flm.pres.-x` and
/// `flm.pres-.x` are both written `flm-pres---x`; `flm.pres..x` is written
/// `flm-pres--x`, the class of `flm.pres-x`). A feature
/// author who writes names the way dotted names are written never breaks it.
/// [`class_of_role`] applies the same rewriting to a role.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::html::names::class_of_element;
/// use flm_core::stdlib::backends::pres;
///
/// assert_eq!(class_of_element(pres::BLOCK_HEADING), "flm-pres-block--heading");
/// assert_eq!(class_of_element(pres::PAR), "flm-pres-par");
/// assert_eq!(class_of_element(pres::DECO_RUNIN), "flm-pres-deco-runin");
/// ```
/// Returns the class a node carries for its payload's role: the role's name with every
/// hyphen doubled and every dot turned into one hyphen.
///
/// A **role** is a feature-owned dotted name on a presentation payload that
/// says what a container is (a theorem's heading, a quotation's attribution;
/// see [`payload`](crate::stdlib::backends::pres::payload)). Because the
/// feature that introduced it owns the name, two features cannot declare the
/// same role by accident. The class is the same rewriting [`class_of_element`]
/// applies, applied to the role's name.
///
/// A class reads back to exactly one role name when the name is well formed;
/// [`class_of_element`] states the condition, which nothing checks.
///
/// Because a stylesheet selects on these classes, **a role's name is a public
/// interface**: renaming a role changes the class, and every stylesheet written
/// against the old one stops matching. The same is true of a presentation
/// element's name.
///
/// A role is one class and no more, which holds as long as the role's name is
/// one token: a name holding whitespace would be read as several classes,
/// because that is how the `class` attribute separates them. Nothing in the
/// library's name type enforces that, so the emitter checks a role with
/// [`is_valid_token`] before it writes the class and refuses a name that fails
/// — see that function for what is refused and what the emitter does instead.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::html::names::class_of_role;
///
/// assert_eq!(class_of_role("flm.heading"), "flm-heading");
/// ```
/// Returns whether `name` may be written as **one token**: one class among the
/// classes of an element, or one token of the `data-flm-kind` attribute.
///
/// Both of those attributes hold a list of tokens separated by whitespace, so
/// a name holding whitespace would be read as several tokens, and an empty
/// name as none: a node whose role is `flm.a b` would carry the classes
/// `flm-a` and `b`, the second representing nothing. A name that passes this
/// check is one token wherever it is written.
///
/// Such a name comes from a feature author, not from a document: it is the
/// role or the kind a lowering rule put in a presentation node's payload, and
/// the library's name type accepts any text. The emitter therefore checks it
/// with this function, leaves out the class or the token that fails, and
/// reports it as `flm.html.invalid-name`
/// ([`InvalidName`](super::emit::InvalidName)); everything else about the node
/// is written as usual.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::html::names::is_valid_token;
///
/// assert!(is_valid_token("flm.heading"));
/// assert!(is_valid_token("enumerate"));
/// assert!(!is_valid_token(""));
/// assert!(!is_valid_token("flm.a b"));
/// ```
/// Returns the attribute name a payload field is written under: [`DATA_PREFIX`]
/// followed by `field` with every `_` and `.` turned into `-`.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::html::names::data_attribute;
///
/// assert_eq!(data_attribute("level"), "data-flm-level");
/// assert_eq!(data_attribute("source_span"), "data-flm-source-span");
/// ```
/// Returns the custom property a payload's length field is written as, in the
/// element's `style` attribute: [`STYLE_PROPERTY_PREFIX`] followed by `field`
/// turned into a name by the rule of [`data_attribute`], every `_` and `.`
/// turned into `-`.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::html::names::style_property;
///
/// assert_eq!(style_property("indent_left"), "--flm-element-indent-left");
/// assert_eq!(style_property("width"), "--flm-element-width");
/// ```
/// `prefix` followed by `field` with every `_` and `.` turned into `-`: the
/// only rule by which a payload field's name becomes a name on the page.
/// Returns whether `name` may be written as a tag name: an ASCII letter followed by
/// any number of ASCII letters, digits and `-`.
///
/// This is narrower than HTML's own rule, so that the check stays simple and
/// safe. It admits every element name HTML has and every custom element name
/// written in ASCII (which must hold a `-`), and it admits nothing that could
/// end the tag early or start something else (no space, no `/`, no `>`, no
/// `<`, nothing outside ASCII), so a name that passes is written unchanged and
/// the tag it opens is the tag that was asked for.
///
/// A name reaches this check from a [construct node](super::construct)'s
/// payload, which a lowering rule wrote, or from the tag a host maps a heading
/// kind to ([`HtmlRenderOptions::heading_tags`](super::HtmlRenderOptions::heading_tags));
/// the emitter checks it before opening the tag and reports a name that fails
/// instead of writing it.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::html::names::is_valid_tag_name;
///
/// assert!(is_valid_tag_name("div"));
/// assert!(is_valid_tag_name("tex-html"));
/// assert!(!is_valid_tag_name(""));
/// assert!(!is_valid_tag_name("2div"));
/// assert!(!is_valid_tag_name("div onclick=x"));
/// assert!(!is_valid_tag_name("div>"));
/// ```
/// Returns whether `name` may be written as an attribute name: an ASCII letter
/// followed by any number of ASCII letters, digits, `-`, `_`, `:` and `.`.
///
/// It is [`is_valid_tag_name`]'s set with the three characters attribute names
/// carry in practice — `data-flm-level`, `aria-label`, `xml:lang`,
/// `data.pinned` — and, like it, admits nothing that could end the attribute
/// or start another: no space, no `=`, no quotation mark, no `/`, no `>`.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::html::names::is_valid_attribute_name;
///
/// assert!(is_valid_attribute_name("data-flm-level"));
/// assert!(is_valid_attribute_name("xml:lang"));
/// assert!(!is_valid_attribute_name("href=\"x\" onclick"));
/// assert!(!is_valid_attribute_name("-hidden"));
/// ```