1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
//! patch module
use crate::node::Text;
use crate::{Attribute, Node};
use std::fmt::Debug;
pub use tree_path::TreePath;
mod tree_path;
/// A Patch encodes an operation that modifies a real DOM element or native UI element
///
/// To update the real DOM that a user sees you'll want to first diff your
/// old virtual dom and new virtual dom.
///
/// This diff operation will generate `Vec<Patch>` with zero or more patches that, when
/// applied to your real DOM, will make your real DOM look like your new virtual dom.
///
/// Each of the Patch contains `TreePath` which contains an array of indexes for each node
/// that we need to traverse to get the target element.
///
/// Consider the following html:
///
/// ```html
/// <body>
/// <main>
/// <input type="text"/>
/// <img src="pic.jpg"/>
/// </main>
/// <footer>
/// <a>Link</a>
/// <nav/>
/// </footer>
/// </body>
/// ```
/// The corresponding DOM tree would be
/// ```bob
/// .─.
/// ( 0 ) <body>
/// `-'
/// / \
/// / \
/// / \
/// ▼ ▼
/// <main> .─. .─. <footer>
/// ( 0 ) ( 1 )
/// `-' `-'
/// / \ | \ '.
/// / \ | \ '.
/// ▼ ▼ | \ '.
/// .─. .─. ▼ ▼ ▼
/// ( 0 ) ( 1 ) .─. .─. .─.
/// `─' `─' ( 0 ) ( 1 ) ( 2 )
/// <input> <img> `─' `─' `─'
/// <a> <Text> <nav>
/// ```
/// To traverse to the `<nav>` element we follow the TreePath([0,1,2]).
/// 0 - is the root element which is always zero.
/// 1 - is the `footer` element since it is the 2nd element of the body.
/// 2 - is the `nav` element since it is the 3rd node in the `footer` element.
#[derive(Clone, Debug, PartialEq)]
pub enum Patch<'a, NS, TAG, ATT, VAL>
where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug,
{
/// Insert a vector of child nodes to the current node being patch.
/// The usize is the index of of the children of the node to be
/// patch to insert to. The new children will be inserted before this usize
InsertNode {
/// the tag of the target node we insert this node into
tag: Option<&'a TAG>,
/// the path to traverse to get to the target element of which our node will be inserted before it.
patch_path: TreePath,
/// the node to be inserted
node: &'a Node<NS, TAG, ATT, VAL>,
},
/// Append a vector of child nodes to a parent node id.
AppendChildren {
/// the tag of the node we are appending the children into
tag: &'a TAG,
/// index of the node we are going to append the children into
patch_path: TreePath,
/// children nodes to be appended and their corresponding new_node_idx
children: Vec<&'a Node<NS, TAG, ATT, VAL>>,
},
/// remove node
RemoveNode {
/// The tag of the node that is to be removed.
/// This is only used for additional check where are removing the correct node.
tag: Option<&'a TAG>,
/// the node_idx of the node to be removed
patch_path: TreePath,
},
/// ReplaceNode a node with another node. This typically happens when a node's tag changes.
/// ex: <div> becomes <span>
ReplaceNode {
/// The tag of the node we are going to replace.
/// This is only used for additional checking that we are removing the correct node.
tag: Option<&'a TAG>,
/// the traversal path of the node we are going to replace
patch_path: TreePath,
/// the node that will replace the target node
replacement: &'a Node<NS, TAG, ATT, VAL>,
},
/// Add attributes that the new node has that the old node does not
/// Note: the attributes is not a reference since attributes of same
/// name are merged to produce a new unify attribute
AddAttributes {
/// node tag
/// use for verifying that the we are patching the correct node which
/// should match the same tag
tag: &'a TAG,
/// the path to traverse to get to the target lement of which we add the attributes.
patch_path: TreePath,
/// the attributes to be patched into the target node
attrs: Vec<&'a Attribute<NS, ATT, VAL>>,
},
/// Remove attributes that the old node had that the new node doesn't
RemoveAttributes {
/// The tag of the node we are removing the attributes from.
/// This is only used for additional check that we are patching the correct node
tag: &'a TAG,
/// the path to traverse to get to the target lement of which we remove the attributes
patch_path: TreePath,
/// attributes that are to be removed from this target node
attrs: Vec<&'a Attribute<NS, ATT, VAL>>,
},
/// Change the text of a Text node.
ChangeText {
/// the target element to be patch can be traverse using this patch path
patch_path: TreePath,
/// the old text is not really needed for applying the patch.
/// but it is useful for debugging purposed, that we are changing the intended target text by
/// visual inspection
old: &'a Text,
/// the neew text patch
new: &'a Text,
},
/// Change comment content of a Comment node
ChangeComment {
/// the target element to be patch can be traverse using this patch path
patch_path: TreePath,
/// old comment
old: &'a String,
/// new comment
new: &'a String,
},
}
impl<'a, NS, TAG, ATT, VAL> Patch<'a, NS, TAG, ATT, VAL>
where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug,
{
/// return the path to traverse for this patch to get to the target Node
pub fn path(&self) -> &[usize] {
match self {
Patch::InsertNode { patch_path, .. } => &patch_path.path,
Patch::AppendChildren { patch_path, .. } => &patch_path.path,
Patch::RemoveNode { patch_path, .. } => &patch_path.path,
Patch::ReplaceNode { patch_path, .. } => &patch_path.path,
Patch::AddAttributes { patch_path, .. } => &patch_path.path,
Patch::RemoveAttributes { patch_path, .. } => &patch_path.path,
Patch::ChangeText { patch_path, .. } => &patch_path.path,
Patch::ChangeComment { patch_path, .. } => &patch_path.path,
}
}
/// return the tag of this patch
pub fn tag(&self) -> Option<&TAG> {
match self {
Patch::InsertNode { tag, .. } => *tag,
Patch::AppendChildren { tag, .. } => Some(tag),
Patch::RemoveNode { tag, .. } => *tag,
Patch::ReplaceNode { tag, .. } => *tag,
Patch::AddAttributes { tag, .. } => Some(tag),
Patch::RemoveAttributes { tag, .. } => Some(tag),
Patch::ChangeText { .. } => None,
Patch::ChangeComment { .. } => None,
}
}
/// create an InsertNode patch
pub fn insert_node(
tag: Option<&'a TAG>,
patch_path: TreePath,
node: &'a Node<NS, TAG, ATT, VAL>,
) -> Patch<'a, NS, TAG, ATT, VAL> {
Patch::InsertNode {
tag,
patch_path,
node,
}
}
/// create a patch where we add children to the target node
pub fn append_children(
tag: &'a TAG,
patch_path: TreePath,
children: Vec<&'a Node<NS, TAG, ATT, VAL>>,
) -> Patch<'a, NS, TAG, ATT, VAL> {
Patch::AppendChildren {
tag,
patch_path,
children,
}
}
/// create a patch where the target element that can be traverse
/// using the patch path will be remove
pub fn remove_node(
tag: Option<&'a TAG>,
patch_path: TreePath,
) -> Patch<'a, NS, TAG, ATT, VAL> {
Patch::RemoveNode { tag, patch_path }
}
/// create a patch where a node is replaced by the `replacement` node.
/// The target node to be replace is traverse using the `patch_path`
pub fn replace_node(
tag: Option<&'a TAG>,
patch_path: TreePath,
replacement: &'a Node<NS, TAG, ATT, VAL>,
) -> Patch<'a, NS, TAG, ATT, VAL> {
Patch::ReplaceNode {
tag,
patch_path,
replacement,
}
}
/// create a patch where a new attribute is added to the target element
pub fn add_attributes(
tag: &'a TAG,
patch_path: TreePath,
attrs: Vec<&'a Attribute<NS, ATT, VAL>>,
) -> Patch<'a, NS, TAG, ATT, VAL> {
Patch::AddAttributes {
tag,
patch_path,
attrs,
}
}
/// create patch where it remove attributes of the target element that can be traversed by the
/// patch_path.
pub fn remove_attributes(
tag: &'a TAG,
patch_path: TreePath,
attrs: Vec<&'a Attribute<NS, ATT, VAL>>,
) -> Patch<'a, NS, TAG, ATT, VAL> {
Patch::RemoveAttributes {
tag,
patch_path,
attrs,
}
}
/// create a patch where the text content of a text node is changed
pub fn change_text(
patch_path: TreePath,
old: &'a Text,
new: &'a Text,
) -> Patch<'a, NS, TAG, ATT, VAL> {
Patch::ChangeText {
patch_path,
old,
new,
}
}
/// A patch where the comment node is changed
pub fn change_comment(
patch_path: TreePath,
old: &'a String,
new: &'a String,
) -> Patch<'a, NS, TAG, ATT, VAL> {
Patch::ChangeComment {
patch_path,
old,
new,
}
}
}