User:Cacycle/diff

News
(Newest first)
 * Version 0.9.3: Fixed moved block highlighting ending at first change; fixed occasional &amp;para;s. Cacycle 02:22, 28 April 2007 (UTC)
 * Version 0.9.2b: Fixes for IE and wikEdDiff compatibility. Cacycle 19:55, 15 April 2007 (UTC)
 * Version 0.9.2: Variables and functions have been renamed again, they are now preceded with "wDiff" and "WDiff", respectively. New filter function WDiffShortenOutput to remove unchanged regions from output. Cacycle 21:43, 13 April 2007 (UTC)
 * Version 0.9.1: Renaming variables and functions. For compatibility reasons they are now preceded with "cDiff" and "CDiff", respectively. Cacycle 00:03, 24 March 2007 (UTC)
 * Version 0.9.0: new version numbering, dead loop fix (thanks to Shamsian) Cacycle 21:43, 13 May 2006 (UTC)
 * SetupDiff is not needed anymore and has been removed.
 * The code should now work for all modern browsers (thanks to Shamsian).
 * Trivial changes are now handled without complex calculations. The html code and the styles of the indicators are now fully customizable. Cacycle 21:55, 27 March 2006 (UTC)


 * All function names have been changed for compatibility with Tools/Navigation popups. Cacycle 12:18, 8 February 2006 (UTC)

Code
The JavaScript code can be found under User:Cacycle/diff.js.

Supported browsers
The code should work for all modern browsers. The following browsers have been tested (please add your experiences to the list):


 * Mozilla Firefox 1.5.0.1, 1.5.0.7
 * Mozilla SeaMonkey 1.0, 1.1.1
 * Opera 8.53
 * Internet Explorer 6.0.2900.2180, 7.0.5730.11

Description
A JavaScript diff algorithm by User:Cacycle (en.wikipedia.org) that has been optimized for comparison of Wikipedia source texts. The output highlights additions (dark green) and deletions (dark red) in the new text. Additionally, moved text blocks and their original positions are highlighted and color-coded.

This script is used by Cacycle editor, its successor wikEd, and by wikEdDiff.

The code is an implementation and extension of the following basic algorithm:


 * Paul Heckel: A technique for isolating differences between files
 * Communications of the ACM 21(4):264 (1978)
 * http://doi.acm.org/10.1145/359460.359467 (access restricted)

Features

 * Cross-browser implementation, code should work for all modern browsers.
 * Additional post-pass 5 code for resolving islands caused by adding two common words at the end of sequences of common words
 * Additional detection of block borders and color coding of moved blocks and their original position
 * The block move detection is optimized for the shortest block being the moved one
 * The indicator at the original position points into the direction of the move
 * The code and the used datastructures are extensively commented
 * The used CSS and HTML elements are fully customizable
 * Optimization steps can be switched off

Usage
var htmlText = WDiffString(oldText, newText); var htmlTextShortened = WDiffShortenOutput(htmlText);

Customization
The following variables can be set in your monobook.js before loading the diff.js script:

CSS for change indicators:

var wDiffStyleDelete = 'font-weight: normal; text-decoration: none; color: #ffffff; background-color: #990033;'; var wDiffStyleInsert = 'font-weight: normal; text-decoration: none; color: #ffffff; background-color: #009933;'; var wDiffStyleMoved = 'font-weight: bold; vertical-align: text-bottom; font-size: xx-small; padding: 0; border: solid 1px;'; var wDiffStyleBlock = [ 'background-color: #ffff44;', 'background-color: #b0ff90;', 'background-color: #ffcc99;', 'background-color: #99ffff;', 'background-color: #99ccff;', 'background-color: #cc99ff;', 'background-color: #ff99cc;', 'background-color: #ffd040;', 'background-color: #d0d0d0;' ];

Html for change indicators, {number} is replaced by the block number, {block} is replaced by the block style:

var wDiffHtmlMovedRight = '" style="' + styleMoved + ' {block}">'; var wDiffHtmlMovedLeft  = '';

var wDiffHtmlBlockStart = ''; var wDiffHtmlBlockEnd   = ' ';

var wDiffHtmlDeleteStart = ''; var wDiffHtmlDeleteEnd  = ' ';

var wDiffHtmlInsertStart = ''; var wDiffHtmlInsertEnd  = ' ';

Minimal number of real words for a moved block (0 for always displaying block move indicators):

var wDiffBlockMinLength = 3;

Exclude identical sequence starts and endings from change marking:

var wDiffWordDiff = true;

Enable recursive diff to resolve problematic sequences:

var wDiffRecursiveDiff = true;

Enable block move display:

var wDiffShowBlockMoves = true;

Remove unchanged parts from final output.

Characters before diff tag to search for previous heading, paragraph, line break, cut characters:

var wDiffHeadingBefore  = 1500; var wDiffParagraphBefore = 1500; var wDiffLineBeforeMax  = 1000; var wDiffLineBeforeMin  = 500; var wDiffBlankBeforeMax = 1000; var wDiffBlankBeforeMin = 500; var wDiffCharsBefore    = 500;

Characters after diff tag to search for next heading, paragraph, line break, or characters:

var wDiffHeadingAfter  = 1500; var wDiffParagraphAfter = 1500; var wDiffLineAfterMax  = 1000; var wDiffLineAfterMin  = 500; var wDiffBlankAfterMax = 1000; var wDiffBlankAfterMin = 500; var wDiffCharsAfter    = 500;

Maximal fragment distance to join close fragments: var wDiffFragmentJoin = 1000; var wDiffOmittedChars = '…'; var wDiffOmittedLines = ''; var wDiffNoChange    = '';