TextCompareForBlazor 1.0.0
See the version list below for details.
dotnet add package TextCompareForBlazor --version 1.0.0
NuGet\Install-Package TextCompareForBlazor -Version 1.0.0
<PackageReference Include="TextCompareForBlazor" Version="1.0.0" />
<PackageVersion Include="TextCompareForBlazor" Version="1.0.0" />
<PackageReference Include="TextCompareForBlazor" />
paket add TextCompareForBlazor --version 1.0.0
#r "nuget: TextCompareForBlazor, 1.0.0"
#:package TextCompareForBlazor@1.0.0
#addin nuget:?package=TextCompareForBlazor&version=1.0.0
#tool nuget:?package=TextCompareForBlazor&version=1.0.0
TextCompareForBlazor
TextCompareForBlazor is a lightweight, client-side Blazor (WebAssembly or Server) component for visually comparing two text blobs side-by-side with selectable diff granularity (Character / Word / Line), optional whitespace normalization, synchronized scrolling, line alignment (with gap placeholders), and customizable theming.
Features
- Side-by-side synchronized scroll viewing (toggleable)
- Granularity selector: Character, Word, or Line
- Word diff uses token mapping to preserve original spacing
- Intelligent line alignment (modified / inserted / deleted handling)
- Optional whitespace collapsing (
IgnoreWhitespace) - Optional NBSP width preservation for deleted/inserted tokens (
UseTokenPadding) - Line numbers (toggleable)
- Footer metrics: lines, words, characters, differences
- Fully styleable via parameters + CSS variable hooks
- Accessible structure: ARIA labels, focusable line items
Getting Started
Add a project/package reference to TextCompareForBlazor (project reference or NuGet, if published). Then:
Set originalText / modifiedText in your page/component C# code.
Minimal Example (.razor)
<CompareText Text="@textA" Text2="@textB" Width="800px" Height="600px" />
@code {
string textA = "...";
string textB = "...";
}
Component Parameters
| Name | Type | Default | Description |
|---|---|---|---|
LeftCaption |
string |
Original |
Caption above left panel. |
RightCaption |
string |
Modified |
Caption above right panel. |
LeftText |
string |
"" |
Source/original content. |
RightText |
string |
"" |
Target/modified content. |
ShowGranularitySelector |
bool |
true |
Shows the Character/Word/Line dropdown. |
ShowIgnoreWhitespace |
bool |
true |
Toggle to collapse whitespace runs. |
ShowSyncToggle |
bool |
true |
Toggle for scroll sync. |
ShowLineNumbers |
bool |
true |
Displays gutter line numbers. |
SyncScrolling |
bool |
true |
Initial sync state (can be toggled). |
IgnoreWhitespace |
bool |
false |
Collapses all whitespace to single spaces for comparison. |
UseTokenPadding |
bool |
false |
Pads deleted/inserted word tokens with NBSPs to retain horizontal alignment. |
ShowFooter |
bool |
true |
Shows metrics footer. |
Granularity |
DiffGranularity |
Character |
Current diff level: Character, Word, or Line. |
UserStyle |
string? |
width:auto; height:auto; |
Inline style appended to root container. |
| Styling params | string |
(various) | Colors / theming (caption, gutter, diff highlights). |
Styling Parameters
| Param | Purpose | CSS Variable |
|---|---|---|
CaptionBackground |
Caption background | --caption-bg |
CaptionTextColor |
Caption text color | --caption-text |
FooterBackground |
Footer background | --footer-bg |
FooterTextColor |
Footer text | --footer-text |
GutterBackground |
Line number gutter | --gutter-bg |
GutterNumberColor |
Line number color | --gutter-number-color |
LeftDiffColor |
Highlight for left diffs | --left-diff |
RightDiffColor |
Highlight for right diffs | --right-diff |
EmptyLineBackground |
Placeholder line background | --empty-line-bg |
EmptyLineBorder |
Placeholder line border | --empty-line-border |
Override by passing parameter values or extend with a wrapping CSS class via UserStyle.
Granularity Behavior
Character: Fine-grained semantic diff (uses DiffMatchPatch) on each line.Word: Tokenizes into whitespace + word runs; preserves spacing; optional NBSP padding.Line: Each aligned line treated as a single segment (modified vs equal).
Alignment Logic
Lines are normalized (optionally whitespace collapsed), then encoded into tokens for diffing. Deletes followed immediately by inserts are paired as modified lines; pure inserts/deletes create blank counterpart placeholders for visual alignment.
Accessibility
- Panels have
aria-labelregions. - Lines are focusable (
tabindex="0"), enabling keyboard navigation. - Placeholder lines include a
titlefor context.
JavaScript Interop
Scroll synchronization uses a tiny JS module (tsCompareSync) injected inline. No external script file needed. If you bundle scripts separately, you may extract that <script> block.
Performance Notes
- DiffMatchPatch is efficient for typical text sizes; extremely large files (> several MB) may benefit from pre-truncation.
- Word granularity token mapping uses in-memory dictionaries; memory footprint proportional to distinct tokens.
- Re-render triggers recomputation when
IgnoreWhitespaceorGranularitychanges�cache externally if needed.
Extensibility Ideas
- Add export of diff summary (counts)
- Provide virtualized rendering for very large files (wrap lines list)
- Add unified diff view mode (single panel) or inline change markers
Example With Custom Theme
<SideBySide Text="@textA" Text2="@textB"
LeftCaption="Before" RightCaption="After"
IgnoreWhitespace="true" Granularity="Word"
UserStyle="--caption-bg:#f0f0f0; --footer-bg:#f8f8f8;"
Width="800px" Height="600px" />
@code {
string textA = "...";
string textB = "...";
}
Footer Metrics
Lines= aligned line pair countWords/charactersrefer to right side counts (you can extend to show both)Differences= number of non-equal segments under current granularity
Namespace
Use @using TextCompareForBlazor.Components (or add in _Imports.razor).
License / Attribution
Uses DiffMatchPatch via NuGet (DiffMatchPatch 3.0.0). Ensure compliance with its license (Apache 2.0).
Questions or improvements? Extend SideBySideTextCompareBase in your own partial class for additional behaviors.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- DiffMatchPatch (>= 3.0.0)
- Microsoft.AspNetCore.Components.Web (>= 8.0.22)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release:
- Side-by-side diff rendering
- Character / word / line granularity
- Blazor Server and WebAssembly compatible