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 trimmed_text = full_text.trim();
22		let text = if trimmed_text.is_empty() {
23			None
24		} else {
25			Some(trimmed_text.to_string())
26		};
27
28		let html_content = el.inner_html();
29		let trimmed_html = html_content.trim();
30		let inner_html = if trimmed_html.is_empty() {
31			None
32		} else {
33			Some(trimmed_html.to_string())
34		};
35
36		Elem {
37			tag,
38			attrs,
39			text,
40			inner_html,
41		}
42	}
43}