Save a Snapshot() before editing, then use ChangesSince to read the net changes.
dotnet add package RecursiveAI.OneHtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneHtml.Mutation; var doc = MutableDocument.Parse( "<html lang=en>"); var before = doc.Snapshot(); var root = doc.DocumentElement; root.SetAttribute("lang", "es"); doc.Commit(); var diff = doc.ChangesSince(before); var lang = diff.Attributes[0].Deltas[0]; Console.WriteLine($"{lang.Old} -> {lang.New}");
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 change set gives the old and new lang values:
en -> esThe full program
Across two commits, the <html> language changes from en to fr to es. A data-preview attribute appears in the first commit and disappears in the second. The diff names only the lang change that remains.
using RecursiveAI.OneHtml.Mutation; var doc = MutableDocument.Parse("<html lang=en><title>News</title>"); var root = doc.DocumentElement; var before = doc.Snapshot(); root.SetAttribute("lang", "fr"); root.SetAttribute("data-preview", "true"); doc.Commit(); root.SetAttribute("lang", "es"); root.RemoveAttribute("data-preview"); doc.Commit(); var diff = doc.ChangesSince(before); Console.WriteLine($"Versions: {diff.FromVersion} -> {diff.ToVersion}"); foreach (var change in diff.Attributes) foreach (var delta in change.Deltas) Console.WriteLine($"{delta.LocalName}: {delta.Old} -> {delta.New}"); Console.WriteLine($"Approximate: {diff.IsApproximate}");
dotnet run prints:
Versions: 0 -> 2 lang: en -> es Approximate: False
Commit your edits before asking for the diff: ChangesSince throws InvalidOperationException while edits are pending, and ArgumentException if the snapshot came from a different mutable document.
Variations
Recognize when the baseline is too old. In the full program, replace the parse line with this one. Leave both commits in place; the journal will retain only the most recent one.
var doc = MutableDocument.Parse("<html lang=en><title>News</title>", options: new MutableDocumentOptions { JournalCommits = 1 });
Versions: 0 -> 2 Approximate: True
No attribute lines are printed because the per-node lists aren’t populated. Check IsApproximate before using an empty list as evidence that nothing changed.
Related recipes
The short answer
Save var before = doc.Snapshot() from a MutableDocument, commit your edits, and call doc.ChangesSince(before) on that same document. This gives you a DocumentChanges from the saved version to the latest commit. It’s a net diff, not a replay: in the two-commit example fr never appears in the diff because the final value is es.
What is an HTML DOM snapshot?
A onehtml WorkingSnapshot holds the state of the HTML tree at a committed version. Its Document is immutable: later commits don’t change what you read from it. Use the snapshot when you want to query the old state after edits are made, or keep it as a baseline for a later diff. Reads from a mutable node, by contrast, can see edits before they’re committed.
This is a snapshot of the parsed tree, not a screenshot or a copy of the source file’s formatting. Later snapshots contain tree data but no original source bytes. A snapshot node index is its position in that version’s tree; moving or inserting nodes may change those positions. When you need to follow an element across versions, use the mutable node identity and the snapshot’s node mapping rather than comparing indexes.
How do you compare two versions of the same DOM?
Keep the earlier snapshot from the same MutableDocument you’ll edit. ChangesSince compares it with the latest commit; it doesn’t take two arbitrary HTML documents and match their elements. For an exact result, read Inserted, Removed, and Moved for structure, or Attributes, Classes, and Text for changes to existing nodes. Each attribute delta gives the old and new value, with null marking an absent attribute. You don’t have to serialize both trees and compare lines of markup.
Can you still get a diff if the snapshot is old?
Keeping a snapshot doesn’t keep the journal entries needed for an exact diff. By default onehtml retains 256 commits in the journal. If it no longer reaches the baseline version, IsApproximate is true and you can’t use the empty lists to decide which individual nodes stayed the same. For example, if the diff drives a cached list of links, rebuild the list from the current snapshot rather than trying to patch it. Then keep that newer snapshot as the baseline for the next comparison.