Skip to main content

tablo_test/protocol/
controls.rs

1//! Reads a page's action buttons: whether one confirms before it posts, whether the bulk bar
2//! offers every matching record, and which custom actions show disabled with their reason.
3
4use super::dom::{Attrs, Element, attr_value, elements, open_tags, strip_text};
5
6/// A custom action whose button shows disabled, as the record's state refuses it.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct DisabledAction {
9    /// The action's `NAME`.
10    pub name: String,
11    /// Why the button is disabled, as the action's `disabled_reason` words it.
12    pub reason: String,
13}
14
15/// Whether the button posting to `action` asks first: a confirming trigger is a plain button that
16/// opens the dialog, a direct write is a submit button. `None` when no button posts there.
17pub 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
26/// The route the choice posting `field` fetches its options from, as the page names it for the
27/// browser: `{list}/-/options` on a record form, `{url}/-/actions/{name}/options` on an action's
28/// input. `None` when the choice fetches nothing: its options fit and it depends on no field, or
29/// no option search serves the page.
30pub 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
37/// The button posting to `url`, a custom action's route, on a row or a record page, as its
38/// element. A row's button posts itself; a record page's button sits in the form posting to `url`.
39fn 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
54/// The text of the button posting to `url`, its icon aside. `None` when no button posts there.
55pub fn action_label(html: &str, url: &str) -> Option<String> {
56    posting_button(html, url).map(|(_, _, inner)| strip_text(&inner))
57}
58
59/// The hint the button posting to `url` shows on hover. `None` when no button posts there or it
60/// shows none.
61pub fn action_tooltip(html: &str, url: &str) -> Option<String> {
62    posting_button(html, url).and_then(|(_, attrs, _)| attr_value(&attrs, "title"))
63}
64
65/// The count a header action's button shows as its badge, posting to `url`. `None` when no button
66/// posts there or it shows no badge.
67pub 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
75/// Whether the bulk bar offers to select every record the list's query selects, beyond its page.
76pub 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
82/// The custom actions `html` shows disabled, in document order: a row's, or a record page
83/// header's.
84pub 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
96/// The input keys the action submits in `html` post checked, in document order: one per button an
97/// action's [`submits`](tablo_core::Action::submits) declare, in each dialog or input page.
98pub 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}