Merge pull request #190 from brewern/render_nothing_when_empty

Add configuration for rendering nothing if the diff shows no changes were made.
This commit is contained in:
Rodrigo Fernandes 2019-02-07 21:47:17 +00:00 committed by GitHub
commit 8055f309da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 2 deletions

View file

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

View file

@ -19,7 +19,8 @@
matching: 'none', matching: 'none',
matchWordsThreshold: 0.25, matchWordsThreshold: 0.25,
matchingMaxComparisons: 2500, matchingMaxComparisons: 2500,
maxLineLengthHighlight: 10000 maxLineLengthHighlight: 10000,
renderNothingWhenEmpty: false
}; };
/* /*

View file

@ -26,6 +26,8 @@
} }
LineByLinePrinter.prototype.makeFileDiffHtml = function(file, diffs) { LineByLinePrinter.prototype.makeFileDiffHtml = function(file, diffs) {
if (this.config.renderNothingWhenEmpty && file.blocks && !file.blocks.length) return '';
var fileDiffTemplate = hoganUtils.template(baseTemplatesPath, 'file-diff'); var fileDiffTemplate = hoganUtils.template(baseTemplatesPath, 'file-diff');
var filePathTemplate = hoganUtils.template(genericTemplatesPath, 'file-path'); var filePathTemplate = hoganUtils.template(genericTemplatesPath, 'file-path');
var fileIconTemplate = hoganUtils.template(iconsBaseTemplatesPath, 'file'); var fileIconTemplate = hoganUtils.template(iconsBaseTemplatesPath, 'file');

View file

@ -279,6 +279,23 @@ describe('LineByLinePrinter', function() {
' </div>\n' + ' </div>\n' +
'</div>'; '</div>';
assert.equal(expected, fileHtml);
});
it('should return empty when option renderNothingWhenEmpty is true and file blocks not present', function() {
var lineByLinePrinter = new LineByLinePrinter({
renderNothingWhenEmpty: true
});
var file = {
blocks: []
};
var diffs = '<span>Random Html</span>';
var fileHtml = lineByLinePrinter.makeFileDiffHtml(file, diffs);
var expected = '';
assert.equal(expected, fileHtml); assert.equal(expected, fileHtml);
}); });
}); });
@ -399,7 +416,7 @@ describe('LineByLinePrinter', function() {
isCombined: false isCombined: false
}]; }];
var lineByLinePrinter = new LineByLinePrinter(); var lineByLinePrinter = new LineByLinePrinter({ renderNothingWhenEmpty: false });
var html = lineByLinePrinter.generateLineByLineJsonHtml(exampleJson); var html = lineByLinePrinter.generateLineByLineJsonHtml(exampleJson);
var expected = var expected =
'<div class="d2h-wrapper">\n' + '<div class="d2h-wrapper">\n' +