1
//! What a browser posts from rendered HTML: named, enabled controls outside
2
//! `<template>`, with textarea semantics. Shared by the renderer's unit tests
3
//! and the integration tests.
4

            
5
pub struct Control {
6
    tag: String,
7
    attrs: Vec<(String, String)>,
8
    text: String,
9
}
10

            
11
impl Control {
12
10173
    pub fn attr(&self, name: &str) -> Option<&str> {
13
10173
        self.attrs
14
10173
            .iter()
15
29412
            .find(|(n, _)| n == name)
16
7147
            .map(|(_, v)| v.as_str())
17
10173
    }
18

            
19
2896
    pub fn value(&self) -> String {
20
2896
        if self.tag == "select" {
21
158
            return selected(&self.text);
22
2738
        }
23
2738
        if self.tag == "textarea" {
24
340
            return self
25
340
                .text
26
340
                .strip_prefix('\n')
27
340
                .unwrap_or(&self.text)
28
340
                .to_string();
29
2398
        }
30
2398
        self.attr("value").unwrap_or_default().to_string()
31
2896
    }
32
}
33

            
34
30852
pub fn unescape(text: &str) -> String {
35
30852
    let mut out = String::new();
36
30852
    let mut rest = text;
37
50994
    while let Some(start) = rest.find('&') {
38
20142
        out.push_str(&rest[..start]);
39
20142
        rest = &rest[start..];
40
20142
        let end = rest.find(';').unwrap_or(1);
41
20142
        let decoded = match &rest[1..end] {
42
2
            "amp" => Some('&'),
43
            "lt" => Some('<'),
44
            "gt" => Some('>'),
45
            "quot" => Some('"'),
46
20140
            entity => entity
47
20140
                .strip_prefix('#')
48
20134
                .and_then(|n| n.parse().ok())
49
20140
                .and_then(char::from_u32),
50
        };
51
20142
        let consumed = decoded.map_or(1, |_| end + 1);
52
20142
        out.push(decoded.unwrap_or('&'));
53
20142
        rest = &rest[consumed..];
54
    }
55
30852
    out + rest
56
30852
}
57

            
58
/// The value a browser posts for a select: its selected option, else its
59
/// first.
60
158
fn selected(options: &str) -> String {
61
158
    let options: Vec<Vec<(String, String)>> = options
62
158
        .split("<option")
63
158
        .skip(1)
64
452
        .map(|option| parse_attrs(option.split('>').next().unwrap_or_default()))
65
        .collect();
66
158
    options
67
158
        .iter()
68
345
        .find(|attrs| attrs.iter().any(|(n, _)| n == "selected"))
69
158
        .or(options.first())
70
158
        .and_then(|attrs| attrs.iter().find(|(n, _)| n == "value"))
71
158
        .map(|(_, v)| v.clone())
72
        .unwrap_or_default()
73
158
}
74

            
75
7753
fn parse_attrs(source: &str) -> Vec<(String, String)> {
76
7753
    let mut attrs = Vec::new();
77
7753
    let mut rest = source.trim_start();
78
36368
    while !rest.is_empty() {
79
28615
        let end = rest
80
175120
            .find(|c: char| c == '=' || c.is_whitespace())
81
28615
            .unwrap_or(rest.len());
82
28615
        let name = rest[..end].to_string();
83
28615
        rest = rest[end..].trim_start();
84
28615
        let mut value = String::new();
85
28615
        if let Some(after) = rest.strip_prefix('=') {
86
23519
            let body = after.trim_start().strip_prefix('"').unwrap_or(after);
87
23519
            let close = body.find('"').unwrap_or(body.len());
88
23519
            value = unescape(&body[..close]);
89
23519
            rest = &body[(close + 1).min(body.len())..];
90
5096
        }
91
28615
        attrs.push((name, value));
92
28615
        rest = rest.trim_start();
93
    }
94
7753
    attrs
95
7753
}
96

            
97
/// Named, enabled controls outside `<template>`, in document order: what a
98
/// browser would post.
99
292
pub fn controls(html: &str) -> Vec<Control> {
100
292
    let mut found = all_controls(html);
101
3015
    found.retain(|c| c.attr("disabled").is_none());
102
292
    found
103
292
}
104

            
105
/// Named controls outside `<template>`, disabled ones included.
106
434
pub fn all_controls(html: &str) -> Vec<Control> {
107
434
    let mut found = Vec::new();
108
434
    let mut rest = html;
109
434
    let mut in_template = false;
110
56317
    while let Some(start) = rest.find('<') {
111
55883
        rest = &rest[start + 1..];
112
55883
        let end = rest.find('>').unwrap_or(rest.len());
113
55883
        let tag_src = &rest[..end];
114
55883
        let (tag, attr_src) = tag_src
115
55883
            .split_once(char::is_whitespace)
116
55883
            .unwrap_or((tag_src, ""));
117
55883
        match tag {
118
758
            "template" => in_template = true,
119
758
            "/template" => in_template = false,
120
2450
            "input" | "textarea" | "select" | "button" if !in_template => {
121
7301
                let close = match tag {
122
244
                    "select" => "</select",
123
7057
                    _ => "</",
124
                };
125
7301
                let text = rest[end + 1..]
126
7301
                    .split_once(close)
127
7293
                    .map(|(t, _)| unescape(t))
128
                    .unwrap_or_default();
129
7301
                let attrs = parse_attrs(attr_src);
130
14114
                if attrs.iter().any(|(n, _)| n == "name") {
131
5439
                    found.push(Control {
132
5439
                        tag: tag.into(),
133
5439
                        attrs,
134
5439
                        text,
135
5439
                    });
136
1862
                }
137
            }
138
47066
            _ => {}
139
        }
140
55883
        rest = &rest[end..];
141
    }
142
434
    found
143
434
}
144

            
145
286
pub fn pairs(html: &str) -> Vec<(String, String)> {
146
286
    controls(html)
147
286
        .iter()
148
2846
        .map(|c| (c.attr("name").unwrap_or_default().to_string(), c.value()))
149
        .collect()
150
286
}