MCPcopy Create free account
hub / github.com/Stranger6667/css-inline / insert_before

Method insert_before

css-inline/src/html/document.rs:362–384  ·  view source on GitHub ↗

Insert a new node right before a given sibling node. In general, this method transforms this relationship: ... <--> [Previous] <--> [Sibling] <--> [Next] --> ... into this: ... <--> [Previous] <--> [New] <--> [Sibling] <--> [Next] <--> ... If [Sibling] node is the first child (i.e., no [Previous] exists), the method also updates the parent node: Before: [Parent] ↓ [Sibling] <--> [Next] <-->

(&mut self, sibling: NodeId, node: NodeId)

Source from the content-addressed store, hash-verified

360 ///
361 /// So, [New] becomes the first child of [Parent].
362 pub(super) fn insert_before(&mut self, sibling: NodeId, node: NodeId) {
363 // Detach `node` from its current parent (if any)
364 self.detach(node);
365
366 // Set `node` parent to `sibling` parent
367 self[node].parent = self[sibling].parent;
368
369 // As it is inserted before, then `next_sibling` should point to `sibling`
370 self[node].next_sibling = Some(sibling);
371
372 if let Some(previous_sibling) = self[sibling].previous_sibling.take() {
373 // Connect `node` with the previous sibling (if any)
374 self[node].previous_sibling = Some(previous_sibling);
375 self[previous_sibling].next_sibling = Some(node);
376 } else if let Some(parent) = self[sibling].parent {
377 // No previous sibling - then it is the first child of its parent, so the parent node
378 // should be updated too
379 self[parent].first_child = Some(node);
380 }
381
382 // Now `node` is the previous sibling of the `sibling` node
383 self[sibling].previous_sibling = Some(node);
384 }
385
386 /// Returns an iterator over the direct children of a node.
387 pub(super) fn children(&self, node: NodeId) -> impl Iterator<Item = NodeId> + '_ {

Callers 2

test_insert_beforeFunction · 0.80
append_before_siblingMethod · 0.80

Calls 1

detachMethod · 0.80

Tested by 1

test_insert_beforeFunction · 0.64