pub enum Patch<'a> {
Show 14 variants
AppendChildren {
parent_old_node_idx: u32,
new_nodes: Vec<&'a VirtualNode>,
},
MoveToEndOfSiblings {
parent_old_node_idx: u32,
siblings_to_move: Vec<u32>,
},
RemoveChildren {
parent_old_node_idx: u32,
to_remove: Vec<u32>,
},
Replace {
old_idx: u32,
new_node: &'a VirtualNode,
},
InsertBefore {
anchor_old_node_idx: u32,
new_nodes: Vec<&'a VirtualNode>,
},
MoveNodesBefore {
anchor_old_node_idx: u32,
to_move: Vec<u32>,
},
ValueAttributeUnchanged(u32, &'a AttributeValue),
AddAttributes(u32, HashMap<&'a str, &'a AttributeValue>),
RemoveAttributes(u32, Vec<&'a str>),
ChangeText(u32, &'a VText),
SpecialAttribute(PatchSpecialAttribute<'a>),
AddEvents(u32, HashMap<&'a EventName, &'a EventHandler>),
RemoveEvents(u32, Vec<(&'a EventName, &'a EventHandler)>),
RemoveAllVirtualEventsWithNodeIdx(u32),
}
Expand description
A Patch encodes an operation that modifies a real DOM 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 Patch has a u32 node index that helps us identify the real DOM node that it applies to.
Our old virtual dom’s nodes are indexed breadth first, as shown in this illustration (0 being the root node, 1 being it’s first child, 2 being it’s second child).
.─.
( 0 )
`┬'
┌────┴──────┐
│ │
▼ ▼
.─. .─.
( 1 ) ( 2 )
`┬' `─'
┌────┴───┐ │
│ │ ├─────┬─────┐
▼ ▼ │ │ │
.─. .─. ▼ ▼ ▼
( 3 ) ( 4 ) .─. .─. .─.
`─' `─' ( 5 ) ( 6 ) ( 7 )
`─' `─' `─'
The patching process is tested in a real browser in crates/percy-dom/tests/diff_patch.rs
Variants§
AppendChildren
Append a vector of child nodes to a parent node id.
MoveToEndOfSiblings
Move the nodes to be the last of their parent’s children.
RemoveChildren
Fields
Remove child nodes.
Replace
Replace a node with another node. This typically happens when a node’s tag changes. ex:
InsertBefore
Fields
new_nodes: Vec<&'a VirtualNode>
Insert a new element before some other sibling element.
MoveNodesBefore
Fields
Move nodes to be before some other node.
ValueAttributeUnchanged(u32, &'a AttributeValue)
The value attribute of a textarea or input element has not changed, but we will still patch it anyway in case something was typed into the field.
AddAttributes(u32, HashMap<&'a str, &'a AttributeValue>)
Add attributes that the new node has that the old node does not
RemoveAttributes(u32, Vec<&'a str>)
Remove attributes that the old node had that the new node doesn’t
ChangeText(u32, &'a VText)
Change the text of a Text node.
SpecialAttribute(PatchSpecialAttribute<'a>)
Patches that apply to [SpecialAttributes
].
AddEvents(u32, HashMap<&'a EventName, &'a EventHandler>)
Insert events in the EventsByNodeIdx. If it is a non-delegated event the event will also get added to the DOM node.
RemoveEvents(u32, Vec<(&'a EventName, &'a EventHandler)>)
Remove events from the EventsByNodeIdx. If it is a non-delegated event the event will also get removed from the DOM node.
RemoveAllVirtualEventsWithNodeIdx(u32)
Delete all events in the EventsByNodeIdx for the given index, since the node has been removed from the DOM.