Merge pull request #39 from rtfpessoa/improve-code-style

Update contributors list, add codacy badge and clean code
This commit is contained in:
Rodrigo Fernandes 2015-12-20 23:01:05 +00:00
commit c80a4c6415
16 changed files with 579 additions and 501 deletions

View file

@ -17,3 +17,5 @@ Paulo Bu, [@pbu88](https://github.com/pbu88)
Nuno Teixeira, [@nmatpt](https://github.com/nmatpt) Nuno Teixeira, [@nmatpt](https://github.com/nmatpt)
Mikko Rantanen, [@Rantanen](https://github.com/Rantanen) Mikko Rantanen, [@Rantanen](https://github.com/Rantanen)
Wolfgang Illmeyer, [@escitalopram](https://github.com/escitalopram)

View file

@ -1,5 +1,7 @@
# Diff to Html by [rtfpessoa](https://github.com/rtfpessoa) # Diff to Html by [rtfpessoa](https://github.com/rtfpessoa)
[![Codacy Badge](https://api.codacy.com/project/badge/grade/06412dc3f5a14f568778d0db8a1f7dc8)](https://www.codacy.com/app/Codacy/diff2html)
Diff to Html generates pretty HTML diffs from git diff output. Diff to Html generates pretty HTML diffs from git diff output.
## Features ## Features

View file

@ -42,7 +42,6 @@
color: #399839; color: #399839;
border-radius: 5px 0 0 5px; border-radius: 5px 0 0 5px;
padding: 2px; padding: 2px;
width: 25px;
} }
.d2h-lines-deleted { .d2h-lines-deleted {
@ -55,12 +54,10 @@
color: #c33; color: #c33;
border-radius: 0 5px 5px 0; border-radius: 0 5px 5px 0;
padding: 2px; padding: 2px;
width: 25px;
} }
.d2h-file-name { .d2h-file-name {
display: inline; display: inline;
height: 33px;
line-height: 33px; line-height: 33px;
max-width: 80%; max-width: 80%;
white-space: nowrap; white-space: nowrap;
@ -152,7 +149,6 @@
} }
.line-num1 { .line-num1 {
display: inline-block;
float: left; float: left;
width: 30px; width: 30px;
overflow: hidden; overflow: hidden;
@ -160,7 +156,6 @@
} }
.line-num2 { .line-num2 {
display: inline-block;
float: right; float: right;
width: 30px; width: 30px;
overflow: hidden; overflow: hidden;
@ -169,7 +164,6 @@
.d2h-code-linenumber { .d2h-code-linenumber {
position: absolute; position: absolute;
width: 2%;
min-width: 65px; min-width: 65px;
padding-left: 10px; padding-left: 10px;
padding-right: 10px; padding-right: 10px;
@ -232,10 +226,7 @@
.d2h-file-list-header { .d2h-file-list-header {
font-weight: bold; font-weight: bold;
margin-bottom: 5px; float: left;
text-align: left;
display: inline;
float:left;
} }
.d2h-file-list-line { .d2h-file-list-line {
@ -243,28 +234,31 @@
font: 13px Helvetica, arial, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol"; font: 13px Helvetica, arial, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol";
} }
.d2h-file-list-line .d2h-file-name { .d2h-file-list-line .d2h-file-name {
line-height: 21px; line-height: 21px;
} }
.d2h-file-list { .d2h-file-list {
display:none; display: none;
} }
.d2h-clear { .d2h-clear {
display:block; display: block;
clear: both; clear: both;
} }
.d2h-del.d2h-change, .d2h-ins.d2h-change { .d2h-del.d2h-change, .d2h-ins.d2h-change {
background-color: #ffc; background-color: #ffc;
} }
ins.d2h-change, del.d2h-change { ins.d2h-change, del.d2h-change {
background-color: #fad771; background-color: #fad771;
} }
.d2h-file-diff .d2h-del.d2h-change { .d2h-file-diff .d2h-del.d2h-change {
background-color: #fae1af; background-color: #fae1af;
} }
.d2h-file-diff .d2h-ins.d2h-change { .d2h-file-diff .d2h-ins.d2h-change {
background-color: #ded; background-color: #ded;
} }
@ -272,17 +266,21 @@ ins.d2h-change, del.d2h-change {
/* CSS only show/hide */ /* CSS only show/hide */
.d2h-show { .d2h-show {
display: none; display: none;
float:left; float: left;
} }
.d2h-hide { .d2h-hide {
float:left; float: left;
} }
.d2h-hide:target + .d2h-show { .d2h-hide:target + .d2h-show {
display: inline; display: inline;
} }
.d2h-hide:target { .d2h-hide:target {
display: none; display: none;
} }
.d2h-hide:target ~ .d2h-file-list { .d2h-hide:target ~ .d2h-file-list {
display:block; display: block;
} }

30
dist/diff2html.css vendored
View file

@ -42,7 +42,6 @@
color: #399839; color: #399839;
border-radius: 5px 0 0 5px; border-radius: 5px 0 0 5px;
padding: 2px; padding: 2px;
width: 25px;
} }
.d2h-lines-deleted { .d2h-lines-deleted {
@ -55,12 +54,10 @@
color: #c33; color: #c33;
border-radius: 0 5px 5px 0; border-radius: 0 5px 5px 0;
padding: 2px; padding: 2px;
width: 25px;
} }
.d2h-file-name { .d2h-file-name {
display: inline; display: inline;
height: 33px;
line-height: 33px; line-height: 33px;
max-width: 80%; max-width: 80%;
white-space: nowrap; white-space: nowrap;
@ -152,7 +149,6 @@
} }
.line-num1 { .line-num1 {
display: inline-block;
float: left; float: left;
width: 30px; width: 30px;
overflow: hidden; overflow: hidden;
@ -160,7 +156,6 @@
} }
.line-num2 { .line-num2 {
display: inline-block;
float: right; float: right;
width: 30px; width: 30px;
overflow: hidden; overflow: hidden;
@ -169,7 +164,6 @@
.d2h-code-linenumber { .d2h-code-linenumber {
position: absolute; position: absolute;
width: 2%;
min-width: 65px; min-width: 65px;
padding-left: 10px; padding-left: 10px;
padding-right: 10px; padding-right: 10px;
@ -232,10 +226,7 @@
.d2h-file-list-header { .d2h-file-list-header {
font-weight: bold; font-weight: bold;
margin-bottom: 5px; float: left;
text-align: left;
display: inline;
float:left;
} }
.d2h-file-list-line { .d2h-file-list-line {
@ -243,28 +234,31 @@
font: 13px Helvetica, arial, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol"; font: 13px Helvetica, arial, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol";
} }
.d2h-file-list-line .d2h-file-name { .d2h-file-list-line .d2h-file-name {
line-height: 21px; line-height: 21px;
} }
.d2h-file-list { .d2h-file-list {
display:none; display: none;
} }
.d2h-clear { .d2h-clear {
display:block; display: block;
clear: both; clear: both;
} }
.d2h-del.d2h-change, .d2h-ins.d2h-change { .d2h-del.d2h-change, .d2h-ins.d2h-change {
background-color: #ffc; background-color: #ffc;
} }
ins.d2h-change, del.d2h-change { ins.d2h-change, del.d2h-change {
background-color: #fad771; background-color: #fad771;
} }
.d2h-file-diff .d2h-del.d2h-change { .d2h-file-diff .d2h-del.d2h-change {
background-color: #fae1af; background-color: #fae1af;
} }
.d2h-file-diff .d2h-ins.d2h-change { .d2h-file-diff .d2h-ins.d2h-change {
background-color: #ded; background-color: #ded;
} }
@ -272,17 +266,21 @@ ins.d2h-change, del.d2h-change {
/* CSS only show/hide */ /* CSS only show/hide */
.d2h-show { .d2h-show {
display: none; display: none;
float:left; float: left;
} }
.d2h-hide { .d2h-hide {
float:left; float: left;
} }
.d2h-hide:target + .d2h-show { .d2h-hide:target + .d2h-show {
display: inline; display: inline;
} }
.d2h-hide:target { .d2h-hide:target {
display: none; display: none;
} }
.d2h-hide:target ~ .d2h-file-list { .d2h-hide:target ~ .d2h-file-list {
display:block; display: block;
} }

512
dist/diff2html.js vendored
View file

@ -51,7 +51,7 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var diffParser = __webpack_require__(1).DiffParser; var diffParser = __webpack_require__(1).DiffParser;
var fileLister = __webpack_require__(3).FileListPrinter; var fileLister = __webpack_require__(3).FileListPrinter;
@ -63,9 +63,9 @@
/* /*
* Line diff type configuration * Line diff type configuration
var config = { var config = {
"wordByWord": true, // (default) 'wordByWord': true, // (default)
// OR // OR
"charByChar": true 'charByChar': true
}; };
*/ */
@ -83,23 +83,23 @@
var configOrEmpty = config || {}; var configOrEmpty = config || {};
var diffJson = diffInput; var diffJson = diffInput;
if(!configOrEmpty.inputFormat || configOrEmpty.inputFormat === 'diff') { if (!configOrEmpty.inputFormat || configOrEmpty.inputFormat === 'diff') {
diffJson = diffParser.generateDiffJson(diffInput); diffJson = diffParser.generateDiffJson(diffInput);
} }
var fileList = ""; var fileList = '';
if(configOrEmpty.showFiles === true) { if (configOrEmpty.showFiles === true) {
fileList = fileLister.generateFileList(diffJson, configOrEmpty); fileList = fileLister.generateFileList(diffJson, configOrEmpty);
} }
var diffOutput = ""; var diffOutput = '';
if(configOrEmpty.outputFormat === 'side-by-side') { if (configOrEmpty.outputFormat === 'side-by-side') {
diffOutput = htmlPrinter.generateSideBySideJsonHtml(diffJson, configOrEmpty); diffOutput = htmlPrinter.generateSideBySideJsonHtml(diffJson, configOrEmpty);
} else { } else {
diffOutput = htmlPrinter.generateLineByLineJsonHtml(diffJson, configOrEmpty); diffOutput = htmlPrinter.generateLineByLineJsonHtml(diffJson, configOrEmpty);
} }
return fileList + diffOutput return fileList + diffOutput;
}; };
@ -112,9 +112,9 @@
*/ */
Diff2Html.prototype.getPrettyHtmlFromDiff = function(diffInput, config) { Diff2Html.prototype.getPrettyHtmlFromDiff = function(diffInput, config) {
var configOrEmpty = config || {}; var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'diff'; configOrEmpty.inputFormat = 'diff';
configOrEmpty['outputFormat'] = 'line-by-line'; configOrEmpty.outputFormat = 'line-by-line';
return this.getPrettyHtml(diffInput, configOrEmpty) return this.getPrettyHtml(diffInput, configOrEmpty);
}; };
/* /*
@ -122,9 +122,9 @@
*/ */
Diff2Html.prototype.getPrettyHtmlFromJson = function(diffJson, config) { Diff2Html.prototype.getPrettyHtmlFromJson = function(diffJson, config) {
var configOrEmpty = config || {}; var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'json'; configOrEmpty.inputFormat = 'json';
configOrEmpty['outputFormat'] = 'line-by-line'; configOrEmpty.outputFormat = 'line-by-line';
return this.getPrettyHtml(diffJson, configOrEmpty) return this.getPrettyHtml(diffJson, configOrEmpty);
}; };
/* /*
@ -132,9 +132,9 @@
*/ */
Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function(diffInput, config) { Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function(diffInput, config) {
var configOrEmpty = config || {}; var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'diff'; configOrEmpty.inputFormat = 'diff';
configOrEmpty['outputFormat'] = 'side-by-side'; configOrEmpty.outputFormat = 'side-by-side';
return this.getPrettyHtml(diffInput, configOrEmpty) return this.getPrettyHtml(diffInput, configOrEmpty);
}; };
/* /*
@ -142,18 +142,18 @@
*/ */
Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function(diffJson, config) { Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function(diffJson, config) {
var configOrEmpty = config || {}; var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'json'; configOrEmpty.inputFormat = 'json';
configOrEmpty['outputFormat'] = 'side-by-side'; configOrEmpty.outputFormat = 'side-by-side';
return this.getPrettyHtml(diffJson, configOrEmpty) return this.getPrettyHtml(diffJson, configOrEmpty);
}; };
var diffName = 'Diff2Html';
var diffObject = new Diff2Html(); var diffObject = new Diff2Html();
module.exports[diffName] = diffObject; module.exports.Diff2Html = diffObject;
// Expose diff2html in the browser
global[diffName] = diffObject;
})(this); // Expose diff2html in the browser
global.Diff2Html = diffObject;
})();
/* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }())))
@ -168,7 +168,7 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var utils = __webpack_require__(2).Utils; var utils = __webpack_require__(2).Utils;
@ -194,6 +194,7 @@
var newLine = null; var newLine = null;
var saveBlock = function() { var saveBlock = function() {
/* Add previous block(if exists) before start a new file */ /* Add previous block(if exists) before start a new file */
if (currentBlock) { if (currentBlock) {
currentFile.blocks.push(currentBlock); currentFile.blocks.push(currentBlock);
@ -202,6 +203,7 @@
}; };
var saveFile = function() { var saveFile = function() {
/* /*
* Add previous file(if exists) before start a new one * Add previous file(if exists) before start a new one
* if it has name (to avoid binary files errors) * if it has name (to avoid binary files errors)
@ -379,14 +381,14 @@
var nameSplit = filename.split('.'); var nameSplit = filename.split('.');
if (nameSplit.length > 1) { if (nameSplit.length > 1) {
return nameSplit[nameSplit.length - 1]; return nameSplit[nameSplit.length - 1];
} else {
return language;
} }
return language;
} }
module.exports['DiffParser'] = new DiffParser(); module.exports.DiffParser = new DiffParser();
})(this); })();
/***/ }, /***/ },
@ -400,7 +402,7 @@
* *
*/ */
(function(ctx, undefined) { (function() {
function Utils() { function Utils() {
} }
@ -414,7 +416,7 @@
}; };
Utils.prototype.getRandomId = function(prefix) { Utils.prototype.getRandomId = function(prefix) {
return prefix + "-" + Math.random().toString(36).slice(-3); return prefix + '-' + Math.random().toString(36).slice(-3);
}; };
Utils.prototype.startsWith = function(str, start) { Utils.prototype.startsWith = function(str, start) {
@ -436,9 +438,9 @@
return value ? value : ''; return value ? value : '';
}; };
module.exports['Utils'] = new Utils(); module.exports.Utils = new Utils();
})(this); })();
/***/ }, /***/ },
@ -452,42 +454,42 @@
* *
*/ */
(function (ctx, undefined) { (function() {
var printerUtils = __webpack_require__(4).PrinterUtils; var printerUtils = __webpack_require__(4).PrinterUtils;
var utils = __webpack_require__(2).Utils; var utils = __webpack_require__(2).Utils;
function FileListPrinter() { function FileListPrinter() {
} }
FileListPrinter.prototype.generateFileList = function (diffFiles) { FileListPrinter.prototype.generateFileList = function(diffFiles) {
var hideId = utils.getRandomId("d2h-hide"); //necessary if there are 2 elements like this in the same page var hideId = utils.getRandomId('d2h-hide'); //necessary if there are 2 elements like this in the same page
var showId = utils.getRandomId("d2h-show"); var showId = utils.getRandomId('d2h-show');
return '<div class="d2h-file-list-wrapper">\n' + return '<div class="d2h-file-list-wrapper">\n' +
' <div class="d2h-file-list-header">Files changed (' + diffFiles.length + ')&nbsp&nbsp</div>\n' + ' <div class="d2h-file-list-header">Files changed (' + diffFiles.length + ')&nbsp&nbsp</div>\n' +
' <a id="' + hideId + '" class="d2h-hide" href="#' + hideId + '">+</a>\n' + ' <a id="' + hideId + '" class="d2h-hide" href="#' + hideId + '">+</a>\n' +
' <a id="' + showId + 'd2h-show" class="d2h-show" href="#' + showId + '">-</a>\n' + ' <a id="' + showId + 'd2h-show" class="d2h-show" href="#' + showId + '">-</a>\n' +
' <div class="d2h-clear"></div>\n' + ' <div class="d2h-clear"></div>\n' +
' <table class="d2h-file-list">\n' + ' <table class="d2h-file-list">\n' +
diffFiles.map(function (file) { diffFiles.map(function(file) {
return ' <tr class="d2h-file-list-line">\n' + return ' <tr class="d2h-file-list-line">\n' +
' <td class="d2h-lines-added">\n' + ' <td class="d2h-lines-added">\n' +
' <span>+' + file.addedLines + '</span>\n' + ' <span>+' + file.addedLines + '</span>\n' +
' </td>\n' + ' </td>\n' +
' <td class="d2h-lines-deleted">\n' + ' <td class="d2h-lines-deleted">\n' +
' <span>-' + file.deletedLines + '</span>\n' + ' <span>-' + file.deletedLines + '</span>\n' +
' </td>\n' + ' </td>\n' +
' <td class="d2h-file-name"><a href="#' + printerUtils.getHtmlId(file) + '">&nbsp;' + printerUtils.getDiffName(file) + '</a></td>\n' + ' <td class="d2h-file-name"><a href="#' + printerUtils.getHtmlId(file) + '">&nbsp;' + printerUtils.getDiffName(file) + '</a></td>\n' +
' </tr>\n' ' </tr>\n';
}).join('\n') + }).join('\n') +
'</table></div>\n'; '</table></div>\n';
}; };
module.exports['FileListPrinter'] = new FileListPrinter(); module.exports.FileListPrinter = new FileListPrinter();
})(this); })();
/***/ }, /***/ },
@ -501,7 +503,7 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var jsDiff = __webpack_require__(5); var jsDiff = __webpack_require__(5);
var utils = __webpack_require__(2).Utils; var utils = __webpack_require__(2).Utils;
@ -511,39 +513,41 @@
} }
PrinterUtils.prototype.getHtmlId = function(file) { PrinterUtils.prototype.getHtmlId = function(file) {
var hashCode = function(text) { var hashCode = function(text) {
var hash = 0, i, chr, len; var i, chr, len;
if (text.length == 0) return hash; var hash = 0;
if (text.length === 0) return hash;
for (i = 0, len = text.length; i < len; i++) { for (i = 0, len = text.length; i < len; i++) {
chr = text.charCodeAt(i); chr = text.charCodeAt(i);
hash = ((hash << 5) - hash) + chr; hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer hash |= 0; // Convert to 32bit integer
} }
return hash; return hash;
}; };
return "d2h-" + hashCode(this.getDiffName(file)).toString().slice(-6); return 'd2h-' + hashCode(this.getDiffName(file)).toString().slice(-6);
}; };
PrinterUtils.prototype.getDiffName = function(file) { PrinterUtils.prototype.getDiffName = function(file) {
var oldFilename = file.oldName; var oldFilename = file.oldName;
var newFilename = file.newName; var newFilename = file.newName;
if (oldFilename && newFilename if (oldFilename && newFilename && oldFilename !== newFilename && !isDeletedName(newFilename)) {
&& oldFilename !== newFilename
&& !isDeletedName(newFilename)) {
return oldFilename + ' -> ' + newFilename; return oldFilename + ' -> ' + newFilename;
} else if (newFilename && !isDeletedName(newFilename)) { } else if (newFilename && !isDeletedName(newFilename)) {
return newFilename; return newFilename;
} else if (oldFilename) { } else if (oldFilename) {
return oldFilename; return oldFilename;
} else {
return 'Unknown filename';
} }
return 'Unknown filename';
}; };
PrinterUtils.prototype.diffHighlight = function(diffLine1, diffLine2, config) { PrinterUtils.prototype.diffHighlight = function(diffLine1, diffLine2, config) {
var lineStart1, lineStart2; var linePrefix1, linePrefix2, unprefixedLine1, unprefixedLine2;
var prefixSize = 1; var prefixSize = 1;
@ -551,17 +555,16 @@
prefixSize = 2; prefixSize = 2;
} }
lineStart1 = diffLine1.substr(0, prefixSize); linePrefix1 = diffLine1.substr(0, prefixSize);
lineStart2 = diffLine2.substr(0, prefixSize); linePrefix2 = diffLine2.substr(0, prefixSize);
unprefixedLine1 = diffLine1.substr(prefixSize);
diffLine1 = diffLine1.substr(prefixSize); unprefixedLine2 = diffLine2.substr(prefixSize);
diffLine2 = diffLine2.substr(prefixSize);
var diff; var diff;
if (config.charByChar) { if (config.charByChar) {
diff = jsDiff.diffChars(diffLine1, diffLine2); diff = jsDiff.diffChars(unprefixedLine1, unprefixedLine2);
} else { } else {
diff = jsDiff.diffWordsWithSpace(diffLine1, diffLine2); diff = jsDiff.diffWordsWithSpace(unprefixedLine1, unprefixedLine2);
} }
var highlightedLine = ''; var highlightedLine = '';
@ -569,25 +572,30 @@
var changedWords = []; var changedWords = [];
if (!config.charByChar && config.matching === 'words') { if (!config.charByChar && config.matching === 'words') {
var treshold = 0.25; var treshold = 0.25;
if (typeof(config.matchWordsThreshold) !== "undefined") {
if (typeof(config.matchWordsThreshold) !== 'undefined') {
treshold = config.matchWordsThreshold; treshold = config.matchWordsThreshold;
} }
var matcher = Rematch.rematch(function(a, b) { var matcher = Rematch.rematch(function(a, b) {
var amod = a.value, var amod = a.value;
bmod = b.value, var bmod = b.value;
result = Rematch.distance(amod, bmod);
return result; return Rematch.distance(amod, bmod);
}); });
var removed = diff.filter(function isRemoved(element){
var removed = diff.filter(function isRemoved(element) {
return element.removed; return element.removed;
}); });
var added = diff.filter(function isAdded(element){
var added = diff.filter(function isAdded(element) {
return element.added; return element.added;
}); });
var chunks = matcher(added, removed); var chunks = matcher(added, removed);
chunks = chunks.forEach(function(chunk){ chunks.forEach(function(chunk) {
if(chunk[0].length === 1 && chunk[1].length === 1) { if (chunk[0].length === 1 && chunk[1].length === 1) {
var dist = Rematch.distance(chunk[0][0].value, chunk[1][0].value) var dist = Rematch.distance(chunk[0][0].value, chunk[1][0].value);
if (dist < treshold) { if (dist < treshold) {
changedWords.push(chunk[0][0]); changedWords.push(chunk[0][0]);
changedWords.push(chunk[1][0]); changedWords.push(chunk[1][0]);
@ -595,6 +603,7 @@
} }
}); });
} }
diff.forEach(function(part) { diff.forEach(function(part) {
var addClass = changedWords.indexOf(part) > -1 ? ' class="d2h-change"' : ''; var addClass = changedWords.indexOf(part) > -1 ? ' class="d2h-change"' : '';
var elemType = part.added ? 'ins' : part.removed ? 'del' : null; var elemType = part.added ? 'ins' : part.removed ? 'del' : null;
@ -609,11 +618,11 @@
return { return {
first: { first: {
prefix: lineStart1, prefix: linePrefix1,
line: removeIns(highlightedLine) line: removeIns(highlightedLine)
}, },
second: { second: {
prefix: lineStart2, prefix: linePrefix2,
line: removeDel(highlightedLine) line: removeDel(highlightedLine)
} }
} }
@ -631,9 +640,9 @@
return line.replace(/(<del[^>]*>((.|\n)*?)<\/del>)/g, ''); return line.replace(/(<del[^>]*>((.|\n)*?)<\/del>)/g, '');
} }
module.exports['PrinterUtils'] = new PrinterUtils(); module.exports.PrinterUtils = new PrinterUtils();
})(this); })();
/***/ }, /***/ },
@ -1892,72 +1901,76 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var Rematch = {}; var Rematch = {};
Rematch.arrayToString = function arrayToString(a) { Rematch.arrayToString = function arrayToString(a) {
if (Object.prototype.toString.apply(a,[]) === "[object Array]") { if (Object.prototype.toString.apply(a, []) === "[object Array]") {
return "[" + a.map(arrayToString).join(", ") + "]"; return "[" + a.map(arrayToString).join(", ") + "]";
} else { } else {
return a; return a;
} }
} };
/* /*
Copyright (c) 2011 Andrei Mackenzie Copyright (c) 2011 Andrei Mackenzie
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
function levenshtein(a, b){ function levenshtein(a, b) {
if(a.length == 0) return b.length; if (a.length == 0) return b.length;
if(b.length == 0) return a.length; if (b.length == 0) return a.length;
var matrix = []; var matrix = [];
// increment along the first column of each row // increment along the first column of each row
var i; var i;
for(i = 0; i <= b.length; i++){ for (i = 0; i <= b.length; i++) {
matrix[i] = [i]; matrix[i] = [i];
} }
// increment each column in the first row // increment each column in the first row
var j; var j;
for(j = 0; j <= a.length; j++){ for (j = 0; j <= a.length; j++) {
matrix[0][j] = j; matrix[0][j] = j;
} }
// Fill in the rest of the matrix // Fill in the rest of the matrix
for(i = 1; i <= b.length; i++){ for (i = 1; i <= b.length; i++) {
for(j = 1; j <= a.length; j++){ for (j = 1; j <= a.length; j++) {
if(b.charAt(i-1) == a.charAt(j-1)){ if (b.charAt(i - 1) == a.charAt(j - 1)) {
matrix[i][j] = matrix[i-1][j-1]; matrix[i][j] = matrix[i - 1][j - 1];
} else { } else {
matrix[i][j] = Math.min(matrix[i-1][j-1] + 1, // substitution matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
Math.min(matrix[i][j-1] + 1, // insertion Math.min(matrix[i][j - 1] + 1, // insertion
matrix[i-1][j] + 1)); // deletion matrix[i - 1][j] + 1)); // deletion
} }
} }
} }
return matrix[b.length][a.length]; return matrix[b.length][a.length];
} }
Rematch.levenshtein = levenshtein; Rematch.levenshtein = levenshtein;
Rematch.distance = function distance(x,y) { Rematch.distance = function distance(x, y) {
x=x.trim(); x = x.trim();
y=y.trim(); y = y.trim();
var lev = levenshtein(x,y), var lev = levenshtein(x, y);
score = lev / (x.length + y.length); var score = lev / (x.length + y.length);
return score; return score;
} };
Rematch.rematch = function rematch(distanceFunction) { Rematch.rematch = function rematch(distanceFunction) {
function findBestMatch(a, b, cache) { function findBestMatch(a, b, cache) {
var cachecount = 0; var cachecount = 0;
for(var key in cache) { for (var key in cache) {
cachecount++; cachecount++;
} }
var bestMatchDist = Infinity; var bestMatchDist = Infinity;
var bestMatch; var bestMatch;
for (var i = 0; i < a.length; ++i) { for (var i = 0; i < a.length; ++i) {
@ -1972,25 +1985,29 @@
} }
if (md < bestMatchDist) { if (md < bestMatchDist) {
bestMatchDist = md; bestMatchDist = md;
bestMatch = { indexA: i, indexB: j, score: bestMatchDist }; bestMatch = {indexA: i, indexB: j, score: bestMatchDist};
} }
} }
} }
return bestMatch; return bestMatch;
} }
function group(a, b, level, cache) {
if (typeof(cache)==="undefined") { function group(a, b, level, cache) {
if (typeof(cache) === "undefined") {
cache = {}; cache = {};
} }
var minLength = Math.min(a.length, b.length);
var bm = findBestMatch(a,b, cache); var bm = findBestMatch(a, b, cache);
if (!level) { if (!level) {
level = 0; level = 0;
} }
if (!bm || (a.length + b.length < 3)) { if (!bm || (a.length + b.length < 3)) {
return [[a, b]]; return [[a, b]];
} }
var a1 = a.slice(0, bm.indexA), var a1 = a.slice(0, bm.indexA),
b1 = b.slice(0, bm.indexB), b1 = b.slice(0, bm.indexB),
aMatch = [a[bm.indexA]], aMatch = [a[bm.indexA]],
@ -2000,24 +2017,28 @@
a2 = a.slice(tailA), a2 = a.slice(tailA),
b2 = b.slice(tailB); b2 = b.slice(tailB);
var group1 = group(a1, b1, level+1, cache); var group1 = group(a1, b1, level + 1, cache);
var groupMatch = group(aMatch, bMatch, level+1, cache); var groupMatch = group(aMatch, bMatch, level + 1, cache);
var group2 = group(a2, b2, level+1, cache); var group2 = group(a2, b2, level + 1, cache);
var result = groupMatch; var result = groupMatch;
if (bm.indexA > 0 || bm.indexB > 0) { if (bm.indexA > 0 || bm.indexB > 0) {
result = group1.concat(result); result = group1.concat(result);
} }
if (a.length > tailA || b.length > tailB ) {
if (a.length > tailA || b.length > tailB) {
result = result.concat(group2); result = result.concat(group2);
} }
return result; return result;
} }
return group; return group;
} };
module.exports['Rematch'] = Rematch; module.exports.Rematch = Rematch;
})(this); })();
/***/ }, /***/ },
@ -2031,21 +2052,24 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var lineByLinePrinter = __webpack_require__(22).LineByLinePrinter; var LineByLinePrinter = __webpack_require__(22).LineByLinePrinter;
var sideBySidePrinter = __webpack_require__(23).SideBySidePrinter; var sideBySidePrinter = __webpack_require__(23).SideBySidePrinter;
function HtmlPrinter() { function HtmlPrinter() {
} }
HtmlPrinter.prototype.generateLineByLineJsonHtml = lineByLinePrinter.generateLineByLineJsonHtml; HtmlPrinter.prototype.generateLineByLineJsonHtml = function(diffFiles, config) {
var lineByLinePrinter = new LineByLinePrinter(config);
return lineByLinePrinter.generateLineByLineJsonHtml(diffFiles);
};
HtmlPrinter.prototype.generateSideBySideJsonHtml = sideBySidePrinter.generateSideBySideJsonHtml; HtmlPrinter.prototype.generateSideBySideJsonHtml = sideBySidePrinter.generateSideBySideJsonHtml;
module.exports['HtmlPrinter'] = new HtmlPrinter(); module.exports.HtmlPrinter = new HtmlPrinter();
})(this); })();
/***/ }, /***/ },
@ -2059,25 +2083,27 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var diffParser = __webpack_require__(1).DiffParser; var diffParser = __webpack_require__(1).DiffParser;
var printerUtils = __webpack_require__(4).PrinterUtils; var printerUtils = __webpack_require__(4).PrinterUtils;
var utils = __webpack_require__(2).Utils; var utils = __webpack_require__(2).Utils;
var Rematch = __webpack_require__(20).Rematch; var Rematch = __webpack_require__(20).Rematch;
function LineByLinePrinter() { function LineByLinePrinter(config) {
this.config = config;
} }
LineByLinePrinter.prototype.generateLineByLineJsonHtml = function(diffFiles, config) { LineByLinePrinter.prototype.generateLineByLineJsonHtml = function(diffFiles) {
var that = this;
return '<div class="d2h-wrapper">\n' + return '<div class="d2h-wrapper">\n' +
diffFiles.map(function(file) { diffFiles.map(function(file) {
var diffs; var diffs;
if (file.blocks.length) { if (file.blocks.length) {
diffs = generateFileHtml(file, config); diffs = that._generateFileHtml(file);
} else { } else {
diffs = generateEmptyDiff(); diffs = that._generateEmptyDiff();
} }
return '<div id="' + printerUtils.getHtmlId(file) + '" class="d2h-file-wrapper" data-lang="' + file.language + '">\n' + return '<div id="' + printerUtils.getHtmlId(file) + '" class="d2h-file-wrapper" data-lang="' + file.language + '">\n' +
@ -2106,13 +2132,15 @@
'</div>\n'; '</div>\n';
}; };
var matcher=Rematch.rematch(function(a,b) { var matcher = Rematch.rematch(function(a, b) {
var amod = a.content.substr(1), var amod = a.content.substr(1);
bmod = b.content.substr(1); var bmod = b.content.substr(1);
return Rematch.distance(amod, bmod); return Rematch.distance(amod, bmod);
}); });
function generateFileHtml(file, config) { LineByLinePrinter.prototype._generateFileHtml = function(file) {
var that = this;
return file.blocks.map(function(block) { return file.blocks.map(function(block) {
var lines = '<tr>\n' + var lines = '<tr>\n' +
@ -2124,50 +2152,53 @@
var oldLines = []; var oldLines = [];
var newLines = []; var newLines = [];
function processChangeBlock() { function processChangeBlock() {
var matches; var matches;
var insertType; var insertType;
var deleteType; var deleteType;
var doMatching = config.matching === "lines" || config.matching === "words";
var doMatching = that.config.matching === 'lines' || that.config.matching === 'words';
if (doMatching) { if (doMatching) {
matches = matcher(oldLines, newLines); matches = matcher(oldLines, newLines);
insertType = diffParser.LINE_TYPE.INSERT_CHANGES; insertType = diffParser.LINE_TYPE.INSERT_CHANGES;
deleteType = diffParser.LINE_TYPE.DELETE_CHANGES; deleteType = diffParser.LINE_TYPE.DELETE_CHANGES;
} else { } else {
matches = [[oldLines,newLines]]; matches = [[oldLines, newLines]];
insertType = diffParser.LINE_TYPE.INSERTS; insertType = diffParser.LINE_TYPE.INSERTS;
deleteType = diffParser.LINE_TYPE.DELETES; deleteType = diffParser.LINE_TYPE.DELETES;
} }
matches.forEach(function(match){
var oldLines = match[0]; matches.forEach(function(match) {
var newLines = match[1]; oldLines = match[0];
newLines = match[1];
var processedOldLines = []; var processedOldLines = [];
var processedNewLines = []; var processedNewLines = [];
var j = 0;
var oldLine, newLine,
common = Math.min(oldLines.length, newLines.length),
max = Math.max(oldLines.length, newLines.length);
for (j = 0; j < common; j++) {
oldLine = oldLines[j];
newLine = newLines[j];
config.isCombined = file.isCombined; var common = Math.min(oldLines.length, newLines.length);
var diff = printerUtils.diffHighlight(oldLine.content, newLine.content, config);
processedOldLines += var oldLine, newLine;
generateLineHtml(deleteType, oldLine.oldNumber, oldLine.newNumber, for (var j = 0; j < common; j++) {
diff.first.line, diff.first.prefix); oldLine = oldLines[j];
processedNewLines += newLine = newLines[j];
generateLineHtml(insertType, newLine.oldNumber, newLine.newNumber,
diff.second.line, diff.second.prefix);
}
lines += processedOldLines + processedNewLines; that.config.isCombined = file.isCombined;
lines += processLines(oldLines.slice(common), newLines.slice(common)); var diff = printerUtils.diffHighlight(oldLine.content, newLine.content, that.config);
processedOldLines = []; processedOldLines +=
processedNewLines = []; that._generateLineHtml(deleteType, oldLine.oldNumber, oldLine.newNumber,
diff.first.line, diff.first.prefix);
processedNewLines +=
that._generateLineHtml(insertType, newLine.oldNumber, newLine.newNumber,
diff.second.line, diff.second.prefix);
}
lines += processedOldLines + processedNewLines;
lines += that._processLines(oldLines.slice(common), newLines.slice(common));
}); });
oldLines = []; oldLines = [];
newLines = []; newLines = [];
} }
@ -2176,20 +2207,21 @@
var line = block.lines[i]; var line = block.lines[i];
var escapedLine = utils.escape(line.content); var escapedLine = utils.escape(line.content);
if ( line.type !== diffParser.LINE_TYPE.INSERTS && if (line.type !== diffParser.LINE_TYPE.INSERTS &&
(newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) { (newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) {
processChangeBlock(); processChangeBlock();
} }
if (line.type == diffParser.LINE_TYPE.CONTEXT) {
lines += generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine); if (line.type === diffParser.LINE_TYPE.CONTEXT) {
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !oldLines.length) { lines += that._generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
lines += generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine); } else if (line.type === diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
} else if (line.type == diffParser.LINE_TYPE.DELETES) { lines += that._generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
} else if (line.type === diffParser.LINE_TYPE.DELETES) {
oldLines.push(line); oldLines.push(line);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !!oldLines.length) { } else if (line.type === diffParser.LINE_TYPE.INSERTS && Boolean(oldLines.length)) {
newLines.push(line); newLines.push(line);
} else { } else {
console.error('unknown state in html line-by-line generator'); console.error('Unknown state in html line-by-line generator');
processChangeBlock(); processChangeBlock();
} }
} }
@ -2198,27 +2230,27 @@
return lines; return lines;
}).join('\n'); }).join('\n');
} };
function processLines(oldLines, newLines) { LineByLinePrinter.prototype._processLines = function(oldLines, newLines) {
var lines = ''; var lines = '';
for (j = 0; j < oldLines.length; j++) { for (var i = 0; i < oldLines.length; i++) {
var oldLine = oldLines[j]; var oldLine = oldLines[i];
var oldEscapedLine = utils.escape(oldLine.content); var oldEscapedLine = utils.escape(oldLine.content);
lines += generateLineHtml(oldLine.type, oldLine.oldNumber, oldLine.newNumber, oldEscapedLine); lines += this._generateLineHtml(oldLine.type, oldLine.oldNumber, oldLine.newNumber, oldEscapedLine);
} }
for (j = 0; j < newLines.length; j++) { for (var j = 0; j < newLines.length; j++) {
var newLine = newLines[j]; var newLine = newLines[j];
var newEscapedLine = utils.escape(newLine.content); var newEscapedLine = utils.escape(newLine.content);
lines += generateLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, newEscapedLine); lines += this._generateLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, newEscapedLine);
} }
return lines; return lines;
} };
function generateLineHtml(type, oldNumber, newNumber, content, prefix) { LineByLinePrinter.prototype._generateLineHtml = function(type, oldNumber, newNumber, content, prefix) {
var htmlPrefix = ''; var htmlPrefix = '';
if (prefix) { if (prefix) {
htmlPrefix = '<span class="d2h-code-line-prefix">' + prefix + '</span>'; htmlPrefix = '<span class="d2h-code-line-prefix">' + prefix + '</span>';
@ -2238,9 +2270,9 @@
' <div class="d2h-code-line ' + type + '">' + htmlPrefix + htmlContent + '</div>' + ' <div class="d2h-code-line ' + type + '">' + htmlPrefix + htmlContent + '</div>' +
' </td>\n' + ' </td>\n' +
'</tr>\n'; '</tr>\n';
} };
function generateEmptyDiff() { LineByLinePrinter.prototype._generateEmptyDiff = function() {
return '<tr>\n' + return '<tr>\n' +
' <td class="' + diffParser.LINE_TYPE.INFO + '">' + ' <td class="' + diffParser.LINE_TYPE.INFO + '">' +
' <div class="d2h-code-line ' + diffParser.LINE_TYPE.INFO + '">' + ' <div class="d2h-code-line ' + diffParser.LINE_TYPE.INFO + '">' +
@ -2248,11 +2280,11 @@
' </div>' + ' </div>' +
' </td>\n' + ' </td>\n' +
'</tr>\n'; '</tr>\n';
} };
module.exports['LineByLinePrinter'] = new LineByLinePrinter(); module.exports.LineByLinePrinter = LineByLinePrinter;
})(this); })();
/***/ }, /***/ },
@ -2266,7 +2298,7 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var diffParser = __webpack_require__(1).DiffParser; var diffParser = __webpack_require__(1).DiffParser;
var printerUtils = __webpack_require__(4).PrinterUtils; var printerUtils = __webpack_require__(4).PrinterUtils;
@ -2324,9 +2356,9 @@
'</div>\n'; '</div>\n';
}; };
var matcher=Rematch.rematch(function(a,b) { var matcher = Rematch.rematch(function(a, b) {
var amod = a.content.substr(1), var amod = a.content.substr(1),
bmod = b.content.substr(1); bmod = b.content.substr(1);
return Rematch.distance(amod, bmod); return Rematch.distance(amod, bmod);
}); });
@ -2355,31 +2387,33 @@
var oldLines = []; var oldLines = [];
var newLines = []; var newLines = [];
function processChangeBlock() { function processChangeBlock() {
var matches; var matches;
var insertType; var insertType;
var deleteType; var deleteType;
var doMatching = config.matching === "lines" || config.matching === "words"; var doMatching = config.matching === 'lines' || config.matching === 'words';
if (doMatching) { if (doMatching) {
matches = matcher(oldLines, newLines); matches = matcher(oldLines, newLines);
insertType = diffParser.LINE_TYPE.INSERT_CHANGES; insertType = diffParser.LINE_TYPE.INSERT_CHANGES;
deleteType = diffParser.LINE_TYPE.DELETE_CHANGES; deleteType = diffParser.LINE_TYPE.DELETE_CHANGES;
} else { } else {
matches = [[oldLines,newLines]]; matches = [[oldLines, newLines]];
insertType = diffParser.LINE_TYPE.INSERTS; insertType = diffParser.LINE_TYPE.INSERTS;
deleteType = diffParser.LINE_TYPE.DELETES; deleteType = diffParser.LINE_TYPE.DELETES;
} }
matches.forEach(function(match){
var oldLines = match[0]; matches.forEach(function(match) {
var newLines = match[1]; oldLines = match[0];
var tmpHtml; newLines = match[1];
var j = 0;
var oldLine, newLine, var common = Math.min(oldLines.length, newLines.length);
common = Math.min(oldLines.length, newLines.length), var max = Math.max(oldLines.length, newLines.length);
max = Math.max(oldLines.length, newLines.length);
for (j = 0; j < common; j++) { for (var j = 0; j < common; j++) {
oldLine = oldLines[j]; var oldLine = oldLines[j];
newLine = newLines[j]; var newLine = newLines[j];
config.isCombined = file.isCombined; config.isCombined = file.isCombined;
@ -2391,36 +2425,41 @@
fileHtml.right += fileHtml.right +=
generateSingleLineHtml(insertType, newLine.newNumber, generateSingleLineHtml(insertType, newLine.newNumber,
diff.second.line, diff.second.prefix); diff.second.line, diff.second.prefix);
} }
if (max > common) {
var oldSlice = oldLines.slice(common), if (max > common) {
newSlice = newLines.slice(common); var oldSlice = oldLines.slice(common);
tmpHtml = processLines(oldLines.slice(common), newLines.slice(common)); var newSlice = newLines.slice(common);
fileHtml.left += tmpHtml.left;
fileHtml.right += tmpHtml.right; var tmpHtml = processLines(oldSlice, newSlice);
} fileHtml.left += tmpHtml.left;
fileHtml.right += tmpHtml.right;
}
}); });
oldLines = []; oldLines = [];
newLines = []; newLines = [];
} }
for (var i = 0; i < block.lines.length; i++) { for (var i = 0; i < block.lines.length; i++) {
var line = block.lines[i]; var line = block.lines[i];
var prefix = line[0]; var prefix = line[0];
var escapedLine = utils.escape(line.content.substr(1)); var escapedLine = utils.escape(line.content.substr(1));
if ( line.type !== diffParser.LINE_TYPE.INSERTS && if (line.type !== diffParser.LINE_TYPE.INSERTS &&
(newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) { (newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) {
processChangeBlock(); processChangeBlock();
} }
if (line.type == diffParser.LINE_TYPE.CONTEXT) {
if (line.type === diffParser.LINE_TYPE.CONTEXT) {
fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, escapedLine, prefix); fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, escapedLine, prefix);
fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix); fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !oldLines.length) { } else if (line.type === diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
fileHtml.left += generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, '', '', ''); fileHtml.left += generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, '', '', '');
fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix); fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix);
} else if (line.type == diffParser.LINE_TYPE.DELETES) { } else if (line.type === diffParser.LINE_TYPE.DELETES) {
oldLines.push(line); oldLines.push(line);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !!oldLines.length) { } else if (line.type === diffParser.LINE_TYPE.INSERTS && Boolean(oldLines.length)) {
newLines.push(line); newLines.push(line);
} else { } else {
console.error('unknown state in html side-by-side generator'); console.error('unknown state in html side-by-side generator');
@ -2440,21 +2479,24 @@
fileHtml.right = ''; fileHtml.right = '';
var maxLinesNumber = Math.max(oldLines.length, newLines.length); var maxLinesNumber = Math.max(oldLines.length, newLines.length);
for (j = 0; j < maxLinesNumber; j++) { for (var i = 0; i < maxLinesNumber; i++) {
var oldLine = oldLines[j]; var oldLine = oldLines[i];
var newLine = newLines[j]; var newLine = newLines[i];
var oldContent; var oldContent;
var newContent; var newContent;
var oldPrefix; var oldPrefix;
var newPrefix; var newPrefix;
if (oldLine) { if (oldLine) {
oldContent = utils.escape(oldLine.content.substr(1)); oldContent = utils.escape(oldLine.content.substr(1));
oldPrefix = oldLine.content[0]; oldPrefix = oldLine.content[0];
} }
if (newLine) { if (newLine) {
newContent = utils.escape(newLine.content.substr(1)); newContent = utils.escape(newLine.content.substr(1));
newPrefix = newLine.content[0]; newPrefix = newLine.content[0];
} }
if (oldLine && newLine) { if (oldLine && newLine) {
fileHtml.left += generateSingleLineHtml(oldLine.type, oldLine.oldNumber, oldContent, oldPrefix); fileHtml.left += generateSingleLineHtml(oldLine.type, oldLine.oldNumber, oldContent, oldPrefix);
fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, newContent, newPrefix); fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, newContent, newPrefix);
@ -2506,9 +2548,9 @@
return fileHtml; return fileHtml;
} }
module.exports['SideBySidePrinter'] = new SideBySidePrinter(); module.exports.SideBySidePrinter = new SideBySidePrinter();
})(this); })();
/***/ } /***/ }

