Skip to content

Commit

Permalink
feat: add clone method for an elements set, implement feature #21
Browse files Browse the repository at this point in the history
  • Loading branch information
fefit committed Apr 17, 2024
1 parent 3970a79 commit f0f50ec
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "visdom"
version = "0.5.10"
version = "1.0.1"
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", "scrape", "jquery", "query", "selector"]
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,15 @@ impl INodeTrait for Rc<RefCell<Node>> {
self.borrow().index
}

/// impl `clone_node`
/// The current version of this method only implements the clone of `Rc` pointers.
/// This is different from the standard `clone_node` method.
/// If you want to use `clone_node` method with the standard semantics, now you can use `copy_node` instead.
/// This method may be changed in future versions to be consist with the standard `clone_node` semantics.
fn clone_node<'b>(&self) -> BoxDynNode<'b> {
Box::new(self.clone())
}

/// impl `copy_node`
/// impl standard semantics `clone_node` method
fn copy_node<'b>(&self) -> BoxDynNode<'b> {
Box::new(self.borrow().clone_node())
}
Expand Down
6 changes: 4 additions & 2 deletions src/mesdoc/interface/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ cfg_feat_insertion! {
pub trait IElementTrait: INodeTrait {
fn is(&self, ele: &BoxDynElement) -> bool;
fn is_root_element(&self) -> bool;
// cloned, will decrept
/// The current version is an implementation of `Rc` pointers with non-copy semantics.
/// If you want to achieve complete copying of nodes in the current version, please use the `copied` method instead.
/// The semantics of this method may be changed in future versions to be consist with the `copied` method.
fn cloned<'b>(&self) -> BoxDynElement<'b> {
let ele = self.clone_node();
ele.typed().into_element().unwrap()
}
// copy a node
/// Copy a element
fn copied<'b>(&self) -> BoxDynElement<'b> {
let ele = self.copy_node();
ele.typed().into_element().unwrap()
Expand Down
21 changes: 13 additions & 8 deletions src/mesdoc/interface/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,10 @@ impl<'a> Elements<'a> {
elements
}

// cloned
/// This method will be removed in future versions.
/// If you want to clone an elements set, please use the `clone()` method instead.
/// This method only clone the element's `Rc` pointer in the elements set.
/// Any modifications to the cloned elements set will be reflected on the original elements set.
pub fn cloned(&self) -> Elements<'a> {
let mut result = Elements::with_capacity(self.length());
for ele in &self.nodes {
Expand Down Expand Up @@ -3545,7 +3548,7 @@ cfg_feat_mutation! {
/// <body>
/// <dl>
/// <dt>Title</dt>
/// <dd><span>item1</span></dd>
/// <dd><span class="span">item1</span></dd>
/// <dd class="item2"><span>item2</span></dd>
/// <dd class="item3"><!--comment-->item3</dd>
/// </dl>
Expand All @@ -3554,12 +3557,14 @@ cfg_feat_mutation! {
/// "##;
/// let doc = Vis::load(html)?;
/// let dl = doc.find("dl");
/// let items = dl.children("");
/// assert_eq!(items.length(), 4);
/// // remove the dt element
/// items.filter("dt").remove();
/// let now_items = dl.children("");
/// assert_eq!(now_items.length(), 3);
/// let span = dl.find("span.span");
/// assert_eq!(span.text(), "item1");
/// // clone the "dl" Elements
/// let clone_dl = dl.clone();
/// let mut clone_span = clone_dl.find("span.span");
/// clone_span.set_text("span");
/// assert_eq!(span.text(), "item1");
/// assert_eq!(clone_dl.find("span.span").text(), "span");
/// Ok(())
/// }
/// ```
Expand Down
36 changes: 36 additions & 0 deletions tests/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,39 @@ fn test_replace_with() -> Result {
assert_eq!(now_svg.length(), 1);
Ok(())
}

#[test]
fn test_clone() -> Result {
let menu_html = r#"<menu class="menu">
<h3>Title</h3>
<ul class="list">
<li class="item-1">item1</li>
<li class="item-2">item2</li>
</ul>
</menu>"#;
let html = format!(
r#"
<h2>logo</h2>
{}
"#,
menu_html
);
let fragement = Vis::load(html)?;
let menu = fragement.find(">.menu");
let clone_menu = menu.clone();
let mut clone_h3 = clone_menu.find(">h3");
clone_h3.set_text("h3");
assert_eq!(menu.outer_html(), menu_html);
assert_eq!(clone_h3.text(), "h3");
let mut clone_item_1 = clone_menu.find(".item-1");
clone_item_1.add_class("item");
assert_eq!(menu.outer_html(), menu_html);
assert!(clone_item_1.has_class("item"));
clone_item_1.remove_class("item-1").add_class("item-3");
clone_item_1.append_to(&mut menu.find("ul.list"));
assert_eq!(menu.find(".list > li").length(), 3);
assert!(menu.find(".list > li").eq(2).has_class("item-3"));
assert_eq!(clone_menu.find(".list > li").length(), 1);
assert_eq!(clone_menu.find(".list > li").first().text(), "item2");
Ok(())
}

0 comments on commit f0f50ec

Please sign in to comment.