Call AppendChild on the new parent to move the existing node. Look up the element in the read-only snapshot, use ToMutable to get an editable handle, and call Commit() to make your edits visible in the next snapshot.
dotnet add package RecursiveAI.OneHtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneHtml.Mutation; var doc = MutableDocument.Parse( "<ul><li>Tea</li></ul>" + "<ol id='to'></ol>"); var start = doc.Snapshot(); var target = start.Document.GetElementById("to"); var to = start.ToMutable(target); var item = to.PreviousSibling.FirstChild; var key = item.Key; to.AppendChild(item); doc.Commit(); Console.WriteLine(doc.SerializeNode(to)); Console.WriteLine(key == item.Key);
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
The new list contains Tea, and True means the node kept its key:
<ol id="to"><li>Tea</li></ol> True
The full program
The sample lists are adjacent, so to.PreviousSibling is the old list. The full program also prints that now-empty list and checks that the new snapshot maps back to the same mutable node.
using RecursiveAI.OneHtml.Mutation; var doc = MutableDocument.Parse( "<ul><li>Tea</li></ul>" + "<ol id='to'></ol>"); var start = doc.Snapshot(); var target = start.Document.GetElementById("to"); var to = start.ToMutable(target); var item = to.PreviousSibling.FirstChild; var key = item.Key; to.AppendChild(item); doc.Commit(); Console.WriteLine(doc.SerializeNode(to)); Console.WriteLine(key == item.Key); var after = doc.Snapshot(); Console.WriteLine(doc.SerializeNode(to.PreviousSibling)); Console.WriteLine(after.ToMutable(after.ToNode(item)) == item);
<ol id="to"><li>Tea</li></ol> True <ul></ul> True
If you try to move a node into its own descendant, AppendChild throws DomException before changing the tree.
Variations
Insert before an existing sibling. Add this after the full program to put Tea before Coffee. The reference node must already be a child of to.
var coffee = doc.CreateElement("li"); coffee.SetTextContent("Coffee"); to.PrependChild(coffee); to.InsertBefore(item, coffee); doc.Commit(); Console.WriteLine(doc.SerializeChildren(to));
<li>Tea</li><li>Coffee</li>Related recipes
The short answer
Call newParent.AppendChild(node) when both handles belong to the same MutableDocument. If the node already has a parent, onehtml removes it from there and appends it to the end of the new parent’s children. The node’s Key stays the same. Commit the edit when you want an immutable snapshot of the result.
Does appendChild move or copy an existing DOM node?
It moves the node. Like the browser’s appendChild, onehtml doesn’t ask you to remove it first. The original list in the program becomes empty, and the destination gets the original <li>, not a second element with the same text. A node’s descendants move with it. If you want the original to stay put, use CloneNode for a copy in the same mutable document, or Import for a copy in a different document.
The node’s position can change without its identity changing. For example, an editor can associate a note with a mutable node and still find it after that node moves. Use MutableNode as the dictionary key, or use Key in a dictionary scoped to one document. A DOM id attribute is just markup; it isn’t the same as the mutable handle or its key.
How do I insert a DOM node before another child?
Use InsertBefore on the new parent when the position matters. Pass the existing node you want to move, then the destination child that should follow it. In the variation, Coffee is that reference child. You don’t have to clone Tea or work out a numeric child index. For a move to the beginning, PrependChild inserts before the current first child.
How do I find a moved DOM node after a commit?
Keep the MutableNode handle and call ToNode(item) on the committed snapshot. A snapshot Node points to a location in one immutable Document, so don’t carry that location over as a numeric index in the next snapshot. The snapshot mapping works both ways: the full program maps the saved handle into the new snapshot, then uses ToMutable to recover the same handle. The older snapshot still shows the item in its original list.