Skip to content

Commit 621f649

Browse files
committedMar 13, 2019
abstract htmldiffer
1 parent d069d0d commit 621f649

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed
 

‎jasmine.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"**/*-spec.js"
55
],
66
"helpers": [
7-
"helpers/**/*.js"
7+
"helpers/helpers.js"
88
],
99
"stopSpecOnExpectationFailure": false,
1010
"random": true

‎test/helpers/helpers.js

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
const marked = require('../../');
2-
const HtmlDiffer = require('@markedjs/html-differ').HtmlDiffer;
3-
const htmlDiffer = new HtmlDiffer({ignoreSelfClosingSlash: true});
4-
5-
const EXCERPT_LENGTH = 30;
2+
const htmlDiffer = require('./html-differ.js');
63

74
beforeEach(() => {
85
marked.setOptions(marked.getDefaults());
@@ -18,23 +15,8 @@ beforeEach(() => {
1815
if (result.pass) {
1916
result.message = spec.markdown + '\n------\n\nExpected: Should Fail';
2017
} else {
21-
var expectedHtml = expected.replace(/\s/g, '');
22-
var actualHtml = actual.replace(/\s/g, '');
23-
24-
for (var i = 0; i < expectedHtml.length; i++) {
25-
if (actualHtml[i] !== expectedHtml[i]) {
26-
actualHtml = actualHtml.substring(
27-
Math.max(i - EXCERPT_LENGTH, 0),
28-
Math.min(i + EXCERPT_LENGTH, actualHtml.length));
29-
30-
expectedHtml = expectedHtml.substring(
31-
Math.max(i - EXCERPT_LENGTH, 0),
32-
Math.min(i + EXCERPT_LENGTH, expectedHtml.length));
33-
34-
break;
35-
}
36-
}
37-
result.message = 'Expected:\n' + expectedHtml + '\n\nActual:\n' + actualHtml;
18+
const diff = htmlDiffer.firstDiff(actual, expected);
19+
result.message = 'Expected: ' + diff.expected + '\n Actual: ' + diff.actual;
3820
}
3921
return result;
4022
}

‎test/helpers/html-differ.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const HtmlDiffer = require('@markedjs/html-differ').HtmlDiffer;
2+
const htmlDiffer = new HtmlDiffer({ignoreSelfClosingSlash: true});
3+
4+
module.exports = {
5+
isEqual: htmlDiffer.isEqual.bind(htmlDiffer),
6+
firstDiff: (actual, expected, padding) => {
7+
padding = padding || 30;
8+
const result = htmlDiffer
9+
.diffHtml(actual, expected)
10+
.reduce((obj, diff) => {
11+
if (diff.added) {
12+
if (obj.firstIndex === null) {
13+
obj.firstIndex = obj.expected.length;
14+
}
15+
obj.expected += diff.value;
16+
} else if (diff.removed) {
17+
if (obj.firstIndex === null) {
18+
obj.firstIndex = obj.actual.length;
19+
}
20+
obj.actual += diff.value;
21+
} else {
22+
obj.actual += diff.value;
23+
obj.expected += diff.value;
24+
}
25+
26+
return obj;
27+
}, {
28+
firstIndex: null,
29+
actual: '',
30+
expected: ''
31+
});
32+
33+
return {
34+
actual: result.actual.substring(result.firstIndex - padding, result.firstIndex + padding),
35+
expected: result.expected.substring(result.firstIndex - padding, result.firstIndex + padding)
36+
};
37+
}
38+
};

0 commit comments

Comments
 (0)
Please sign in to comment.