Lines
97.39 %
Functions
100 %
Branches
//! What a browser posts from rendered HTML: named, enabled controls outside
//! `<template>`, with textarea semantics. Shared by the renderer's unit tests
//! and the integration tests.
pub struct Control {
tag: String,
attrs: Vec<(String, String)>,
text: String,
}
impl Control {
pub fn attr(&self, name: &str) -> Option<&str> {
self.attrs
.iter()
.find(|(n, _)| n == name)
.map(|(_, v)| v.as_str())
pub fn value(&self) -> String {
if self.tag == "select" {
return selected(&self.text);
if self.tag == "textarea" {
return self
.text
.strip_prefix('\n')
.unwrap_or(&self.text)
.to_string();
self.attr("value").unwrap_or_default().to_string()
pub fn unescape(text: &str) -> String {
let mut out = String::new();
let mut rest = text;
while let Some(start) = rest.find('&') {
out.push_str(&rest[..start]);
rest = &rest[start..];
let end = rest.find(';').unwrap_or(1);
let decoded = match &rest[1..end] {
"amp" => Some('&'),
"lt" => Some('<'),
"gt" => Some('>'),
"quot" => Some('"'),
entity => entity
.strip_prefix('#')
.and_then(|n| n.parse().ok())
.and_then(char::from_u32),
};
let consumed = decoded.map_or(1, |_| end + 1);
out.push(decoded.unwrap_or('&'));
rest = &rest[consumed..];
out + rest
/// The value a browser posts for a select: its selected option, else its
/// first.
fn selected(options: &str) -> String {
let options: Vec<Vec<(String, String)>> = options
.split("<option")
.skip(1)
.map(|option| parse_attrs(option.split('>').next().unwrap_or_default()))
.collect();
options
.find(|attrs| attrs.iter().any(|(n, _)| n == "selected"))
.or(options.first())
.and_then(|attrs| attrs.iter().find(|(n, _)| n == "value"))
.map(|(_, v)| v.clone())
.unwrap_or_default()
fn parse_attrs(source: &str) -> Vec<(String, String)> {
let mut attrs = Vec::new();
let mut rest = source.trim_start();
while !rest.is_empty() {
let end = rest
.find(|c: char| c == '=' || c.is_whitespace())
.unwrap_or(rest.len());
let name = rest[..end].to_string();
rest = rest[end..].trim_start();
let mut value = String::new();
if let Some(after) = rest.strip_prefix('=') {
let body = after.trim_start().strip_prefix('"').unwrap_or(after);
let close = body.find('"').unwrap_or(body.len());
value = unescape(&body[..close]);
rest = &body[(close + 1).min(body.len())..];
attrs.push((name, value));
rest = rest.trim_start();
attrs
/// Named, enabled controls outside `<template>`, in document order: what a
/// browser would post.
pub fn controls(html: &str) -> Vec<Control> {
let mut found = all_controls(html);
found.retain(|c| c.attr("disabled").is_none());
found
/// Named controls outside `<template>`, disabled ones included.
pub fn all_controls(html: &str) -> Vec<Control> {
let mut found = Vec::new();
let mut rest = html;
let mut in_template = false;
while let Some(start) = rest.find('<') {
rest = &rest[start + 1..];
let end = rest.find('>').unwrap_or(rest.len());
let tag_src = &rest[..end];
let (tag, attr_src) = tag_src
.split_once(char::is_whitespace)
.unwrap_or((tag_src, ""));
match tag {
"template" => in_template = true,
"/template" => in_template = false,
"input" | "textarea" | "select" | "button" if !in_template => {
let close = match tag {
"select" => "</select",
_ => "</",
let text = rest[end + 1..]
.split_once(close)
.map(|(t, _)| unescape(t))
.unwrap_or_default();
let attrs = parse_attrs(attr_src);
if attrs.iter().any(|(n, _)| n == "name") {
found.push(Control {
tag: tag.into(),
attrs,
text,
});
_ => {}
rest = &rest[end..];
pub fn pairs(html: &str) -> Vec<(String, String)> {
controls(html)
.map(|c| (c.attr("name").unwrap_or_default().to_string(), c.value()))
.collect()