marked/test/helpers/html-differ.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-03-13 08:50:11 -05:00
const HtmlDiffer = require('@markedjs/html-differ').HtmlDiffer;
2020-08-07 11:19:47 -05:00
const htmlDiffer = new HtmlDiffer({
ignoreSelfClosingSlash: true,
ignoreComments: false
});
2019-03-13 08:50:11 -05:00
module.exports = {
isEqual: htmlDiffer.isEqual.bind(htmlDiffer),
2019-08-27 13:56:04 -05:00
firstDiff: async(actual, expected, padding) => {
2019-03-13 08:50:11 -05:00
padding = padding || 30;
2019-08-27 13:56:04 -05:00
const diffHtml = await htmlDiffer.diffHtml(actual, expected);
const result = diffHtml.reduce((obj, diff) => {
if (diff.added) {
if (obj.firstIndex === null) {
obj.firstIndex = obj.expected.length;
2019-03-13 08:50:11 -05:00
}
2019-08-27 13:56:04 -05:00
obj.expected += diff.value;
} else if (diff.removed) {
if (obj.firstIndex === null) {
obj.firstIndex = obj.actual.length;
}
obj.actual += diff.value;
} else {
obj.actual += diff.value;
obj.expected += diff.value;
}
2019-03-13 08:50:11 -05:00
2019-08-27 13:56:04 -05:00
return obj;
}, {
firstIndex: null,
actual: '',
expected: ''
});
2019-03-13 08:50:11 -05:00
return {
actual: result.actual.substring(result.firstIndex - padding, result.firstIndex + padding),
expected: result.expected.substring(result.firstIndex - padding, result.firstIndex + padding)
};
}
};