DEV Community

Cover image for Get Diff and Patch Html
Antoine
Antoine

Posted on

Get Diff and Patch Html

Photo by Markus Spiske on Diff.Match.Patch based on Google library.

Diff

Diff are made of:

  • text (part of the text related)
  • operation to be applied (delete / insert / equal)

It's easy to get diff:

    string a = "<SPAN TITLE=\"i=0\">a&para;<BR></SPAN><DEL STYLE=\"background:#FFE6E6;\" TITLE=\"i=2\">&lt;B&gt;b&lt;/B&gt;</DEL><INS STYLE=\"background:#E6FFE6;\" TITLE=\"i=2\">c&amp;d</INS>";
    string b = "<SPAN TITLE=\"i=0\">testa&para;<BR></SPAN><DEL STYLE=\":#FFE6E6;\" TITLE=\"i=2\">&lt;B&gt;b&lt;/B&gt;</DEL><INS STYLE=\"background:#E6FFE6;\" TITLE=\"i=2\">c&amp;d</INS>";

var diffs = DiffMatchPatchModule.Default.DiffMain(a, b);

Enter fullscreen mode Exit fullscreen mode

Please note that diffs will contains a lot of Equal diff !
Result: Result

Patch

It's simple too to get patch from multiple texts!

    string a = "<SPAN TITLE=\"i=0\">a&para;<BR></SPAN><DEL STYLE=\"background:#FFE6E6;\" TITLE=\"i=2\">&lt;B&gt;b&lt;/B&gt;</DEL><INS STYLE=\"background:#E6FFE6;\" TITLE=\"i=2\">c&amp;d</INS>";
    string b = "<SPAN TITLE=\"i=0\">testa&para;<BR></SPAN><DEL STYLE=\":#FFE6E6;\" TITLE=\"i=2\">&lt;B&gt;b&lt;/B&gt;</DEL><INS STYLE=\"background:#E6FFE6;\" TITLE=\"i=2\">c&amp;d</INS>";
    string c = "<SPAN TITLE=\"i=0\">a&para;<BR></SPAN><DEL STYLE=\"background:#FFE6E6;\" TITLE=\"i=2\">mon tittre en Or&lt;B&gt;b&lt;/B&gt;</DEL><INS STYLE=\"background:#E6FFE6;\" TITLE=\"i=2\">c&amp;d</INS>";

    var firstPatch = DiffMatchPatchModule.Default.PatchMake(a, b);
    var secondPatch = DiffMatchPatchModule.Default.PatchMake(a, c);
    DiffMatchPatchModule.Default.PatchApply(secondPatch, DiffMatchPatchModule.Default.PatchApply(firstPatch, a)[0].ToString());

Enter fullscreen mode Exit fullscreen mode

Ouptut:

<SPAN TITLE="i=0">testa&para;<BR></SPAN><DEL STYLE=":#FFE6E6;" TITLE="i=2">mon tittre en Or&lt;B&gt;b&lt;/B&gt;</DEL><INS STYLE="background:#E6FFE6;" TITLE="i=2">c&amp;d</INS>
Enter fullscreen mode Exit fullscreen mode

Please note that DiffMatchPatchModule.Default.PatchApply(firstPatch, a) outputs object[] !

Hope that helps !

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay