tablo_test/protocol/
sections.rs1use super::dom::{attr_value, strip_text, tags_where};
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct Section {
9 pub heading: String,
11 pub collapsible: bool,
13 pub open: bool,
15 pub keys: Vec<String>,
17}
18
19pub fn sections(html: &str) -> Vec<Section> {
22 tags_where(html, |_, attrs| attr_value(attrs, "data-section").is_some())
23 .into_iter()
24 .map(|(tag, attrs, inner)| {
25 let heading = tags_where(&inner, |_, attrs| {
26 attr_value(attrs, "data-section-title").is_some()
27 })
28 .into_iter()
29 .next()
30 .map(|(_, _, heading)| strip_text(&heading))
31 .unwrap_or_default();
32 let mut keys: Vec<String> = Vec::new();
33 for (_, attrs, _) in tags_where(&inner, |_, attrs| attr_value(attrs, "name").is_some())
34 {
35 let key = attr_value(&attrs, "name").unwrap_or_default();
36 if !keys.contains(&key) {
37 keys.push(key);
38 }
39 }
40 let collapsible = tag == "details";
41 Section {
42 heading,
43 collapsible,
44 open: !collapsible || attrs.iter().any(|(name, _)| name == "open"),
45 keys,
46 }
47 })
48 .collect()
49}