use scraper::{ElementRef, Selector};
pub fn safe_parse_selector(selector_str: &str) -> Option<Selector> {
Selector::parse(selector_str).ok()
}
pub fn parse_first_selector(selectors: &str) -> Option<Selector> {
selectors
.split(',')
.find_map(|s| safe_parse_selector(s.trim()))
}
pub fn extract_clean_text_from_element(element: &ElementRef) -> String {
element.text().collect::<String>().trim().to_string()
}
pub fn escape_html_text(text: &str) -> String {
html_escape::encode_text(text).trim().to_string()
}
pub fn build_query_string<S: AsRef<str>>(params: &[(&str, S)]) -> String {
let estimated: usize = params
.iter()
.map(|(k, v)| k.len() + 1 + v.as_ref().len() * 3 + 1)
.sum();
params
.iter()
.enumerate()
.fold(String::with_capacity(estimated), |mut acc, (i, (k, v))| {
if i > 0 {
acc.push('&');
}
acc.push_str(&urlencoding::encode(k));
acc.push('=');
acc.push_str(&urlencoding::encode(v.as_ref()));
acc
})
}
#[cfg(test)]
mod tests {
use super::*;
use scraper::Html;
fn with_element<F, R>(html: &str, selector_str: &str, f: F) -> R
where
F: FnOnce(&ElementRef) -> R,
{
let fragment = Html::parse_fragment(html);
let selector = Selector::parse(selector_str).expect("selector must be valid");
let element = fragment
.select(&selector)
.next()
.expect("html must contain a matching element");
f(&element)
}
#[test]
fn test_safe_parse_selector_valid_simple() {
let result = safe_parse_selector("div");
assert!(result.is_some(), "a simple tag selector should parse");
}
#[test]
fn test_safe_parse_selector_valid_class() {
let result = safe_parse_selector(".container");
assert!(result.is_some(), "a class selector should parse");
}
#[test]
fn test_safe_parse_selector_valid_id() {
let result = safe_parse_selector("#main");
assert!(result.is_some(), "an id selector should parse");
}
#[test]
fn test_safe_parse_selector_valid_compound() {
let result = safe_parse_selector("div.container > p#intro");
assert!(result.is_some(), "a compound selector should parse");
}
#[test]
fn test_safe_parse_selector_valid_attribute() {
let result = safe_parse_selector("a[href]");
assert!(result.is_some(), "an attribute selector should parse");
}
#[test]
fn test_safe_parse_selector_invalid_empty() {
let result = safe_parse_selector("");
assert!(result.is_none(), "an empty selector should not parse");
}
#[test]
fn test_safe_parse_selector_invalid_combinator() {
let result = safe_parse_selector(">");
assert!(result.is_none(), "a bare combinator should not parse");
}
#[test]
fn test_safe_parse_selector_invalid_pseudo_only() {
let result = safe_parse_selector(":hover");
assert!(result.is_none(), "a bare pseudo-class should not parse");
}
#[test]
fn test_parse_first_selector_returns_first_valid() {
let result = parse_first_selector("div, span, p");
assert!(result.is_some(), "should return the first valid selector");
}
#[test]
fn test_parse_first_selector_skips_invalid_leading() {
let result = parse_first_selector(">, div");
assert!(
result.is_some(),
"should skip invalid entries and return the first valid one"
);
}
#[test]
fn test_parse_first_selector_all_invalid_returns_none() {
let result = parse_first_selector(">, :, +");
assert!(
result.is_none(),
"should return None when all entries are invalid"
);
}
#[test]
fn test_parse_first_selector_empty_string_returns_none() {
let result = parse_first_selector("");
assert!(result.is_none(), "an empty string should yield no selector");
}
#[test]
fn test_parse_first_selector_trims_whitespace() {
let result = parse_first_selector(" div , span");
assert!(
result.is_some(),
"whitespace around selectors should be trimmed"
);
}
#[test]
fn test_parse_first_selector_single_valid() {
let result = parse_first_selector(".item");
assert!(result.is_some(), "a single valid selector should parse");
}
#[test]
fn test_extract_clean_text_simple() {
let text = with_element("<div>Hello World</div>", "div", |element| {
extract_clean_text_from_element(element)
});
assert_eq!(text, "Hello World");
}
#[test]
fn test_extract_clean_text_trims_leading_trailing_whitespace() {
let text = with_element("<div> Hello </div>", "div", |element| {
extract_clean_text_from_element(element)
});
assert_eq!(text, "Hello");
}
#[test]
fn test_extract_clean_text_preserves_inner_whitespace() {
let text = with_element("<div>Hello\n World</div>", "div", |element| {
extract_clean_text_from_element(element)
});
assert_eq!(text, "Hello\n World");
}
#[test]
fn test_extract_clean_text_nested_elements() {
let text = with_element("<div>Hello <span>World</span></div>", "div", |element| {
extract_clean_text_from_element(element)
});
assert_eq!(text, "Hello World");
}
#[test]
fn test_extract_clean_text_empty_element() {
let text = with_element("<div></div>", "div", |element| {
extract_clean_text_from_element(element)
});
assert_eq!(text, "");
}
#[test]
fn test_extract_clean_text_only_whitespace() {
let text = with_element("<div> </div>", "div", |element| {
extract_clean_text_from_element(element)
});
assert_eq!(text, "");
}
#[test]
fn test_extract_clean_text_with_nested_whitespace() {
let text = with_element("<div> <span> Hello </span> </div>", "div", |element| {
extract_clean_text_from_element(element)
});
assert_eq!(text, "Hello");
}
#[test]
fn test_escape_html_text_plain_text_unchanged() {
assert_eq!(
escape_html_text("Rust Programming Language"),
"Rust Programming Language"
);
}
#[test]
fn test_escape_html_text_special_chars_encoded() {
let text = "<script>alert('xss')</script> & more";
let escaped = escape_html_text(text);
assert!(!escaped.contains('<'), "should not contain raw <");
assert!(!escaped.contains('>'), "should not contain raw >");
assert!(escaped.contains("<"), "should contain <");
assert!(escaped.contains(">"), "should contain >");
assert!(escaped.contains("&"), "should contain &");
}
#[test]
fn test_escape_html_text_empty_string() {
assert_eq!(escape_html_text(""), "");
}
#[test]
fn test_escape_html_text_trims_whitespace() {
assert_eq!(escape_html_text(" trimmed content "), "trimmed content");
}
#[test]
fn test_build_query_string_single_param() {
let params = vec![("q", "rust".to_string())];
assert_eq!(build_query_string(¶ms), "q=rust");
}
#[test]
fn test_build_query_string_multiple_params() {
let params = vec![
("q", "rust lang".to_string()),
("ie", "utf8".to_string()),
("num", "10".to_string()),
];
assert_eq!(build_query_string(¶ms), "q=rust%20lang&ie=utf8&num=10");
}
#[test]
fn test_build_query_string_empty_params() {
let params: Vec<(&str, String)> = vec![];
assert_eq!(build_query_string(¶ms), "");
}
#[test]
fn test_build_query_string_url_encodes_special_chars() {
let params = vec![("q", "a&b=c?d".to_string())];
let result = build_query_string(¶ms);
assert!(
!result.contains('&') || result.matches('&').count() == 0 || result.starts_with("q=a"),
"special chars should be encoded: {}",
result
);
assert_eq!(result, "q=a%26b%3Dc%3Fd");
}
#[test]
fn test_build_query_string_encodes_non_ascii() {
let params = vec![("wd", "中文搜索".to_string())];
let result = build_query_string(¶ms);
assert!(
result.starts_with("wd="),
"should start with key: {}",
result
);
assert!(
!result.contains('中') && !result.contains('文'),
"non-ASCII should be percent-encoded: {}",
result
);
}
}