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
use crate::dom::tag::Tag;
use crate::dom::Dom;
use crate::dom::DomType;

/// Returns Tag structures from which the needle is a sufficient condition from the Dom structure tree.
///
/// # Examples
/// Get `li` tags that `class` attribute value is `target` from the following HTML.
/// ```text
/// <ol>
///    <li class="target">first</li>
///    <li>second</li>
///    <li id="third" class="target">therd</li>
/// </ol>
/// ```
///
/// ```compile_fail
/// let mut needle = Tag::new("li");
/// needle.set_attr("class", "target");
/// if let Some(tags) = parsercher::search_tag(&dom, &needle) {
///     println!("{:#?}", tags);
/// }
/// ```
/// output:
/// ```text
/// [
///     Tag {
///         name: "li",
///         attr: Some(
///             {
///                 "class": "target",
///             },
///         ),
///         terminated: false,
///         terminator: false,
///     },
///     Tag {
///         name: "li",
///         attr: Some(
///             {
///                 "id": "third",
///                 "class": "target",
///             },
///         ),
///         terminated: false,
///         terminator: false,
///     },
/// ]
/// ```
pub fn search_tag(dom: &Dom, needle: &Tag) -> Option<Vec<Tag>> {
    let mut res: Vec<Tag> = Vec::new();
    search_tag_exe(&mut res, dom, needle);
    if res.is_empty() {
        return None;
    }
    Some(res)
}

fn search_tag_exe(res: &mut Vec<Tag>, dom: &Dom, needle: &Tag) {
    if let Some(tag) = dom.get_tag() {
        if Tag::p_implies_q(needle, tag) {
            res.push(tag.clone());
        }

        if let Some(children) = dom.get_children() {
            for child in children {
                search_tag_exe(res, child, needle);
            }
        }
    }
}

/// Returns Tag structures with a tag name equal to `name` from the Dom structure tree.
///
/// # Examples
/// Get only the `h2` tag from the following HTML.
/// ```text
/// <body>
///    <h1 class="h1">section1</h1>
///    <h2 class="h2">section2</h2>
///    <h3 class="h3">section3</h3>
/// </body>
/// ```
///
/// ```compile_fail
/// if let Some(tags) = parsercher::search_tag_from_name(&dom, "h2") {
///     println!("{:#?}", tags);
/// }
/// ```
///
/// output:
/// ```text
/// [
///     Tag {
///         name: "h2",
///         attr: Some(
///             {
///                 "class": "h2",
///             },
///         ),
///         terminated: false,
///         terminator: false,
///     },
/// ]
/// ```
pub fn search_tag_from_name(dom: &Dom, name: &str) -> Option<Vec<Tag>> {
    let mut res: Vec<Tag> = Vec::new();
    search_tag_from_name_exe(&mut res, dom, name);
    if res.is_empty() {
        return None;
    }
    Some(res)
}

fn search_tag_from_name_exe(res: &mut Vec<Tag>, dom: &Dom, name: &str) {
    if let DomType::Tag = dom.dom_type {
        let tag = dom.get_tag().unwrap();
        if name == tag.get_name() {
            res.push(tag.clone());
        }

        if let Some(children) = dom.get_children() {
            for child in children {
                search_tag_from_name_exe(res, child, name);
            }
        }
    }
}

/// Returns texts of the child of the Tag structure for which `needle` is a sufficient condition from the Dom structure tree.
///
/// # Examples
/// Get just texts of `li` tags that `class` attribute value is `target` from the following HTML.
/// ```text
/// <ol>
///    <li class="target">first</li>
///    <li>second</li>
///    <li class="target">therd</li>
/// </ol>
/// ```
///
/// ```compile_fail
/// let mut needle = Tag::new("li");
/// let mut attr = HashMap::new();
/// attr.insert("class".to_string(), "target".to_string());
/// needle.set_attr(attr);
/// if let Some(texts) = parsercher::search_text_from_tag_children(&dom, &needle) {
///     assert_eq!(texts.len(), 2);
///     assert_eq!(texts[0], "first".to_string());
///     assert_eq!(texts[1], "therd".to_string());
/// }
/// ```
pub fn search_text_from_tag_children(dom: &Dom, needle: &Tag) -> Option<Vec<String>> {
    let mut res: Vec<String> = Vec::new();
    search_text_from_tag_children_exe(&mut res, dom, needle);
    if res.is_empty() {
        return None;
    }
    Some(res)
}

