Skip to content

Commit

Permalink
feat: add clone api
Browse files Browse the repository at this point in the history
  • Loading branch information
xusd320 committed Jul 15, 2024
1 parent 409daa0 commit 7e100a5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 37 deletions.
17 changes: 17 additions & 0 deletions __test__/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,20 @@ test("should remove all attributes correctly", (t) => {

t.deepEqual($.select("div").getAttributes(), {});
});

test("should clone correctly", (t) => {
const $ = parse(
'<html><head></head><body><div class="one">first</div><div id="two">second</div></body></html>',
);

t.is($.select(".one").clone().outerHtml(), '<div class="one"></div>');
t.is(
$.select(".one").cloneRecursive().outerHtml(),
'<div class="one">first</div>',
);

const $cloned = $.select(".one").cloneRecursive();
$cloned.select(".one").getChildren()[0].remove();
t.is($cloned.select(".one").outerHtml(), '<div class="one"></div>');
t.is($.select(".one").outerHtml(), '<div class="one">first</div>');
});
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "niddle",
"version": "0.0.5",
"version": "0.0.6",
"main": "index.js",
"types": "index.d.ts",
"files": [
Expand Down Expand Up @@ -51,22 +51,22 @@
},
"repository": "[email protected]: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"
}
}

25 changes: 25 additions & 0 deletions src/node_repr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,28 @@ impl From<NodeRef> 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
}
24 changes: 4 additions & 20 deletions src/node_repr/modify.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use html5ever::LocalName;
use indexmap::IndexMap;
use kuchikiki::NodeRef;

use super::NodeRepr;

#[napi]
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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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
}

0 comments on commit 7e100a5

Please sign in to comment.