Skip to main content

tablo_test/protocol/
list_tabs.rs

1//! Reads a list's tab bar: `[data-list-tab]` names each tab's key, `aria-current` marks the one
2//! the list shows, and `[data-list-tab-count]` holds a badged tab's count.
3
4use super::dom::{attr_value, tags_where};
5
6/// One tab of a list's tab bar.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct TabLink {
9    /// The tab's `?tab=` key.
10    pub key: String,
11    /// Whether the list shows this tab.
12    pub active: bool,
13    /// The rows the tab holds, when it shows a badge.
14    pub count: Option<u64>,
15    /// Where the tab links.
16    pub href: String,
17}
18
19/// The tabs of every list `html` renders, in document order.
20pub fn tab_links(html: &str) -> Vec<TabLink> {
21    tags_where(html, |tag, attrs| {
22        tag == "a" && attr_value(attrs, "data-list-tab").is_some()
23    })
24    .into_iter()
25    .map(|(_, attrs, inner)| TabLink {
26        key: attr_value(&attrs, "data-list-tab").unwrap_or_default(),
27        active: attr_value(&attrs, "aria-current").as_deref() == Some("page"),
28        count: tags_where(&inner, |_, attrs| {
29            attr_value(attrs, "data-list-tab-count").is_some()
30        })
31        .into_iter()
32        .next()
33        .and_then(|(_, attrs, _)| attr_value(&attrs, "data-list-tab-count")?.parse().ok()),
34        href: attr_value(&attrs, "href").unwrap_or_default(),
35    })
36    .collect()
37}