diff --git a/__test__/index.spec.mjs b/__test__/index.spec.mjs
index 4bc2d4b..27b1f9c 100644
--- a/__test__/index.spec.mjs
+++ b/__test__/index.spec.mjs
@@ -285,3 +285,20 @@ test("should remove all attributes correctly", (t) => {
t.deepEqual($.select("div").getAttributes(), {});
});
+
+test("should clone correctly", (t) => {
+ const $ = parse(
+ '
first
second
',
+ );
+
+ t.is($.select(".one").clone().outerHtml(), '');
+ t.is(
+ $.select(".one").cloneRecursive().outerHtml(),
+ 'first
',
+ );
+
+ const $cloned = $.select(".one").cloneRecursive();
+ $cloned.select(".one").getChildren()[0].remove();
+ t.is($cloned.select(".one").outerHtml(), '');
+ t.is($.select(".one").outerHtml(), 'first
');
+});
diff --git a/package.json b/package.json
index 9d83fb0..45aeb36 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "niddle",
- "version": "0.0.5",
+ "version": "0.0.6",
"main": "index.js",
"types": "index.d.ts",
"files": [
@@ -51,22 +51,22 @@
},
"repository": "git@github.com:xusd320/niddle.git",
"optionalDependencies": {
- "niddle-win32-x64-msvc": "0.0.4",
- "niddle-darwin-x64": "0.0.4",
- "niddle-linux-x64-gnu": "0.0.4",
- "niddle-darwin-arm64": "0.0.4",
- "niddle-android-arm64": "0.0.4",
- "niddle-linux-arm64-gnu": "0.0.4",
- "niddle-linux-arm64-musl": "0.0.4",
- "niddle-win32-arm64-msvc": "0.0.4",
- "niddle-linux-arm-gnueabihf": "0.0.4",
- "niddle-linux-arm-musleabihf": "0.0.4",
- "niddle-linux-x64-musl": "0.0.4",
- "niddle-freebsd-x64": "0.0.4",
- "niddle-win32-ia32-msvc": "0.0.4",
- "niddle-android-arm-eabi": "0.0.4",
- "niddle-darwin-universal": "0.0.4",
- "niddle-linux-riscv64-gnu": "0.0.4"
+ "niddle-win32-x64-msvc": "0.0.5",
+ "niddle-darwin-x64": "0.0.5",
+ "niddle-linux-x64-gnu": "0.0.5",
+ "niddle-darwin-arm64": "0.0.5",
+ "niddle-android-arm64": "0.0.5",
+ "niddle-linux-arm64-gnu": "0.0.5",
+ "niddle-linux-arm64-musl": "0.0.5",
+ "niddle-win32-arm64-msvc": "0.0.5",
+ "niddle-linux-arm-gnueabihf": "0.0.5",
+ "niddle-linux-arm-musleabihf": "0.0.5",
+ "niddle-linux-x64-musl": "0.0.5",
+ "niddle-freebsd-x64": "0.0.5",
+ "niddle-win32-ia32-msvc": "0.0.5",
+ "niddle-android-arm-eabi": "0.0.5",
+ "niddle-darwin-universal": "0.0.5",
+ "niddle-linux-riscv64-gnu": "0.0.5"
}
}
diff --git a/src/node_repr/mod.rs b/src/node_repr/mod.rs
index e6b3574..88bb686 100644
--- a/src/node_repr/mod.rs
+++ b/src/node_repr/mod.rs
@@ -21,3 +21,28 @@ impl From for NodeRepr {
Self { node_ref }
}
}
+
+#[napi]
+impl NodeRepr {
+ #[napi(js_name = "clone")]
+ pub fn clone_self_only(&self) -> NodeRepr {
+ let new_node_ref = NodeRef::new(self.node_ref.data().clone());
+ NodeRepr::from(new_node_ref)
+ }
+
+ #[napi]
+ pub fn clone_recursive(&self) -> NodeRepr {
+ NodeRepr::from(clone_node_ref_recursive(&self.node_ref))
+ }
+}
+
+fn clone_node_ref_recursive(node_ref: &NodeRef) -> NodeRef {
+ let new_node_ref = NodeRef::new(node_ref.data().clone());
+
+ node_ref.children().for_each(|child| {
+ let child_node_ref = clone_node_ref_recursive(&child);
+ new_node_ref.append(child_node_ref);
+ });
+
+ new_node_ref
+}
diff --git a/src/node_repr/modify.rs b/src/node_repr/modify.rs
index c447e67..02bfd7f 100644
--- a/src/node_repr/modify.rs
+++ b/src/node_repr/modify.rs
@@ -1,6 +1,5 @@
use html5ever::LocalName;
use indexmap::IndexMap;
-use kuchikiki::NodeRef;
use super::NodeRepr;
@@ -8,8 +7,7 @@ use super::NodeRepr;
impl NodeRepr {
#[napi]
pub fn append(&self, new_child: &NodeRepr) {
- let node_ref = clone_node_ref_recursive(&new_child.node_ref);
- self.node_ref.append(node_ref)
+ self.node_ref.append(new_child.node_ref.clone())
}
#[napi]
@@ -21,8 +19,7 @@ impl NodeRepr {
#[napi]
pub fn prepend(&self, new_child: &NodeRepr) {
- let node_ref = clone_node_ref_recursive(&new_child.node_ref);
- self.node_ref.prepend(node_ref)
+ self.node_ref.prepend(new_child.node_ref.clone())
}
#[napi]
@@ -39,8 +36,7 @@ impl NodeRepr {
#[napi]
pub fn insert_after(&self, new_sibling: &NodeRepr) {
- let node_ref = clone_node_ref_recursive(&new_sibling.node_ref);
- self.node_ref.insert_after(node_ref)
+ self.node_ref.insert_after(new_sibling.node_ref.clone())
}
#[napi]
@@ -61,8 +57,7 @@ impl NodeRepr {
#[napi]
pub fn insert_before(&self, new_sibling: &NodeRepr) {
- let node_ref = clone_node_ref_recursive(&new_sibling.node_ref);
- self.node_ref.insert_before(node_ref)
+ self.node_ref.insert_before(new_sibling.node_ref.clone())
}
#[napi]
@@ -118,14 +113,3 @@ impl NodeRepr {
}
}
}
-
-fn clone_node_ref_recursive(node_ref: &NodeRef) -> NodeRef {
- let new_node_ref = NodeRef::new(node_ref.data().clone());
-
- node_ref.children().for_each(|child| {
- let child_node_ref = clone_node_ref_recursive(&child);
- new_node_ref.append(child_node_ref);
- });
-
- new_node_ref
-}