Skip to content

Commit

Permalink
fix: fix clippy the needless borrow, fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
fefit committed Oct 4, 2021
1 parent 1da6b04 commit 79d3db6
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 110 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "visdom"
version = "0.4.10"
version = "0.4.11"
edition = "2018"
description = "A html document syntax and operation library, use APIs similar to jquery, easy to use for web scraping and confused html."
keywords = ["html-parser", "dom", "jquery", "scrape", "crawl"]
Expand All @@ -13,7 +13,7 @@ license = "MIT"
exclude = [".vscode/*.*", ".editorconfig", ".travis.yml", "src/main.rs", "performance/*.*"]

[dependencies]
rphtml = "0.5.3"
rphtml = "0.5.4"
lazy_static = "1.4.0"
thiserror = "1.0.24"
regex = "1.4.3"
Expand Down
54 changes: 27 additions & 27 deletions performance/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,33 +520,33 @@ fn nth_child_find() -> RunResult {
fn main() -> UniResult {
let mut total_info: Vec<TotalInfo> = Vec::with_capacity(10);
total_info.push(load_html()?);
// total_info.push(find_id()?);
// total_info.push(find_class()?);
// total_info.push(find_name()?);
// total_info.push(find_attr()?);
// total_info.push(find_prev()?);
// total_info.push(find_prev_all()?);
// total_info.push(find_next()?);
// total_info.push(find_next_all()?);
// total_info.push(empty()?);
// total_info.push(contains()?);
// total_info.push(first_child()?);
// total_info.push(last_child()?);
// total_info.push(first_of_type()?);
// total_info.push(last_of_type()?);
// total_info.push(nth_child()?);
// total_info.push(nth_child_10()?);
// total_info.push(nth_child_2n5()?);
// total_info.push(nth_last_child()?);
// total_info.push(nth_last_child_10()?);
// total_info.push(nth_last_child_2n5()?);
// total_info.push(nth_of_type()?);
// total_info.push(nth_of_type_10()?);
// total_info.push(nth_of_type_2n5()?);
// total_info.push(nth_last_of_type()?);
// total_info.push(nth_last_of_type_10()?);
// total_info.push(nth_last_of_type_2n5()?);
// total_info.push(nth_child_find()?);
total_info.push(find_id()?);
total_info.push(find_class()?);
total_info.push(find_name()?);
total_info.push(find_attr()?);
total_info.push(find_prev()?);
total_info.push(find_prev_all()?);
total_info.push(find_next()?);
total_info.push(find_next_all()?);
total_info.push(empty()?);
total_info.push(contains()?);
total_info.push(first_child()?);
total_info.push(last_child()?);
total_info.push(first_of_type()?);
total_info.push(last_of_type()?);
total_info.push(nth_child()?);
total_info.push(nth_child_10()?);
total_info.push(nth_child_2n5()?);
total_info.push(nth_last_child()?);
total_info.push(nth_last_child_10()?);
total_info.push(nth_last_child_2n5()?);
total_info.push(nth_of_type()?);
total_info.push(nth_of_type_10()?);
total_info.push(nth_of_type_2n5()?);
total_info.push(nth_last_of_type()?);
total_info.push(nth_last_of_type_10()?);
total_info.push(nth_last_of_type_2n5()?);
total_info.push(nth_child_find()?);
println!("Total info: {:?}", total_info);
Ok(())
}
49 changes: 26 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ impl Dom {
return false;
}
// test if same node
if dom.is(&node) {
if dom.is(node) {
Dom::halt(dom, method, &format!("Can't {} of dom.", method));
return false;
}
// test if the node is dom's parent node
let mut cur = dom.cloned();
while let Some(parent) = cur.parent() {
if parent.is(&node) {
if parent.is(node) {
Dom::halt(dom, method, &format!("Can't {} of self's parent", method));
return false;
}
Expand Down Expand Up @@ -183,9 +183,11 @@ impl INodeTrait for Rc<RefCell<Node>> {
if let Some(root) = &self.borrow().root {
if let Some(root) = &root.upgrade() {
if let Some(doc) = &root.borrow().document {
return Some(Box::new(Document {
doc: Rc::clone(doc).into(),
}));
if let Some(ref_doc) = &doc.upgrade() {
return Some(Box::new(Document {
doc: Rc::clone(ref_doc).into(),
}));
}
}
}
}
Expand Down Expand Up @@ -225,7 +227,7 @@ impl INodeTrait for Rc<RefCell<Node>> {
let content = encode(content, EntitySet::Html, EncodeType::NamedOrDecimal);
let mut text_node = Node::create_text_node(content, None);
// set text node parent
text_node.parent = Some(Rc::downgrade(&self));
text_node.parent = Some(Rc::downgrade(self));
// set childs
node.childs = Some(vec![Rc::new(RefCell::new(text_node))]);
} else {
Expand Down Expand Up @@ -260,7 +262,7 @@ impl INodeTrait for Rc<RefCell<Node>> {
fn set_html(&mut self, content: &str) {
let mut is_element = true;
let target = match self.node_type() {
INodeType::Element => Some(Rc::clone(&self)),
INodeType::Element => Some(Rc::clone(self)),
INodeType::Text => {
if let Some(parent) = &self.borrow_mut().parent {
if let Some(parent) = &parent.upgrade() {
Expand Down Expand Up @@ -632,7 +634,7 @@ impl IElementTrait for Rc<RefCell<Node>> {
fn insert_adjacent(&mut self, position: &InsertPosition, node: &BoxDynElement) {
// base validate
let action = position.action();
if !Dom::validate_dom_change(self, &node, action) {
if !Dom::validate_dom_change(self, node, action) {
return;
}
let node_type = node.node_type();
Expand All @@ -644,7 +646,7 @@ impl IElementTrait for Rc<RefCell<Node>> {
if let Some(childs) = &dom.borrow().childs {
childs
.iter()
.map(|v| Rc::clone(&v))
.map(|v| Rc::clone(v))
.collect::<Vec<RefNode>>()
} else {
vec![]
Expand Down Expand Up @@ -714,7 +716,7 @@ impl IElementTrait for Rc<RefCell<Node>> {
AfterBegin | BeforeEnd => {
// set nodes parent
for node in &nodes {
node.borrow_mut().parent = Some(Rc::downgrade(&self));
node.borrow_mut().parent = Some(Rc::downgrade(self));
}
// prepend, append
if let Some(childs) = &mut self.borrow_mut().childs {
Expand All @@ -727,7 +729,7 @@ impl IElementTrait for Rc<RefCell<Node>> {
// always reset nodes index
reset_next_siblings_index(0, &nodes);
// reset childs index
reset_next_siblings_index(nodes.len(), &childs);
reset_next_siblings_index(nodes.len(), childs);
// append childs to nodes
nodes.append(childs);
// set childs to nodes
Expand Down Expand Up @@ -815,7 +817,7 @@ impl IElementTrait for Rc<RefCell<Node>> {
}
}
}
let ele = Box::new(Rc::clone(&self)) as BoxDynElement;
let ele = Box::new(Rc::clone(self)) as BoxDynElement;
loop_handle(ele, &mut result, 0, limit_depth, handle);
if !result.is_empty() {
return Some(result);
Expand All @@ -839,7 +841,7 @@ impl IElementTrait for Rc<RefCell<Node>> {
fn is(&self, ele: &BoxDynElement) -> bool {
let specified: Box<dyn Any> = ele.cloned().to_node();
if let Ok(dom) = specified.downcast::<RefNode>() {
return Node::is_same(&self, &dom);
return Node::is_same(self, &dom);
}
false
}
Expand All @@ -850,18 +852,21 @@ impl IElementTrait for Rc<RefCell<Node>> {
}
}

struct Document {
pub struct Document {
doc: DocHolder,
}

impl Document {
fn bind_error(&mut self, handle: IErrorHandle) {
*self.doc.borrow().onerror.borrow_mut() = Some(Rc::new(handle));
}
fn list<'b>(&self) -> Elements<'b> {
/// Get an elements set.
pub fn elements<'b>(self) -> Elements<'b> {
let root = Rc::clone(&self.doc.borrow().root);
Elements::with_nodes(vec![Box::new(root)])
Elements::with_all(vec![Box::new(root)], Some(Box::new(self)))
}
/// Destory the document
pub fn destory(self) {}
}

impl IDocumentTrait for Document {
Expand All @@ -882,11 +887,9 @@ impl IDocumentTrait for Document {
}
// onerror
fn onerror(&self) -> Option<Rc<IErrorHandle>> {
if let Some(error_handle) = &(*self.doc.borrow().onerror.borrow()) {
Some(Rc::clone(error_handle))
} else {
None
}
(*self.doc.borrow().onerror.borrow())
.as_ref()
.map(|error_handle| Rc::clone(error_handle))
}
}

Expand Down Expand Up @@ -985,14 +988,14 @@ impl Vis {
/// load the html with options, get an elements collection
pub fn load_options(html: &str, options: ParseOptions) -> Result<Elements, Box<dyn Error>> {
let doc = Vis::parse_doc_options(html, options)?;
Ok(doc.list())
Ok(doc.elements())
}
/// load the html with options, and catch the errors
pub fn load_options_catch(html: &str, options: ParseOptions, handle: IErrorHandle) -> Elements {
let doc = Vis::parse_doc_options(html, options);
if let Ok(mut doc) = doc {
doc.bind_error(handle);
doc.list()
doc.elements()
} else {
handle(doc.err().unwrap());
Elements::new()
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// let nth_2n_child = ul.find(":nth-child(2n),:nth-child(1),:nth-child(n+8)");
// println!("2n:{}", nth_2n_child.length());
let html = r##"
<html>
<html lang="utf-8">
<head></head>
<body>
<div id="id" name="#id">
Expand Down Expand Up @@ -247,5 +247,11 @@ fn main() -> Result<(), Box<dyn Error>> {
// true
// });
// println!("{}", root.outer_html());
let html = r#"
<div id="content">FIRST-ABC<div>SECOND-ABC<style>.a{{color:red}}</style>SECOND-DEF</div><script>var a = 1;</script>FIRST-DEF</div>
"#;
let root = Vis::load(html)?;
println!("root:{}", root.find("div").length());
println!("root:{:?}", root.find("#content").length());
Ok(())
}
Loading

0 comments on commit 79d3db6

Please sign in to comment.