html_helpers/
elem.rs

1use serde::Serialize;
2use std::collections::HashMap;
3
4/// Represents a simplified HTML element, suitable for serialization.
5#[derive(Debug, Serialize)]
6pub struct Elem {
7	pub tag: String,
8	pub attrs: HashMap<String, String>,
9	pub text: Option<String>,
10	pub inner_html: Option<String>,
11}
12
13impl Elem {
14	/// Creates a new `Elem` from a `scraper::ElementRef`.
15	pub(crate) fn from_element_ref(el: scraper::ElementRef) -> Self {
16		let tag = el.value().name().to_string();
17
18		let attrs = el.value().attrs().map(|(k, v)| (k.to_string(), v.to_string())).collect();
19
20		let full_text = el.text().collect::<String>();
21		let text = if full_text.trim().is_empty() {
22			None
23		} else {
24			Some(full_text.to_string())
25		};
26
27		let html_content = el.inner_html();
28		let inner_html = if html_content.trim().is_empty() {
29			None
30		} else {
31			Some(html_content.to_string())
32		};
33
34		Elem {
35			tag,
36			attrs,
37			text,
38			inner_html,
39		}
40	}
41}