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
//! The HTML renderer, transliterated from `reference/src/renderers/html.ts`.
//!
//! Forty-eight lines upstream, and the last stage before bytes. It takes a
//! renderable tree and writes markup: no schema, no config, no policy. Whether
//! a tag is allowed to exist was decided by the validator; what it is called
//! was decided by the transform. This layer only spells it.
//!
//! Upstream's one function is two things here. The **walk** owns the tree's
//! shape -- which nodes are visited, in what order, and which of them are
//! markup at all -- and is [`render_into`]. The **markup** is [`Html`], behind
//! the [`TagRenderer`] seam. [`render`] and [`render_all`] are the walk with
//! `Html` plugged in; [`render_with`] and [`render_all_with`] are the walk with
//! anything else.
//!
//! # The four early-outs, in upstream's order
//!
//! The order is load-bearing, because the checks overlap. All four belong to
//! the walk; only the escaping inside the first belongs to the markup, which
//! is the split the seam makes:
//!
//! 1. A string or a number is escaped and emitted. Nothing else is. The walk
//! picks them out: a string goes to [`TagRenderer::text`], which escapes,
//! and a number to [`TagRenderer::number`], whose default spells it as
//! ECMAScript does and passes that to `text`.
//! 2. An array is rendered element by element and concatenated. This is checked
//! *before* the tag check, so a [`Scalar::Array`] child renders its
//! elements: `[1, 2, 3]` as a child is `123`, while the same array as an
//! *attribute* is `1,2,3`, because an attribute goes through ECMAScript's
//! `String` and a child does not.
//! 3. Anything that is not a tag renders as the empty string. Upstream reaches
//! this with `null`, a boolean, an object, or any value failing
//! `Tag.isTag`; here it is the remaining [`Scalar`] variants. Silently, on
//! purpose: the renderer is not a validator, and a tree that got this far
//! has already been graded.
//! 4. A tag with **no name** renders its children with no wrapper. Upstream
//! writes `if (!name) return render(children)`, and the transform relies on
//! it -- an unnamed tag is how a schema says "these children, no element".
//! The walk takes this one too, so a renderer is never asked to open a tag
//! it has no name for.
//!
//! # What `Html` decides, and does not
//!
//! - **Attribute order is authored order.** `IndexMap`, never a hash map, so
//! two runs over one document produce identical bytes.
//! - **Attribute names are lowercased on output**, values are not. `colSpan`
//! becomes `colspan`; `Data` stays `Data`.
//! - **An attribute value is coerced, not rendered.** It holds a whole subtree,
//! because a rendered slot is stored there as its transformed nodes, and
//! upstream writes `String(v)` over it rather than recursing. A tag in an
//! attribute is therefore `[object Object]`, which is upstream's answer and
//! not a good one. [`attribute_value`] is that coercion.
//! - **The void-element list is the HTML standard's fourteen**, hard-coded
//! upstream and hard-coded here. See [`VOID_ELEMENTS`].
//! - **Escaping is markdown-it's**, exactly. See [`super::escape_html`].
use crate;
use escape_html_into;
use js;
use ;
/// The HTML elements that have no closing tag.
///
/// Upstream hard-codes this list from
/// [the HTML standard](https://html.spec.whatwg.org/#void-elements), and so
/// does this port. Substituting a crate's notion of void elements would make
/// the rendered output depend on that crate's reading of the spec and on its
/// release cadence; the list is fourteen strings and has not changed in years.
///
/// Matched against the tag name **as authored**. Only attribute names are
/// lowercased, so a tag named `HR` is not void here, exactly as upstream.
pub const VOID_ELEMENTS: = ;
/// Reports whether `name` is one of the [`VOID_ELEMENTS`].
/// ECMAScript `String(v)` over an attribute value: what upstream writes
/// between the quotes.
///
/// An attribute holds a whole subtree rather than a scalar, because a rendered
/// slot is stored in the attribute map as its transformed nodes, and upstream
/// coerces whatever it finds there instead of rendering it. The rules are
/// ECMAScript's, not a formatting choice -- `[1, 2, 3]` is `1,2,3`, a `null`
/// element of an array contributes nothing, a number outside `1e-6..1e21`
/// switches to exponent notation, and a tag is `[object Object]` -- and the
/// conformance corpus grades on them.
///
/// Public so that a [`TagRenderer`] that writes attributes differently can
/// still write their values the way upstream does. The result is not escaped:
/// escaping is the renderer's policy, and [`Html`] applies
/// [`escape_html_into`](super::escape_html_into) to it afterwards.
///
/// # Examples
///
/// ```
/// use accent_proust::render::attribute_value;
/// use accent_proust::renderable::{RenderableTreeNode, RenderableTreeNodes, Scalar};
///
/// let list = Scalar::Array(vec![Scalar::Number(1.0), Scalar::Null, Scalar::Number(3.0)]);
/// let value = RenderableTreeNodes::One(RenderableTreeNode::Scalar(list));
/// assert_eq!(attribute_value(&value), "1,,3");
/// ```
/// Upstream's HTML renderer, as a [`TagRenderer`].
///
/// The default for [`render`] and [`render_all`], and the reference for what a
/// different implementation is departing from. It makes exactly upstream's
/// choices: attribute names lowercased, values coerced with
/// [`attribute_value`] and escaped, the HTML standard's [`VOID_ELEMENTS`]
/// closed by nothing, and text escaped with markdown-it's four replacements.
///
/// A unit struct because it holds no policy a caller could vary. A renderer
/// that does -- a different void list, say -- is its own type.
;
/// Render one node of the renderable tree to HTML.
///
/// Upstream's `render` takes `RenderableTreeNodes`, a TypeScript union of "one
/// node or an array of them". Rust has no such union without inventing a type
/// for it, and inventing one buys nothing: the two arms are two functions, and
/// which one you want is known at the call site. Use [`render_all`] for a
/// document's children.
///
/// This is [`render_with`] with [`Html`], byte for byte.
///
/// # Examples
///
/// ```
/// use indexmap::IndexMap;
/// use accent_proust::render::render;
/// use accent_proust::renderable::{RenderableTreeNode, Tag};
///
/// let heading = Tag::with("h1", IndexMap::new(), vec![RenderableTreeNode::text("test")]);
/// assert_eq!(render(&RenderableTreeNode::tag(heading)), "<h1>test</h1>");
/// ```
/// Render a sequence of nodes to HTML, concatenated with no separator.
///
/// This is upstream's `node.map(render).join('')` arm, and
/// [`render_all_with`] with [`Html`].
///
/// # Examples
///
/// ```
/// use indexmap::IndexMap;
/// use accent_proust::render::render_all;
/// use accent_proust::renderable::{RenderableTreeNode, Tag};
///
/// let paragraph = |text: &str| {
/// RenderableTreeNode::tag(Tag::with(
/// "p",
/// IndexMap::new(),
/// vec![RenderableTreeNode::text(text)],
/// ))
/// };
/// assert_eq!(
/// render_all(&[paragraph("foo"), paragraph("bar")]),
/// "<p>foo</p><p>bar</p>"
/// );
/// ```
/// Render one node through `renderer`.
///
/// [`render`] with the markup decided by the caller. `renderer` may be a
/// concrete type or a `dyn TagRenderer`; the walk is the same either way and
/// is this crate's, so the depth of the tree is never on the renderer's stack.
///
/// # Examples
///
/// ```
/// use indexmap::IndexMap;
/// use accent_proust::render::{Html, render, render_with};
/// use accent_proust::renderable::{RenderableTreeNode, Tag};
///
/// let heading = Tag::with("h1", IndexMap::new(), vec![RenderableTreeNode::text("test")]);
/// let heading = RenderableTreeNode::tag(heading);
/// assert_eq!(render_with(&heading, &Html), render(&heading));
/// ```
Sized>
/// Render a sequence of nodes through `renderer`, concatenated with no
/// separator.
///
/// [`render_all`] with the markup decided by the caller.
Sized>
/// One item of the renderer's work stack.
/// Render `nodes` into `out` through `renderer`.
///
/// Iterative, with an explicit stack, where upstream recurses. Nesting depth in
/// a renderable tree comes from the document that produced it, which is
/// attacker-controlled, and a stack overflow in Rust aborts the process rather
/// than raising something a caller could catch. That makes recursion here
/// incompatible with the crate's panic-freedom promise, for the same reason
/// `crate::ast::Node` and [`Tag`](crate::renderable::Tag) both carry a manual
/// iterative `Drop`.
///
/// The stack holds children in reverse so they pop in document order, with the
/// closing tag pushed underneath them.
///
/// The renderer is called once per step and never re-entered. That is the
/// property [`TagRenderer`] promises its implementations, and the reason the
/// trait has no "render the children" method: the only stack in play is this
/// one, and it is on the heap.
Sized>
/// Open `tag` through `renderer` and queue what follows it.
Sized>