View file

@ -1 +1 @@
.d2h-wrapper{display:block;margin:0 auto;text-align:left;width:100%}.d2h-file-wrapper{border:1px solid #ddd;border-radius:3px;margin-bottom:1em}.d2h-file-header{padding:5px 10px;border-bottom:1px solid #d8d8d8;background-color:#f7f7f7;font:13px Helvetica,arial,freesans,clean,sans-serif,"Segoe UI Emoji","Segoe UI Symbol"}.d2h-file-stats{display:inline;font-size:12px;text-align:center;max-width:15%}.d2h-lines-added{text-align:right}.d2h-lines-added>*{background-color:#ceffce;border:1px solid #b4e2b4;color:#399839;border-radius:5px 0 0 5px;padding:2px;width:25px}.d2h-lines-deleted{text-align:left}.d2h-lines-deleted>*{background-color:#f7c8c8;border:1px solid #e9aeae;color:#c33;border-radius:0 5px 5px 0;padding:2px;width:25px}.d2h-file-name{display:inline;height:33px;line-height:33px;max-width:80%;white-space:nowrap;text-overflow:ellipsis;overflow:hidden}.d2h-diff-table{border-collapse:collapse;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;height:18px;line-height:18px;width:100%}.d2h-files-diff{width:100%}.d2h-file-diff{overflow-x:scroll;overflow-y:hidden}.d2h-file-side-diff{display:inline-block;overflow-x:scroll;overflow-y:hidden;width:50%;margin-right:-4px}.d2h-code-line{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:80px;color:inherit;overflow-x:inherit;background:none}.d2h-code-side-line{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:50px;color:inherit;overflow-x:inherit;background:none}.d2h-code-line del,.d2h-code-side-line del{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#ffb6ba;border-radius:.2em}.d2h-code-line ins,.d2h-code-side-line ins{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#97f295;border-radius:.2em}.d2h-code-line-prefix{float:left;background:none;padding:0}.d2h-code-line-ctn{background:none;padding:0}.line-num1{display:inline-block;float:left;width:30px;overflow:hidden;text-overflow:ellipsis}.line-num2{display:inline-block;float:right;width:30px;overflow:hidden;text-overflow:ellipsis}.d2h-code-linenumber{position:absolute;width:2%;min-width:65px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,0.3);text-align:right;border:solid #eeeeee;border-width:0 1px 0 1px;cursor:pointer}.d2h-code-side-linenumber{position:absolute;width:35px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,0.3);text-align:right;border:solid #eeeeee;border-width:0 1px 0 1px;cursor:pointer;overflow:hidden;text-overflow:ellipsis}.d2h-del{background-color:#fee8e9;border-color:#e9aeae}.d2h-ins{background-color:#dfd;border-color:#b4e2b4}.d2h-info{background-color:#f8fafd;color:rgba(0,0,0,0.3);border-color:#d5e4f2}.d2h-file-list-wrapper{margin-bottom:10px;padding:0 10px}.d2h-file-list-wrapper a{text-decoration:none;color:#3572b0}.d2h-file-list-wrapper a:visited{color:#3572b0}.d2h-file-list-header{font-weight:bold;margin-bottom:5px;text-align:left;display:inline;float:left}.d2h-file-list-line{text-align:left;font:13px Helvetica,arial,freesans,clean,sans-serif,"Segoe UI Emoji","Segoe UI Symbol"}.d2h-file-list-line .d2h-file-name{line-height:21px}.d2h-file-list{display:none}.d2h-clear{display:block;clear:both}.d2h-del.d2h-change,.d2h-ins.d2h-change{background-color:#ffc}ins.d2h-change,del.d2h-change{background-color:#fad771}.d2h-file-diff .d2h-del.d2h-change{background-color:#fae1af}.d2h-file-diff .d2h-ins.d2h-change{background-color:#ded}.d2h-show{display:none;float:left}.d2h-hide{float:left}.d2h-hide:target+.d2h-show{display:inline}.d2h-hide:target{display:none}.d2h-hide:target~.d2h-file-list{display:block} .d2h-wrapper{display:block;margin:0 auto;text-align:left;width:100%}.d2h-file-wrapper{border:1px solid #ddd;border-radius:3px;margin-bottom:1em}.d2h-file-header{padding:5px 10px;border-bottom:1px solid #d8d8d8;background-color:#f7f7f7;font:13px Helvetica,arial,freesans,clean,sans-serif,"Segoe UI Emoji","Segoe UI Symbol"}.d2h-file-stats{display:inline;font-size:12px;text-align:center;max-width:15%}.d2h-lines-added{text-align:right}.d2h-lines-added>*{background-color:#ceffce;border:1px solid #b4e2b4;color:#399839;border-radius:5px 0 0 5px;padding:2px}.d2h-lines-deleted{text-align:left}.d2h-lines-deleted>*{background-color:#f7c8c8;border:1px solid #e9aeae;color:#c33;border-radius:0 5px 5px 0;padding:2px}.d2h-file-name{display:inline;line-height:33px;max-width:80%;white-space:nowrap;text-overflow:ellipsis;overflow:hidden}.d2h-diff-table{border-collapse:collapse;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;height:18px;line-height:18px;width:100%}.d2h-files-diff{width:100%}.d2h-file-diff{overflow-x:scroll;overflow-y:hidden}.d2h-file-side-diff{display:inline-block;overflow-x:scroll;overflow-y:hidden;width:50%;margin-right:-4px}.d2h-code-line{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:80px;color:inherit;overflow-x:inherit;background:none}.d2h-code-side-line{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:50px;color:inherit;overflow-x:inherit;background:none}.d2h-code-line del,.d2h-code-side-line del{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#ffb6ba;border-radius:.2em}.d2h-code-line ins,.d2h-code-side-line ins{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#97f295;border-radius:.2em}.d2h-code-line-prefix{float:left;background:none;padding:0}.d2h-code-line-ctn{background:none;padding:0}.line-num1{float:left;width:30px;overflow:hidden;text-overflow:ellipsis}.line-num2{float:right;width:30px;overflow:hidden;text-overflow:ellipsis}.d2h-code-linenumber{position:absolute;min-width:65px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,0.3);text-align:right;border:solid #eeeeee;border-width:0 1px 0 1px;cursor:pointer}.d2h-code-side-linenumber{position:absolute;width:35px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,0.3);text-align:right;border:solid #eeeeee;border-width:0 1px 0 1px;cursor:pointer;overflow:hidden;text-overflow:ellipsis}.d2h-del{background-color:#fee8e9;border-color:#e9aeae}.d2h-ins{background-color:#dfd;border-color:#b4e2b4}.d2h-info{background-color:#f8fafd;color:rgba(0,0,0,0.3);border-color:#d5e4f2}.d2h-file-list-wrapper{margin-bottom:10px;padding:0 10px}.d2h-file-list-wrapper a{text-decoration:none;color:#3572b0}.d2h-file-list-wrapper a:visited{color:#3572b0}.d2h-file-list-header{font-weight:bold;float:left}.d2h-file-list-line{text-align:left;font:13px Helvetica,arial,freesans,clean,sans-serif,"Segoe UI Emoji","Segoe UI Symbol"}.d2h-file-list-line .d2h-file-name{line-height:21px}.d2h-file-list{display:none}.d2h-clear{display:block;clear:both}.d2h-del.d2h-change,.d2h-ins.d2h-change{background-color:#ffc}ins.d2h-change,del.d2h-change{background-color:#fad771}.d2h-file-diff .d2h-del.d2h-change{background-color:#fae1af}.d2h-file-diff .d2h-ins.d2h-change{background-color:#ded}.d2h-show{display:none;float:left}.d2h-hide{float:left}.d2h-hide:target+.d2h-show{display:inline}.d2h-hide:target{display:none}.d2h-hide:target~.d2h-file-list{display:block}

File diff suppressed because one or more lines are too long

View file

@ -5,7 +5,7 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var utils = require('./utils.js').Utils; var utils = require('./utils.js').Utils;
@ -31,6 +31,7 @@
var newLine = null; var newLine = null;
var saveBlock = function() { var saveBlock = function() {
/* Add previous block(if exists) before start a new file */ /* Add previous block(if exists) before start a new file */
if (currentBlock) { if (currentBlock) {
currentFile.blocks.push(currentBlock); currentFile.blocks.push(currentBlock);
@ -39,6 +40,7 @@
}; };
var saveFile = function() { var saveFile = function() {
/* /*
* Add previous file(if exists) before start a new one * Add previous file(if exists) before start a new one
* if it has name (to avoid binary files errors) * if it has name (to avoid binary files errors)
@ -216,11 +218,11 @@
var nameSplit = filename.split('.'); var nameSplit = filename.split('.');
if (nameSplit.length > 1) { if (nameSplit.length > 1) {
return nameSplit[nameSplit.length - 1]; return nameSplit[nameSplit.length - 1];
} else {
return language;
} }
return language;
} }
module.exports['DiffParser'] = new DiffParser(); module.exports.DiffParser = new DiffParser();
})(this); })();

View file

@ -5,7 +5,7 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var diffParser = require('./diff-parser.js').DiffParser; var diffParser = require('./diff-parser.js').DiffParser;
var fileLister = require('./file-list-printer.js').FileListPrinter; var fileLister = require('./file-list-printer.js').FileListPrinter;
@ -17,9 +17,9 @@
/* /*
* Line diff type configuration * Line diff type configuration
var config = { var config = {
"wordByWord": true, // (default) 'wordByWord': true, // (default)
// OR // OR
"charByChar": true 'charByChar': true
}; };
*/ */
@ -37,23 +37,23 @@
var configOrEmpty = config || {}; var configOrEmpty = config || {};
var diffJson = diffInput; var diffJson = diffInput;
if(!configOrEmpty.inputFormat || configOrEmpty.inputFormat === 'diff') { if (!configOrEmpty.inputFormat || configOrEmpty.inputFormat === 'diff') {
diffJson = diffParser.generateDiffJson(diffInput); diffJson = diffParser.generateDiffJson(diffInput);
} }
var fileList = ""; var fileList = '';
if(configOrEmpty.showFiles === true) { if (configOrEmpty.showFiles === true) {
fileList = fileLister.generateFileList(diffJson, configOrEmpty); fileList = fileLister.generateFileList(diffJson, configOrEmpty);
} }
var diffOutput = ""; var diffOutput = '';
if(configOrEmpty.outputFormat === 'side-by-side') { if (configOrEmpty.outputFormat === 'side-by-side') {
diffOutput = htmlPrinter.generateSideBySideJsonHtml(diffJson, configOrEmpty); diffOutput = htmlPrinter.generateSideBySideJsonHtml(diffJson, configOrEmpty);
} else { } else {
diffOutput = htmlPrinter.generateLineByLineJsonHtml(diffJson, configOrEmpty); diffOutput = htmlPrinter.generateLineByLineJsonHtml(diffJson, configOrEmpty);
} }
return fileList + diffOutput return fileList + diffOutput;
}; };
@ -66,9 +66,9 @@
*/ */
Diff2Html.prototype.getPrettyHtmlFromDiff = function(diffInput, config) { Diff2Html.prototype.getPrettyHtmlFromDiff = function(diffInput, config) {
var configOrEmpty = config || {}; var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'diff'; configOrEmpty.inputFormat = 'diff';
configOrEmpty['outputFormat'] = 'line-by-line'; configOrEmpty.outputFormat = 'line-by-line';
return this.getPrettyHtml(diffInput, configOrEmpty) return this.getPrettyHtml(diffInput, configOrEmpty);
}; };
/* /*
@ -76,9 +76,9 @@
*/ */
Diff2Html.prototype.getPrettyHtmlFromJson = function(diffJson, config) { Diff2Html.prototype.getPrettyHtmlFromJson = function(diffJson, config) {
var configOrEmpty = config || {}; var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'json'; configOrEmpty.inputFormat = 'json';
configOrEmpty['outputFormat'] = 'line-by-line'; configOrEmpty.outputFormat = 'line-by-line';
return this.getPrettyHtml(diffJson, configOrEmpty) return this.getPrettyHtml(diffJson, configOrEmpty);
}; };
/* /*
@ -86,9 +86,9 @@
*/ */
Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function(diffInput, config) { Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function(diffInput, config) {
var configOrEmpty = config || {}; var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'diff'; configOrEmpty.inputFormat = 'diff';
configOrEmpty['outputFormat'] = 'side-by-side'; configOrEmpty.outputFormat = 'side-by-side';
return this.getPrettyHtml(diffInput, configOrEmpty) return this.getPrettyHtml(diffInput, configOrEmpty);
}; };
/* /*
@ -96,15 +96,15 @@
*/ */
Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function(diffJson, config) { Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function(diffJson, config) {
var configOrEmpty = config || {}; var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'json'; configOrEmpty.inputFormat = 'json';
configOrEmpty['outputFormat'] = 'side-by-side'; configOrEmpty.outputFormat = 'side-by-side';
return this.getPrettyHtml(diffJson, configOrEmpty) return this.getPrettyHtml(diffJson, configOrEmpty);
}; };
var diffName = 'Diff2Html';
var diffObject = new Diff2Html(); var diffObject = new Diff2Html();
module.exports[diffName] = diffObject; module.exports.Diff2Html = diffObject;
// Expose diff2html in the browser
global[diffName] = diffObject;
})(this); // Expose diff2html in the browser
global.Diff2Html = diffObject;
})();

