diff_html.ts

Diff-viewer HTML generation: syntax-highlighted diffs with CSS classes, as unified rows or side-by-side cells, composed from @fuzdev/fuz_util's diff data and the lexer substrate's per-line renderer. The returned HTML is the rows only — the caller owns the wrapper element (CodeDiff.svelte renders <div class="code_diff">, with split added for the side-by-side view, styled by theme_diff.css).

The pipeline is three cacheable stages. A DiffSide is one lexed text, reusable across any number of diffs (a consumer caches it by content). A DiffModel is two sides plus their DiffLines, computed by diff_lines or supplied by the caller, with the pairing of changed lines and their emphasis worked out lazily, one change at a time. The renderers draw any DiffRowSource — the narrower surface a model implements — as a range of rows (render_diff_unified_rows_html, render_diff_split_rows_html) or the whole document with its unchanged regions elided (render_diff_model_unified_html, render_diff_model_split_html); render_diff_unified_html and render_diff_split_html do all three stages from two strings.

Every row carries its line numbers as data-a / data-b (absent on the side it lacks) and, inside a hunk, the hunk's index as data-hunk, so a consumer maps an element to its line by event delegation; apply_diff_highlight restyles highlighted rows in place.

A diff known only by a patch's hunks renders through create_diff_patch_source, highlighted approximately — each run of hunks lexed on its own — with the lines between hunks counted, until the texts arrive and a DiffModel replaces it.

elide: 'expand' bounds the DOM of a big diff with a way back: elided regions render as count rows with expand controls, and the lines in expanded (b line numbers) as rows. render_diff_segments renders the document in keyed pieces, so a component re-renders only the pieces an expansion changes.

view source

Declarations
#

35 declarations

apply_diff_highlight
#

diff_html.ts view source

(root: { querySelectorAll(selector: string): Iterable<DiffHighlightRow>; }, highlight: DiffLineRange[] | null | undefined): void import {apply_diff_highlight} from '@fuzdev/fuz_code/diff_html.js';

Sets the diff_highlight class on every rendered row under root to match highlight, as the renderers' highlight option would have — so a highlight change touches only classes, not the rows, keeping opened elisions open and the rows' identity stable. Rows are found by their data-a / data-b attributes.

root

type { querySelectorAll(selector: string): Iterable<DiffHighlightRow>; }

highlight

type DiffLineRange[] | null | undefined

returns

void

mutates

  • root — toggles the `diff_highlight` class on its rows

clamp_diff_expanded
#

diff_html.ts view source

(model: DiffRowSource, expanded: readonly DiffExpandedRange[], context_lines?: number): DiffExpandedRange[] import {clamp_diff_expanded} from '@fuzdev/fuz_code/diff_html.js';

Returns expanded normalized (see normalize_diff_expanded) and clamped to the elided regions of model for context_lines — the lines an elide: 'expand' render of it would show as rows from expanded, plus any it counts because the source holds no rows for them. A region of unknown length (after a patch's last hunk) keeps every line past its start.

model

expanded

type readonly DiffExpandedRange[]

context_lines?

type number
optional default 3

returns

DiffExpandedRange[]

create_diff_model
#

diff_html.ts view source

(a: DiffSide, b: DiffSide, options?: DiffModelOptions): DiffModel import {create_diff_model} from '@fuzdev/fuz_code/diff_html.js';

Creates the DiffModel of sides a and b. Throws when supplied lines don't number the sides' lines — a bug in whatever produced them, since lines from outside (a git diff, say) are validated where they're parsed, and a diff of only some lines (a -U0 patch's hunks) is a different input.

a

b

options?

optional default {}

returns

DiffModel

throws

  • Error - when `options.lines` has an unknown type, a line number that isn't the next integer on its side, a number on a side its type lacks, or stops short of a side's last line

create_diff_patch_source
#

diff_html.ts view source

(hunks: readonly DiffHunk[], options?: DiffPatchSourceOptions): DiffPatchSource import {create_diff_patch_source} from '@fuzdev/fuz_code/diff_html.js';

