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
//! 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.
//!
//! # The four early-outs, in upstream's order
//!
//! The order is load-bearing, because the checks overlap:
//!
//! 1. A string or a number is escaped and emitted. Nothing else is.
//! 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".
//!
//! # What the renderer does not decide
//!
//! - **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.
//! - **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;
/// 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`].
/// 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.
///
/// # 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.
///
/// # 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>"
/// );
/// ```
/// One item of the renderer's work stack.
/// Render `nodes` into `out`.
///
/// 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.
/// Write a tag's opening markup and queue what follows it.