View file

@ -5,39 +5,39 @@
* *
*/ */
(function (ctx, undefined) { (function() {
var printerUtils = require('./printer-utils.js').PrinterUtils; var printerUtils = require('./printer-utils.js').PrinterUtils;
var utils = require('./utils.js').Utils; var utils = require('./utils.js').Utils;
function FileListPrinter() { function FileListPrinter() {
} }
FileListPrinter.prototype.generateFileList = function (diffFiles) { FileListPrinter.prototype.generateFileList = function(diffFiles) {
var hideId = utils.getRandomId("d2h-hide"); //necessary if there are 2 elements like this in the same page var hideId = utils.getRandomId('d2h-hide'); //necessary if there are 2 elements like this in the same page
var showId = utils.getRandomId("d2h-show"); var showId = utils.getRandomId('d2h-show');
return '<div class="d2h-file-list-wrapper">\n' + return '<div class="d2h-file-list-wrapper">\n' +
' <div class="d2h-file-list-header">Files changed (' + diffFiles.length + ')&nbsp&nbsp</div>\n' + ' <div class="d2h-file-list-header">Files changed (' + diffFiles.length + ')&nbsp&nbsp</div>\n' +
' <a id="' + hideId + '" class="d2h-hide" href="#' + hideId + '">+</a>\n' + ' <a id="' + hideId + '" class="d2h-hide" href="#' + hideId + '">+</a>\n' +
' <a id="' + showId + 'd2h-show" class="d2h-show" href="#' + showId + '">-</a>\n' + ' <a id="' + showId + 'd2h-show" class="d2h-show" href="#' + showId + '">-</a>\n' +
' <div class="d2h-clear"></div>\n' + ' <div class="d2h-clear"></div>\n' +
' <table class="d2h-file-list">\n' + ' <table class="d2h-file-list">\n' +
diffFiles.map(function (file) { diffFiles.map(function(file) {
return ' <tr class="d2h-file-list-line">\n' + return ' <tr class="d2h-file-list-line">\n' +
' <td class="d2h-lines-added">\n' + ' <td class="d2h-lines-added">\n' +
' <span>+' + file.addedLines + '</span>\n' + ' <span>+' + file.addedLines + '</span>\n' +
' </td>\n' + ' </td>\n' +
' <td class="d2h-lines-deleted">\n' + ' <td class="d2h-lines-deleted">\n' +
' <span>-' + file.deletedLines + '</span>\n' + ' <span>-' + file.deletedLines + '</span>\n' +
' </td>\n' + ' </td>\n' +
' <td class="d2h-file-name"><a href="#' + printerUtils.getHtmlId(file) + '">&nbsp;' + printerUtils.getDiffName(file) + '</a></td>\n' + ' <td class="d2h-file-name"><a href="#' + printerUtils.getHtmlId(file) + '">&nbsp;' + printerUtils.getDiffName(file) + '</a></td>\n' +
' </tr>\n' ' </tr>\n';
}).join('\n') + }).join('\n') +
'</table></div>\n'; '</table></div>\n';
}; };
module.exports['FileListPrinter'] = new FileListPrinter(); module.exports.FileListPrinter = new FileListPrinter();
})(this); })();