Creates a DiffPatchSource from a patch's hunks — one file's, in order, as fuz_util's diff_hunks returns them or its patch parser reads them from git diff output. Each hunk's lines must number its header's lines consecutively (a side with no lines starts at the line before the hunk, as in @@ headers), hunks must be in order without overlapping, and the unchanged lines between two hunks must be as many on both sides. Throws otherwise — a bug in whatever produced the hunks, since a patch from outside is validated where it's parsed. Nothing is lexed until rows are drawn.

The line counts and patch_context are claims about the texts that the hunks can contradict — a count short of the hunks' last line, two counts leaving different numbers of lines after the last hunk, or a count past a proven end — and a contradiction throws too. CodeDiff, which may get a count meant for other hunks while new ones arrive, treats a contradicted count as unknown instead.

hunks

type readonly DiffHunk[]

options?

optional default {}

returns

DiffPatchSource

throws

  • Error - when a hunk has no lines, a start or count that isn't a whole number, lines that don't number its header's, a line after a side's `no_newline` line, or comes out of order; or when a line count is short of the hunks' lines or disagrees with the other side's or with a proven end

create_diff_side
#

diff_html.ts view source

(text: string, options?: DiffSideOptions): DiffSide import {create_diff_side} from '@fuzdev/fuz_code/diff_html.js';

Lexes text into a DiffSide. An unregistered lang renders as plain text, like null.

text

type string

options?

optional default {}

returns

DiffSide

diff_expand_line_range
#

diff_html.ts view source

(model: DiffRowSource, side: "a" | "b", line: number, options?: DiffExpandLineOptions): DiffExpandedRange | null import {diff_expand_line_range} from '@fuzdev/fuz_code/diff_html.js';

Returns the lines to reveal so an elide: 'expand' render shows line of side with context: a window of expand_step lines centered on it, kept inside the run of rows holding it. null when no elided row has the line — it's in a hunk, out of range, not an integer, or among lines the source holds no rows for. CodeDiff's scroll_to_line reveals this window before scrolling.

model

side

type "a" | "b"

line

type number

options?

optional default {}

returns

DiffExpandedRange | null

diff_expand_range
#

diff_html.ts view source

(start: number, end: number, direction: DiffExpandDirection, step?: number | undefined): DiffExpandedRange import {diff_expand_range} from '@fuzdev/fuz_code/diff_html.js';

Returns the lines an expand control reveals in a hidden region of b lines start through end — what CodeDiff adds to its expanded on a click, from the count row's data-b-start and data-b-end.

start

the region's first b line

type number

end

the region's last b line

type number

direction

the control's data-expand

step?

lines revealed up or down, as expand_step

type number
optional

returns

DiffExpandedRange

DIFF_EXPAND_STEP
#

diff_html.ts view source

20 import {DIFF_EXPAND_STEP} from '@fuzdev/fuz_code/diff_html.js';

How many lines an expand-up or expand-down control of an elide: 'expand' render reveals unless expand_step says otherwise, as GitHub's controls do.

DiffExpandDirection
#

diff_html.ts view source

DiffExpandDirection

type "up" | "down" | "all"

import type {DiffExpandDirection} from '@fuzdev/fuz_code/diff_html.js';

Which way an expand control reveals lines: 'down' from the top of a hidden region (continuing the rows above it), 'up' from its bottom (continuing the rows below it), or 'all' of it.

DiffExpandedRange
#

diff_html.ts view source

DiffExpandedRange import type {DiffExpandedRange} from '@fuzdev/fuz_code/diff_html.js';

A run of unchanged lines an elide: 'expand' render shows as rows: b line numbers, 1-based and inclusive. An unchanged line has a number on both sides; b is the key because it's the version a reader links into and edits, and because line numbers, unlike row indices, mean the same thing in every model of the same two texts — a range survives a new model of the same diff, such as a source holding only hunks upgraded to one holding every line.

normalize_diff_expanded caps end at Number.MAX_SAFE_INTEGER, which reads as "to the end of the file", so a normalized value is finite and survives JSON.stringify; clamping to a diff whose length is known brings it down to the last line.

start

type number

end

type number

