Dom

Struct Dom 

Source
pub struct Dom {
    pub dom_type: DomType,
    /* private fields */
}
Expand description

A structure that represents the parsing result of a tag document.

Fields§

§dom_type: DomType

Type of Dom structure

Implementations§

Source§

impl Dom

Source

pub fn new(dom_type: DomType) -> Dom

Create new Dom structure.

Source

pub fn new_root() -> Dom

Create the new root dom.

The root dom has a Tag structure whose name is root.

Source

pub fn set_tag(&mut self, tag: Tag)

Set Tag structure.

§Panics

self.dom_type is not DomType::Tag

Source

pub fn get_tag(&self) -> Option<&Tag>

Returns the Tag structure. If it does not have a Tag structure, it returns None.

Source

pub fn set_text(&mut self, text: Text)

Set Text structure.

§Panics

self.dom_type is not DomType::Text

Source

pub fn get_text(&self) -> Option<&Text>

Returns the Text structure. If it does not have a Text structure, it returns None.

Source

pub fn set_comment(&mut self, comment: Comment)

Set Comment structure.

§Panics

self.dom_type is not DomType::Comment

Source

pub fn get_comment(&self) -> Option<&Comment>

Returns the Comment structure. If it does not have a Comment structure, it returns None.

Source

pub fn add_child(&mut self, dom: Dom)

Add a child Dom structure.

Source

pub fn get_children(&self) -> Option<&Vec<Box<Dom>>>

Returns child Dom structures as Vec. If it does not have children, it returns None.

Examples found in repository?
examples/search_dom.rs (line 54)
3fn main() {
4    let html = r#"
5<!DOCTYPE html>
6<html>
7  <head>
8    <meta charset="UTF-8">
9    <title>sample html</title>
10  </head>
11  <body>
12
13    <ul id="list1" class="targetList">
14      <li class="key1">1-1</li>
15      <li class="key2"><span>1-2</span></li>
16    </ul>
17
18    <ul id="list2">
19      <li class="key1">2-1</li>
20      <li>2-2</li>
21    </ul>
22
23    <div>
24      <div>
25        <ul class="targetList">
26          <ul id="list3" class="targetList">
27            <li class="key1">3-1</li>
28            <li class="item">3-2</li>
29            <li class="key2">3-3</li>
30          </ul>
31        </ul>
32      </div>
33    </div>
34
35    <ul id="list4">
36      <li class="key1">4-1</li>
37      <li class="key2">4-2</li>
38    </ul>
39
40  </body>
41</html>
42"#;
43
44    let root_dom = parsercher::parse(&html).unwrap();
45
46    let needle = r#"
47<ul class="targetList">
48  <li class="key1"></li>
49  <li class="key2"></li>
50</ul>
51"#;
52    let needle_dom = parsercher::parse(&needle).unwrap();
53    // Remove `root`dom of needle_dom
54    let needle_dom = needle_dom.get_children().unwrap().get(0).unwrap();
55
56    if let Some(dom) = parsercher::search_dom(&root_dom, &needle_dom) {
57        parsercher::print_dom_tree(&dom);
58    }
59}
Source

pub fn p_implies_q(p: &Dom, q: &Dom) -> bool

Returns true if p is a sufficient condition for q. p => q

§Examples
use parsercher::dom::DomType;
use parsercher::dom::Dom;
use parsercher::dom::Tag;

let mut p = Dom::new(DomType::Tag);
let mut tag = Tag::new("h1");
tag.set_attr("class", "target");
p.set_tag(tag);

let mut q = Dom::new(DomType::Tag);
let mut tag = Tag::new("h1");
tag.set_attr("id", "q");
tag.set_attr("class", "target");
q.set_tag(tag);

assert_eq!(Dom::p_implies_q(&p, &q), true);

let mut q = Dom::new(DomType::Tag);
let mut tag = Tag::new("h1");
tag.set_attr("id", "q");
q.set_tag(tag);

assert_eq!(Dom::p_implies_q(&p, &q), false);
Source

pub fn p_implies_q_tree(p: &Dom, q: &Dom) -> bool

Returns true if p is a sufficient condition for q. Compare the entire tree. p => q

§Examples
use parsercher;
use parsercher::dom::Dom;

// p
let p = r#"
<h1>
  <div></div>
  <ul>
    <li></li>
  </ul>
</h1>
"#;

let p_dom = parsercher::parse(&p).unwrap();
// Remove `root`dom of p_dom
let p_dom = p_dom.get_children().unwrap().get(0).unwrap();

// q
let q = r#"
<h1>
  <div id="divId"></div>
  <ul>
    <li></li>
  </ul>
  <span></span>
</h1>
"#;

let q_dom = parsercher::parse(&q).unwrap();
// Remove `root`dom of p_dom
let q_dom = q_dom.get_children().unwrap().get(0).unwrap();

assert_eq!(Dom::p_implies_q_tree(&p_dom, &q_dom), true);
Source

pub fn search(&self, needle: &str) -> Result<Option<Vec<Box<Dom>>>, String>

Return the needle-like subtree from the Dom structure tree. The needle argument must be parsable html.

§Examples

Get the subtree that satisfies the following tag names and attribute values.

<ul class="targetList">
  <li class="key1></li>
  <li class="key2></li>
</ul>
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 result = root_dom.search(&needle).unwrap().unwrap();

assert!(result.len() == 2);

for dom in result.iter() {
    parsercher::print_dom_tree(&dom);
}

output:

<ul class="targetList" id="list1">
  <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"

Trait Implementations§

Source§

impl Clone for Dom

Source§

fn clone(&self) -> Dom

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Dom

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Dom

Source§

fn eq(&self, other: &Dom) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Dom

Auto Trait Implementations§

§

impl Freeze for Dom

§

impl RefUnwindSafe for Dom

§

impl Send for Dom

§

impl Sync for Dom

§

impl Unpin for Dom

§

impl UnwindSafe for Dom

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.