fn search_text_from_tag_children_exe(res: &mut Vec<String>, dom: &Dom, needle: &Tag) {
    if let Some(tag) = dom.get_tag() {
        if Tag::p_implies_q(needle, tag) {
            if let Some(children) = dom.get_children() {
                for child in children {
                    if let Some(text) = child.get_text() {
                        res.push(text.get_text().to_string());
                    }
                }
            }
        }

        if let Some(children) = dom.get_children() {
            for child in children {
                search_text_from_tag_children_exe(res, child, needle);
            }
        }
    }
}

/// Returns partial trees from the Dom structure tree.
/// Duplicate everything below the subtree that matches the `needle` tree.
///
/// The Tag structures contained in the `needle` tree are evaluated under sufficient conditions.
///
/// # Examples
/// Get the subtree that satisfies the following tag names and attribute values.
/// ```text
/// <ul class="targetList">
///   <li class="key1></li>
///   <li class="key2></li>
/// </ul>
/// ```
///
/// ```rust
/// use parsercher;
///
/// let html = r#"
/// <body>
///   <ul id="list1" class="targetList">
///     <li class="key1">1-1</li>
///     <li class="key2">
///       <span>1-2</span>
///     </li>
///   </ul>
///
///   <ul id="list2">
///     <li class="key1">2-1</li>
///     <li>2-2</li>
///   </ul>
///
///   <div>
///     <div>
///       <ul class="targetList">
///         <ul id="list3" class="targetList">
///           <li class="key1">3-1</li>
///           <li class="item">3-2</li>
///           <li class="key2">3-3</li>
///         </ul>
///       </ul>
///     </div>
///   </div>
///
///   <ul id="list4">
///     <li class="key1">4-1</li>
///     <li class="key2">4-2</li>
///   </ul>
///
/// </body>
/// "#;
///
/// let root_dom = parsercher::parse(&html).unwrap();
///
/// let needle = r#"
/// <ul class="targetList">
///   <li class="key1"></li>
///   <li class="key2"></li>
/// </ul>
/// "#;
/// let needle_dom = parsercher::parse(&needle).unwrap();
/// // Remove `root`dom of needle_dom
/// let needle_dom = needle_dom.get_children().unwrap().get(0).unwrap();
///
/// if let Some(dom) = parsercher::search_dom(&root_dom, &needle_dom) {
///     parsercher::print_dom_tree(&dom);
/// }
/// ```
/// output:
/// ```text
/// <root>
///   <ul id="list1" class="targetList">
///     <li class="key1">
///       TEXT: "1-1"
///     <li class="key2">
///       <span>
///         TEXT: "1-2"
///   <ul class="targetList" id="list3">
///     <li class="key1">
///       TEXT: "3-1"
///     <li class="item">
///       TEXT: "3-2"
///     <li class="key2">
///       TEXT: "3-3"
/// ```
pub fn search_dom(dom: &Dom, needle: &Dom) -> Option<Dom> {
    let mut res = Dom::new_root();
    search_dom_exe(&mut res, dom, needle);
    match res.get_children() {
        Some(_) => return Some(res),
        None => return None,
    }
}

fn search_dom_exe(res: &mut Dom, dom: &Dom, needle: &Dom) {
    if Dom::p_implies_q(needle, dom) {
        if Dom::p_implies_q_tree(needle, dom) {
            res.add_child(dom.clone());
            return;
        }
    }
    match dom.get_children() {
        Some(children) => {
            for child in children.iter() {
                search_dom_exe(res, child, needle);
            }
        }
        None => return,
    }
}