DiffExpandLineOptions
#

diff_html.ts view source

DiffExpandLineOptions import type {DiffExpandLineOptions} from '@fuzdev/fuz_code/diff_html.js';

context_lines?

type number

default 3

expand_step?

The window's size — see RenderDiffModelOptions.expand_step.

type number

default DIFF_EXPAND_STEP

DiffHighlightRow
#

diff_html.ts view source

DiffHighlightRow import type {DiffHighlightRow} from '@fuzdev/fuz_code/diff_html.js';

The part of an element apply_diff_highlight reads and writes, so it runs on any DOM-like tree.

getAttribute

type (name: string): string | null

name

type string
returns string | null

classList

type { toggle(token: string, force?: boolean | undefined): boolean; }

DiffLineRange
#

diff_html.ts view source

DiffLineRange import type {DiffLineRange} from '@fuzdev/fuz_code/diff_html.js';

A range of lines on one side of a diff, 1-based and inclusive, as in a line link like #L10-L20.

side

type "a" | "b"

start

type number

end?

The last line, inclusive.

type number

default start

DiffModel
#

diff_html.ts view source

DiffModel import type {DiffModel} from '@fuzdev/fuz_code/diff_html.js';

A diff of two whole DiffSides, what the renderers draw. The model owns what depends on both sides — the hunks for a context size, which removed line pairs with which added line, and the pairs' emphasis — and computes each lazily, so rendering a few rows of a large diff pairs only the changes those rows are in.

Within a change (a run of removes and the adds after it), lines pair by similarity: among the ways to pair the change's lines without crossing, the one with the highest total similarity, so a removed line pairs with the added line it became wherever that sits. A change whose estimated pairing work is past a fixed budget pairs by position instead.

inheritance

extends: DiffRowSource

a

type DiffSide

readonly

b

type DiffSide

readonly

lines

type DiffLine[]

readonly

hunk_ranges

Cached per count.

type (context_lines?: number | undefined): readonly Readonly<DiffRowRange>[]

context_lines?

type number
optional
returns readonly Readonly<DiffRowRange>[]

DiffModelOptions
#

diff_html.ts view source

DiffModelOptions import type {DiffModelOptions} from '@fuzdev/fuz_code/diff_html.js';

Options for create_diff_model.

lines?

The diff's lines — git's own diff converted to DiffLines, say. When absent, diff_lines computes them from the sides' texts. Supplied lines must number every line of both sides: each side's numbers are integers counting up from 1 with no gaps to the side's diff_line_count, present exactly on the sides a line's type has. The texts aren't compared with the sides'. The model keeps the array, so don't mutate it after.

type DiffLine[]

max_cost?

See DiffOptions.max_cost in @fuzdev/fuz_util/diff.ts; unused when lines is supplied.

type number

DiffPatchSource
#

diff_html.ts view source

DiffPatchSource import type {DiffPatchSource} from '@fuzdev/fuz_code/diff_html.js';

A diff known only by its hunks — a patch's, as from git diff, without either text — that the row renderers draw like a DiffModel of the same two texts: the same rows with the same numbers, pairing, and emphasis wherever it holds the rows, and a count row for each stretch of unchanged lines it doesn't. Up to the patch's own context, context_lines makes the same hunks with the same data-hunk indices; past it the patch has no more lines to show, so its hunks are smaller than the model's and may index differently. Until the line counts are known, the gutter fits the largest line number the hunks hold, so it can be a char narrower than the model's.