View file

@ -5,7 +5,7 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var LineByLinePrinter = require('./line-by-line-printer.js').LineByLinePrinter; var LineByLinePrinter = require('./line-by-line-printer.js').LineByLinePrinter;
var sideBySidePrinter = require('./side-by-side-printer.js').SideBySidePrinter; var sideBySidePrinter = require('./side-by-side-printer.js').SideBySidePrinter;
@ -20,6 +20,6 @@
HtmlPrinter.prototype.generateSideBySideJsonHtml = sideBySidePrinter.generateSideBySideJsonHtml; HtmlPrinter.prototype.generateSideBySideJsonHtml = sideBySidePrinter.generateSideBySideJsonHtml;
module.exports['HtmlPrinter'] = new HtmlPrinter(); module.exports.HtmlPrinter = new HtmlPrinter();
})(this); })();

View file

@ -5,7 +5,7 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var diffParser = require('./diff-parser.js').DiffParser; var diffParser = require('./diff-parser.js').DiffParser;
var printerUtils = require('./printer-utils.js').PrinterUtils; var printerUtils = require('./printer-utils.js').PrinterUtils;
@ -23,9 +23,9 @@
var diffs; var diffs;
if (file.blocks.length) { if (file.blocks.length) {
diffs = that.generateFileHtml(file); diffs = that._generateFileHtml(file);
} else { } else {
diffs = that.generateEmptyDiff(); diffs = that._generateEmptyDiff();
} }
return '<div id="' + printerUtils.getHtmlId(file) + '" class="d2h-file-wrapper" data-lang="' + file.language + '">\n' + return '<div id="' + printerUtils.getHtmlId(file) + '" class="d2h-file-wrapper" data-lang="' + file.language + '">\n' +
@ -54,13 +54,14 @@
'</div>\n'; '</div>\n';
}; };
var matcher=Rematch.rematch(function(a,b) { var matcher = Rematch.rematch(function(a, b) {
var amod = a.content.substr(1), var amod = a.content.substr(1);
bmod = b.content.substr(1); var bmod = b.content.substr(1);
return Rematch.distance(amod, bmod); return Rematch.distance(amod, bmod);
}); });
LineByLinePrinter.prototype.generateFileHtml = function(file) { LineByLinePrinter.prototype._generateFileHtml = function(file) {
var that = this; var that = this;
return file.blocks.map(function(block) { return file.blocks.map(function(block) {
@ -73,50 +74,53 @@
var oldLines = []; var oldLines = [];
var newLines = []; var newLines = [];
function processChangeBlock() { function processChangeBlock() {
var matches; var matches;
var insertType; var insertType;
var deleteType; var deleteType;
var doMatching = that.config.matching === "lines" || that.config.matching === "words";
var doMatching = that.config.matching === 'lines' || that.config.matching === 'words';
if (doMatching) { if (doMatching) {
matches = matcher(oldLines, newLines); matches = matcher(oldLines, newLines);
insertType = diffParser.LINE_TYPE.INSERT_CHANGES; insertType = diffParser.LINE_TYPE.INSERT_CHANGES;
deleteType = diffParser.LINE_TYPE.DELETE_CHANGES; deleteType = diffParser.LINE_TYPE.DELETE_CHANGES;
} else { } else {
matches = [[oldLines,newLines]]; matches = [[oldLines, newLines]];
insertType = diffParser.LINE_TYPE.INSERTS; insertType = diffParser.LINE_TYPE.INSERTS;
deleteType = diffParser.LINE_TYPE.DELETES; deleteType = diffParser.LINE_TYPE.DELETES;
} }
matches.forEach(function(match){
var oldLines = match[0]; matches.forEach(function(match) {
var newLines = match[1]; oldLines = match[0];
newLines = match[1];
var processedOldLines = []; var processedOldLines = [];
var processedNewLines = []; var processedNewLines = [];
var j = 0;
var oldLine, newLine,
common = Math.min(oldLines.length, newLines.length),
max = Math.max(oldLines.length, newLines.length);
for (j = 0; j < common; j++) {
oldLine = oldLines[j];
newLine = newLines[j];
that.config.isCombined = file.isCombined; var common = Math.min(oldLines.length, newLines.length);
var diff = printerUtils.diffHighlight(oldLine.content, newLine.content, that.config);
processedOldLines += var oldLine, newLine;
that.generateLineHtml(deleteType, oldLine.oldNumber, oldLine.newNumber, for (var j = 0; j < common; j++) {
diff.first.line, diff.first.prefix); oldLine = oldLines[j];
processedNewLines += newLine = newLines[j];
that.generateLineHtml(insertType, newLine.oldNumber, newLine.newNumber,
diff.second.line, diff.second.prefix);
}
lines += processedOldLines + processedNewLines; that.config.isCombined = file.isCombined;
lines += that.processLines(oldLines.slice(common), newLines.slice(common)); var diff = printerUtils.diffHighlight(oldLine.content, newLine.content, that.config);
processedOldLines = []; processedOldLines +=
processedNewLines = []; that._generateLineHtml(deleteType, oldLine.oldNumber, oldLine.newNumber,
diff.first.line, diff.first.prefix);
processedNewLines +=
that._generateLineHtml(insertType, newLine.oldNumber, newLine.newNumber,
diff.second.line, diff.second.prefix);
}
lines += processedOldLines + processedNewLines;
lines += that._processLines(oldLines.slice(common), newLines.slice(common));
}); });
oldLines = []; oldLines = [];
newLines = []; newLines = [];
} }
@ -125,20 +129,21 @@
var line = block.lines[i]; var line = block.lines[i];
var escapedLine = utils.escape(line.content); var escapedLine = utils.escape(line.content);
if ( line.type !== diffParser.LINE_TYPE.INSERTS && if (line.type !== diffParser.LINE_TYPE.INSERTS &&
(newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) { (newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) {
processChangeBlock(); processChangeBlock();
} }
if (line.type == diffParser.LINE_TYPE.CONTEXT) {
lines += that.generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine); if (line.type === diffParser.LINE_TYPE.CONTEXT) {
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !oldLines.length) { lines += that._generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
lines += that.generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine); } else if (line.type === diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
} else if (line.type == diffParser.LINE_TYPE.DELETES) { lines += that._generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
} else if (line.type === diffParser.LINE_TYPE.DELETES) {
oldLines.push(line); oldLines.push(line);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !!oldLines.length) { } else if (line.type === diffParser.LINE_TYPE.INSERTS && Boolean(oldLines.length)) {
newLines.push(line); newLines.push(line);
} else { } else {
console.error('unknown state in html line-by-line generator'); console.error('Unknown state in html line-by-line generator');
processChangeBlock(); processChangeBlock();
} }
} }
@ -147,27 +152,27 @@
return lines; return lines;
}).join('\n'); }).join('\n');
} };
LineByLinePrinter.prototype.processLines = function(oldLines, newLines) { LineByLinePrinter.prototype._processLines = function(oldLines, newLines) {
var lines = ''; var lines = '';
for (j = 0; j < oldLines.length; j++) { for (var i = 0; i < oldLines.length; i++) {
var oldLine = oldLines[j]; var oldLine = oldLines[i];
var oldEscapedLine = utils.escape(oldLine.content); var oldEscapedLine = utils.escape(oldLine.content);
lines += this.generateLineHtml(oldLine.type, oldLine.oldNumber, oldLine.newNumber, oldEscapedLine); lines += this._generateLineHtml(oldLine.type, oldLine.oldNumber, oldLine.newNumber, oldEscapedLine);
} }
for (j = 0; j < newLines.length; j++) { for (var j = 0; j < newLines.length; j++) {
var newLine = newLines[j]; var newLine = newLines[j];
var newEscapedLine = utils.escape(newLine.content); var newEscapedLine = utils.escape(newLine.content);
lines += this.generateLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, newEscapedLine); lines += this._generateLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, newEscapedLine);
} }
return lines; return lines;
} };
LineByLinePrinter.prototype.generateLineHtml = function(type, oldNumber, newNumber, content, prefix) { LineByLinePrinter.prototype._generateLineHtml = function(type, oldNumber, newNumber, content, prefix) {
var htmlPrefix = ''; var htmlPrefix = '';
if (prefix) { if (prefix) {
htmlPrefix = '<span class="d2h-code-line-prefix">' + prefix + '</span>'; htmlPrefix = '<span class="d2h-code-line-prefix">' + prefix + '</span>';
@ -187,9 +192,9 @@
' <div class="d2h-code-line ' + type + '">' + htmlPrefix + htmlContent + '</div>' + ' <div class="d2h-code-line ' + type + '">' + htmlPrefix + htmlContent + '</div>' +
' </td>\n' + ' </td>\n' +
'</tr>\n'; '</tr>\n';
} };
LineByLinePrinter.prototype.generateEmptyDiff = function() { LineByLinePrinter.prototype._generateEmptyDiff = function() {
return '<tr>\n' + return '<tr>\n' +
' <td class="' + diffParser.LINE_TYPE.INFO + '">' + ' <td class="' + diffParser.LINE_TYPE.INFO + '">' +
' <div class="d2h-code-line ' + diffParser.LINE_TYPE.INFO + '">' + ' <div class="d2h-code-line ' + diffParser.LINE_TYPE.INFO + '">' +
@ -197,8 +202,8 @@
' </div>' + ' </div>' +
' </td>\n' + ' </td>\n' +
'</tr>\n'; '</tr>\n';
} };
module.exports['LineByLinePrinter'] = LineByLinePrinter; module.exports.LineByLinePrinter = LineByLinePrinter;
})(this); })();

