| 1 |
use std::{fmt, io}; |
| 2 |
|
| 3 |
use html5ever::tendril::StrTendril; |
| 4 |
use html5ever::{serialize, QualName}; |
| 5 |
|
| 6 |
use crate::document::Document; |
| 7 |
use crate::predicate::Predicate; |
| 8 |
use crate::selection::Selection; |
| 9 |
|
| 10 |
|
| 11 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 12 |
pub enum Data { |
| 13 |
Text(StrTendril), |
| 14 |
Element(QualName, Vec<(QualName, StrTendril)>), |
| 15 |
Comment(StrTendril), |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 21 |
pub struct Raw { |
| 22 |
pub index: usize, |
| 23 |
pub parent: Option<usize>, |
| 24 |
pub prev: Option<usize>, |
| 25 |
pub next: Option<usize>, |
| 26 |
pub first_child: Option<usize>, |
| 27 |
pub last_child: Option<usize>, |
| 28 |
pub data: Data, |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
#[derive(Copy, Clone, PartialEq, Eq)] |
| 33 |
pub struct Node<'a> { |
| 34 |
document: &'a Document, |
| 35 |
index: usize, |
| 36 |
} |
| 37 |
|
| 38 |
impl<'a> Node<'a> { |
| 39 |
|
| 40 |
pub fn new(document: &'a Document, index: usize) -> Option<Node<'a>> { |
| 41 |
if index < document.nodes.len() { |
| 42 |
Some(Node { document, index }) |
| 43 |
} else { |
| 44 |
None |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
pub fn index(&self) -> usize { |
| 50 |
self.index |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
pub fn raw(&self) -> &'a Raw { |
| 55 |
&self.document.nodes[self.index] |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
pub fn data(&self) -> &'a Data { |
| 60 |
&self.raw().data |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
pub fn name(&self) -> Option<&'a str> { |
| 65 |
match *self.data() { |
| 66 |
Data::Element(ref name, _) => Some(&name.local), |
| 67 |
_ => None, |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
pub fn attr(&self, name: &str) -> Option<&'a str> { |
| 73 |
match *self.data() { |
| 74 |
Data::Element(_, ref attrs) => attrs |
| 75 |
.iter() |
| 76 |
.find(|&&(ref name_, _)| name == &name_.local) |
| 77 |
.map(|&(_, ref value)| value.as_ref()), |
| 78 |
_ => None, |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
pub fn attrs(&self) -> impl Iterator<Item = (&'a str, &'a str)> { |
| 85 |
match *self.data() { |
| 86 |
Data::Element(_, ref attrs) => attrs, |
| 87 |
_ => [].as_ref(), |
| 88 |
} |
| 89 |
.iter() |
| 90 |
.map(|(name, value)| (name.local.as_ref(), value.as_ref())) |
| 91 |
} |
| 92 |
|
| 93 |
pub fn parent(&self) -> Option<Node<'a>> { |
| 94 |
self.raw() |
| 95 |
.parent |
| 96 |
.map(|index| self.document.nth(index).unwrap()) |
| 97 |
} |
| 98 |
|
| 99 |
pub fn prev(&self) -> Option<Node<'a>> { |
| 100 |
self.raw() |
| 101 |
.prev |
| 102 |
.map(|index| self.document.nth(index).unwrap()) |
| 103 |
} |
| 104 |
|
| 105 |
pub fn next(&self) -> Option<Node<'a>> { |
| 106 |
self.raw() |
| 107 |
.next |
| 108 |
.map(|index| self.document.nth(index).unwrap()) |
| 109 |
} |
| 110 |
|
| 111 |
pub fn first_child(&self) -> Option<Node<'a>> { |
| 112 |
self.raw() |
| 113 |
.first_child |
| 114 |
.map(|index| self.document.nth(index).unwrap()) |
| 115 |
} |
| 116 |
|
| 117 |
pub fn last_child(&self) -> Option<Node<'a>> { |
| 118 |
self.raw() |
| 119 |
.last_child |
| 120 |
.map(|index| self.document.nth(index).unwrap()) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
pub fn text(&self) -> String { |
| 125 |
let mut string = String::new(); |
| 126 |
recur(self, &mut string); |
| 127 |
return string; |
| 128 |
|
| 129 |
fn recur(node: &Node, string: &mut String) { |
| 130 |
if let Some(text) = node.as_text() { |
| 131 |
string.push_str(text); |
| 132 |
} |
| 133 |
for child in node.children() { |
| 134 |
recur(&child, string) |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
pub fn html(&self) -> String { |
| 141 |
let mut buf = Vec::new(); |
| 142 |
serialize::serialize(&mut buf, self, Default::default()).unwrap(); |
| 143 |
String::from_utf8(buf).unwrap() |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
pub fn inner_html(&self) -> String { |
| 148 |
let mut buf = Vec::new(); |
| 149 |
for child in self.children() { |
| 150 |
serialize::serialize(&mut buf, &child, Default::default()).unwrap(); |
| 151 |
} |
| 152 |
String::from_utf8(buf).unwrap() |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
pub fn find<P: Predicate>(&self, predicate: P) -> Find<'a, P> { |
| 157 |
Find { |
| 158 |
document: self.document, |
| 159 |
descendants: self.descendants(), |
| 160 |
predicate, |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
pub fn is<P: Predicate>(&self, p: P) -> bool { |
| 166 |
p.matches(self) |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
pub fn as_text(&self) -> Option<&'a str> { |
| 171 |
match *self.data() { |
| 172 |
Data::Text(ref text) => Some(text), |
| 173 |
_ => None, |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
pub fn as_comment(&self) -> Option<&'a str> { |
| 179 |
match *self.data() { |
| 180 |
Data::Comment(ref comment) => Some(comment), |
| 181 |
_ => None, |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
pub fn children(&self) -> Children<'a> { |
| 187 |
Children { |
| 188 |
document: self.document, |
| 189 |
next: self.first_child(), |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
pub fn descendants(&self) -> Descendants<'a> { |
| 195 |
Descendants { |
| 196 |
start: *self, |
| 197 |
current: *self, |
| 198 |
done: false, |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
impl<'a> fmt::Debug for Node<'a> { |
| 204 |
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 205 |
struct Attrs<'a>(&'a [(QualName, StrTendril)]); |
| 206 |
|
| 207 |
impl<'a> fmt::Debug for Attrs<'a> { |
| 208 |
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 209 |
self.0 |
| 210 |
.iter() |
| 211 |
.fold(f.debug_list(), |mut f, &(ref name, ref value)| { |
| 212 |
f.entry(&(&*name.local, &&**value)); |
| 213 |
f |
| 214 |
}) |
| 215 |
.finish() |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
struct Children<'a>(&'a Node<'a>); |
| 220 |
|
| 221 |
impl<'a> fmt::Debug for Children<'a> { |
| 222 |
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 223 |
f.debug_list().entries(self.0.children()).finish() |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
match *self.data() { |
| 228 |
Data::Text(ref text) => f.debug_tuple("Text").field(&&**text).finish(), |
| 229 |
Data::Element(ref name, ref attrs) => f |
| 230 |
.debug_struct("Element") |
| 231 |
.field("name", &&*name.local) |
| 232 |
.field("attrs", &Attrs(attrs)) |
| 233 |
.field("children", &Children(self)) |
| 234 |
.finish(), |
| 235 |
Data::Comment(ref comment) => f.debug_tuple("Comment").field(&&**comment).finish(), |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
impl<'a> serialize::Serialize for Node<'a> { |
| 241 |
fn serialize<S: serialize::Serializer>( |
| 242 |
&self, |
| 243 |
serializer: &mut S, |
| 244 |
traversal_scope: serialize::TraversalScope, |
| 245 |
) -> io::Result<()> { |
| 246 |
match *self.data() { |
| 247 |
Data::Text(ref text) => serializer.write_text(text), |
| 248 |
Data::Element(ref name, ref attrs) => { |
| 249 |
let attrs = attrs.iter().map(|&(ref name, ref value)| (name, &**value)); |
| 250 |
|
| 251 |
serializer.start_elem(name.clone(), attrs)?; |
| 252 |
|
| 253 |
for child in self.children() { |
| 254 |
serialize::Serialize::serialize(&child, serializer, traversal_scope.clone())?; |
| 255 |
} |
| 256 |
|
| 257 |
serializer.end_elem(name.clone())?; |
| 258 |
|
| 259 |
Ok(()) |
| 260 |
} |
| 261 |
Data::Comment(ref comment) => serializer.write_comment(comment), |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
#[derive(Clone, Debug)] |
| 267 |
pub struct Descendants<'a> { |
| 268 |
start: Node<'a>, |
| 269 |
current: Node<'a>, |
| 270 |
done: bool, |
| 271 |
} |
| 272 |
|
| 273 |
impl<'a> Iterator for Descendants<'a> { |
| 274 |
type Item = Node<'a>; |
| 275 |
|
| 276 |
fn next(&mut self) -> Option<Node<'a>> { |
| 277 |
if self.done { |
| 278 |
return None; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
if self.start.index() == self.current.index() { |
| 283 |
if let Some(first_child) = self.current.first_child() { |
| 284 |
self.current = first_child; |
| 285 |
} else { |
| 286 |
self.done = true; |
| 287 |
return None; |
| 288 |
} |
| 289 |
} else { |
| 290 |
|
| 291 |
if let Some(first_child) = self.current.first_child() { |
| 292 |
self.current = first_child; |
| 293 |
} else if let Some(next) = self.current.next() { |
| 294 |
self.current = next; |
| 295 |
} else { |
| 296 |
loop { |
| 297 |
|
| 298 |
let parent = self.current.parent().unwrap(); |
| 299 |
if parent.index() == self.start.index() { |
| 300 |
self.done = true; |
| 301 |
return None; |
| 302 |
} |
| 303 |
if let Some(next) = parent.next() { |
| 304 |
self.current = next; |
| 305 |
break; |
| 306 |
} |
| 307 |
self.current = parent; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
Some(self.current) |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
pub struct Find<'a, P: Predicate> { |
| 317 |
document: &'a Document, |
| 318 |
descendants: Descendants<'a>, |
| 319 |
predicate: P, |
| 320 |
} |
| 321 |
|
| 322 |
impl<'a, P: Predicate> std::fmt::Debug for Find<'a, P> { |
| 323 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 324 |
f.debug_struct("Find") |
| 325 |
.field("document", &self.document) |
| 326 |
.field("descendants", &self.descendants) |
| 327 |
|
| 328 |
.finish() |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
impl<'a, P: Predicate> Find<'a, P> { |
| 333 |
pub fn into_selection(self) -> Selection<'a> { |
| 334 |
Selection::new(self.document, self.map(|node| node.index()).collect()) |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
impl<'a, P: Predicate> Iterator for Find<'a, P> { |
| 339 |
type Item = Node<'a>; |
| 340 |
|
| 341 |
fn next(&mut self) -> Option<Node<'a>> { |
| 342 |
self.descendants.find(|&node| self.predicate.matches(&node)) |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
#[derive(Debug)] |
| 347 |
pub struct Children<'a> { |
| 348 |
document: &'a Document, |
| 349 |
next: Option<Node<'a>>, |
| 350 |
} |
| 351 |
|
| 352 |
impl<'a> Children<'a> { |
| 353 |
pub fn into_selection(self) -> Selection<'a> { |
| 354 |
Selection::new(self.document, self.map(|node| node.index()).collect()) |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
impl<'a> Iterator for Children<'a> { |
| 359 |
type Item = Node<'a>; |
| 360 |
|
| 361 |
fn next(&mut self) -> Option<Node<'a>> { |
| 362 |
if let Some(next) = self.next { |
| 363 |
self.next = next.next(); |
| 364 |
Some(next) |
| 365 |
} else { |
| 366 |
None |
| 367 |
} |
| 368 |
} |
| 369 |
} |