Its highlighting is approximate. With no whole text to lex, each run of contiguous hunks lexes as a snippet, one per side (its context and removed lines for a, its context and added lines for b), so a hunk that starts inside a construct opened in the lines before it — a block comment, a template literal — highlights as if the construct weren't open. A language with a snippet_prefix guesses the context a snippet starts in, carried from snippet to snippet of a side — the built-in Svelte, HTML, and markdown lexers find a hunk inside a <script> or <style>, or a fence — and a wrong guess mis-highlights the snippet. A snippet's damage stays in its own hunk. The cure is to fetch both texts and render a DiffModel instead, which CodeDiff swaps in without losing its place (see CodeDiff's hunks).

inheritance

extends: DiffRowSource

hunks

The hunks the source was created from. Don't mutate.

type readonly DiffHunk[]

readonly

lang

The language id the source was created with, null for plain text — as given, like DiffSide.lang: an unregistered id is reported as is, and its snippets lex as plain text.

type string | null

readonly

a_line_count

How many lines the original text has: given, or proven by patch_context or a final line's no_newline, else null.

type number | null

readonly

b_line_count

How many lines the updated text has — see a_line_count.

type number | null

readonly

hunk_ranges

Cached per count. A count above the patch's own context can't show more lines than the patch holds, so the hunks are as big as the patch's (merged where the patch's lines touch); a smaller count trims them, and splits a hunk whose changes are far enough apart, the rows it trims rendering as elided runs.

type (context_lines?: number | undefined): readonly Readonly<DiffRowRange>[]

context_lines?

type number
optional
returns readonly Readonly<DiffRowRange>[]

DiffPatchSourceOptions
#

diff_html.ts view source

DiffPatchSourceOptions import type {DiffPatchSourceOptions} from '@fuzdev/fuz_code/diff_html.js';

inheritance

a_line_count?

How many lines the original text has, when known, counted as DiffSide.diff_line_count counts them (a trailing newline adds no line). It counts the unchanged lines after the last hunk, which a patch doesn't say, and widens the gutter to fit the final line number. A null or absent count is unknown, unless patch_context or a final line's no_newline proves where the file ends.

type number | null

b_line_count?

How many lines the updated text has, when known — see a_line_count.

type number | null

patch_context?

The context the patch was made with, the <n> of git diff -U<n>. A patch gives each change that many unchanged lines after it where the file has them, so a last hunk ending in fewer proves the file ends there, and the count after it is 0. Unrelated to the renderers' context_lines, which says how much of the held context to show. A value larger than the patch's real context wrongly proves the end of any file whose last change is near it; null, absent, or 0 proves nothing.

type number | null

DiffPrepareOptions
#

diff_html.ts view source

DiffPrepareOptions import type {DiffPrepareOptions} from '@fuzdev/fuz_code/diff_html.js';

Options for DiffRowSource.prepare, saying which fragments the rows will be drawn with.

split?

Both sides of every row, as the split view draws them, rather than the unified view's removed lines from a and the rest from b.

type boolean

default false

intraline?

The paired lines' emphasized fragments too.

type boolean

default false

DiffRowRange
#

diff_html.ts view source

DiffRowRange import type {DiffRowRange} from '@fuzdev/fuz_code/diff_html.js';

A half-open [start, end) range of indices into DiffRowSource.lines.

start

type number

end

type number

DiffRowSource
#

diff_html.ts view source

DiffRowSource import type {DiffRowSource} from '@fuzdev/fuz_code/diff_html.js';

What the row renderers read: a diff's lines and, per line, its partner and its highlighted HTML. DiffModel is the one built from two whole texts; a source holding only some of a diff's lines (its hunks, say) implements the same surface, with elided_count counting the lines it doesn't hold.

lines

The rows, in order. Don't mutate.

type readonly DiffLine[]

readonly

width

The gutter's width in chars, fitting the larger side's final line number, so every range of rows pads its numbers alike.

type number

readonly

hunk_ranges

Returns the hunks for context_lines as ranges of lines, in order — the rows a whole-document render shows, the gaps around them being the elided regions. The count is floored, and a negative or NaN count is 0.

type (context_lines?: number | undefined): readonly Readonly<DiffRowRange>[]

context_lines?

type number
optional
returns readonly Readonly<DiffRowRange>[]

elided_count

Returns how many unchanged lines the elided region at rows `[start, end)` stands for — a gap between hunks, or before the first or after the last — counted from line numbers, so a source that doesn't hold a gap's rows still counts them. null when the count is unknown, as the gap after a patch's last hunk is: a patch never says how long the file is. A gap with no rows renders as a count row whatever elide says.

type (start: number, end: number): number | null

start

type number

end

type number
returns number | null

b_through

