Skip to content

Commit 8614599

Browse files
committed
fix new and delete line identification
* add different prefixes for simple and combined diffs
1 parent de13e95 commit 8614599

File tree

6 files changed

+46
-8
lines changed

6 files changed

+46
-8
lines changed

dist/diff2html.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,11 @@
213213
var currentLine = {};
214214
currentLine.content = line;
215215

216+
var newLinePrefixes = !currentFile.isCombined ? ['+'] : ['+', ' +'];
217+
var delLinePrefixes = !currentFile.isCombined ? ['-'] : ['-', ' -'];
218+
216219
/* Fill the line data */
217-
if (utils.startsWith(line, '+') || utils.startsWith(line, ' +')) {
220+
if (utils.startsWith(line, newLinePrefixes)) {
218221
currentFile.addedLines++;
219222

220223
currentLine.type = LINE_TYPE.INSERTS;
@@ -223,7 +226,7 @@
223226

224227
currentBlock.lines.push(currentLine);
225228

226-
} else if (utils.startsWith(line, '-') || utils.startsWith(line, ' -')) {
229+
} else if (utils.startsWith(line, delLinePrefixes)) {
227230
currentFile.deletedLines++;
228231

229232
currentLine.type = LINE_TYPE.DELETES;
@@ -371,6 +374,17 @@
371374
};
372375

373376
Utils.prototype.startsWith = function(str, start) {
377+
if (typeof start === 'object') {
378+
var result = false;
379+
start.forEach(function(s) {
380+
if (str.indexOf(s) === 0) {
381+
result = true;
382+
}
383+
});
384+
385+
return result;
386+
}
387+
374388
return str.indexOf(start) === 0;
375389
};
376390

dist/diff2html.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "diff2html",
3-
"version": "0.2.7-1",
3+
"version": "1.0.0-1",
44
"homepage": "http://rtfpessoa.github.io/diff2html/",
55
"description": "Fast Diff to colorized HTML",
66
"keywords": [
@@ -40,7 +40,7 @@
4040
},
4141
"main": "./src/diff2html.js",
4242
"dependencies": {
43-
"diff": "2.0.1"
43+
"diff": "1.4.0"
4444
},
4545
"devDependencies": {},
4646
"license": "MIT",

sample/index.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,17 @@
202202
"+var text = 'diff --git a/components/app/app.html b/components/app/app.html\\nindex ecb7a95..027bd9b 100644\\n--- a/components/app/app.html\\n+++ b/components/app/app.html\\n@@ -52,0 +53,3 @@\\n+\\n+\\n+\\n@@ -56,0 +60,3 @@\\n+\\n+\\n+\\n'\n" +
203203
'+var patchLineList = [ false, false, false, false ];\n' +
204204
'+\n' +
205-
'+console.log(parser.parsePatchDiffResult(text, patchLineList));\n';
205+
'+console.log(parser.parsePatchDiffResult(text, patchLineList));\n' +
206+
"diff --git a/a.xml b/b.xml\n" +
207+
"index e54317e..82a9a56 100644\n" +
208+
"--- a/a.xml\n" +
209+
"+++ b/b.xml\n" +
210+
"@@ -242,4 +242,6 @@ need to create a new job for native server java api and move these tests to a ne\n" +
211+
" </packages>\n" +
212+
" </test>\n" +
213+
" -->\n" +
214+
"+\n" +
215+
"+\n";
206216

207217
document.addEventListener("DOMContentLoaded", function() {
208218
// parse the diff to json

src/diff-parser.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@
8787
var currentLine = {};
8888
currentLine.content = line;
8989

90+
var newLinePrefixes = !currentFile.isCombined ? ['+'] : ['+', ' +'];
91+
var delLinePrefixes = !currentFile.isCombined ? ['-'] : ['-', ' -'];
92+
9093
/* Fill the line data */
91-
if (utils.startsWith(line, '+') || utils.startsWith(line, ' +')) {
94+
if (utils.startsWith(line, newLinePrefixes)) {
9295
currentFile.addedLines++;
9396

9497
currentLine.type = LINE_TYPE.INSERTS;
@@ -97,7 +100,7 @@
97100

98101
currentBlock.lines.push(currentLine);
99102

100-
} else if (utils.startsWith(line, '-') || utils.startsWith(line, ' -')) {
103+
} else if (utils.startsWith(line, delLinePrefixes)) {
101104
currentFile.deletedLines++;
102105

103106
currentLine.type = LINE_TYPE.DELETES;

src/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919
};
2020

2121
Utils.prototype.startsWith = function(str, start) {
22+
if (typeof start === 'object') {
23+
var result = false;
24+
start.forEach(function(s) {
25+
if (str.indexOf(s) === 0) {
26+
result = true;
27+
}
28+
});
29+
30+
return result;
31+
}
32+
2233
return str.indexOf(start) === 0;
2334
};
2435

0 commit comments

Comments
 (0)