/// Returns the value of a specific attribute for all tags.
///
/// # Examples
/// Get the value of the `target` attribute of all tags.
///
/// ```rust
/// use parsercher;
///
/// let html = r#"
/// <!DOCTYPE html>
/// <html>
///   <head>
///     <meta charset="UTF-8">
///     <meta target="value1">
///     <title>sample html</title>
///   </head>
///   <body target="value2">
///     <h1>sample</h1>
///
///     <div id="content" target="value3"></div>
///
///     <ol>
///       <li>first</li>
///       <li target="value4">second</li>
///       <li>therd</li>
///     </ol>
///   </body>
/// </html>
/// "#;
///
/// let dom = parsercher::parse(&html).unwrap();
///
/// let values = parsercher::search_attr(&dom, "target").unwrap();
/// assert_eq!(values.len(), 4);
/// assert_eq!(values[0], "value1".to_string());
/// assert_eq!(values[1], "value2".to_string());
/// assert_eq!(values[2], "value3".to_string());
/// assert_eq!(values[3], "value4".to_string());
/// ```
pub fn search_attr(dom: &Dom, attr: &str) -> Option<Vec<String>> {
    let mut res: Vec<String> = Vec::new();
    search_attr_exe(&mut res, dom, attr);
    if res.is_empty() {
        return None;
    }
    Some(res)
}

fn search_attr_exe(res: &mut Vec<String>, dom: &Dom, attr: &str) {
    if DomType::Tag == dom.dom_type {
        if let Some(tag) = dom.get_tag() {
            if let Some(value) = tag.get_attr(attr) {
                res.push(value);
            }
        }
    }

    if let Some(children) = dom.get_children() {
        for child in children.iter() {
            search_attr_exe(res, child, attr);
        }
    }
}

/// Returns the value of specific attributes for all tags.
///
/// # Examples
/// Get the values of the `id` and` class` attributes of all tags.
///
/// ```rust
/// use parsercher;
///
/// let html = r#"
/// <!DOCTYPE html>
/// <html>
///   <head>
///     <meta charset="UTF-8">
///     <meta id="id1">
///     <title>sample html</title>
///   </head>
///   <body id="id2" class="class1">
///     <h1>sample</h1>
///
///     <div align="center" class="class2"></div>
///
///     <ol>
///       <li>first</li>
///       <li class="class3">second</li>
///       <li>therd</li>
///     </ol>
///   </body>
/// </html>
/// "#;
///
/// let dom = parsercher::parse(&html).unwrap();
///
/// let attrs = vec!("id", "class");
/// let values = parsercher::search_attrs(&dom, &attrs).unwrap();
/// assert_eq!(values.len(), 5);
/// assert_eq!(values[0], "id1".to_string());
/// assert_eq!(values[1], "id2".to_string());
/// assert_eq!(values[2], "class1".to_string());
/// assert_eq!(values[3], "class2".to_string());
/// assert_eq!(values[4], "class3".to_string());
/// ```
pub fn search_attrs<'a>(dom: &Dom, attrs: &Vec<&str>) -> Option<Vec<String>> {
    let mut res: Vec<String> = Vec::new();
    search_attrs_exe(&mut res, dom, attrs);
    if res.is_empty() {
        return None;
    }
    Some(res)
}

fn search_attrs_exe(res: &mut Vec<String>, dom: &Dom, attrs: &Vec<&str>) {
    if DomType::Tag == dom.dom_type {
        if let Some(tag) = dom.get_tag() {
            for attr in attrs.iter() {
                if let Some(value) = tag.get_attr(attr) {
                    res.push(value);
                }
            }
        }
    }

    if let Some(children) = dom.get_children() {
        for child in children.iter() {
            search_attrs_exe(res, child, attrs);
        }
    }
}