Function mt_dom::diff::diff_with_key
source · pub fn diff_with_key<'a, Ns, Tag, Leaf, Att, Val>(
old_node: &'a Node<Ns, Tag, Leaf, Att, Val>,
new_node: &'a Node<Ns, Tag, Leaf, Att, Val>,
key: &Att
) -> Vec<Patch<'a, Ns, Tag, Leaf, Att, Val>>
Expand description
Return the patches needed for old_node
to have the same DOM as new_node
§Agruments
- old_node - the old virtual dom node
- new_node - the new virtual dom node
- key - the literal name of key attribute, ie: “key”
§Example
use mt_dom::{diff::*, patch::*, *};
pub type MyNode =
Node<&'static str, &'static str, &'static str, &'static str, &'static str>;
let old: MyNode = element(
"main",
vec![attr("class", "container")],
vec![
element("div", vec![attr("key", "1")], vec![]),
element("div", vec![attr("key", "2")], vec![]),
],
);
let new: MyNode = element(
"main",
vec![attr("class", "container")],
vec![element("div", vec![attr("key", "2")], vec![])],
);
let diff = diff_with_key(&old, &new, &"key");
assert_eq!(
diff,
vec![Patch::remove_node(
Some(&"div"),
TreePath::new(vec![ 0]),
)
]
);