← 返回首页
Fix: Ensure equalities processed via new processEqualities hook (Fixes #219) by tusharsoni52 · Pull Request #224 · java-diff-utils/java-diff-utils · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (2) All 1 file type selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
  • java-diff-utils/src
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ static void wrapInTag(
private final Function<String, String> lineNormalizer;
private final Function<String, String> processDiffs;
private final Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> inlineDeltaMerger;
// processor for equal (unchanged) lines
private final Function<String, String> equalityProcessor;

private final boolean showInlineDiffs;
private final boolean replaceOriginalLinefeedInChangesWithSpaces;
Expand All @@ -214,6 +216,7 @@ private DiffRowGenerator(Builder builder) {
lineNormalizer = builder.lineNormalizer;
processDiffs = builder.processDiffs;
inlineDeltaMerger = builder.inlineDeltaMerger;
equalityProcessor = builder.equalityProcessor;

replaceOriginalLinefeedInChangesWithSpaces = builder.replaceOriginalLinefeedInChangesWithSpaces;

Expand Down Expand Up @@ -262,7 +265,8 @@ public List<DiffRow> generateDiffRows(final List<String> original, Patch<String>

// Copy the final matching chunk if any.
for (String line : original.subList(endPos, original.size())) {
diffRows.add(buildDiffRow(Tag.EQUAL, line, line));
String processed = processEqualities(line);
diffRows.add(buildDiffRow(Tag.EQUAL, processed, processed));
}
return diffRows;
}
Expand All @@ -276,7 +280,8 @@ private int transformDeltaIntoDiffRow(
Chunk<String> rev = delta.getTarget();

for (String line : original.subList(endPos, orig.getPosition())) {
diffRows.add(buildDiffRow(Tag.EQUAL, line, line));
String processed = processEqualities(line);
diffRows.add(buildDiffRow(Tag.EQUAL, processed, processed));
}

switch (delta.getType()) {
Expand Down Expand Up @@ -496,6 +501,19 @@ private String preprocessLine(String line) {
}
}

/**
* Hook for processing equal (unchanged) text segments.
* Delegates to the builder-configured equalityProcessor if present.
*
* @author tusharsoni52
* @param text
* @return
*
*/
protected String processEqualities(final String text) {
return equalityProcessor != null ? equalityProcessor.apply(text) : text;
}

/**
* This class used for building the DiffRowGenerator.
*
Expand All @@ -521,6 +539,8 @@ public static class Builder {
private boolean replaceOriginalLinefeedInChangesWithSpaces = false;
private Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> inlineDeltaMerger =
DEFAULT_INLINE_DELTA_MERGER;
// Processor for equalities
private Function<String, String> equalityProcessor = null;

private Builder() {}

Expand Down Expand Up @@ -613,6 +633,20 @@ public Builder processDiffs(Function<String, String> processDiffs) {
return this;
}

/**
* Processor for equal (unchanged) text parts.
* Allows applying the same escaping/transformation as for diffs.
*
* @author tusharsoni52
* @param equalityProcessor
* @return
*
*/
public Builder processEqualities(Function<String, String> equalityProcessor) {
this.equalityProcessor = equalityProcessor;
return this;
}

/**
* Set the column width of generated lines of original and revised
* texts.
Expand Down
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.github.difflib.text;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

public class DiffRowGeneratorEqualitiesTest {

@Test
public void testDefaultEqualityProcessingLeavesTextUnchanged() {
DiffRowGenerator generator =
DiffRowGenerator.create().showInlineDiffs(false).build();

List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("hello world"), Arrays.asList("hello world"));

assertEquals(1, rows.size());
assertEquals("hello world", rows.get(0).getOldLine());
assertEquals("hello world", rows.get(0).getNewLine());
assertEquals(DiffRow.Tag.EQUAL, rows.get(0).getTag());
}

@Test
public void testCustomEqualityProcessingIsApplied() {
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(false)
.processEqualities(text -> "[" + text + "]")
.build();

List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("A", "B"), Arrays.asList("A", "B"));

assertEquals(2, rows.size());
assertEquals("[A]", rows.get(0).getOldLine());
assertEquals("[B]", rows.get(1).getOldLine());
}

/**
* Verifies that processEqualities can be used to HTML-escape unchanged
* lines while still working together with the default HTML-oriented
* lineNormalizer.
*/
@Test
public void testHtmlEscapingEqualitiesWorksWithDefaultNormalizer() {
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.inlineDiffByWord(true)
.processEqualities(s -> s.replace("<", "&lt;").replace(">", "&gt;"))
.build();

// both lines are equal -> Tag.EQUAL, processEqualities is invoked
List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("hello <world>"), Arrays.asList("hello <world>"));

DiffRow row = rows.get(0);

assertTrue(row.getOldLine().contains("&lt;world&gt;"));
assertTrue(row.getNewLine().contains("&lt;world&gt;"));
}

/**
* Ensures equalities are processed while inline diff markup is still
* present somewhere in the line.
*/
@Test
public void testEqualitiesProcessedButInlineDiffStillPresent() {
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.inlineDiffByWord(true)
.processEqualities(s -> "(" + s + ")")
.build();

List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("hello world"), Arrays.asList("hello there"));

DiffRow row = rows.get(0);

System.out.println("OLD = " + row.getOldLine());
System.out.println("NEW = " + row.getNewLine());

// Row must be CHANGE
assertEquals(DiffRow.Tag.CHANGE, row.getTag());

// Inline diff markup must appear
assertTrue(
row.getOldLine().contains("span") || row.getNewLine().contains("span"),
"Expected inline <span> diff markup in old or new line");

// Equalities inside CHANGE row must NOT be wrapped by processEqualities
// Option 3 does NOT modify inline equalities
assertTrue(row.getOldLine().startsWith("hello "), "Equal (unchanged) inline segment should remain unchanged");
}
}
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.