View file

@ -5,7 +5,7 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var jsDiff = require('diff'); var jsDiff = require('diff');
var utils = require('./utils.js').Utils; var utils = require('./utils.js').Utils;
@ -15,39 +15,41 @@
} }
PrinterUtils.prototype.getHtmlId = function(file) { PrinterUtils.prototype.getHtmlId = function(file) {
var hashCode = function(text) { var hashCode = function(text) {
var hash = 0, i, chr, len; var i, chr, len;
if (text.length == 0) return hash; var hash = 0;
if (text.length === 0) return hash;
for (i = 0, len = text.length; i < len; i++) { for (i = 0, len = text.length; i < len; i++) {
chr = text.charCodeAt(i); chr = text.charCodeAt(i);
hash = ((hash << 5) - hash) + chr; hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer hash |= 0; // Convert to 32bit integer
} }
return hash; return hash;
}; };
return "d2h-" + hashCode(this.getDiffName(file)).toString().slice(-6); return 'd2h-' + hashCode(this.getDiffName(file)).toString().slice(-6);
}; };
PrinterUtils.prototype.getDiffName = function(file) { PrinterUtils.prototype.getDiffName = function(file) {
var oldFilename = file.oldName; var oldFilename = file.oldName;
var newFilename = file.newName; var newFilename = file.newName;
if (oldFilename && newFilename if (oldFilename && newFilename && oldFilename !== newFilename && !isDeletedName(newFilename)) {
&& oldFilename !== newFilename
&& !isDeletedName(newFilename)) {
return oldFilename + ' -> ' + newFilename; return oldFilename + ' -> ' + newFilename;
} else if (newFilename && !isDeletedName(newFilename)) { } else if (newFilename && !isDeletedName(newFilename)) {
return newFilename; return newFilename;
} else if (oldFilename) { } else if (oldFilename) {
return oldFilename; return oldFilename;
} else {
return 'Unknown filename';
} }
return 'Unknown filename';
}; };
PrinterUtils.prototype.diffHighlight = function(diffLine1, diffLine2, config) { PrinterUtils.prototype.diffHighlight = function(diffLine1, diffLine2, config) {
var lineStart1, lineStart2; var linePrefix1, linePrefix2, unprefixedLine1, unprefixedLine2;
var prefixSize = 1; var prefixSize = 1;
@ -55,17 +57,16 @@
prefixSize = 2; prefixSize = 2;
} }
lineStart1 = diffLine1.substr(0, prefixSize); linePrefix1 = diffLine1.substr(0, prefixSize);
lineStart2 = diffLine2.substr(0, prefixSize); linePrefix2 = diffLine2.substr(0, prefixSize);
unprefixedLine1 = diffLine1.substr(prefixSize);
diffLine1 = diffLine1.substr(prefixSize); unprefixedLine2 = diffLine2.substr(prefixSize);
diffLine2 = diffLine2.substr(prefixSize);
var diff; var diff;
if (config.charByChar) { if (config.charByChar) {
diff = jsDiff.diffChars(diffLine1, diffLine2); diff = jsDiff.diffChars(unprefixedLine1, unprefixedLine2);
} else { } else {
diff = jsDiff.diffWordsWithSpace(diffLine1, diffLine2); diff = jsDiff.diffWordsWithSpace(unprefixedLine1, unprefixedLine2);
} }
var highlightedLine = ''; var highlightedLine = '';
@ -73,25 +74,30 @@
var changedWords = []; var changedWords = [];
if (!config.charByChar && config.matching === 'words') { if (!config.charByChar && config.matching === 'words') {
var treshold = 0.25; var treshold = 0.25;
if (typeof(config.matchWordsThreshold) !== "undefined") {
if (typeof(config.matchWordsThreshold) !== 'undefined') {
treshold = config.matchWordsThreshold; treshold = config.matchWordsThreshold;
} }
var matcher = Rematch.rematch(function(a, b) { var matcher = Rematch.rematch(function(a, b) {
var amod = a.value, var amod = a.value;
bmod = b.value, var bmod = b.value;
result = Rematch.distance(amod, bmod);
return result; return Rematch.distance(amod, bmod);
}); });
var removed = diff.filter(function isRemoved(element){
var removed = diff.filter(function isRemoved(element) {
return element.removed; return element.removed;
}); });
var added = diff.filter(function isAdded(element){
var added = diff.filter(function isAdded(element) {
return element.added; return element.added;
}); });
var chunks = matcher(added, removed); var chunks = matcher(added, removed);
chunks = chunks.forEach(function(chunk){ chunks.forEach(function(chunk) {
if(chunk[0].length === 1 && chunk[1].length === 1) { if (chunk[0].length === 1 && chunk[1].length === 1) {
var dist = Rematch.distance(chunk[0][0].value, chunk[1][0].value) var dist = Rematch.distance(chunk[0][0].value, chunk[1][0].value);
if (dist < treshold) { if (dist < treshold) {
changedWords.push(chunk[0][0]); changedWords.push(chunk[0][0]);
changedWords.push(chunk[1][0]); changedWords.push(chunk[1][0]);
@ -99,6 +105,7 @@
} }
}); });
} }
diff.forEach(function(part) { diff.forEach(function(part) {
var addClass = changedWords.indexOf(part) > -1 ? ' class="d2h-change"' : ''; var addClass = changedWords.indexOf(part) > -1 ? ' class="d2h-change"' : '';
var elemType = part.added ? 'ins' : part.removed ? 'del' : null; var elemType = part.added ? 'ins' : part.removed ? 'del' : null;
@ -113,11 +120,11 @@
return { return {
first: { first: {
prefix: lineStart1, prefix: linePrefix1,
line: removeIns(highlightedLine) line: removeIns(highlightedLine)
}, },
second: { second: {
prefix: lineStart2, prefix: linePrefix2,
line: removeDel(highlightedLine) line: removeDel(highlightedLine)
} }
} }
@ -135,6 +142,6 @@
return line.replace(/(<del[^>]*>((.|\n)*?)<\/del>)/g, ''); return line.replace(/(<del[^>]*>((.|\n)*?)<\/del>)/g, '');
} }
module.exports['PrinterUtils'] = new PrinterUtils(); module.exports.PrinterUtils = new PrinterUtils();
})(this); })();

