{"record":{"id":"6e038c71d9365c31","repo":"leptos-rs/leptos","slug":"anchor-is-not-a-child-of-the-parent","errorCode":null,"errorMessage":"anchor is not a child of the parent","messagePattern":"anchor is not a child of the parent","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tachys/src/renderer/mock_dom.rs","lineNumber":493,"sourceCode":"        debug_assert!(&parent.0 != new_child);\n        // remove if already mounted\n        if let Some(parent) = MockDom::get_parent(new_child) {\n            let parent = Element(parent);\n            MockDom::remove_node(&parent, new_child);\n        }\n        // mount on new parent\n        Document::with_node_mut(parent.0 .0, |parent| {\n            if let NodeType::Element {\n                ref mut children, ..\n            } = parent.ty\n            {\n                match anchor {\n                    None => children.push(new_child.clone()),\n                    Some(anchor) => {\n                        let anchor_pos = children\n                            .iter()\n                            .position(|item| item.0 == anchor.0)\n                            .expect(\"anchor is not a child of the parent\");\n                        children.insert(anchor_pos, new_child.clone());\n                    }\n                }\n            } else {\n                panic!(\"parent is not an element\");\n            }\n        });\n        // set parent on child node\n        Document::with_node_mut(new_child.0, |node| {\n            node.parent = Some(parent.0 .0)\n        });\n    }\n\n    fn remove_node(\n        parent: &Self::Element,\n        child: &Self::Node,\n    ) -> Option<Self::Node> {\n        let child = Document::with_node_mut(parent.0 .0, |parent| {","sourceCodeStart":475,"sourceCodeEnd":511,"githubUrl":"https://github.com/leptos-rs/leptos/blob/32d20f6c9d517451d77beb68d88c7716823dac41/tachys/src/renderer/mock_dom.rs#L475-L511","documentation":"MockRenderer::insert_node inserts a child before an anchor by finding the anchor's position in the parent's children list; if the anchor is not among the parent's children, position() returns None and it panics. The mock DOM mirrors real renderer invariants, so this flags bookkeeping drift between parent/anchor tracking.","triggerScenarios":"Calling insert_node with an anchor belonging to a different parent, an anchor already removed from the mock DOM, or stale anchor references after mock-DOM mutations; also implicitly when parent bookkeeping (children vec) diverges from anchor references.","commonSituations":"Unit/integration tests using the mock DOM renderer with manually manipulated children; view reconciliation code paths that move anchors without updating the parent's child list; cloning anchors across trees in tests.","solutions":["Ensure the anchor passed to insert_node is a current child of the same parent node","Update anchors after moving/removing nodes in the mock DOM before re-inserting","Re-render or rebuild the mock tree when structural tests mutate children directly","If testing reconciliation, use the renderer's own move/remove APIs instead of poking the children vec"],"exampleFix":"// before\nmock.insert_node(&mut node, &stale_anchor); // panics\n// after\nassert!(parent.children().any(|c| c == stale_anchor));\nmock.insert_node(&mut node, &stale_anchor); // anchor verified in parent\n","handlingStrategy":"validation","validationCode":"fn is_child(parent: &MockNode, anchor: &MockNode) -> bool {\n    parent.children().iter().any(|c| c.id == anchor.id)\n}\n// call before: assert!(is_child(&parent, &anchor));","typeGuard":"fn find_anchor_pos(children: &[MockNode], anchor: &MockNode) -> Option<usize> {\n    children.iter().position(|c| c.id == anchor.id)\n}","tryCatchPattern":null,"preventionTips":["Always insert/move anchors through the renderer's own APIs","After removing/moving nodes, refresh anchor references before re-use","In tests, assert the anchor is present in the parent before insert_node","Avoid cloning anchors between different mock trees"],"tags":["mock-dom","testing","panic"],"backgroundTag":"anchor-not-child","analyzedSha":"32d20f6c9d517451d77beb68d88c7716823dac41","analyzedAt":"2026-09-01T18:55:42.580Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T01:17:15.007Z"}