Returns the last b line number at or before row index in the file, 0 before the first — for a removed row, the b line it follows. A source lacking rows counts the lines it lacks: a run of removals that starts past the file's first line follows a b line no row holds, so the renderers can't find it by looking at earlier rows. -1 is 0.

type (index: number): number

index

type number
returns number

partner

Returns the index of the line that the line at index pairs with, or -1 for an unpaired or unchanged line.

type (index: number): number

index

type number
returns number

throws

  • RangeError - when `index` isn't a row

fragment

Returns the highlighted HTML of the line at index on side, with its pair's emphasis when intraline is set. Unlike the renderers' intraline option, which defaults to true, this defaults to plain.

type (index: number, side: "a" | "b", intraline?: boolean | undefined): string

index

type number

side

type "a" | "b"

intraline?

type boolean
optional
returns string

throws

  • RangeError - when `index` isn't a row, or the line has no number on `side`

prepare

Renders ahead the fragments that drawing rows [start, end) reads, a few passes over each side rather than one per line — see DiffPrepareOptions. May be a no-op: fragment renders whatever wasn't prepared.

type (start: number, end: number, options?: DiffPrepareOptions | undefined): void

start

type number

end

type number

options?

optional
returns void

DiffSegment
#

diff_html.ts view source

DiffSegment import type {DiffSegment} from '@fuzdev/fuz_code/diff_html.js';

A piece of a whole-document render, as render_diff_segments returns it. Rendering each with its own {@html}, keyed by key, lets an expansion replace only the segments it changes.

key

Names the segment's rows: from renders of one source with the same options apart from expanded, equal keys are equal segments.

type string

readonly

kind

A 'hunk', a run of 'rows' an elided region shows (all of it with elide: 'none', or what expanded reveals with 'expand'), or an 'elided' region or the hidden part of one, rendered as elide says.

type "hunk" | "rows" | "elided"

readonly

start

The first row, an index into DiffRowSource.lines.

type number

readonly

end

The row after the last; equal to start for lines with no rows.

type number

readonly

html

type string

readonly

DiffSide
#

diff_html.ts view source

DiffSide import type {DiffSide} from '@fuzdev/fuz_code/diff_html.js';

One side of a diff: a text lexed once as a whole document, so multi-line constructs highlight correctly on every line, with its per-line HTML fragments rendered on demand and cached. A side doesn't know what it's diffed against, so one side serves any number of DiffModels — cache it by content (a blob hash, say) and a moving diff range lexes each text once. Line numbers are 1-based, as in DiffLine.

text

type string

readonly

lang

The language id the side was created with, null for plain text.

type string | null

readonly

lexed

The side's own copy of its whole-document lex, kept past later lexes.

type LexedSyntax

readonly

line_count

The number of lines render_syntax_html_lines gives the text, counting the empty line after a trailing newline.

type number

readonly

diff_line_count

The number of lines a diff numbers, as diff_lines splits the text: no line after a trailing newline, and none at all for ''.

type number

readonly

line_start

Returns the offset in text where line starts.

type (line: number): number

line

type number
returns number

throws

  • RangeError - when `line` isn't one of the side's lines

fragment

Returns the highlighted HTML of line, without emphasis — rendered on first use and cached.

type (line: number): string

line

type number
returns string

throws

  • RangeError - when `line` isn't one of the side's lines

prepare_fragments

Renders and caches the fragments of lines first through last not yet rendered, a contiguous run per render call, so drawing a range of rows renders its lines together rather than one at a time. The range is clamped to the side's lines.

type (first: number, last: number): void

first

type number

last

type number
returns void

render_fragments

Renders lines first through last with marks, absolute `[start, end) ranges into text wrapped in <mark>` — emphasis, which depends on the line's partner and so isn't cached here. The range is clamped to the side's lines.

type (first: number, last: number, marks?: SyntaxHtmlMark[] | undefined): string[]

first

type number

last

type number

marks?

optional
returns string[]

DiffSideOptions
#

diff_html.ts view source

DiffSideOptions import type {DiffSideOptions} from '@fuzdev/fuz_code/diff_html.js';

