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>> where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Debug,
LEAF: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug,
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]),
)
]
);