scrapey 0.1.1

A basic library for tokenising HTML and generating a node tree with some basic utility functions
Documentation
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use std::{
    cell::RefCell,
    collections::HashMap,
    rc::{Rc, Weak},
};

use crate::{
    html_elements::HtmlElement,
    tokeniser::{TokenStream, TokenType},
};

type NodeRef = Rc<RefCell<Node>>;

#[derive(Clone, Debug, PartialEq)]
pub enum NodeType {
    Document,
    Element(HtmlElement),
    Text(String),
    Comment(String),
}

#[derive(Clone, Debug)]
pub struct Node {
    node_type: NodeType,
    _parent_element: Option<Weak<RefCell<Node>>>,
    children: Vec<NodeRef>,
    properties: HashMap<String, String>,
}

impl From<&Node> for String {
    fn from(node: &Node) -> Self {
        match &node.node_type {
            NodeType::Text(text) => text.clone(),
            NodeType::Element(element) => {
                if node.properties.is_empty() {
                    return format!("<{}>", element.tag_name());
                }
                let properties = node
                    .properties
                    .iter()
                    .map(|(k, v)| {
                        if v.is_empty() {
                            k.clone()
                        } else {
                            format!("{}=\"{}\"", k, v)
                        }
                    })
                    .collect::<Vec<String>>()
                    .join(" ");
                let string_repr = format!("<{} {}>", element.tag_name(), properties);
                string_repr
            }
            NodeType::Document => "Document".to_string(),
            NodeType::Comment(comment) => comment.to_string(),
        }
    }
}

impl Node {
    pub fn get_children(&self) -> Vec<NodeRef> {
        self.children.clone()
    }

    pub fn walk_tree(&self) {
        match &self.node_type {
            NodeType::Document => println!("Document"),
            NodeType::Element(element) => println!("Element: {:?}", element),
            NodeType::Text(text) => println!("Text: {}", text),
            NodeType::Comment(comment) => println!("Comment: {}", comment),
        }

        for child in &self.children {
            child.borrow().walk_tree();
        }
    }

    pub fn get_elements_by_class(&self, class: &str) -> Vec<Node> {
        let mut elements = Vec::new();
        if let NodeType::Element(_element) = &self.node_type
            && self.get_class_list().contains(&class.to_string())
        {
            elements.push(self.clone());
        }

        for child in &self.children {
            elements.extend(child.borrow().get_elements_by_class(class));
        }
        elements
    }

    pub fn outer_html(&self) -> String {
        let mut html = String::new();
        // Add opening tag
        html.push_str(String::from(self).as_str());
        // Add children recursively
        for child in &self.children {
            html.push_str(child.borrow().outer_html().as_str());
        }
        // Add closing tag if not void
        if let NodeType::Element(e) = &self.node_type {
            if e.is_void_element() {
                return html;
            }
            html.push_str("</");
            html.push_str(e.tag_name());
            html.push('>');
        }
        html
    }

    pub fn inner_html(&self) -> String {
        let mut html = String::new();
        // Add children recursively
        for child in &self.children {
            html.push_str(child.borrow().outer_html().as_str());
        }
        html
    }

    pub fn inner_text(&self) -> String {
        let mut inner_text = String::new();
        for child in &self.children {
            inner_text.push_str(child.borrow().inner_text().as_str());
        }

        if let NodeType::Text(value) = &self.node_type {
            return value.to_string();
        }

        inner_text
    }

    pub fn get_elements_by_tag(&self, tag: &HtmlElement) -> Vec<Node> {
        let mut elements = Vec::new();
        if let NodeType::Element(element) = &self.node_type
            && element == tag
        {
            elements.push(self.clone());
        }

        for child in &self.children {
            elements.extend(child.borrow().get_elements_by_tag(tag));
        }

        elements
    }

    pub fn get_class_list(&self) -> Vec<String> {
        let mut classes = vec![];
        if let Some(class_list) = self.properties.get("class") {
            classes = class_list
                .split_whitespace()
                .map(|v| v.to_string())
                .collect();
        }

        classes
    }