Options for create_diff_side.

lang?

Language id for syntax highlighting. null (or an unregistered id) renders plain escaped text with diff chrome only.

type string | null

default null

syntax_styler?

type SyntaxStyler

default syntax_styler_global

normalize_diff_expanded
#

diff_html.ts view source

(ranges: readonly DiffExpandedRange[]): DiffExpandedRange[] import {normalize_diff_expanded} from '@fuzdev/fuz_code/diff_html.js';

Returns ranges sorted, with overlapping and adjacent ranges merged, the bounds rounded inward to whole lines, a start below 1 raised to 1 and an end past Number.MAX_SAFE_INTEGER (an Infinity, say) lowered to it, and empty or NaN ranges dropped. Doesn't mutate ranges.

ranges

type readonly DiffExpandedRange[]

returns

DiffExpandedRange[]

render_diff_model_split_html
#

diff_html.ts view source

(model: DiffRowSource, options?: RenderDiffModelOptions): string import {render_diff_model_split_html} from '@fuzdev/fuz_code/diff_html.js';

Renders a DiffModel (or any DiffRowSource) as side-by-side cells, whole — the split-view sibling of render_diff_model_unified_html. Elided regions span both columns, with their rows in a nested .diff_split_rows grid. The caller owns the <div class="code_diff split"> grid wrapper (CodeDiff.svelte with split).

model

options?

optional default {}

returns

string

render_diff_model_unified_html
#

diff_html.ts view source

(model: DiffRowSource, options?: RenderDiffModelOptions): string import {render_diff_model_unified_html} from '@fuzdev/fuz_code/diff_html.js';

Renders a DiffModel (or any DiffRowSource) as unified-diff rows, whole: the hunks' rows (see render_diff_unified_rows_html) with the unchanged regions between them elided as elide says. Returns rows only — the caller owns the <div class="code_diff"> wrapper (CodeDiff.svelte does this).

model

options?

optional default {}

returns

string

render_diff_segments
#

diff_html.ts view source

(model: DiffRowSource, options?: RenderDiffSegmentsOptions, previous?: readonly DiffSegment[] | undefined): DiffSegment[] import {render_diff_segments} from '@fuzdev/fuz_code/diff_html.js';

Renders a DiffModel (or any DiffRowSource) whole, as render_diff_model_unified_html or, with split, render_diff_model_split_html does, but in pieces whose HTML joins to that render: each hunk, each elided region, and with elide: 'expand' each run of revealed rows and each count row between them.

A component renders each segment with its own keyed {@html}, so an expansion replaces the segments it changes and leaves the rest of the DOM alone; the split view's segments are cells of one flat grid, its count rows spanning both columns. Pass the last render's segments as previous — of the same source with the same options apart from expanded — and segments with the same key are reused rather than rendered, and runs of revealed rows keep their old bounds as cuts, so revealing lines next to a run renders only those lines as a new segment.

A count row with expand controls is <div class="diff_skip diff_expand" data-b-start data-b-end> naming the hidden b lines, holding a <button data-expand> per DiffExpandDirection it offers, labeled for assistive tech; diff_expand_range says what a control reveals. Lines of an elided region the source holds no rows for render as a count row with no controls in every mode but 'omit', which counts the whole region in one row.

model

options?

optional default {}

previous?

type readonly DiffSegment[]
optional

returns

DiffSegment[]

render_diff_split_html
#

render_diff_split_rows_html
#

diff_html.ts view source

(model: DiffRowSource, start: number, end: number, options?: RenderDiffRowsOptions): string import {render_diff_split_rows_html} from '@fuzdev/fuz_code/diff_html.js';

Renders rows start up to end (indices into model.lines) as side-by-side diff cells — the split-view sibling of render_diff_unified_rows_html, same options and row semantics. Output is a flat sequence of half-row cells for a two-column grid: each visual row is one a-side cell then one b-side cell (<del>/<ins>/<span> with diff_cell classes). A context line's two cells both carry its data-a and data-b. Within a change, a removed line sits beside the added line it pairs with, the unpaired lines between two pairs are top-aligned, and a side with no line in a row gets an empty .diff_spacer cell, which carries only data-hunk. A range that cuts a change lays out the part inside it as its own change. The range is clamped to model.lines.

