aboutsummaryrefslogtreecommitdiffstats
path: root/tests/utils/mod.rs
blob: 64433fc56a82e7b5c88d17fe3018f7c3a956bac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#![allow(dead_code)]

use select::document::Document;
use select::node::Node;
use select::predicate::Name;
use select::predicate::Predicate;

/// Return the href attribute content for the closest anchor found by `text`.
pub fn get_link_from_text(document: &Document, text: &str) -> Option<String> {
    let a_elem = document
        .find(Name("a").and(|x: &Node| x.children().any(|x| x.text() == text)))
        .next()?;
    Some(a_elem.attr("href")?.to_string())
}

/// Return the href attributes of all links that start with the specified prefix `text`.
pub fn get_link_hrefs_from_text_with_prefix(document: &Document, text: &str) -> Vec<String> {
    let mut vec: Vec<String> = Vec::new();

    let a_elem = document.find(Name("a"));

    for element in a_elem {
        let str = element.attr("href").unwrap_or("");
        if str.to_string().starts_with(text) {
            vec.push(str.to_string());
        }
    }

    return vec;
}