    pub fn get_element_by_id(&self, id: &str) -> Option<Node> {
        if let NodeType::Element(_element) = &self.node_type
            && self.properties.get("id") == Some(&id.to_string())
        {
            return Some(self.clone());
        }

        for child in &self.children {
            if let Some(node) = child.borrow().get_element_by_id(id) {
                return Some(node);
            }
        }

        None
    }

    pub fn from_token_stream(token_stream: TokenStream) -> NodeRef {
        let root = Rc::new(RefCell::new(Node {
            node_type: NodeType::Document,
            _parent_element: None,
            children: vec![],
            properties: HashMap::new(),
        }));

        let mut open_tags: Vec<NodeRef> = vec![];

        for token in token_stream.into_iter() {
            let parent_element = open_tags.last().unwrap_or(&root);
            match token.get_token_type() {
                TokenType::OpeningTag => {
                    let new_node = Rc::new(RefCell::new(Node {
                        node_type: NodeType::Element(token.get_html_element().unwrap()),
                        _parent_element: Some(Rc::downgrade(parent_element)),
                        children: vec![],
                        properties: token.get_properties(),
                    }));
                    parent_element.borrow_mut().children.push(new_node.clone());
                    open_tags.push(new_node);
                }
                TokenType::ClosingTag => {
                    open_tags.pop();
                }
                TokenType::VoidTag => {
                    let new_node = Rc::new(RefCell::new(Node {
                        node_type: NodeType::Element(token.get_html_element().unwrap()),
                        _parent_element: Some(Rc::downgrade(parent_element)),
                        children: vec![],
                        properties: token.get_properties(),
                    }));
                    parent_element.borrow_mut().children.push(new_node.clone());
                }
                TokenType::Text => {
                    let new_node = Rc::new(RefCell::new(Node {
                        node_type: NodeType::Text(token.get_text()),
                        _parent_element: Some(Rc::downgrade(parent_element)),
                        children: vec![],
                        properties: HashMap::new(),
                    }));
                    parent_element.borrow_mut().children.push(new_node.clone());
                }
                TokenType::Comment => {
                    let new_node = Rc::new(RefCell::new(Node {
                        node_type: NodeType::Comment(token.get_text()),
                        _parent_element: Some(Rc::downgrade(parent_element)),
                        children: vec![],
                        properties: token.get_properties(),
                    }));
                    parent_element.borrow_mut().children.push(new_node.clone());
                }
                TokenType::Unknown => {} // TODO
            }
        }
        root
    }
}

mod tests {
    #[cfg(test)]
    use super::*;
    #[cfg(test)]
    use crate::tokeniser::get_tokens;

    #[cfg(test)]
    const TEST: &str = r##"<html><head><title id="hmm">Test</title><br /></head><body><p id="some-paragraph">Hello, world!</p><div id='classy' class='bg-red p-10 primary'>This is a div with a few classes</div>
    <div class = 'malformed'>Broken</div>
    <div class ='malformed'>Broken</div>
    <div class= 'malformed'>Broken</div>
    <div class   =   'malformed'>Broken</div>
    <div id='nested'><p>Some text inside</p></div>
    <div class='bg-red'>This is another div with the same class</div><footer>No props footer</footer><hr class="thicc"/>
    <ul id="some-list">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
    </ul>
    </body></html>"##;

    #[cfg(test)]
    const INNER_TEXT_EXAMPLE: &str = r##"<div><a href="https://google.com"><span>The quick brown <b>fox</b> jumped over the lazy dog</span></a></div>"##;

    // #[test]
    // fn try_walk_tree() {
    //     let tokens = get_tokens(TEST);
    //     let document = Node::from_token_stream(tokens);
    //     document.borrow().walk_tree();
    // }

    #[test]
    fn check_children_and_inner_html() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let element = document.borrow().get_element_by_id("some-list").unwrap();
        let children = element.get_children();

