<p>I'm interested in seeing a good diff algorithm, possibly in Javascript, for rendering a side-by-side diff of two HTML pages. The idea would be that the diff would show the differences of the <em>rendered</em> HTML.</p>

<p>To clarify, I want to be able to see the side-by-side diffs <em>as</em> rendered output. So if I delete a paragraph, the side by side view would know to space things correctly.</p>

<hr>

<p>@Josh exactly. Though maybe it would show the deleted text in red or something. The idea is that if I use a WYSIWYG editor for my HTML content, I don't want to have to switch to HTML to do diffs. I want to do it with two WYSIWYG editors side by side maybe. Or at least display diffs side-by-side in an end-user friendly matter.</p>

[{'answer_id': 31727, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>I believe a good way to do this is to <a href="http://weblogs.mozillazine.org/roc/archives/2005/05/rendering_web_p.html" rel="nofollow noreferrer">render the HTML to an image</a> and then use some <a href="http://www.scootersoftware.com/" rel="nofollow noreferrer">diff tool that can compare images</a> to spot the differences. </p>\n'}, {'answer_id': 31728, 'author': 'Josh', 'author_id': 257, 'author_profile': 'https://Stackoverflow.com/users/257', 'pm_score': 1, 'selected': False, 'text': '<p>So, you expect</p>\n\n<pre><code>&lt;font face="Arial"&gt;Hi Mom&lt;/font&gt;\n</code></pre>\n\n<p>and</p>\n\n<pre><code>&lt;span style="font-family:Arial;"&gt;Hi Mom&lt;/span&gt;\n</code></pre>\n\n<p>to be considered the same?</p>\n\n<p>The output depends very much on the User Agent.  Like <a href="https://stackoverflow.com/questions/31722#31727">Ionut Anghelcovici suggests</a>, make an image.  Do one for every browser you care about.</p>\n'}, {'answer_id': 33500, 'author': 'kamens', 'author_id': 1335, 'author_profile': 'https://Stackoverflow.com/users/1335', 'pm_score': 5, 'selected': True, 'text': '<p>There\'s another nice trick you can use to significantly improve the look of a rendered HTML diff.  Although this doesn\'t fully solve the initial problem, it will make a significant difference in the appearance of your rendered HTML diffs.</p>\n\n<p>Side-by-side rendered HTML will make it very difficult for your diff to line up vertically.  Vertical alignment is crucial for comparing side-by-side diffs.  In order to improve the vertical alignment of a side-by-side diff, you can insert invisible HTML elements in each version of the diff at "checkpoints" where the diff should be vertically aligned.  Then you can use a bit of client-side JavaScript to add vertical spacing around checkpoint until the sides line up vertically.</p>\n\n<p>Explained in a little more detail:</p>\n\n<p>If you want to use this technique, run your diff algorithm and insert a bunch of <code>visibility:hidden</code> <code>&lt;span&gt;</code>s or tiny <code>&lt;div&gt;</code>s wherever your side-by-side versions should match up, according to the diff.  Then run JavaScript that finds each checkpoint (and its side-by-side neighbor) and adds vertical spacing to the checkpoint that is higher-up (shallower) on the page.  Now your rendered HTML diff will be vertically aligned up to that checkpoint, and you can continue repairing vertical alignment down the rest of your side-by-side page.</p>\n'}, {'answer_id': 66065, 'author': 'MotoWilliams', 'author_id': 2730, 'author_profile': 'https://Stackoverflow.com/users/2730', 'pm_score': 0, 'selected': False, 'text': '<p>If it is XHTML (which assumes a lot on my part) would the Xml Diff Patch Toolkit help?  <a href="http://msdn.microsoft.com/en-us/library/aa302294.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa302294.aspx</a></p>\n'}, {'answer_id': 66147, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 0, 'selected': False, 'text': "<p>For smaller differences you might be able to do a normal text-diff, and then analyse the missing or inserted pieces to see how to resolve it, but for any larger differences you're going to have a very tough time doing this.</p>\n\n<p>For instance, how would you detect, and show, that a left-aligned image (floating left of a paragraph of text) has suddenly become right-aligned?</p>\n"}, {'answer_id': 66539, 'author': 'kooshmoose', 'author_id': 7436, 'author_profile': 'https://Stackoverflow.com/users/7436', 'pm_score': 2, 'selected': False, 'text': '<p>I ended up needing something similar awhile back. To get the HTML to line up side to side, you could use two iFrames, but you\'d then have to tie their scrolling together via javascript as you scroll (if you allow scrolling).</p>\n\n<p>To see the diff, however, you will more than likely want to use someone else\'s library. I used <a href="http://code.google.com/p/daisydiff/" rel="nofollow noreferrer">DaisyDiff</a>, a Java library, for a similar project where my client was happy with seeing a single HTML rendering of the content with MS Word "track changes"-like markup.</p>\n\n<p>HTH</p>\n'}, {'answer_id': 172822, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Using a text differ will break on non-trivial documents.\nDepending on what you think is intuitive, XML differs will probably generate diffs that aren\'t very good for text with markup.\nAFAIK, <a href="http://code.google.com/p/daisydiff/" rel="nofollow noreferrer">DaisyDiff</a> is the only library specialized in HTML. It works great for a subset of HTML.</p>\n'}, {'answer_id': 172866, 'author': 'Ates Goral', 'author_id': 23501, 'author_profile': 'https://Stackoverflow.com/users/23501', 'pm_score': 0, 'selected': False, 'text': '<p>If you were working with Java and XHTML, <a href="http://xmlunit.sourceforge.net/" rel="nofollow noreferrer">XMLUnit</a> allows you to compare two XML documents via the <a href="http://xmlunit.sourceforge.net/api/org/custommonkey/xmlunit/DetailedDiff.html" rel="nofollow noreferrer">org.custommonkey.xmlunit.DetailedDiff</a> class:</p>\n\n<blockquote>\n  <p>Compares and describes all the\n  differences between two XML documents.\n  The document comparison does not stop\n  once the first unrecoverable\n  difference is found, unlike the Diff\n  class.</p>\n</blockquote>\n'}, {'answer_id': 437297, 'author': 'Arafangion', 'author_id': 52273, 'author_profile': 'https://Stackoverflow.com/users/52273', 'pm_score': 2, 'selected': False, 'text': '<p>Consider using the output of links or lynx to render a text-only version of the html, and then diff that.</p>\n'}, {'answer_id': 1407133, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Use the markup mode of Pretty Diff for HTML.  It is written entirely in JavaScript.</p>\n\n<p><a href="http://prettydiff.com/" rel="nofollow noreferrer">http://prettydiff.com/</a></p>\n'}, {'answer_id': 1593296, 'author': 'elhoim', 'author_id': 171469, 'author_profile': 'https://Stackoverflow.com/users/171469', 'pm_score': 2, 'selected': False, 'text': '<p>What about <a href="http://code.google.com/p/daisydiff/" rel="nofollow noreferrer">DaisyDiff</a> (<a href="http://code.google.com/p/daisydiff/" rel="nofollow noreferrer">Java</a> and <a href="http://www.mediawiki.org/wiki/Visual_Diff" rel="nofollow noreferrer">PHP</a> vesions available).</p>\n\n<p>Following features are really nice:</p>\n\n<ul>\n<li>Works with badly formed HTML that can be found "in the wild".</li>\n<li>The diffing is more specialized in HTML than XML tree differs. Changing part of a text node will not cause the entire node to be changed.</li>\n<li>In addition to the default visual diff, HTML source can be diffed coherently.</li>\n<li>Provides easy to understand descriptions of the changes.</li>\n<li>The default GUI allows easy browsing of the modifications through keyboard shortcuts and links. </li>\n</ul>\n'}, {'answer_id': 1660448, 'author': 'Rohland', 'author_id': 87181, 'author_profile': 'https://Stackoverflow.com/users/87181', 'pm_score': 4, 'selected': False, 'text': '<p>Over the weekend I posted a new project on codeplex that implements an HTML diff algorithm in C#. The original algorithm was written in Ruby. I understand you were looking for a JavaScript implementation, perhaps having one available in C# with source code could assist you to port the algorithm. Here is the link if you are interested: <a href="http://htmldiff.codeplex.com" rel="noreferrer">htmldiff.codeplex.com</a>. You can read more about it <a href="http://www.rohland.co.za/index.php/2009/10/31/csharp-html-diff-algorithm/" rel="noreferrer">here</a>.</p>\n\n<p><strong>UPDATE:</strong> This library has been moved to <a href="https://github.com/Rohland/htmldiff.net" rel="noreferrer">GitHub</a>.</p>\n'}]
['https://Stackoverflow.com/questions/31722', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/598/']