Skip to content

DiffViewer Text Diff ​

A read-only text diff viewer: line-level differences with word-level highlights, unified and split views, collapsed unchanged regions, fixed-row-height virtual scrolling, and full keyboard/screen-reader support. Suited to config version comparison, prompt review, and formatted JSON before/after checks. It does not edit, merge, accept/reject changes, or compute structural JSON diffs.

Basic usage ​

Pass the two texts through old-text / new-text. The default unified view marks deleted lines with − and added lines with +, showing both line-number columns. Unchanged runs longer than context (default 3) collapse into an expandable row.

Config file versions

Word-level highlighting only marks the truly changed fragments of paired replacement lines; replacement blocks with unequal line counts stay line-level to avoid misleading matches.

Empty and identical ​

Empty inputs show a placeholder; identical inputs keep every row and show “Both sides are identical”. Both states downgrade ARIA semantics so no empty grid structure is exposed.

Empty and identical

Split view ​

mode="split" renders two columns: old text on the start side, new text on the end side (mirrored automatically in RTL), aligned by change block. old-label / new-label override the column headers.

Side-by-side review

When the container is narrower than split-min-width, the viewer degrades to unified (the mode attribute keeps its value) so neither side becomes unreadable. Split rows use data-op="replace"; the real insert/delete op lives on each before-code / after-code cell — read the cell when styling via ::part().

Folding and bulk actions ​

Fold rows are clickable, focusable with Tab, and expandable with Enter. expandAll() / collapseAll() switch all regions at once; fold state resets when the data changes so old positions cannot point at new content.

Expand all / collapse unchanged

Expand allCollapse unchangedSimulate update

Last event: —

Long files and virtual scrolling ​

When aligned rows exceed virtual-threshold (default 2000), fixed-row-height virtual scrolling keeps only viewport-near rows in the DOM. virtual="true" forces it on, virtual="false" turns it off. height and row-height control the viewport and row height.

3000 lines, viewport only

Generating data…

Comparison strategy ​

ignore-whitespace normalizes whitespace before comparing (runs collapsed, line edges trimmed) while displaying the original text. max-edits (default 1000) caps the line-level edit distance; beyond it the viewer degrades to block replacement and shows a notice, so huge diffs cannot block the main thread.

Ignore whitespace

Plain-text review ​

toolbar="false" hides stats and bulk actions, show-line-numbers="false" hides line numbers, and wrap allows long lines to wrap (wrapping disables virtual scrolling and lets row height follow content).

No toolbar, wrapped

Stats and events ​

The read-only stats property returns { added, removed, unchanged, hunks }; oas-diff-change is dispatched on mount and after data updates with the same shape. Expanding a fold dispatches oas-hunk-toggle; expandAll() / collapseAll() dispatch one event per changed fold region (expanded true / false) with { index, expanded, start, count }.

js
const viewer = document.querySelector('#dv-basic')
viewer.stats // { added: 3, removed: 3, unchanged: 3, hunks: 1 }
viewer.scrollToChange(0) // scroll to the first change block
viewer.expandAll()
viewer.addEventListener('oas-hunk-toggle', (event) => {
  console.log(event.detail) // { index, expanded, start, count }
})

Keyboard and accessibility ​

The viewport is a single Tab stop; aria-activedescendant points at the active row. Semantics are role="grid" + row + gridcell; line numbers are hidden from screen readers and changed rows carry "Added line N / Removed line M" accessible names. Additions and deletions are conveyed by the + / − prefix, background color, and accessible name together — never color alone.

KeyBehavior
↑ / ↓Previous / next row
Home / EndFirst / last row
PageUp / PageDownPage up / down
Enter / SpaceExpand / collapse the current fold

Theme and parts ​

Add/remove backgrounds and word highlights use dedicated opaque tokens (--oas-color-diff-add-bg / --oas-color-diff-del-bg plus their -word- variants, switched automatically in dark mode); line numbers use --oas-color-diff-gutter-text. Style with ::part(): viewer, toolbar, status, added, removed, summary, split-head, row, code, before-no, after-no, fold, fold-toggle, placeholder. RTL mirrors logical properties and keeps old text on the start side in split view.

API ​

Pure model helpers are also available from @oas-ui/ui/data/diff-viewer (or @oas-ui/ui) for host-side preprocessing or custom rendering: diffLines, layoutRows, buildDiffRows, layoutSplitRows, buildSplitRows, inlineSegments, splitLines. They do not depend on the DOM.

oas-diff-viewer ​

Attributes ​

AttributeDescriptionTypeDefault
aria-labelAccessible name of the diff table (defaults to “Diff”)——
contextUnchanged lines kept on each side when folding (default 3; 0 keeps only the changed lines)string3
heightViewport height in px (default 480)string480
ignore-whitespaceNormalize whitespace before comparing (collapse runs, trim line edges) while displaying the original textboolean—
max-editsLine-level edit-distance cap (default 1000); beyond it the viewer degrades to block replacement and shows a noticestring1000
modeView mode: unified (default) / splitstringunified
new-labelHeader text for the new-text split column (defaults to “After”)string—
new-textNew text (attribute channel; SSR/DSD friendly)string—
old-labelHeader text for the old-text split column (defaults to “Before”)string—
old-textOld text (attribute channel; SSR/DSD friendly)string—
row-heightFixed row height in px (default 22, minimum 12)string22
show-line-numbersLine-number toggle; false hides the line-number cellsstring—
split-min-widthMinimum container width in px for split view (default 640); narrower containers fall back to unifiedstring640
toolbarToolbar toggle; false hides stats and bulk actionsstring—
virtualVirtual scrolling: auto by default (enabled when aligned rows exceed virtual-threshold), true forces it on, false turns it offstring—
virtual-thresholdAligned-row threshold for enabling virtual scrolling (default 2000)string2000
wrapWrap long lines; wrapping disables virtual scrolling and lets row height follow contentboolean—

Property (JS property; see each row for attribute reflection) ​

PropertyDescriptionTypeDefault
newTextNew-text property; writes reflect the new-text attributestring—
oldTextOld-text property; writes reflect the old-text attributestring—
statsDiff statistics (read-only): { added, removed, unchanged, hunks }DiffStats—

Methods ​

MethodSignatureDescription
expandAllexpandAll(): voidExpand all unchanged regions
collapseAllcollapseAll(): voidCollapse all unchanged regions
scrollToChangescrollToChange(index: number): booleanScroll to the given change block and focus its first row; returns false when out of range

Events ​

EventDescription
oas-diff-changeDispatched after data updates, detail: { added, removed, unchanged, hunks }
oas-hunk-toggleDispatched after a fold expands/collapses, detail: { index, expanded, start, count }

CSS Variables ​

CSS VariableDefault
--oas-diff-viewer-add-bgvar(--oas-color-diff-add-bg)
--oas-diff-viewer-add-word-bgvar(--oas-color-diff-add-word-bg)
--oas-diff-viewer-del-bgvar(--oas-color-diff-del-bg)
--oas-diff-viewer-del-word-bgvar(--oas-color-diff-del-word-bg)
--oas-diff-viewer-height480px
--oas-diff-viewer-row-height22px

Released under the MIT OR Apache-2.0 License.