Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using Tree::merge_with_fn instead of Tree::merge #23

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to the `dom_query` crate will be documented in this file.

## [Unreleased]

### Changing
- Using `Tree::merge_with_fn` instead of `Tree::merge` to reduce code duplication.

### Added
- Added `NodeRef::prepend_child` method, that inserts a child at the beginning of node content.
- Added `NodeRef::prepend_children` method, that inserts a child and it's siblings at the beginning of the node content.
Expand Down
8 changes: 8 additions & 0 deletions src/dom_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,12 @@ impl Tree {
pub(crate) fn get_new_id(&self) -> NodeId {
NodeId::new(self.nodes.borrow().len())
}

/// Adds nodes from another tree to the current tree and
/// then applies a function to the first merged node
pub(crate) fn merge_with_fn<F>(&self, other: Tree, f: F) where F: FnOnce(NodeId) {
let new_node_id = self.get_new_id();
self.merge(other);
f(new_node_id);
}
}
20 changes: 10 additions & 10 deletions src/node/node_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ impl<'a> NodeRef<'a> {
where
T: Into<StrTendril>,
{
let fragment = Document::fragment(html);
let new_node_id = self.tree.get_new_id();
self.tree.merge(fragment.tree);
self.append_prev_siblings(&new_node_id);
let fragment = Document::fragment(html);
self.tree.merge_with_fn(fragment.tree, |node_id|{
self.append_prev_siblings(&node_id);
});
self.remove_from_parent();
}

Expand All @@ -244,9 +244,9 @@ impl<'a> NodeRef<'a> {
T: Into<StrTendril>,
{
let fragment = Document::fragment(html);
let new_node_id = self.tree.get_new_id();
self.tree.merge(fragment.tree);
self.append_children(&new_node_id);
self.tree.merge_with_fn(fragment.tree, |node_id|{
self.append_children(&node_id);
});
}

/// Parses given fragment html and appends its contents to the selected node.
Expand All @@ -255,9 +255,9 @@ impl<'a> NodeRef<'a> {
T: Into<StrTendril>,
{
let fragment = Document::fragment(html);
let new_node_id = self.tree.get_new_id();
self.tree.merge(fragment.tree);
self.prepend_children(&new_node_id);
self.tree.merge_with_fn(fragment.tree, |node_id|{
self.prepend_children(&node_id);
});
}

/// Parses given fragment html and sets its contents to the selected node.
Expand Down
18 changes: 9 additions & 9 deletions src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ impl<'a> Selection<'a> {
let fragment = Document::fragment(html);

for node in self.nodes().iter() {
let new_node_id = node.tree.get_new_id();
node.tree.merge(fragment.tree.clone());
node.append_prev_siblings(&new_node_id);
node.tree.merge_with_fn(fragment.tree.clone(), |node_id| {
node.append_prev_siblings(&node_id)
});
}

self.remove()
Expand Down Expand Up @@ -350,9 +350,9 @@ impl<'a> Selection<'a> {
let fragment = Document::fragment(html);

for node in self.nodes().iter() {
let new_node_id = node.tree.get_new_id();
node.tree.merge(fragment.tree.clone());
node.append_children(&new_node_id);
node.tree.merge_with_fn(fragment.tree.clone(), |node_id| {
node.append_children(&node_id)
});
}
}

Expand All @@ -364,9 +364,9 @@ impl<'a> Selection<'a> {
let fragment = Document::fragment(html);

for node in self.nodes().iter() {
let new_node_id = node.tree.get_new_id();
node.tree.merge(fragment.tree.clone());
node.prepend_children(&new_node_id);
node.tree.merge_with_fn(fragment.tree.clone(), |node_id| {
node.prepend_children(&node_id)
});
}
}

Expand Down
Loading