html-differ.js 1.1 KB

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