model

start

type number

end

type number

options?

optional default {}

returns

string

render_diff_unified_html
#

diff_html.ts view source

(a: string, b: string, options?: RenderDiffOptions): string import {render_diff_unified_html} from '@fuzdev/fuz_code/diff_html.js';

Generates syntax-highlighted unified-diff rows for two versions of a source text — create_diff_side for each, create_diff_model, then render_diff_model_unified_html. Both sides are lexed as whole documents, so multi-line constructs highlight correctly. To reuse a side across diffs, or to supply the diff's lines, build the model directly.

a

type string

b

type string

options?

optional default {}

returns

string

render_diff_unified_rows_html
#

diff_html.ts view source

(model: DiffRowSource, start: number, end: number, options?: RenderDiffRowsOptions): string import {render_diff_unified_rows_html} from '@fuzdev/fuz_code/diff_html.js';

Renders rows start up to end (indices into model.lines) as unified-diff rows: one element per line — <ins>/<del> for added/removed lines (semantics survive copy/paste and screen readers), <span> for context — carrying diff_line plus diff_add/diff_remove/diff_same, diff_highlight when highlight matches, data-a/data-b line numbers, and data-hunk inside a hunk. Gutters are aria-hidden and unselectable, and the +/- markers are CSS generated content, so selecting and copying yields clean code (an empty line carries a <br> so the copy keeps it). Removed rows render from a's highlight, added and context rows from b's. No elision: the caller decides which ranges to show, and how to show the rest. The range is clamped to model.lines.

model

start

type number

end

type number

options?

optional default {}

returns

string

RenderDiffModelOptions
#

diff_html.ts view source

RenderDiffModelOptions import type {RenderDiffModelOptions} from '@fuzdev/fuz_code/diff_html.js';

Options for the whole-document renderers render_diff_model_unified_html and render_diff_model_split_html.

inheritance

elide?

How elided unchanged regions render: 'details' as collapsed native <details> blocks (expandable with zero JS, full content in the DOM), 'omit' as an inert count row (bounded output for huge inputs), 'expand' as count rows with expand controls, showing the lines in expanded as rows (bounded output with a way back — the controls are <button>s that do nothing until a script wires them, as CodeDiff does; see render_diff_segments), 'none' fully expanded.

type "details" | "omit" | "expand" | "none"

default 'details'

expanded?

With elide: 'expand', the unchanged lines to show as rows rather than count — b line numbers, normalized as normalize_diff_expanded does. Lines outside the elided regions are ignored, and so is a region whose rows the source doesn't hold.

type readonly DiffExpandedRange[]

expand_step?

With elide: 'expand', how many lines an expand-up or expand-down control reveals; a region of at most this many offers only expand-all. Floored, and a value under 1 or NaN is the default.

type number

default DIFF_EXPAND_STEP

RenderDiffOptions
#

RenderDiffRowsOptions
#

diff_html.ts view source

RenderDiffRowsOptions import type {RenderDiffRowsOptions} from '@fuzdev/fuz_code/diff_html.js';

Options for the row-range renderers render_diff_unified_rows_html and render_diff_split_rows_html.

context_lines?

Unchanged lines of context around changes, which decides the hunks rows belong to. The row renderers draw exactly the rows asked for, so here it only sets data-hunk; the whole-document renderers also elide by it. Floored to an integer; a negative or NaN value counts as 0, and Infinity shows every line.

type number

default 3

intraline?

Whether paired remove/add lines get intra-line changed-range emphasis (<mark> spans via diff_segments) — see DiffModel for how lines pair; lines too different from every candidate stay unpaired.

type boolean

default true

line_numbers?

Whether to render line-number gutters.

type boolean

default true

highlight?

Lines whose rows get the diff_highlight class: a row matches when its a line is in an a range or its b line in a b range, so a context line matches through either side.

type DiffLineRange[]

RenderDiffSegmentsOptions
#

Depends on
#

Imported by
#