All files / diff-viewer/src/components Row.tsx

100% Statements 28/28
100% Branches 44/44
100% Functions 9/9
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303                                                                                                        33197x 33197x                                           9272x 16x   9256x 2320x 3519x                       9273x 9272x 7082x 4893x                         9273x 4379x                                                                       9392x 9392x                                       9076x   9076x               9076x                                                                                                   4538x                                                                                               197x   197x 197x             197x                                    
/**
 * Row renderers for both views (SPEC §3.3).
 *
 * Split and unified share every cell primitive below, so a change to how a
 * line is drawn lands in both views at once — the views differ only in how
 * many columns they arrange those cells into.
 */
 
import type { ReactNode } from "react";
 
import type { DiffLine, DiffSegment } from "../types";
import type { SplitRow } from "../view/rows";
 
/** Slot class names a consumer can inject (SPEC §4.5). */
export interface RowClassNames {
  row?: string;
  gutter?: string;
  lineNumber?: string;
  content?: string;
  marker?: string;
}
 
/** Strings shown to assistive technology; all overridable for i18n. */
export interface RowLabels {
  added: string;
  removed: string;
}
 
/** The syntax-highlighting seam (SPEC §4.5, §9). */
export type RenderContent = (args: {
  text: string;
  segments?: DiffSegment[];
  side: "old" | "new";
  line: DiffLine;
}) => ReactNode;
 
/** The stylesheet's class map, threaded through rather than re-imported. */
export type RowStyles = Record<string, string>;
 
/**
 * Join the truthy class names; keeps JSX free of `&&` noise.
 *
 * Returns `undefined` rather than `""` when nothing applies, so React omits
 * the attribute instead of emitting a bare `class=""`.
 *
 * @example
 * ```ts
 * cx("row", false, undefined, "add"); // "row add"
 * cx(false, undefined);               // undefined
 * ```
 */
export function cx(...values: Array<string | false | null | undefined>): string | undefined {
  const out = values.filter(Boolean).join(" ");
  return out === "" ? undefined : out;
}
 
/**
 * Render a line's text as plain text nodes, with word-level segments
 * highlighted when the model provides them.
 *
 * **Never** uses `dangerouslySetInnerHTML` (SPEC §9): diff content is
 * untrusted by definition. A consumer who wants highlighted markup opts in
 * through `renderContent`, and that is explicitly their trust boundary.
 */
function LineText({
  line,
  side,
  styles,
  renderContent,
}: {
  line: DiffLine;
  side: "old" | "new";
  styles: RowStyles;
  renderContent?: RenderContent;
}): ReactNode {
  if (renderContent) {
    return renderContent({ text: line.content, segments: line.segments, side, line });
  }
  if (!line.segments) return line.content;
  return line.segments.map((segment, index) =>
    segment.type === "change" ? (
      <mark key={index} className={styles.segmentChange}>
        {segment.text}
      </mark>
    ) : (
      <span key={index}>{segment.text}</span>
    ),
  );
}
 
/** The `+` / `−` gutter glyph — colour is never the only signal (SPEC §8). */
function markerFor(line: DiffLine | null): string {
  if (line === null) return "";
  if (line.type === "add") return "+";
  if (line.type === "remove") return "−";
  return " ";
}
 
/** The visually-hidden change announcement for a row. */
function AnnouncementFor({
  line,
  labels,
  styles,
}: {
  line: DiffLine | null;
  labels: RowLabels;
  styles: RowStyles;
}): ReactNode {
  if (line === null || line.type === "context") return null;
  return (
    <span className={styles.srOnly}>
      {line.type === "add" ? labels.added : labels.removed}
    </span>
  );
}
 
interface CellProps {
  line: DiffLine | null;
  side: "old" | "new";
  showLineNumbers: boolean;
  /** Only the FIRST line-number cell of a row is the row's header (N8). */
  isRowHeader: boolean;
  styles: RowStyles;
  classNames: RowClassNames;
  labels: RowLabels;
  renderContent?: RenderContent;
}
 
/**
 * A line-number cell.
 *
 * Exactly one per row is `<th scope="row">` — the row's header. The others
 * are plain `<td>`s: a second `scope="row"` header makes the row's identity
 * ambiguous to assistive technology, which then has two competing answers
 * for "what row is this?".
 */
