Function parsercher::search_attr

source ·
pub fn search_attr(dom: &Dom, attr: &str) -> Option<Vec<String>>
Expand description

Returns the value of a specific attribute for all tags.

Examples

Get the value of the target attribute of all tags.

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());
Examples found in repository?
examples/search_attr.rs (line 28)
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
fn main() {
    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());
}