JSPM

@michaelray/html-diff

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q15059F
  • License ISC

JavaScript port of HtmlDiff.Net which is itself a C# port of HtmlDiff

Package Exports

  • @michaelray/html-diff
  • @michaelray/html-diff/dist/htmldiff.min.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@michaelray/html-diff) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

JavaScript port of HtmlDiff.NET which is itself a C# port of the Ruby implementation, HtmlDiff.

Project Description

Diffs two HTML blocks, and returns a meshing of the two that includes <ins> and <del> elements. The classes of these elements are ins.diffins for new code, del.diffdel for removed code, and del.diffmod and ins.diffmod for sections of code that have been changed.

For "special tags" (primarily style tags such as <em> and <strong>), ins.mod elements are inserted with the new styles.

Further description can be found at this blog post written by Rohland, the author of HtmlDiff.NET.

Note: The diffing algorithm isn't perfect. One example is that if a new <p> ends in the same string as the previous <p> tag did, two <ins> tags will be created: one starting at the beginning of the common string in the first <p> and one in the second <p> containing all the content up to the point the trailing common string begins. It's a little frustrating, but I didn't write the algorithm (and honestly don't really understand it); I only ported it.

Usage

Html

<html>
<body>
    <div id="oldHtml">
        <p>Some <em>old</em> html here</p>
    </div>

    <div id="newHtml">
        <p>Some <b>new</b> html goes here</p>
    </div>

    <div id="htmlDiff">
    </div>
</body>
</html>

JavaScript

import HtmlDiff from 'htmldiff-js';


let oldHtml = document.getById('oldHtml');
let newHtml = document.getById('newHtml');
let diffHtml = document.getById('diffHtml');

diffHtml.innerHTML = HtmlDiff.execute(oldHtml.innerHTML, newHtml.innerHTML);
ins.diff-ins, ins.diff-mod {
    background-color: #cfc;
    text-decoration: none;
}

del.diff-del, del.diff-mod {
    color: #999;
    background-color: #FEC8C8;
}