        assert_eq!(children[0].borrow().inner_html(), "Item 1");
    }

    #[test]
    fn check_children() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let element = document.borrow().get_element_by_id("some-list").unwrap();
        let children = element.get_children();

        assert_eq!(children.len(), 3);
    }

    #[test]
    fn check_element_by_id() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let element = document
            .borrow()
            .get_element_by_id("some-paragraph")
            .unwrap();

        assert_eq!(element.node_type, NodeType::Element(HtmlElement::P))
    }

    #[test]
    fn check_get_classes() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let element = document
            .borrow()
            .get_element_by_id("classy")
            .unwrap()
            .get_class_list();

        assert_eq!(
            element,
            vec![
                "bg-red".to_string(),
                "p-10".to_string(),
                "primary".to_string()
            ]
        );
    }

    #[test]
    fn check_get_elements_by_class() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let elements = document.borrow().get_elements_by_class("bg-red");

        assert_eq!(elements.len(), 2);
    }

    #[test]
    fn check_get_elements_by_tag() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let elements = document.borrow().get_elements_by_tag(&HtmlElement::Div);

        assert_eq!(elements.len(), 7);
    }

    #[test]
    fn check_outer_html() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let element = document.borrow().get_element_by_id("hmm").unwrap();

        assert_eq!(element.outer_html(), *"<title id=\"hmm\">Test</title>");
    }

    #[test]
    fn check_inner_html() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let element = document.borrow().get_element_by_id("hmm").unwrap();

        assert_eq!(element.inner_html(), *"Test");
    }

    #[test]
    fn check_nested_inner_html() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let element = document.borrow().get_element_by_id("nested").unwrap();

        assert_eq!(element.inner_html(), *"<p>Some text inside</p>");
    }

    #[test]
    fn check_nested_outer_html() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let element = document.borrow().get_element_by_id("nested").unwrap();

        assert_eq!(
            element.outer_html(),
            *"<div id=\"nested\"><p>Some text inside</p></div>"
        );
    }

    #[test]
    fn check_no_props_outer_html() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let footers = document.borrow().get_elements_by_tag(&HtmlElement::Footer);

        assert_eq!(footers[0].outer_html(), *"<footer>No props footer</footer>");
    }

    #[test]
    fn check_no_props_inner_html() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let footers = document.borrow().get_elements_by_tag(&HtmlElement::Footer);

        assert_eq!(footers[0].inner_html(), *"No props footer");
    }

    #[test]
    fn check_void_tag_outer_html() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let brs = document.borrow().get_elements_by_tag(&HtmlElement::Br);

        assert_eq!(brs[0].outer_html(), *"<br>");
    }

    #[test]
    fn check_void_tag_with_props_outer_html() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let hrs = document.borrow().get_elements_by_tag(&HtmlElement::Hr);

        assert_eq!(hrs[0].outer_html(), *"<hr class=\"thicc\">");
    }

    #[test]
    fn check_malformed_tag_one() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let divs = document.borrow().get_elements_by_tag(&HtmlElement::Div);

        assert_eq!(
            divs[1].properties.get("class").unwrap(),
            &"malformed".to_string()
        );
    }

    #[test]
    fn check_malformed_tag_two() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let divs = document.borrow().get_elements_by_tag(&HtmlElement::Div);

        assert_eq!(
            divs[2].properties.get("class").unwrap(),
            &"malformed".to_string()
        );
    }

    #[test]
    fn check_malformed_tag_three() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let divs = document.borrow().get_elements_by_tag(&HtmlElement::Div);

        assert_eq!(
            divs[3].properties.get("class").unwrap(),
            &"malformed".to_string()
        );
    }

    #[test]
    fn check_malformed_tag_four() {
        let tokens = get_tokens(TEST);
        let document = Node::from_token_stream(tokens);
        let divs = document.borrow().get_elements_by_tag(&HtmlElement::Div);

        assert_eq!(
            divs[4].properties.get("class").unwrap(),
            &"malformed".to_string()
        );
    }

    #[test]
    fn check_inner_text() {
        let tokens = get_tokens(INNER_TEXT_EXAMPLE);
        let document = Node::from_token_stream(tokens);
        let divs = document.borrow().get_elements_by_tag(&HtmlElement::Div);

        assert_eq!(
            divs[0].inner_text(),
            "The quick brown fox jumped over the lazy dog".to_string()
        );
    }
}