Use Observe to hear about edits when you call Commit() on a parsed HTML tree.
dotnet add package RecursiveAI.OneHtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneHtml.Mutation; var doc = MutableDocument.Parse("<p>Hi"); using var watch = doc.Observe(c => Console.WriteLine( c.Changes.Attributes.Count)); var root = doc.DocumentElement; root.SetAttribute("lang", "en"); doc.Commit();
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 callback prints the number of elements whose attributes changed:
1The full program
Start with an empty <ul>, append an item, and read the inserted subtree from the commit. After the using block ends, another commit still succeeds but the callback no longer runs.
using RecursiveAI.OneHtml.Mutation; var doc = MutableDocument.Parse("<ul></ul>"); // html → body → ul var list = doc.DocumentElement.LastChild.FirstChild; using (doc.Observe(commit => { Console.WriteLine($"Commit {commit.Version}"); foreach (var insertion in commit.Changes.Inserted) Console.WriteLine($"Added: {insertion.Node.TextContent}"); })) { var item = doc.CreateElement("li"); item.SetTextContent("Pack lunch"); list.AppendChild(item); doc.Commit(); } list.AppendChild(doc.CreateElement("li")); var later = doc.Commit(); Console.WriteLine($"Committed after unsubscribe: {later.Version}");
dotnet run prints:
Commit 1 Added: Pack lunch Committed after unsubscribe: 2
If your callback never runs, check that you registered it before the edit and called Commit() while the registration was still active. Calling Commit() with no pending edits doesn’t notify it.
Variations
Run only when the list gains a child. Replace the using block header in the full program (keep its body). Observe subscribes to the whole document, so filter the reported insertions by parent:
using (doc.Observe(commit => { foreach (var insertion in commit.Changes.Inserted) if (insertion.Parent == list) Console.WriteLine(insertion.Node.LocalName); }))
The filtered callback prints li, followed by the program’s Committed after unsubscribe: 2 line.
Related recipes
The short answer
Register a callback with MutableDocument.Observe, make edits, then call Commit(). Your callback reads commit.Changes, which groups the changes by kind rather than recording every method call. Keep the returned IDisposable for as long as you want notifications. Disposing it stops future delivery to that callback; it doesn’t stop the document from accepting edits.
Is this the same as a browser MutationObserver?
Both let you react to changes instead of repeatedly reading the tree. A browser’s MutationObserver watches nodes in the browser and calls back with mutation records that match its options. onehtml’s Observe watches one MutableDocument in your C# process. It won’t report changes to a page open in a browser or edits made to the source file on disk.
onehtml runs the callbacks on the thread that calls Commit(), in registration order. You can read the supplied change set there without building a snapshot. If a callback commits more edits, onehtml queues that commit to deliver after the current one. A callback that repeatedly edits the document it watches can therefore trigger more notifications; check what changed before making another edit.
What DOM mutations can you observe?
Read Inserted, Removed, and Moved for structural changes; Attributes or Classes for changes to existing elements; and Text for changes to character data, such as editing an existing text node. When you insert a whole subtree, Inserted names its root, not every descendant. In the list example the text is already part of the new <li>, so the callback reads it from that node instead of looking for a separate text record.
The changes describe the result of a batch. If you give an existing element a temporary attribute value and replace it before committing, the observer sees the original and final values, not the temporary one. A commit can even notify you with an empty change list when there were pending edits but nothing is listed. Use commit.Changes.IsEmpty to tell whether there is anything in the change set to process. The mutable document reference lists the exact rules for combining edits.