use super::escape;
pub fn form_tag(action: &str, method: &str, csrf_token: Option<&str>) -> String {
let upper = method.to_uppercase();
let (html_method, override_method) = match upper.as_str() {
"GET" => ("get", None),
"POST" => ("post", None),
other => ("post", Some(other.to_string())),
};
let mut html = format!(
"<form action=\"{}\" method=\"{}\">",
escape(action),
html_method
);
if let Some(m) = override_method {
html.push_str(&format!(
"<input type=\"hidden\" name=\"_method\" value=\"{m}\">"
));
}
if let Some(token) = csrf_token {
html.push_str(&format!(
"<input type=\"hidden\" name=\"authenticity_token\" value=\"{}\">",
escape(token)
));
}
html
}
pub fn form_end() -> &'static str {
"</form>"
}
pub fn text_field(name: &str, value: &str) -> String {
format!(
"<input type=\"text\" name=\"{}\" value=\"{}\">",
escape(name),
escape(value)
)
}
pub fn text_area(name: &str, value: &str) -> String {
format!(
"<textarea name=\"{}\">{}</textarea>",
escape(name),
escape(value)
)
}
pub fn hidden_field(name: &str, value: &str) -> String {
format!(
"<input type=\"hidden\" name=\"{}\" value=\"{}\">",
escape(name),
escape(value)
)
}
pub fn label(field: &str, text: &str) -> String {
format!("<label for=\"{}\">{}</label>", escape(field), escape(text))
}
pub fn submit(label: &str) -> String {
format!("<input type=\"submit\" value=\"{}\">", escape(label))
}