Skip to content

Commit 8055f30

Browse files
authored
Merge pull request #190 from brewern/render_nothing_when_empty
Add configuration for rendering nothing if the diff shows no changes were made.
2 parents 2512e72 + 9c30d10 commit 8055f30

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ The HTML output accepts a Javascript object with configuration. Possible options
188188
- `maxLineLengthHighlight`: only perform diff changes highlight if lines are smaller than this, default is `10000`
189189
- `templates`: object with previously compiled templates to replace parts of the html
190190
- `rawTemplates`: object with raw not compiled templates to replace parts of the html
191+
- `renderNothingWhenEmpty`: render nothing if the diff shows no change in its comparison: `true` or `false`, default is `false`
191192
> For more information regarding the possible templates look into [src/templates](https://github.com/rtfpessoa/diff2html/tree/master/src/templates)
192193
193194
** Diff2HtmlUI Helper Options **

src/diff2html.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
matching: 'none',
2020
matchWordsThreshold: 0.25,
2121
matchingMaxComparisons: 2500,
22-
maxLineLengthHighlight: 10000
22+
maxLineLengthHighlight: 10000,
23+
renderNothingWhenEmpty: false
2324
};
2425

2526
/*

src/line-by-line-printer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
}
2727

2828
LineByLinePrinter.prototype.makeFileDiffHtml = function(file, diffs) {
29+
if (this.config.renderNothingWhenEmpty && file.blocks && !file.blocks.length) return '';
30+
2931
var fileDiffTemplate = hoganUtils.template(baseTemplatesPath, 'file-diff');
3032
var filePathTemplate = hoganUtils.template(genericTemplatesPath, 'file-path');
3133
var fileIconTemplate = hoganUtils.template(iconsBaseTemplatesPath, 'file');

test/line-by-line-tests.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,23 @@ describe('LineByLinePrinter', function() {
279279
' </div>\n' +
280280
'</div>';
281281

282+
assert.equal(expected, fileHtml);
283+
});
284+
it('should return empty when option renderNothingWhenEmpty is true and file blocks not present', function() {
285+
var lineByLinePrinter = new LineByLinePrinter({
286+
renderNothingWhenEmpty: true
287+
});
288+
289+
var file = {
290+
blocks: []
291+
};
292+
293+
var diffs = '<span>Random Html</span>';
294+
295+
var fileHtml = lineByLinePrinter.makeFileDiffHtml(file, diffs);
296+
297+
var expected = '';
298+
282299
assert.equal(expected, fileHtml);
283300
});
284301
});
@@ -399,7 +416,7 @@ describe('LineByLinePrinter', function() {
399416
isCombined: false
400417
}];
401418

402-
var lineByLinePrinter = new LineByLinePrinter();
419+
var lineByLinePrinter = new LineByLinePrinter({ renderNothingWhenEmpty: false });
403420
var html = lineByLinePrinter.generateLineByLineJsonHtml(exampleJson);
404421
var expected =
405422
'<div class="d2h-wrapper">\n' +

0 commit comments

Comments
 (0)