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
impl Dom
Sourcepub fn new_root() -> Dom
pub fn new_root() -> Dom
Create the new root dom.
The root dom has a Tag structure whose name is root.
Sourcepub fn get_tag(&self) -> Option<&Tag>
pub fn get_tag(&self) -> Option<&Tag>
Returns the Tag structure.
If it does not have a Tag structure, it returns None
.
Sourcepub fn get_text(&self) -> Option<&Text>
pub fn get_text(&self) -> Option<&Text>
Returns the Text structure.
If it does not have a Text structure, it returns None
.
Sourcepub fn set_comment(&mut self, comment: Comment)
pub fn set_comment(&mut self, comment: Comment)
Sourcepub fn get_comment(&self) -> Option<&Comment>
pub fn get_comment(&self) -> Option<&Comment>
Returns the Comment structure.
If it does not have a Comment structure, it returns None
.
Sourcepub fn get_children(&self) -> Option<&Vec<Box<Dom>>>
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?
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}
Sourcepub fn p_implies_q(p: &Dom, q: &Dom) -> bool
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);
Sourcepub fn p_implies_q_tree(p: &Dom, q: &Dom) -> bool
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);
Sourcepub fn search(&self, needle: &str) -> Result<Option<Vec<Box<Dom>>>, String>
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"