| 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 |
use makeover_layout::{Field, FieldKind}; |
| 43 |
use makeover_webview::{ |
| 44 |
Emit, |
| 45 |
form::{Filling, Value, field_html}, |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 54 |
pub enum Height { |
| 55 |
|
| 56 |
|
| 57 |
Compact, |
| 58 |
|
| 59 |
Standard, |
| 60 |
|
| 61 |
Tall, |
| 62 |
} |
| 63 |
|
| 64 |
impl Height { |
| 65 |
|
| 66 |
const fn class(self) -> &'static str { |
| 67 |
match self { |
| 68 |
Self::Compact => "rich-field rich-field--compact", |
| 69 |
Self::Standard => "rich-field rich-field--standard", |
| 70 |
Self::Tall => "rich-field rich-field--tall", |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#[must_use] |
| 85 |
pub fn html(prefix: &str, label: &str, placeholder: &str, value: &str, height: Height) -> String { |
| 86 |
named(Some(prefix), "body", label, placeholder, value, height) |
| 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 |
#[must_use] |
| 112 |
pub fn named( |
| 113 |
prefix: Option<&str>, |
| 114 |
name: &str, |
| 115 |
label: &str, |
| 116 |
placeholder: &str, |
| 117 |
value: &str, |
| 118 |
height: Height, |
| 119 |
) -> String { |
| 120 |
let mut field = Field::new(FieldKind::Rich, name, label); |
| 121 |
field.placeholder = Some(placeholder); |
| 122 |
|
| 123 |
let filling = Filling { |
| 124 |
value: Value::Text(value), |
| 125 |
trailing: None, |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
control_attrs: None, |
| 130 |
id_prefix: prefix, |
| 131 |
}; |
| 132 |
|
| 133 |
format!( |
| 134 |
"<div class=\"{}\">{}</div>", |
| 135 |
height.class(), |
| 136 |
field_html(&field, &filling, &Emit::default()) |
| 137 |
) |
| 138 |
} |
| 139 |
|
| 140 |
#[cfg(test)] |
| 141 |
mod tests { |
| 142 |
use super::Height; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
#[test] |
| 148 |
fn the_prefix_and_the_name_make_the_id_the_templates_already_had() { |
| 149 |
for prefix in ["text", "post", "new-psec", "edit-psec", "new-section"] { |
| 150 |
let html = super::html(prefix, "Body", "Write it", "", Height::Standard); |
| 151 |
assert!(html.contains(&format!("id=\"{prefix}-body\"")), "{html}"); |
| 152 |
assert!(html.contains(&format!("for=\"{prefix}-body\"")), "{html}"); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
#[test] |
| 160 |
fn the_name_is_the_value_and_never_the_surface() { |
| 161 |
let html = super::html("new-psec", "Body", "", "", Height::Standard); |
| 162 |
assert!(html.contains("name=\"body\""), "{html}"); |
| 163 |
assert!(!html.contains("name=\"new-psec-body\""), "{html}"); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
#[test] |
| 170 |
fn a_named_editor_keeps_the_id_and_gains_a_name_that_tells_it_apart() { |
| 171 |
for name in ["new-sec-body", "edit-sec-body"] { |
| 172 |
let html = super::named(None, name, "Body (Markdown)", "", "", Height::Compact); |
| 173 |
assert!(html.contains(&format!("id=\"{name}\"")), "{html}"); |
| 174 |
assert!(html.contains(&format!("name=\"{name}\"")), "{html}"); |
| 175 |
assert!(html.contains(&format!("for=\"{name}\"")), "{html}"); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
#[test] |
| 184 |
fn the_editor_chrome_is_there_for_the_binder() { |
| 185 |
let html = super::html("text", "Content", "", "", Height::Tall); |
| 186 |
assert!(html.contains(r#"<div data-format="markdown">"#), "{html}"); |
| 187 |
assert!(html.contains(r#"data-editor-mode="write""#), "{html}"); |
| 188 |
assert!(html.contains(r#"data-editor-mode="preview""#), "{html}"); |
| 189 |
assert!(html.contains("data-editor-preview"), "{html}"); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
#[test] |
| 196 |
fn the_value_arrives_escaped() { |
| 197 |
let html = super::html( |
| 198 |
"text", |
| 199 |
"Content", |
| 200 |
"", |
| 201 |
"</textarea><script>x</script>", |
| 202 |
Height::Tall, |
| 203 |
); |
| 204 |
assert!(!html.contains("<script>"), "{html}"); |
| 205 |
assert!(html.contains("</textarea>"), "{html}"); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
#[test] |
| 213 |
fn a_template_call_site_still_carries_the_id_its_script_reads() { |
| 214 |
use askama::Template as _; |
| 215 |
|
| 216 |
let html = crate::templates::WizardItemSectionsTemplate { |
| 217 |
nav: Vec::new(), |
| 218 |
project_slug: "a-project".into(), |
| 219 |
item_id: "an-item".into(), |
| 220 |
sections: Vec::new(), |
| 221 |
} |
| 222 |
.render() |
| 223 |
.expect("render the sections step"); |
| 224 |
|
| 225 |
assert!(html.contains(r#"id="new-section-body""#), "{html}"); |
| 226 |
assert!(html.contains("rich-field--compact"), "{html}"); |
| 227 |
assert!(html.contains("data-editor-preview"), "{html}"); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
#[test] |
| 233 |
fn every_height_is_its_own_class() { |
| 234 |
for (height, class) in [ |
| 235 |
(Height::Compact, "rich-field--compact"), |
| 236 |
(Height::Standard, "rich-field--standard"), |
| 237 |
(Height::Tall, "rich-field--tall"), |
| 238 |
] { |
| 239 |
let html = super::html("text", "Body", "", "", height); |
| 240 |
assert!(html.contains(class), "{html}"); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|