Skip to main content

max / makeover-webview

11.1 KB · 321 lines History Blame Raw
1 //! A render of everything this crate can emit, scraped for class names.
2 //!
3 //! [`vocabulary::names`](crate::vocabulary::names) claims to hold every class
4 //! this crate can put in markup, and until 0.59.0 nothing checked the claim. It
5 //! was wrong by a whole family: `quasi-webview`'s own corpus guard found
6 //! `cell-fill`, `form-group` and `form-label` coming out in rendered documents
7 //! and had to carry them in a `MAKEOVER_UNLISTED` constant of its own, because
8 //! an app checking its stylesheet against our set alone concludes that its
9 //! rules for them are dead and deletes live styling.
10 //!
11 //! What that guard recorded is also why this is a render and not a scan of the
12 //! emitters: a careful read of `quasi-webview`'s emitters produced 35 names and
13 //! its corpus found 14 more. A class assembled at runtime -- a width class, a
14 //! drop class, a state appended to an attribute already open -- is a literal
15 //! nowhere in this source, and that is the shape of every name that was
16 //! missing here.
17 //!
18 //! # What it is not
19 //!
20 //! Not a rendering test. Nothing here asserts what an emitter produced, only
21 //! which classes came out, so it stays quiet when markup changes and speaks
22 //! when the vocabulary does.
23
24 use crate::form::{Filling, Markup, Value, field_html};
25 use crate::list::{Cell, cells_html};
26 use crate::{Emit, facet::facet_html, figure::figures_html};
27 use crate::{meter::meter_html, placeholder::placeholder_html};
28 use makeover_layout::{
29 Accepted, CellPart, Choice, Column, Facet, FacetValue, Field, FieldKind, Figure, Meter,
30 Priority, Readiness, Selecting, Sort, Standing, Tone, Width,
31 };
32 use std::collections::BTreeSet;
33
34 /// Every class the corpus puts in a document, unprefixed.
35 ///
36 /// Scraped from `class="..."` rather than predicted, which is the point.
37 pub(crate) fn emitted() -> BTreeSet<String> {
38 emitted_with(&Emit::default())
39 }
40
41 /// [`emitted`], with the emit options a host would set.
42 fn emitted_with(opts: &Emit) -> BTreeSet<String> {
43 let mut found = BTreeSet::new();
44 for html in documents(opts) {
45 let mut rest = html.as_str();
46 while let Some(at) = rest.find("class=\"") {
47 rest = &rest[at + "class=\"".len()..];
48 let end = rest.find('"').expect("an attribute closes");
49 for name in rest[..end].split_whitespace() {
50 found.insert(name.to_owned());
51 }
52 rest = &rest[end..];
53 }
54 }
55 found
56 }
57
58 /// One document per emitter, over every input that changes what it writes.
59 ///
60 /// Every markup emitter this crate has is called here. A new one that is not
61 /// added is the one hole this guard has, which is why the list is short enough
62 /// to read: `placeholder`, `form`, `figure`, `facet`, `meter` and `list` are
63 /// the whole of what emits markup, and `lib.rs` writes rules rather than
64 /// documents.
65 fn documents(opts: &Emit) -> Vec<String> {
66 let mut out = vec![placeholders(opts), fields(opts), rows(opts)];
67 out.push(figures_html(
68 &[
69 Figure::new("42", "Tasks"),
70 Figure::new("12.5%", "Growth")
71 .change("+3")
72 .tone(Tone::Success),
73 ],
74 opts,
75 ));
76 for mode in [
77 Selecting::OneOf,
78 Selecting::AnyOf,
79 Selecting::Range,
80 Selecting::Text,
81 Selecting::Subtree,
82 ] {
83 let values = [
84 FacetValue::new("music", "Music")
85 .standing(Standing::Taken)
86 .counted(128)
87 .at(0, true),
88 FacetValue::new("music/synths", "Synths")
89 .standing(Standing::Inherited)
90 .at(1, false),
91 FacetValue::new("music/drums", "Drums")
92 .standing(Standing::Pruned)
93 .at(1, false),
94 FacetValue::new("talk", "Talk").at(0, false),
95 ];
96 out.push(facet_html(&Facet::new("Tag", mode, &values), opts));
97 }
98 // Toned, untoned, and over its total, which is the one state that adds an
99 // attribute of its own.
100 for meter in [
101 Meter::new(3, 7),
102 Meter::new(3, 7).tone(Tone::Success).label("subtasks"),
103 Meter::new(9, 7).tone(Tone::Danger),
104 ] {
105 out.push(meter_html(&meter, opts));
106 }
107 out
108 }
109
110 /// Every readiness, with and without the action a stand-in can carry.
111 fn placeholders(opts: &Emit) -> String {
112 let mut html = String::new();
113 for state in [
114 Readiness::Ready,
115 Readiness::Pending,
116 Readiness::Empty,
117 Readiness::Failed,
118 ] {
119 html.push_str(&placeholder_html(state, "Nothing here", None, opts));
120 html.push_str(&placeholder_html(
121 state,
122 "Nothing here",
123 Some(Markup("<button>Add one</button>")),
124 opts,
125 ));
126 }
127 html
128 }
129
130 /// Every field kind, twice: plain, and carrying everything a group can hold.
131 ///
132 /// The second pass is where the vocabulary lives. A hint, a unit and an error
133 /// each add a class, and an error adds two -- one on the message and one on the
134 /// group, which is `Field::invalid`'s own reasoning about a renderer that
135 /// cannot find the group from the message.
136 fn fields(opts: &Emit) -> String {
137 const OPTIONS: &[Choice<'_>] = &[
138 Choice::new("a", "The first"),
139 Choice::new("b", "The second").unless("Not while the first is running"),
140 ];
141 const ACCEPT: &[Accepted<'_>] = &[Accepted::Type("image/png"), Accepted::Suffix(".zip")];
142
143 let mut html = String::new();
144 for kind in [
145 FieldKind::Text,
146 FieldKind::Secret,
147 FieldKind::Number,
148 FieldKind::Range,
149 FieldKind::Interval,
150 FieldKind::Email,
151 FieldKind::Url,
152 FieldKind::Tel,
153 FieldKind::Date,
154 FieldKind::DateTime,
155 FieldKind::Textarea,
156 FieldKind::Rich,
157 FieldKind::Select,
158 FieldKind::Radio,
159 FieldKind::Checkbox,
160 FieldKind::File,
161 FieldKind::Hidden,
162 ] {
163 let plain = Field {
164 options: OPTIONS,
165 accept: ACCEPT,
166 upper_name: Some("upper"),
167 min: Some("0"),
168 max: Some("10"),
169 ..Field::new(kind, "name", "Label")
170 };
171 let dressed = Field {
172 hint: Some("What it is for"),
173 error: Some("That will not do"),
174 unit: Some("minutes"),
175 required: true,
176 ..plain
177 };
178 for field in [plain, dressed] {
179 for value in [
180 Value::Absent,
181 Value::Text("a"),
182 Value::On(true),
183 Value::Between {
184 lower: "1",
185 upper: "9",
186 },
187 ] {
188 html.push_str(&field_html(&field, &Filling::of(value), opts));
189 }
190 }
191 }
192 html
193 }
194
195 /// A cell of every width and every priority, and every part a cell can be.
196 ///
197 /// The width and the drop are the classes no source literal carries: they are
198 /// chosen from the column and pushed, which is how `cell-fill` came to be
199 /// emitted by every table in the tree and named by nothing.
200 fn rows(opts: &Emit) -> String {
201 let mut columns = Vec::new();
202 for (index, width) in [Width::Content, Width::Fixed, Width::Fill]
203 .into_iter()
204 .enumerate()
205 {
206 for (rank, priority) in [Priority::Optional, Priority::Secondary, Priority::Essential]
207 .into_iter()
208 .enumerate()
209 {
210 columns.push(Column {
211 width,
212 priority,
213 sortable: true,
214 sorted: Some(if rank % 2 == 0 {
215 Sort::Ascending
216 } else {
217 Sort::Descending
218 }),
219 ..Column::new(NAMES[index * 3 + rank])
220 });
221 }
222 }
223
224 let parts = [
225 None,
226 Some(CellPart::Value),
227 Some(CellPart::Tokens),
228 Some(CellPart::Actions),
229 Some(CellPart::Link),
230 ];
231 let mut html = String::new();
232 for part in parts {
233 let cells: Vec<Cell<'_>> = columns
234 .iter()
235 .map(|column| Cell {
236 column: column.name,
237 part,
238 content: Markup("<span>x</span>"),
239 })
240 .collect();
241 html.push_str(&cells_html(&columns, &cells, opts));
242 }
243 html
244 }
245
246 /// A name per column, so the nine are nine columns rather than one repeated.
247 const NAMES: [&str; 9] = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
248
249 #[cfg(test)]
250 mod tests {
251 use super::*;
252
253 #[test]
254 fn every_class_this_crate_emits_is_one_its_vocabulary_names() {
255 // The guard `MAKEOVER_UNLISTED` in quasi-webview was standing in for.
256 // A name emitted and not written down shrinks the set an app checks
257 // against, so the app concludes its live rules are dead -- silently,
258 // and in the direction that deletes styling rather than keeping too
259 // much of it.
260 let opts = Emit::default();
261 let names = crate::vocabulary::names(&opts);
262 let emitted = emitted();
263 assert!(
264 emitted.len() > 30,
265 "the corpus rendered {} classes, which reads as the corpus having \
266 stopped calling the emitters rather than the crate having shrunk",
267 emitted.len()
268 );
269
270 let stranger: Vec<&String> = emitted
271 .iter()
272 // The one open family, and it is this crate's: a cell carries a
273 // class built from its column's name, which is app data and which
274 // no set can hold. See `list::push_column_class`.
275 .filter(|class| !class.starts_with("col-"))
276 .filter(|class| !names.contains(*class))
277 .collect();
278 assert!(
279 stranger.is_empty(),
280 "{} class(es) emitted that `vocabulary::names` does not hold. Give \
281 them a rule, or add them to the unruled list `names` reads:\n{}",
282 stranger.len(),
283 stranger
284 .iter()
285 .map(|c| format!(" .{c}"))
286 .collect::<Vec<_>>()
287 .join("\n")
288 );
289 }
290
291 #[test]
292 fn a_class_prefix_reaches_every_class_but_the_states() {
293 // The same guard with a prefix set, which is a different question: a
294 // name that reached markup without going through `class` is prefixed
295 // nowhere and would pass the check above, then fail in the one app that
296 // sets a prefix. `names` answers for both, because it prefixes the
297 // written-down half and leaves the states alone.
298 let opts = Emit {
299 class_prefix: "mk-",
300 ..Emit::default()
301 };
302 let names = crate::vocabulary::names(&opts);
303 let stranger: Vec<String> = emitted_with(&opts)
304 .into_iter()
305 .filter(|class| !class.starts_with("mk-col-"))
306 .filter(|class| !names.contains(class))
307 .collect();
308 assert!(
309 stranger.is_empty(),
310 "{} class(es) a prefixed render emits that `vocabulary::names` does \
311 not hold:\n{}",
312 stranger.len(),
313 stranger
314 .iter()
315 .map(|c| format!(" .{c}"))
316 .collect::<Vec<_>>()
317 .join("\n")
318 );
319 }
320 }
321