function LineNumberCell({
  value,
  isRowHeader,
  className,
}: {
  value: number | undefined;
  isRowHeader: boolean;
  className: string | undefined;
}): ReactNode {
  const content = value ?? "";
  return isRowHeader ? (
    <th scope="row" className={className}>
      {content}
    </th>
  ) : (
    <td className={className}>{content}</td>
  );
}
 
/** One side of a split row: line number, marker, content. */
function SideCells({
  line,
  side,
  showLineNumbers,
  isRowHeader,
  styles,
  classNames,
  labels,
  renderContent,
}: CellProps): ReactNode {
  const number = side === "old" ? line?.oldNumber : line?.newNumber;
  const typeClass =
    line === null
      ? styles.filler
      : line.type === "add"
        ? styles.add
        : line.type === "remove"
          ? styles.remove
          : undefined;
 
  return (
    <>
      {showLineNumbers && (
        <LineNumberCell
          value={number}
          isRowHeader={isRowHeader}
          className={cx(
            styles.lineNumber,
            typeClass,
            classNames.lineNumber,
            classNames.gutter,
          )}
        />
      )}
      <td className={cx(styles.marker, typeClass, classNames.marker)} aria-hidden="true">
        {markerFor(line)}
      </td>
      <td className={cx(styles.content, typeClass, classNames.content)}>
        <AnnouncementFor line={line} labels={labels} styles={styles} />
        {line === null ? null : (
          <LineText line={line} side={side} styles={styles} renderContent={renderContent} />
        )}
      </td>
    </>
  );
}
 
/**
 * One row of the side-by-side view: old on the left, new on the right.
 *
 * A `null` side renders a filler cell so the two columns stay aligned even
 * when a change block is lopsided.
 */
export function SplitRowView({
  row,
  ariaRowIndex,
  showLineNumbers,
  styles,
  classNames,
  labels,
  renderContent,
}: {
  row: SplitRow;
  ariaRowIndex: number;
  showLineNumbers: boolean;
  styles: RowStyles;
  classNames: RowClassNames;
  labels: RowLabels;
  renderContent?: RenderContent;
}): ReactNode {
  return (
    <tr className={cx(styles.row, classNames.row)} aria-rowindex={ariaRowIndex}>
      <SideCells
        line={row.left}
        side="old"
        showLineNumbers={showLineNumbers}
        isRowHeader
        styles={styles}
        classNames={classNames}
        labels={labels}
        renderContent={renderContent}
      />
      <SideCells
        line={row.right}
        side="new"
        showLineNumbers={showLineNumbers}
        isRowHeader={false}
        styles={styles}
        classNames={classNames}
        labels={labels}
        renderContent={renderContent}
      />
    </tr>
  );
}
 
/**
 * One row of the unified view: both line numbers, then a single content
 * column.
 */
export function UnifiedRowView({
  line,
  ariaRowIndex,
  showLineNumbers,
  styles,
  classNames,
  labels,
  renderContent,
}: {
  line: DiffLine;
  ariaRowIndex: number;
  showLineNumbers: boolean;
  styles: RowStyles;
  classNames: RowClassNames;
  labels: RowLabels;
  renderContent?: RenderContent;
}): ReactNode {
  const typeClass =
    line.type === "add" ? styles.add : line.type === "remove" ? styles.remove : undefined;
  // A removal shows the old text, everything else the new.
  const side = line.type === "remove" ? "old" : "new";
  const numberClass = cx(
    styles.lineNumber,
    typeClass,
    classNames.lineNumber,
    classNames.gutter,
  );
 
  return (
    <tr className={cx(styles.row, classNames.row)} aria-rowindex={ariaRowIndex}>
      {showLineNumbers && (
        <>
          <LineNumberCell value={line.oldNumber} isRowHeader className={numberClass} />
          <LineNumberCell value={line.newNumber} isRowHeader={false} className={numberClass} />
        </>
      )}
      <td className={cx(styles.marker, typeClass, classNames.marker)} aria-hidden="true">
        {markerFor(line)}
      </td>
      <td className={cx(styles.content, typeClass, classNames.content)}>
        <AnnouncementFor line={line} labels={labels} styles={styles} />
        <LineText line={line} side={side} styles={styles} renderContent={renderContent} />
      </td>
    </tr>
  );
}