htmldiff.net 1.4.2

dotnet add package htmldiff.net --version 1.4.2
NuGet\Install-Package htmldiff.net -Version 1.4.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="htmldiff.net" Version="1.4.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add htmldiff.net --version 1.4.2
#r "nuget: htmldiff.net, 1.4.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install htmldiff.net as a Cake Addin
#addin nuget:?package=htmldiff.net&version=1.4.2

// Install htmldiff.net as a Cake Tool
#tool nuget:?package=htmldiff.net&version=1.4.2

Project Description

A library for comparing two HTML files/snippets and highlighting the differences using simple HTML.

This HTML Diff implementation is a C# port of the ruby implementation found here.

Usage

Grab the latest stable version from Nuget: https://www.nuget.org/packages/htmldiff.net/

Example input:

string oldText = @"<p>This is some sample text to demonstrate the capability of the <strong>HTML diff tool</strong>.</p>
                    <p>It is based on the Ruby implementation found <a href='http://github.com/myobie/htmldiff'>here</a>. Note how the link has no tooltip</p>
                    <table cellpadding='0' cellspacing='0'>
                    <tr><td>Some sample text</td><td>Some sample value</td></tr>
                    <tr><td>Data 1 (this row will be removed)</td><td>Data 2</td></tr>
                    </table>";

string newText = @"<p>This is some sample text to demonstrate the awesome capabilities of the <strong>HTML diff tool</strong>.</p><br/><br/>Extra spacing here that was not here before.
                    <p>It is based on the Ruby implementation found <a title='Cool tooltip' href='http://github.com/myobie/htmldiff'>here</a>. Note how the link has a tooltip now and the HTML diff algorithm has preserved formatting.</p>
                    <table cellpadding='0' cellspacing='0'>
                    <tr><td>Some sample <strong>bold text</strong></td><td>Some sample value</td></tr>
                    </table>";

HtmlDiff diffHelper = new HtmlDiff(oldText, newText);
string diffOutput = diffHelper.Build();

The example above is included here

This is what the old and new HTML look like:

image

image

image

Styling

You can see that the algorithm as originally developed takes care of the nasty HTML parsing to figure out how to highlight the differences. The changes are marked up using “ins” and “del” tags. You can easily style these tags as I have done. The CSS below is responsible for rendering the differences as per the example.

ins {
	background-color: #cfc;
	text-decoration: none;
}

del {
	color: #999;
	background-color:#FEC8C8;
}
Blocks

The tokenizer works by running the diff on words, but sometimes this isn't ideal. For example, it may look clunky when a date is edited from 12 Jan 2022 to 14 Feb 2022. It might be neater to treat the diff on the entire date rather than the independent tokens.

You can achieve this using AddBlockExpression. Note, the Regex example is not meant to be exhaustive to cover all dates. If text matches the expression, the entire phrase is included as a single token to be compared, and that results in a much neater output.

diffHelper.AddBlockExpression(
  new Regex(@"[\d]{1,2}[\s]*(Jan|Feb)[\s]*[\d]{4}",
  RegexOptions.IgnoreCase));
var diff = diffHelper.Build();

Building and Testing (CLI)

Requirements

  • Node 18+

Install dependencies: npm install

Building

In the root folder of the repo:

npm run build

Running Tests

In the root folder of the repo:

npm test

Release

npm run build-release
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on htmldiff.net:

Package Downloads
SitecoreSidekickContentMigrator

Easily move production Sitecore content to pre-production environments with this Sidekick module.

SitecoreSidekickAuditLog

An app to graphically log and search author activity within Sitecore.

Blazorme.Diff

Diff component library for Blazor apps. It will render diff of the two input strings in different output display formats. It can also be used as a library to get diff or html diff output strings from code behind.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.2 20,570 10/27/2023
1.4.1 96,524 5/29/2023
1.4.0 1,132,452 5/23/2019
1.3.0 586,019 6/9/2016
1.2.0 24,565 2/3/2016
1.1.0 125,808 5/31/2012

1.4.2 - addresses an issue with text decoration (see https://github.com/Rohland/htmldiff.net/pull/59)
     1.4.1 - dependency updates + bug fix for custom block expressions
     1.4.0 - dotnet standard support