View file

@ -6,72 +6,76 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var Rematch = {}; var Rematch = {};
Rematch.arrayToString = function arrayToString(a) { Rematch.arrayToString = function arrayToString(a) {
if (Object.prototype.toString.apply(a,[]) === "[object Array]") { if (Object.prototype.toString.apply(a, []) === "[object Array]") {
return "[" + a.map(arrayToString).join(", ") + "]"; return "[" + a.map(arrayToString).join(", ") + "]";
} else { } else {
return a; return a;
} }
} };
/* /*
Copyright (c) 2011 Andrei Mackenzie Copyright (c) 2011 Andrei Mackenzie
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
function levenshtein(a, b){ function levenshtein(a, b) {
if(a.length == 0) return b.length; if (a.length == 0) return b.length;
if(b.length == 0) return a.length; if (b.length == 0) return a.length;
var matrix = []; var matrix = [];
// increment along the first column of each row // increment along the first column of each row
var i; var i;
for(i = 0; i <= b.length; i++){ for (i = 0; i <= b.length; i++) {
matrix[i] = [i]; matrix[i] = [i];
} }
// increment each column in the first row // increment each column in the first row
var j; var j;
for(j = 0; j <= a.length; j++){ for (j = 0; j <= a.length; j++) {
matrix[0][j] = j; matrix[0][j] = j;
} }
// Fill in the rest of the matrix // Fill in the rest of the matrix
for(i = 1; i <= b.length; i++){ for (i = 1; i <= b.length; i++) {
for(j = 1; j <= a.length; j++){ for (j = 1; j <= a.length; j++) {
if(b.charAt(i-1) == a.charAt(j-1)){ if (b.charAt(i - 1) == a.charAt(j - 1)) {
matrix[i][j] = matrix[i-1][j-1]; matrix[i][j] = matrix[i - 1][j - 1];
} else { } else {
matrix[i][j] = Math.min(matrix[i-1][j-1] + 1, // substitution matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
Math.min(matrix[i][j-1] + 1, // insertion Math.min(matrix[i][j - 1] + 1, // insertion
matrix[i-1][j] + 1)); // deletion matrix[i - 1][j] + 1)); // deletion
} }
} }
} }
return matrix[b.length][a.length]; return matrix[b.length][a.length];
} }
Rematch.levenshtein = levenshtein; Rematch.levenshtein = levenshtein;
Rematch.distance = function distance(x,y) { Rematch.distance = function distance(x, y) {
x=x.trim(); x = x.trim();
y=y.trim(); y = y.trim();
var lev = levenshtein(x,y), var lev = levenshtein(x, y);
score = lev / (x.length + y.length); var score = lev / (x.length + y.length);
return score; return score;
} };
Rematch.rematch = function rematch(distanceFunction) { Rematch.rematch = function rematch(distanceFunction) {
function findBestMatch(a, b, cache) { function findBestMatch(a, b, cache) {
var cachecount = 0; var cachecount = 0;
for(var key in cache) { for (var key in cache) {
cachecount++; cachecount++;
} }
var bestMatchDist = Infinity; var bestMatchDist = Infinity;
var bestMatch; var bestMatch;
for (var i = 0; i < a.length; ++i) { for (var i = 0; i < a.length; ++i) {
@ -86,25 +90,29 @@
} }
if (md < bestMatchDist) { if (md < bestMatchDist) {
bestMatchDist = md; bestMatchDist = md;
bestMatch = { indexA: i, indexB: j, score: bestMatchDist }; bestMatch = {indexA: i, indexB: j, score: bestMatchDist};
} }
} }
} }
return bestMatch; return bestMatch;
} }
function group(a, b, level, cache) {
if (typeof(cache)==="undefined") { function group(a, b, level, cache) {
if (typeof(cache) === "undefined") {
cache = {}; cache = {};
} }
var minLength = Math.min(a.length, b.length);
var bm = findBestMatch(a,b, cache); var bm = findBestMatch(a, b, cache);
if (!level) { if (!level) {
level = 0; level = 0;
} }
if (!bm || (a.length + b.length < 3)) { if (!bm || (a.length + b.length < 3)) {
return [[a, b]]; return [[a, b]];
} }
var a1 = a.slice(0, bm.indexA), var a1 = a.slice(0, bm.indexA),
b1 = b.slice(0, bm.indexB), b1 = b.slice(0, bm.indexB),
aMatch = [a[bm.indexA]], aMatch = [a[bm.indexA]],
@ -114,21 +122,25 @@
a2 = a.slice(tailA), a2 = a.slice(tailA),
b2 = b.slice(tailB); b2 = b.slice(tailB);
var group1 = group(a1, b1, level+1, cache); var group1 = group(a1, b1, level + 1, cache);
var groupMatch = group(aMatch, bMatch, level+1, cache); var groupMatch = group(aMatch, bMatch, level + 1, cache);
var group2 = group(a2, b2, level+1, cache); var group2 = group(a2, b2, level + 1, cache);
var result = groupMatch; var result = groupMatch;
if (bm.indexA > 0 || bm.indexB > 0) { if (bm.indexA > 0 || bm.indexB > 0) {
result = group1.concat(result); result = group1.concat(result);
} }
if (a.length > tailA || b.length > tailB ) {
if (a.length > tailA || b.length > tailB) {
result = result.concat(group2); result = result.concat(group2);
} }
return result; return result;
} }
return group; return group;
} };
module.exports['Rematch'] = Rematch; module.exports.Rematch = Rematch;
})(this); })();

View file

@ -5,7 +5,7 @@
* *
*/ */
(function(ctx, undefined) { (function() {
var diffParser = require('./diff-parser.js').DiffParser; var diffParser = require('./diff-parser.js').DiffParser;
var printerUtils = require('./printer-utils.js').PrinterUtils; var printerUtils = require('./printer-utils.js').PrinterUtils;
@ -63,9 +63,9 @@
'</div>\n'; '</div>\n';
}; };
var matcher=Rematch.rematch(function(a,b) { var matcher = Rematch.rematch(function(a, b) {
var amod = a.content.substr(1), var amod = a.content.substr(1),
bmod = b.content.substr(1); bmod = b.content.substr(1);
return Rematch.distance(amod, bmod); return Rematch.distance(amod, bmod);
}); });
@ -94,31 +94,33 @@
var oldLines = []; var oldLines = [];
var newLines = []; var newLines = [];
function processChangeBlock() { function processChangeBlock() {
var matches; var matches;
var insertType; var insertType;
var deleteType; var deleteType;
var doMatching = config.matching === "lines" || config.matching === "words"; var doMatching = config.matching === 'lines' || config.matching === 'words';
if (doMatching) { if (doMatching) {
matches = matcher(oldLines, newLines); matches = matcher(oldLines, newLines);
insertType = diffParser.LINE_TYPE.INSERT_CHANGES; insertType = diffParser.LINE_TYPE.INSERT_CHANGES;
deleteType = diffParser.LINE_TYPE.DELETE_CHANGES; deleteType = diffParser.LINE_TYPE.DELETE_CHANGES;
} else { } else {
matches = [[oldLines,newLines]]; matches = [[oldLines, newLines]];
insertType = diffParser.LINE_TYPE.INSERTS; insertType = diffParser.LINE_TYPE.INSERTS;
deleteType = diffParser.LINE_TYPE.DELETES; deleteType = diffParser.LINE_TYPE.DELETES;
} }
matches.forEach(function(match){
var oldLines = match[0]; matches.forEach(function(match) {
var newLines = match[1]; oldLines = match[0];
var tmpHtml; newLines = match[1];
var j = 0;
var oldLine, newLine, var common = Math.min(oldLines.length, newLines.length);
common = Math.min(oldLines.length, newLines.length), var max = Math.max(oldLines.length, newLines.length);
max = Math.max(oldLines.length, newLines.length);
for (j = 0; j < common; j++) { for (var j = 0; j < common; j++) {
oldLine = oldLines[j]; var oldLine = oldLines[j];
newLine = newLines[j]; var newLine = newLines[j];
config.isCombined = file.isCombined; config.isCombined = file.isCombined;
@ -130,36 +132,41 @@
fileHtml.right += fileHtml.right +=
generateSingleLineHtml(insertType, newLine.newNumber, generateSingleLineHtml(insertType, newLine.newNumber,
diff.second.line, diff.second.prefix); diff.second.line, diff.second.prefix);
} }
if (max > common) {
var oldSlice = oldLines.slice(common), if (max > common) {
newSlice = newLines.slice(common); var oldSlice = oldLines.slice(common);
tmpHtml = processLines(oldLines.slice(common), newLines.slice(common)); var newSlice = newLines.slice(common);
fileHtml.left += tmpHtml.left;
fileHtml.right += tmpHtml.right; var tmpHtml = processLines(oldSlice, newSlice);
} fileHtml.left += tmpHtml.left;
fileHtml.right += tmpHtml.right;
}
}); });
oldLines = []; oldLines = [];
newLines = []; newLines = [];
} }
for (var i = 0; i < block.lines.length; i++) { for (var i = 0; i < block.lines.length; i++) {
var line = block.lines[i]; var line = block.lines[i];
var prefix = line[0]; var prefix = line[0];
var escapedLine = utils.escape(line.content.substr(1)); var escapedLine = utils.escape(line.content.substr(1));
if ( line.type !== diffParser.LINE_TYPE.INSERTS && if (line.type !== diffParser.LINE_TYPE.INSERTS &&
(newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) { (newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) {
processChangeBlock(); processChangeBlock();
} }
if (line.type == diffParser.LINE_TYPE.CONTEXT) {
if (line.type === diffParser.LINE_TYPE.CONTEXT) {
fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, escapedLine, prefix); fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, escapedLine, prefix);
fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix); fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !oldLines.length) { } else if (line.type === diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
fileHtml.left += generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, '', '', ''); fileHtml.left += generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, '', '', '');
fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix); fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix);
} else if (line.type == diffParser.LINE_TYPE.DELETES) { } else if (line.type === diffParser.LINE_TYPE.DELETES) {
oldLines.push(line); oldLines.push(line);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !!oldLines.length) { } else if (line.type === diffParser.LINE_TYPE.INSERTS && Boolean(oldLines.length)) {
newLines.push(line); newLines.push(line);
} else { } else {
console.error('unknown state in html side-by-side generator'); console.error('unknown state in html side-by-side generator');
@ -179,21 +186,24 @@
fileHtml.right = ''; fileHtml.right = '';
var maxLinesNumber = Math.max(oldLines.length, newLines.length); var maxLinesNumber = Math.max(oldLines.length, newLines.length);
for (j = 0; j < maxLinesNumber; j++) { for (var i = 0; i < maxLinesNumber; i++) {
var oldLine = oldLines[j]; var oldLine = oldLines[i];
var newLine = newLines[j]; var newLine = newLines[i];
var oldContent; var oldContent;
var newContent; var newContent;
var oldPrefix; var oldPrefix;
var newPrefix; var newPrefix;
if (oldLine) { if (oldLine) {
oldContent = utils.escape(oldLine.content.substr(1)); oldContent = utils.escape(oldLine.content.substr(1));
oldPrefix = oldLine.content[0]; oldPrefix = oldLine.content[0];
} }
if (newLine) { if (newLine) {
newContent = utils.escape(newLine.content.substr(1)); newContent = utils.escape(newLine.content.substr(1));
newPrefix = newLine.content[0]; newPrefix = newLine.content[0];
} }
if (oldLine && newLine) { if (oldLine && newLine) {
fileHtml.left += generateSingleLineHtml(oldLine.type, oldLine.oldNumber, oldContent, oldPrefix); fileHtml.left += generateSingleLineHtml(oldLine.type, oldLine.oldNumber, oldContent, oldPrefix);
fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, newContent, newPrefix); fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, newContent, newPrefix);
@ -245,6 +255,6 @@
return fileHtml; return fileHtml;
} }
module.exports['SideBySidePrinter'] = new SideBySidePrinter(); module.exports.SideBySidePrinter = new SideBySidePrinter();
})(this); })();

View file

@ -5,7 +5,7 @@
* *
*/ */
(function(ctx, undefined) { (function() {
function Utils() { function Utils() {
} }
@ -19,7 +19,7 @@
}; };
Utils.prototype.getRandomId = function(prefix) { Utils.prototype.getRandomId = function(prefix) {
return prefix + "-" + Math.random().toString(36).slice(-3); return prefix + '-' + Math.random().toString(36).slice(-3);
}; };
Utils.prototype.startsWith = function(str, start) { Utils.prototype.startsWith = function(str, start) {
@ -41,6 +41,6 @@
return value ? value : ''; return value ? value : '';
}; };
module.exports['Utils'] = new Utils(); module.exports.Utils = new Utils();
})(this); })();