tablo_test/protocol/
controls.rs1use super::dom::{Attrs, Element, attr_value, elements, open_tags, strip_text};
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct DisabledAction {
9 pub name: String,
11 pub reason: String,
13}
14
15pub fn confirms_first(html: &str, action: &str) -> Option<bool> {
18 open_tags(html)
19 .into_iter()
20 .find(|(tag, attrs, _)| {
21 tag == "button" && attr_value(attrs, "formaction").as_deref() == Some(action)
22 })
23 .map(|(_, attrs, _)| attr_value(&attrs, "type").as_deref() == Some("button"))
24}
25
26pub fn options_url(html: &str, field: &str) -> Option<String> {
31 open_tags(html)
32 .into_iter()
33 .find(|(_, attrs, _)| attr_value(attrs, "data-options-field").as_deref() == Some(field))
34 .and_then(|(_, attrs, _)| attr_value(&attrs, "data-options-url"))
35}
36
37fn posting_button(html: &str, url: &str) -> Option<Element> {
40 let posts_to = |attrs: &Attrs, name: &str| {
41 attr_value(attrs, name).is_some_and(|posted| posted.split('?').next() == Some(url))
42 };
43 elements(html, "button")
44 .into_iter()
45 .find(|(_, attrs, _)| posts_to(attrs, "formaction"))
46 .or_else(|| {
47 elements(html, "form")
48 .into_iter()
49 .find(|(_, attrs, _)| posts_to(attrs, "action"))
50 .and_then(|(_, _, inner)| elements(&inner, "button").into_iter().next())
51 })
52}
53
54pub fn action_label(html: &str, url: &str) -> Option<String> {
56 posting_button(html, url).map(|(_, _, inner)| strip_text(&inner))
57}
58
59pub fn action_tooltip(html: &str, url: &str) -> Option<String> {
62 posting_button(html, url).and_then(|(_, attrs, _)| attr_value(&attrs, "title"))
63}
64
65pub fn action_badge(html: &str, url: &str) -> Option<String> {
68 let (_, _, inner) = posting_button(html, url)?;
69 elements(&inner, "span")
70 .into_iter()
71 .find(|(_, attrs, _)| attr_value(attrs, "data-action-badge").is_some())
72 .map(|(_, _, count)| strip_text(&count))
73}
74
75pub fn selects_matching(html: &str) -> bool {
77 open_tags(html).iter().any(|(tag, attrs, _)| {
78 tag == "button" && attr_value(attrs, "data-select-matching").is_some()
79 })
80}
81
82pub fn disabled_actions(html: &str) -> Vec<DisabledAction> {
85 open_tags(html)
86 .into_iter()
87 .filter_map(|(_, attrs, _)| {
88 Some(DisabledAction {
89 name: attr_value(&attrs, "data-disabled-action")?,
90 reason: attr_value(&attrs, "title").unwrap_or_default(),
91 })
92 })
93 .collect()
94}
95
96pub fn action_submits(html: &str) -> Vec<String> {
99 open_tags(html)
100 .into_iter()
101 .filter(|(tag, _, _)| tag == "button")
102 .filter_map(|(_, attrs, _)| attr_value(&attrs, "data-submit"))
103 .collect()
104}