-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.rs
105 lines (88 loc) · 2.51 KB
/
dom.rs
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::selector::Index;
use scraper;
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
/// A list of `Dom`s in the same order as the `actions` in `ActionTrace`
///
/// Has exactly one more `Dom` than `Action` because of
/// the presence of an initial `Dom` before any action
/// is performed.
pub type Doms = Vec<Dom>;
/// A snapshot of the HTML document from the frontend
pub type Dom = scraper::Html;
/// Start index of the dom traces
/// End index is implicitly set to the length of doms vector
/// In the context of eval_selector(), DomTrace has a length of 1
/// and refers to the index of the dom trace in a vector of doms
#[derive(Clone, Copy, Debug)]
pub struct DomTrace {
pub n: Index,
pub number_of_doms: Index,
}
impl DomTrace {
pub fn new(n: Index, number_of_doms: Index) -> Self {
DomTrace { n, number_of_doms }
}
/// Check if the `DomTrace` is empty under `doms`
pub fn is_empty(&self) -> bool {
self.n >= self.number_of_doms
}
/// Check if the `DomTrace` is at the last dom under `doms`
pub fn is_end(&self) -> bool {
self.n == self.number_of_doms - 1
}
pub fn increment_number_of_doms(&self) -> Self {
DomTrace {
n: self.n,
number_of_doms: self.number_of_doms + 1,
}
}
pub fn increment_n_by(&self, m: Index) -> Self {
DomTrace {
n: self.n + m,
number_of_doms: self.number_of_doms,
}
}
pub fn increment_all(&self) -> Self {
DomTrace {
n: self.n + 1,
number_of_doms: self.number_of_doms + 1,
}
}
/// Advance the `DomTrace` to point to the next `Dom`
pub fn next(&self) -> Self {
DomTrace {
n: self.n + 1,
number_of_doms: self.number_of_doms,
}
}
}
impl PartialEq for DomTrace {
fn eq(&self, other: &Self) -> bool {
self.n == other.n
}
}
impl PartialOrd for DomTrace {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.n.partial_cmp(&other.n)
}
}
impl Eq for DomTrace {}
impl Ord for DomTrace {
fn cmp(&self, other: &Self) -> Ordering {
self.n.cmp(&other.n)
}
}
impl Hash for DomTrace {
fn hash<H: Hasher>(&self, state: &mut H) {
self.n.hash(state);
}
}
impl std::fmt::Display for DomTrace {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "d{}", self.n)
}
}
pub fn parse_html_to_dom(html: &str) -> Dom {
scraper::Html::parse_document(html)
}