User:Cacycle/diff.js: Difference between revisions

Content deleted Content added
1.0.22 (September 15, 2014) fix newline and space markup
another background that could use some darkmode friendly color
 
(20 intermediate revisions by 3 users not shown)
Line 2:
 
// ==UserScript==
// @name wDiffwikEd diff
// @version 1.02.224
// @date SeptemberOctober 1523, 2014
// @description improved word-based diff library with block move detection
// @homepage https://en.wikipedia.org/wiki/User:Cacycle/diff
Line 12:
// ==/UserScript==
 
/**
* wikEd diff: inline-style difference engine with block move support
*
* Improved JavaScript diff library that returns html/css-formatted new text version with
* highlighted deletions, insertions, and block moves. It is compatible with all browsers and is
* not dependent on external libraries.
*
* WikEdDiff.php and the JavaScript library wikEd diff are synced one-to-one ports. Changes and
* fixes are to be applied to both versions.
*
* JavaScript library (mirror): https://en.wikipedia.org/wiki/User:Cacycle/diff
* JavaScript online tool: http://cacycle.altervista.org/wikEd-diff-tool.html
* MediaWiki extension: https://www.mediawiki.org/wiki/Extension:wikEdDiff
*
* This difference engine applies a word-based algorithm that uses unique words as anchor points
* to identify matching text and moved blocks (Paul Heckel: A technique for isolating differences
* between files. Communications of the ACM 21(4):264 (1978)).
*
* Additional features:
*
* - Visual inline style, changes are shown in a single output text
* - Block move detection and highlighting
* - Resolution down to characters level
* - Unicode and multilingual support
* - Stepwise split (paragraphs, lines, sentences, words, characters)
* - Recursive diff
* - Optimized code for resolving unmatched sequences
* - Minimization of length of moved blocks
* - Alignment of ambiguous unmatched sequences to next line break or word border
* - Clipping of unchanged irrelevant parts from the output (optional)
* - Fully customizable
* - Text split optimized for MediaWiki source texts
* - Well commented and documented code
*
* Datastructures (abbreviations from publication):
*
* class WikEdDiffText: diff text object (new or old version)
* .text text of version
* .words[] word count table
* .first index of first token in tokens list
* .last index of last token in tokens list
*
* .tokens[]: token list for new or old string (doubly-linked list) (N and O)
* .prev previous list item
* .next next list item
* .token token string
* .link index of corresponding token in new or old text (OA and NA)
* .number list enumeration number
* .unique token is unique word in text
*
* class WikEdDiff: diff object
* .config[]: configuration settings, see top of code for customization options
* .regExp[]: all regular expressions
* .split regular expressions used for splitting text into tokens
* .htmlCode HTML code fragments used for creating the output
* .msg output messages
* .newText new text
* .oldText old text
* .maxWords word count of longest linked block
* .html diff html
* .error flag: result has not passed unit tests
* .bordersDown[] linked region borders downwards, [new index, old index]
* .bordersUp[] linked region borders upwards, [new index, old index]
* .symbols: symbols table for whole text at all refinement levels
* .token[] hash table of parsed tokens for passes 1 - 3, points to symbol[i]
* .symbol[]: array of objects that hold token counters and pointers:
* .newCount new text token counter (NC)
* .oldCount old text token counter (OC)
* .newToken token index in text.newText.tokens
* .oldToken token index in text.oldText.tokens
* .linked flag: at least one unique token pair has been linked
*
* .blocks[]: array, block data (consecutive text tokens) in new text order
* .oldBlock number of block in old text order
* .newBlock number of block in new text order
* .oldNumber old text token number of first token
* .newNumber new text token number of first token
* .oldStart old text token index of first token
* .count number of tokens
* .unique contains unique linked token
* .words word count
* .chars char length
* .type '=', '-', '+', '|' (same, deletion, insertion, mark)
* .section section number
* .group group number of block
* .fixed belongs to a fixed (not moved) group
* .moved moved block group number corresponding with mark block
* .text text of block tokens
*
* .sections[]: array, block sections with no block move crosses outside a section
* .blockStart first block in section
* .blockEnd last block in section
 
* .groups[]: array, section blocks that are consecutive in old text order
Improved JavaScript diff library that returns html/css-formatted new text version with highlighted deletions, inserts, and block moves.
* .oldNumber first block oldNumber
It is compatible with all browsers and is not dependent on external libraries.
* .blockStart first block index
An implementation of the word-based algorithm from:
* .blockEnd last block index
* .unique contains unique linked token
* .maxWords word count of longest linked block
* .words word count
* .chars char count
* .fixed not moved from original position
* .movedFrom group position this group has been moved from
* .color color number of moved group
*
* .fragments[]: diff fragment list ready for markup, abstraction layer for customization
* .text block or mark text
* .color moved block or mark color number
* .type '=', '-', '+' same, deletion, insertion
* '<', '>' mark left, mark right
* '(<', '(>', ')' block start and end
* '~', ' ~', '~ ' omission indicators
* '[', ']', ',' fragment start and end, fragment separator
* '{', '}' container start and end
*
*/
 
// JSHint options
Communications of the ACM 21(4):264 (1978)
/* jshint -W004, -W100, newcap: true, browser: true, jquery: true, sub: true, bitwise: true,
http://doi.acm.org/10.1145/359460.359467
curly: true, evil: true, forin: true, freeze: true, globalstrict: true, immed: true,
latedef: true, loopfunc: true, quotmark: single, strict: true, undef: true */
/* global console */
 
// Turn on ECMAScript 5 strict mode
Additional features:
'use strict';
 
/** Define global objects. */
* Word (token) types have been optimized for MediaWiki source texts
var wikEdDiffConfig;
* Resolution down to characters level
var WED;
* Highlighting of moved blocks and their original position marks
* Stepwise split (paragraphs, sentences, words, chars)
* Recursive diff
* Additional post-pass-5 code for resolving islands caused by common tokens at the border of sequences of common tokens
* Block move detection and visualization
* Minimizing length of moved vs. static blocks
* Sliding of ambiguous unresolved regions to next line break
* Optional omission of unchanged irrelevant parts from the output
* Fully customizable
* Well commented and documented code
 
This code is used by the MediaWiki in-browser text editors [[en:User:Cacycle/editor]] and [[en:User:Cacycle/wikEd]]
and the enhanced diff view tool wikEdDiff [[en:User:Cacycle/wikEd]].
 
/**
Usage:
* wikEd diff main class.
var diffHtml = wDiff.Diff(oldString, newString);
*
diffHtml = wDiff.ShortenOutput(diffHtml);
* @class WikEdDiff
*/
var WikEdDiff = function () {
 
/** @var array config Configuration and customization settings. */
Datastructures (abbreviations from publication):
this.config = {
 
/** Core diff settings (with default values). */
text: objects for text related data
.newText, new text
.oldText: old text
.string: new or old text to be diffed
.tokens[]: token data list for new or old string (N and O)
.prev: previous list item
.next: next list item
.token: token string
.link: index of corresponding token in new or old text (OA and NA)
.number: list enumeration number
.parsed: token has been added to symbol table
.unique: token is unique word in text
.first: index of first token in tokens list
.last: index of last token in tokens list
.words{}: word count
.diff: diff html
 
/**
symbols: object for symbols table data
* @var bool config.fullDiff
.token[]: associative array (hash) of parsed tokens for passes 1 - 3, points to symbol[i]
* Show complete un-clipped diff text (false)
.symbol[]: array of objects that hold token counters and pointers:
*/
.newCount: new text token counter (NC)
'fullDiff': false,
.oldCount: old text token counter (OC)
.newToken: token index in text.newText.tokens
.oldToken: token index in text.oldText.tokens
.linked: flag: at least one unique token pair has been linked
 
/**
* @var bool config.showBlockMoves
* Enable block move layout with highlighted blocks and marks at the original positions (true)
*/
'showBlockMoves': true,
 
/**
blocks[]: array of objects that holds block (consecutive text tokens) data in order of the new text
* @var bool config.charDiff
.oldBlock: number of block in old text order
* Enable character-refined diff (true)
.newBlock: number of block in new text order
*/
.oldNumber: old text token number of first token
'charDiff': true,
.newNumber: new text token number of first token
.oldStart: old text token index of first token
.count number of tokens
.unique: contains unique matched token
.words: word count
.chars: char length
.type: 'same', 'del', 'ins'
.section: section number
.group: group number of block
.fixed: belongs to a fixed (not moved) group
.string: string of block tokens
 
/**
groups[]: section blocks that are consecutive in old text
* @var bool config.repeatedDiff
.oldNumber: first block oldNumber
* Enable repeated diff to resolve problematic sequences (true)
.blockStart: first block index
*/
.blockEnd: last block index
'repeatedDiff': true,
.unique: contains unique matched token
.maxWords: word count of longest block
.words: word count
.chars: char count
.fixed: not moved from original position
.moved[]: list of groups that have been moved from this position
.movedFrom: position this group has been moved from
.color: color number of moved group
.diff: group diff
 
/**
*/
* @var bool config.recursiveDiff
* Enable recursive diff to resolve problematic sequences (true)
*/
'recursiveDiff': true,
 
/**
// JSHint options: W004: is already defined, W097: Use the function form of "use strict", W100: This character may get silently deleted by one or more browsers
* @var int config.recursionMax
/* jshint -W004, -W097, -W100, newcap: false, browser: true, jquery: true, sub: true, bitwise: true, curly: true, evil: true, forin: true, freeze: true, immed: true, latedef: true, loopfunc: true, quotmark: single, undef: true */
* Maximum recursion depth (10)
/* global console */
*/
'recursionMax': 10,
 
/**
// turn on ECMAScript 5 strict mode
* @var bool config.unlinkBlocks
'use strict';
* Reject blocks if they are too short and their words are not unique,
* prevents fragmentated diffs for very different versions (true)
*/
'unlinkBlocks': true,
 
/**
// define global object
* @var int config.unlinkMax
var wDiff; if (wDiff === undefined) { wDiff = {}; }
* Maximum number of rejection cycles (5)
var WED;
*/
'unlinkMax': 5,
 
/**
//
* @var int config.blockMinLength
// core diff settings
* Reject blocks if shorter than this number of real words (3)
//
*/
'blockMinLength': 3,
 
/**
// enable block move layout with highlighted blocks and marks at their original positions
* @var bool config.coloredBlocks
if (wDiff.showBlockMoves === undefined) { wDiff.showBlockMoves = true; }
* Display blocks in differing colors (rainbow color scheme) (false)
*/
'coloredBlocks': false,
 
/**
// minimal number of real words for a moved block (0 for always showing highlighted blocks)
* @var bool config.coloredBlocks
if (wDiff.blockMinLength === undefined) { wDiff.blockMinLength = 3; }
* Do not use UniCode block move marks (legacy browsers) (false)
*/
'noUnicodeSymbols': false,
 
/**
// further resolve replacements character-wise from start and end
* @var bool config.stripTrailingNewline
if (wDiff.charDiff === undefined) { wDiff.charDiff = true; }
* Strip trailing newline off of texts (true in .js, false in .php)
*/
'stripTrailingNewline': true,
 
/**
// enable recursive diff to resolve problematic sequences
* @var bool config.debug
if (wDiff.recursiveDiff === undefined) { wDiff.recursiveDiff = true; }
* Show debug infos and stats (block, group, and fragment data) in debug console (false)
*/
'debug': false,
 
/**
// display blocks in different colors
* @var bool config.timer
if (wDiff.coloredBlocks === undefined) { wDiff.coloredBlocks = false; }
* Show timing results in debug console (false)
*/
'timer': false,
 
/**
// UniCode letter support for regexps, from http://xregexp.com/addons/unicode/unicode-base.js v1.0.0
* @var bool config.unitTesting
if (wDiff.letters === undefined) { wDiff.letters = 'a-zA-Z0-9' + '00AA00B500BA00C0-00D600D8-00F600F8-02C102C6-02D102E0-02E402EC02EE0370-037403760377037A-037D03860388-038A038C038E-03A103A3-03F503F7-0481048A-05270531-055605590561-058705D0-05EA05F0-05F20620-064A066E066F0671-06D306D506E506E606EE06EF06FA-06FC06FF07100712-072F074D-07A507B107CA-07EA07F407F507FA0800-0815081A082408280840-085808A008A2-08AC0904-0939093D09500958-09610971-09770979-097F0985-098C098F09900993-09A809AA-09B009B209B6-09B909BD09CE09DC09DD09DF-09E109F009F10A05-0A0A0A0F0A100A13-0A280A2A-0A300A320A330A350A360A380A390A59-0A5C0A5E0A72-0A740A85-0A8D0A8F-0A910A93-0AA80AAA-0AB00AB20AB30AB5-0AB90ABD0AD00AE00AE10B05-0B0C0B0F0B100B13-0B280B2A-0B300B320B330B35-0B390B3D0B5C0B5D0B5F-0B610B710B830B85-0B8A0B8E-0B900B92-0B950B990B9A0B9C0B9E0B9F0BA30BA40BA8-0BAA0BAE-0BB90BD00C05-0C0C0C0E-0C100C12-0C280C2A-0C330C35-0C390C3D0C580C590C600C610C85-0C8C0C8E-0C900C92-0CA80CAA-0CB30CB5-0CB90CBD0CDE0CE00CE10CF10CF20D05-0D0C0D0E-0D100D12-0D3A0D3D0D4E0D600D610D7A-0D7F0D85-0D960D9A-0DB10DB3-0DBB0DBD0DC0-0DC60E01-0E300E320E330E40-0E460E810E820E840E870E880E8A0E8D0E94-0E970E99-0E9F0EA1-0EA30EA50EA70EAA0EAB0EAD-0EB00EB20EB30EBD0EC0-0EC40EC60EDC-0EDF0F000F40-0F470F49-0F6C0F88-0F8C1000-102A103F1050-1055105A-105D106110651066106E-10701075-1081108E10A0-10C510C710CD10D0-10FA10FC-1248124A-124D1250-12561258125A-125D1260-1288128A-128D1290-12B012B2-12B512B8-12BE12C012C2-12C512C8-12D612D8-13101312-13151318-135A1380-138F13A0-13F41401-166C166F-167F1681-169A16A0-16EA1700-170C170E-17111720-17311740-17511760-176C176E-17701780-17B317D717DC1820-18771880-18A818AA18B0-18F51900-191C1950-196D1970-19741980-19AB19C1-19C71A00-1A161A20-1A541AA71B05-1B331B45-1B4B1B83-1BA01BAE1BAF1BBA-1BE51C00-1C231C4D-1C4F1C5A-1C7D1CE9-1CEC1CEE-1CF11CF51CF61D00-1DBF1E00-1F151F18-1F1D1F20-1F451F48-1F4D1F50-1F571F591F5B1F5D1F5F-1F7D1F80-1FB41FB6-1FBC1FBE1FC2-1FC41FC6-1FCC1FD0-1FD31FD6-1FDB1FE0-1FEC1FF2-1FF41FF6-1FFC2071207F2090-209C21022107210A-211321152119-211D212421262128212A-212D212F-2139213C-213F2145-2149214E218321842C00-2C2E2C30-2C5E2C60-2CE42CEB-2CEE2CF22CF32D00-2D252D272D2D2D30-2D672D6F2D80-2D962DA0-2DA62DA8-2DAE2DB0-2DB62DB8-2DBE2DC0-2DC62DC8-2DCE2DD0-2DD62DD8-2DDE2E2F300530063031-3035303B303C3041-3096309D-309F30A1-30FA30FC-30FF3105-312D3131-318E31A0-31BA31F0-31FF3400-4DB54E00-9FCCA000-A48CA4D0-A4FDA500-A60CA610-A61FA62AA62BA640-A66EA67F-A697A6A0-A6E5A717-A71FA722-A788A78B-A78EA790-A793A7A0-A7AAA7F8-A801A803-A805A807-A80AA80C-A822A840-A873A882-A8B3A8F2-A8F7A8FBA90A-A925A930-A946A960-A97CA984-A9B2A9CFAA00-AA28AA40-AA42AA44-AA4BAA60-AA76AA7AAA80-AAAFAAB1AAB5AAB6AAB9-AABDAAC0AAC2AADB-AADDAAE0-AAEAAAF2-AAF4AB01-AB06AB09-AB0EAB11-AB16AB20-AB26AB28-AB2EABC0-ABE2AC00-D7A3D7B0-D7C6D7CB-D7FBF900-FA6DFA70-FAD9FB00-FB06FB13-FB17FB1DFB1F-FB28FB2A-FB36FB38-FB3CFB3EFB40FB41FB43FB44FB46-FBB1FBD3-FD3DFD50-FD8FFD92-FDC7FDF0-FDFBFE70-FE74FE76-FEFCFF21-FF3AFF41-FF5AFF66-FFBEFFC2-FFC7FFCA-FFCFFFD2-FFD7FFDA-FFDC'.replace(/(\w{4})/g, '\\u$1'); }
* Run unit tests to prove correct working, display results in debug console (false)
*/
'unitTesting': false,
 
/** RegExp character classes. */
// new line characters without and with '\n' and '\r'
if (wDiff.newLines === undefined) { wDiff.newLines = '\\u0085\\u2028'; }
if (wDiff.newLinesAll === undefined) { wDiff.newLinesAll = '\\n\\r\\u0085\\u2028'; }
 
// UniCode letter support for regexps
// full stops without '.'
// From http://xregexp.com/addons/unicode/unicode-base.js v1.0.0
if (wDiff.fullStops === undefined) { wDiff.fullStops = '058906D40701070209640DF41362166E180318092CF92CFE2E3C3002A4FFA60EA6F3FE52FF0EFF61'.replace(/(\w{4})/g, '\\u$1'); }
'regExpLetters':
'a-zA-Z0-9' + (
'00AA00B500BA00C0-00D600D8-00F600F8-02C102C6-02D102E0-02E402EC02EE0370-037403760377037A-' +
'037D03860388-038A038C038E-03A103A3-03F503F7-0481048A-05270531-055605590561-058705D0-05EA' +
'05F0-05F20620-064A066E066F0671-06D306D506E506E606EE06EF06FA-06FC06FF07100712-072F074D-' +
'07A507B107CA-07EA07F407F507FA0800-0815081A082408280840-085808A008A2-08AC0904-0939093D' +
'09500958-09610971-09770979-097F0985-098C098F09900993-09A809AA-09B009B209B6-09B909BD09CE' +
'09DC09DD09DF-09E109F009F10A05-0A0A0A0F0A100A13-0A280A2A-0A300A320A330A350A360A380A39' +
'0A59-0A5C0A5E0A72-0A740A85-0A8D0A8F-0A910A93-0AA80AAA-0AB00AB20AB30AB5-0AB90ABD0AD00AE0' +
'0AE10B05-0B0C0B0F0B100B13-0B280B2A-0B300B320B330B35-0B390B3D0B5C0B5D0B5F-0B610B710B83' +
'0B85-0B8A0B8E-0B900B92-0B950B990B9A0B9C0B9E0B9F0BA30BA40BA8-0BAA0BAE-0BB90BD00C05-0C0C' +
'0C0E-0C100C12-0C280C2A-0C330C35-0C390C3D0C580C590C600C610C85-0C8C0C8E-0C900C92-0CA80CAA-' +
'0CB30CB5-0CB90CBD0CDE0CE00CE10CF10CF20D05-0D0C0D0E-0D100D12-0D3A0D3D0D4E0D600D610D7A-' +
'0D7F0D85-0D960D9A-0DB10DB3-0DBB0DBD0DC0-0DC60E01-0E300E320E330E40-0E460E810E820E840E87' +
'0E880E8A0E8D0E94-0E970E99-0E9F0EA1-0EA30EA50EA70EAA0EAB0EAD-0EB00EB20EB30EBD0EC0-0EC4' +
'0EC60EDC-0EDF0F000F40-0F470F49-0F6C0F88-0F8C1000-102A103F1050-1055105A-105D106110651066' +
'106E-10701075-1081108E10A0-10C510C710CD10D0-10FA10FC-1248124A-124D1250-12561258125A-125D' +
'1260-1288128A-128D1290-12B012B2-12B512B8-12BE12C012C2-12C512C8-12D612D8-13101312-1315' +
'1318-135A1380-138F13A0-13F41401-166C166F-167F1681-169A16A0-16EA1700-170C170E-17111720-' +
'17311740-17511760-176C176E-17701780-17B317D717DC1820-18771880-18A818AA18B0-18F51900-191C' +
'1950-196D1970-19741980-19AB19C1-19C71A00-1A161A20-1A541AA71B05-1B331B45-1B4B1B83-1BA0' +
'1BAE1BAF1BBA-1BE51C00-1C231C4D-1C4F1C5A-1C7D1CE9-1CEC1CEE-1CF11CF51CF61D00-1DBF1E00-1F15' +
'1F18-1F1D1F20-1F451F48-1F4D1F50-1F571F591F5B1F5D1F5F-1F7D1F80-1FB41FB6-1FBC1FBE1FC2-1FC4' +
'1FC6-1FCC1FD0-1FD31FD6-1FDB1FE0-1FEC1FF2-1FF41FF6-1FFC2071207F2090-209C21022107210A-2113' +
'21152119-211D212421262128212A-212D212F-2139213C-213F2145-2149214E218321842C00-2C2E2C30-' +
'2C5E2C60-2CE42CEB-2CEE2CF22CF32D00-2D252D272D2D2D30-2D672D6F2D80-2D962DA0-2DA62DA8-2DAE' +
'2DB0-2DB62DB8-2DBE2DC0-2DC62DC8-2DCE2DD0-2DD62DD8-2DDE2E2F300530063031-3035303B303C3041-' +
'3096309D-309F30A1-30FA30FC-30FF3105-312D3131-318E31A0-31BA31F0-31FF3400-4DB54E00-9FCC' +
'A000-A48CA4D0-A4FDA500-A60CA610-A61FA62AA62BA640-A66EA67F-A697A6A0-A6E5A717-A71FA722-' +
'A788A78B-A78EA790-A793A7A0-A7AAA7F8-A801A803-A805A807-A80AA80C-A822A840-A873A882-A8B3' +
'A8F2-A8F7A8FBA90A-A925A930-A946A960-A97CA984-A9B2A9CFAA00-AA28AA40-AA42AA44-AA4BAA60-' +
'AA76AA7AAA80-AAAFAAB1AAB5AAB6AAB9-AABDAAC0AAC2AADB-AADDAAE0-AAEAAAF2-AAF4AB01-AB06AB09-' +
'AB0EAB11-AB16AB20-AB26AB28-AB2EABC0-ABE2AC00-D7A3D7B0-D7C6D7CB-D7FBF900-FA6DFA70-FAD9' +
'FB00-FB06FB13-FB17FB1DFB1F-FB28FB2A-FB36FB38-FB3CFB3EFB40FB41FB43FB44FB46-FBB1FBD3-FD3D' +
'FD50-FD8FFD92-FDC7FDF0-FDFBFE70-FE74FE76-FEFCFF21-FF3AFF41-FF5AFF66-FFBEFFC2-FFC7FFCA-' +
'FFCFFFD2-FFD7FFDA-FFDC'
).replace( /(\w{4})/g, '\\u$1' ),
 
// newNew paragraphline characters without 'and with \n' and '\r'
'regExpNewLines': '\\u0085\\u2028',
if (wDiff.newParagraph === undefined) { wDiff.newParagraph = '\\u2029'; }
'regExpNewLinesAll': '\\n\\r\\u0085\\u2028',
 
// exclamationBreaking markswhite space characters without '!'\n, \r, and \f
'regExpBlanks': ' \\t\\x0b\\u2000-\\u200b\\u202f\\u205f\\u3000',
if (wDiff.exclamationMarks === undefined) { wDiff.exclamationMarks = '01C301C301C3055C055C07F919441944203C203C20482048FE15FE57FF01'.replace(/(\w{4})/g, '\\u$1'); }
 
// questionFull marksstops without '?.'
'regExpFullStops':
if (wDiff.questionMarks === undefined) { wDiff.questionMarks = '037E055E061F13671945204720492CFA2CFB2E2EA60FA6F7FE56FF1F'.replace(/(\w{4})/g, '\\u$1') + '\\u11143'; }
'\\u0589\\u06D4\\u0701\\u0702\\u0964\\u0DF4\\u1362\\u166E\\u1803\\u1809' +
'\\u2CF9\\u2CFE\\u2E3C\\u3002\\uA4FF\\uA60E\\uA6F3\\uFE52\\uFF0E\\uFF61',
 
// New paragraph characters without \n and \r
// regExps for splitting text (included separators)
'regExpNewParagraph': '\\f\\u2029',
if (wDiff.regExpSplit === undefined) {
wDiff.regExpSplit = {
 
// paragraphs:Exclamation aftermarks doublewithout newlines'!'
'regExpExclamationMarks':
paragraph: new RegExp('(.|\\n)*?((\\r\\n|\\n|\\r){2,}|[' + wDiff.newParagraph + '])+', 'g'),
'\\u01C3\\u01C3\\u01C3\\u055C\\u055C\\u07F9\\u1944\\u1944' +
'\\u203C\\u203C\\u2048\\u2048\\uFE15\\uFE57\\uFF01',
 
// Question marks without '?'
// sentences: after newlines and .spaces
'regExpQuestionMarks':
sentence: new RegExp('[^' + wDiff.newLinesAll + ']*?([.!?;]+[^\\S' + wDiff.newLinesAll + ']+|[' + wDiff.fullStops + wDiff.exclamationMarks + wDiff.questionMarks + ']+[^\\S' + wDiff.newLinesAll + ']*|[' + wDiff.newLines + ']|\\r\\n|\\n|\\r)', 'g'),
'\\u037E\\u055E\\u061F\\u1367\\u1945\\u2047\\u2049' +
'\\u2CFA\\u2CFB\\u2E2E\\uA60F\\uA6F7\\uFE56\\uFF1F',
 
/** Clip settings. */
// inline chunks
// [[wiki link]] | {{template}} | [ext. link] |<html> | [[wiki link| | {{template| | url
chunk: /\[\[[^\[\]\n]+\]\]|\{\{[^\{\}\n]+\}\}|\[[^\[\]\n]+\]|<\/?[^<>\[\]\{\}\n]+>|\[\[[^\[\]\|\n]+\]\]\||\{\{[^\{\}\|\n]+\||\b((https?:|)\/\/)[^\x00-\x20\s"\[\]\x7f]+/g,
 
// Find clip position: characters from right
// words, multi-char markup, and chars
'clipHeadingLeft': 1500,
word: new RegExp('[' + wDiff.letters + ']+([\'’_]?[' + wDiff.letters + ']+)*|\\[\\[|\\]\\]|\\{\\{|\\}\\}|&\\w+;|\'\'\'|\'\'|==+|\\{\\||\\|\\}|\\|-|.', 'g'),
'clipParagraphLeftMax': 1500,
'clipParagraphLeftMin': 500,
'clipLineLeftMax': 1000,
'clipLineLeftMin': 500,
'clipBlankLeftMax': 1000,
'clipBlankLeftMin': 500,
'clipCharsLeft': 500,
 
// Find clip position: characters from right
// chars
'clipHeadingRight': 1500,
character: /./g
'clipParagraphRightMax': 1500,
};
'clipParagraphRightMin': 500,
}
'clipLineRightMax': 1000,
'clipLineRightMin': 500,
'clipBlankRightMax': 1000,
'clipBlankRightMin': 500,
'clipCharsRight': 500,
 
// Maximum number of lines to search for clip position
// regExps for sliding gaps
'clipLinesRightMax': 10,
if (wDiff.regExpSlideStop === undefined) { wDiff.regExpSlideStop = new RegExp('[\\n\\r' + wDiff.newLines + ']$'); }
'clipLinesLeftMax': 10,
if (wDiff.regExpSlideBorder === undefined) { wDiff.regExpSlideBorder = new RegExp('[^' + wDiff.letters + ']$'); }
 
// Skip clipping if ranges are too close
// regExp for counting words
'clipSkipLines': 5,
if (wDiff.regExpWordCount === undefined) { wDiff.regExpWordCount = new RegExp('[' + wDiff.letters + ']+([\'’_]?[' + wDiff.letters + ']+)*', 'g'); }
'clipSkipChars': 1000,
 
// Css stylesheet
// regExp detecting blank-only and single-char blocks
'cssMarkLeft': '◀',
if (wDiff.regExpBlankBlock === undefined) { wDiff.regExpBlankBlock = /^([^\t\S]+|[^\t])$/; }
'cssMarkRight': '▶',
'stylesheet':
 
// Insert
//
'.wikEdDiffInsert {' +
// shorten output settings
'font-weight: bold; background-color: #bbddff; ' +
//
'color: #222; border-radius: 0.25em; padding: 0.2em 1px; ' +
'} ' +
'.wikEdDiffInsertBlank { background-color: #66bbff; } ' +
'.wikEdDiffFragment:hover .wikEdDiffInsertBlank { background-color: #bbddff; } ' +
 
// Delete
// characters before diff tag to search for previous heading, paragraph, line break, cut characters
'.wikEdDiffDelete {' +
if (wDiff.headingBefore === undefined) { wDiff.headingBefore = 1500; }
'font-weight: bold; background-color: #ffe49c; ' +
if (wDiff.paragraphBeforeMax === undefined) { wDiff.paragraphBeforeMax = 1500; }
'color: #222; border-radius: 0.25em; padding: 0.2em 1px; ' +
if (wDiff.paragraphBeforeMin === undefined) { wDiff.paragraphBeforeMin = 500; }
'} ' +
if (wDiff.lineBeforeMax === undefined) { wDiff.lineBeforeMax = 1000; }
'.wikEdDiffDeleteBlank { background-color: #ffd064; } ' +
if (wDiff.lineBeforeMin === undefined) { wDiff.lineBeforeMin = 500; }
'.wikEdDiffFragment:hover .wikEdDiffDeleteBlank { background-color: #ffe49c; } ' +
if (wDiff.blankBeforeMax === undefined) { wDiff.blankBeforeMax = 1000; }
if (wDiff.blankBeforeMin === undefined) { wDiff.blankBeforeMin = 500; }
if (wDiff.charsBefore === undefined) { wDiff.charsBefore = 500; }
 
// Block
// characters after diff tag to search for next heading, paragraph, line break, or characters
'.wikEdDiffBlock {' +
if (wDiff.headingAfter === undefined) { wDiff.headingAfter = 1500; }
'font-weight: bold; background-color: #e8e8e8; ' +
if (wDiff.paragraphAfterMax === undefined) { wDiff.paragraphAfterMax = 1500; }
'border-radius: 0.25em; padding: 0.2em 1px; margin: 0 1px; ' +
if (wDiff.paragraphAfterMin === undefined) { wDiff.paragraphAfterMin = 500; }
'} ' +
if (wDiff.lineAfterMax === undefined) { wDiff.lineAfterMax = 1000; }
'.wikEdDiffBlock { color: #000; } ' +
if (wDiff.lineAfterMin === undefined) { wDiff.lineAfterMin = 500; }
'.wikEdDiffBlock0 { background-color: #ffff80; } ' +
if (wDiff.blankAfterMax === undefined) { wDiff.blankAfterMax = 1000; }
'.wikEdDiffBlock1 { background-color: #d0ff80; } ' +
if (wDiff.blankAfterMin === undefined) { wDiff.blankAfterMin = 500; }
'.wikEdDiffBlock2 { background-color: #ffd8f0; } ' +
if (wDiff.charsAfter === undefined) { wDiff.charsAfter = 500; }
'.wikEdDiffBlock3 { background-color: #c0ffff; } ' +
'.wikEdDiffBlock4 { background-color: #fff888; } ' +
'.wikEdDiffBlock5 { background-color: #bbccff; } ' +
'.wikEdDiffBlock6 { background-color: #e8c8ff; } ' +
'.wikEdDiffBlock7 { background-color: #ffbbbb; } ' +
'.wikEdDiffBlock8 { background-color: #a0e8a0; } ' +
'.wikEdDiffBlockHighlight {' +
'background-color: #777; color: #fff; ' +
'border: solid #777; border-width: 1px 0; ' +
'} ' +
 
// Mark
// lines before and after diff tag to search for previous heading, paragraph, line break, cut characters
'.wikEdDiffMarkLeft, .wikEdDiffMarkRight {' +
if (wDiff.linesBeforeMax === undefined) { wDiff.linesBeforeMax = 10; }
'font-weight: bold; background-color: #ffe49c; ' +
if (wDiff.linesAfterMax === undefined) { wDiff.linesAfterMax = 10; }
'color: #666; border-radius: 0.25em; padding: 0.2em; margin: 0 1px; ' +
'} ' +
'.wikEdDiffMarkLeft:before { content: "{cssMarkLeft}"; } ' +
'.wikEdDiffMarkRight:before { content: "{cssMarkRight}"; } ' +
'.wikEdDiffMarkLeft.wikEdDiffNoUnicode:before { content: "<"; } ' +
'.wikEdDiffMarkRight.wikEdDiffNoUnicode:before { content: ">"; } ' +
'.wikEdDiffMark { background-color: #e8e8e8; color: #666; } ' +
'.wikEdDiffMark0 { background-color: #ffff60; } ' +
'.wikEdDiffMark1 { background-color: #c8f880; } ' +
'.wikEdDiffMark2 { background-color: #ffd0f0; } ' +
'.wikEdDiffMark3 { background-color: #a0ffff; } ' +
'.wikEdDiffMark4 { background-color: #fff860; } ' +
'.wikEdDiffMark5 { background-color: #b0c0ff; } ' +
'.wikEdDiffMark6 { background-color: #e0c0ff; } ' +
'.wikEdDiffMark7 { background-color: #ffa8a8; } ' +
'.wikEdDiffMark8 { background-color: #98e898; } ' +
'.wikEdDiffMarkHighlight { background-color: #777; color: #fff; } ' +
 
// Wrappers
// maximal fragment distance to join close fragments
'.wikEdDiffContainer { } ' +
if (wDiff.fragmentJoinLines === undefined) { wDiff.fragmentJoinLines = 5; }
'.wikEdDiffFragment {' +
if (wDiff.fragmentJoinChars === undefined) { wDiff.fragmentJoinChars = 1000; }
'white-space: pre-wrap; background-color: var(--background-color-base, #fff); border: #bbb solid; ' +
'border-width: 1px 1px 1px 0.5em; border-radius: 0.5em; font-family: sans-serif; ' +
'font-size: 88%; line-height: 1.6; box-shadow: 2px 2px 2px #ddd; padding: 1em; margin: 0; ' +
'} ' +
'.wikEdDiffNoChange { background: var(--background-color-interactive, #eaecf0); border: 1px #bbb solid; border-radius: 0.5em; ' +
'line-height: 1.6; box-shadow: 2px 2px 2px #ddd; padding: 0.5em; margin: 1em 0; ' +
'text-align: center; ' +
'} ' +
'.wikEdDiffSeparator { margin-bottom: 1em; } ' +
'.wikEdDiffOmittedChars { } ' +
 
// Newline
//
'.wikEdDiffNewline:before { content: "¶"; color: transparent; } ' +
// css classes
'.wikEdDiffBlock:hover .wikEdDiffNewline:before { color: #aaa; } ' +
//
'.wikEdDiffBlockHighlight .wikEdDiffNewline:before { color: transparent; } ' +
'.wikEdDiffBlockHighlight:hover .wikEdDiffNewline:before { color: #ccc; } ' +
'.wikEdDiffBlockHighlight:hover .wikEdDiffInsert .wikEdDiffNewline:before, ' +
'.wikEdDiffInsert:hover .wikEdDiffNewline:before' +
'{ color: #999; } ' +
'.wikEdDiffBlockHighlight:hover .wikEdDiffDelete .wikEdDiffNewline:before, ' +
'.wikEdDiffDelete:hover .wikEdDiffNewline:before' +
'{ color: #aaa; } ' +
 
// Tab
if (wDiff.symbolMarkLeft === undefined) { wDiff.symbolMarkLeft = '◀'; }
'.wikEdDiffTab { position: relative; } ' +
if (wDiff.symbolMarkRight === undefined) { wDiff.symbolMarkRight = '▶'; }
'.wikEdDiffTabSymbol { position: absolute; top: -0.2em; } ' +
if (wDiff.stylesheet === undefined) {
'.wikEdDiffTabSymbol:before { content: "→"; font-size: smaller; color: #ccc; } ' +
wDiff.stylesheet =
'.wikEdDiffBlock .wikEdDiffTabSymbol:before { color: #aaa; } ' +
'.wikEdDiffBlockHighlight .wikEdDiffTabSymbol:before { color: #aaa; } ' +
'.wikEdDiffInsert .wikEdDiffTabSymbol:before { color: #aaa; } ' +
'.wikEdDiffDelete .wikEdDiffTabSymbol:before { color: #bbb; } ' +
 
// insertSpace
'.wikEdDiffSpace { position: relative; } ' +
'.wDiffInsert { font-weight: bold; background-color: #bbddff; color: #222; border-radius: 0.25em; padding: 0.2em 1px; }' +
'.wikEdDiffSpaceSymbol { position: absolute; top: -0.2em; left: -0.05em; } ' +
'.wDiffInsertBlank { background-color: #66bbff; }' +
'.wDiffFragmentwikEdDiffSpaceSymbol:hover .wDiffInsertBlankbefore { background-content: "·"; color: #bbddfftransparent; } ' +
'.wikEdDiffBlock:hover .wikEdDiffSpaceSymbol:before { color: #999; } ' +
'.wikEdDiffBlockHighlight .wikEdDiffSpaceSymbol:before { color: transparent; } ' +
'.wikEdDiffBlockHighlight:hover .wikEdDiffSpaceSymbol:before { color: #ddd; } ' +
'.wikEdDiffBlockHighlight:hover .wikEdDiffInsert .wikEdDiffSpaceSymbol:before,' +
'.wikEdDiffInsert:hover .wikEdDiffSpaceSymbol:before ' +
'{ color: #888; } ' +
'.wikEdDiffBlockHighlight:hover .wikEdDiffDelete .wikEdDiffSpaceSymbol:before,' +
'.wikEdDiffDelete:hover .wikEdDiffSpaceSymbol:before ' +
'{ color: #999; } ' +
 
// deleteError
'.wikEdDiffError .wikEdDiffFragment,' +
'.wDiffDelete { font-weight: bold; background-color: #ffe49c; color: #222; border-radius: 0.25em; padding: 0.2em 1px; }' +
'.wikEdDiffError .wikEdDiffNoChange' +
'.wDiffDeleteBlank { background-color: #ffd064; }' +
'.wDiffFragment:hover .wDiffDeleteBlank { background-color: #ffe49cfaa; }' +
};
 
/** Add regular expressions to configuration settings. */
// block
'.wDiffBlockLeft, .wDiffBlockRight { font-weight: bold; background-color: #e8e8e8; border-radius: 0.25em; padding: 0.2em 1px; margin: 0 1px; }' +
'.wDiffBlockHighlight { background-color: #777; color: #fff; border: solid #777; border-width: 1px 0; }' +
'.wDiffBlock { }' +
'.wDiffBlock0 { background-color: #ffff60; }' +
'.wDiffBlock1 { background-color: #c0ff60; }' +
'.wDiffBlock2 { background-color: #ffd8ff; }' +
'.wDiffBlock3 { background-color: #a0ffff; }' +
'.wDiffBlock4 { background-color: #ffe840; }' +
'.wDiffBlock5 { background-color: #bbccff; }' +
'.wDiffBlock6 { background-color: #ffaaff; }' +
'.wDiffBlock7 { background-color: #ffbbbb; }' +
'.wDiffBlock8 { background-color: #a0e8a0; }' +
 
this.config.regExp = {
// mark
'.wDiffMarkLeft, .wDiffMarkRight { font-weight: bold; background-color: #ffe49c; color: #666; border-radius: 0.25em; padding: 0.2em; margin: 0 1px; }' +
'.wDiffMarkRight:before { content: "' + wDiff.symbolMarkRight + '"; }' +
'.wDiffMarkLeft:before { content: "' + wDiff.symbolMarkLeft + '"; }' +
'.wDiffMark { }' +
'.wDiffMarkHighlight { background-color: #777; color: #fff; }' +
'.wDiffMark0 { color: #ffff60; }' +
'.wDiffMark1 { color: #c0ff60; }' +
'.wDiffMark2 { color: #ffd8ff; }' +
'.wDiffMark3 { color: #a0ffff; }' +
'.wDiffMark4 { color: #ffd840; }' +
'.wDiffMark5 { color: #bbccff; }' +
'.wDiffMark6 { color: #ff99ff; }' +
'.wDiffMark7 { color: #ff9999; }' +
'.wDiffMark8 { color: #90d090; }' +
 
// RegExps for splitting text
// wrappers
'split': {
'.wDiffContainer { }' +
'.wDiffFragment { white-space: pre-wrap; background: #fff; border: #bbb solid; border-width: 1px 1px 1px 0.5em; border-radius: 0.5em; font-family: sans-serif; font-size: 88%; line-height: 1.6; box-shadow: 2px 2px 2px #ddd; padding: 1em; margin: 0; }' +
'.wDiffNoChange { white-space: pre-wrap; background: #f0f0f0; border: #bbb solid; border-width: 1px 1px 1px 0.5em; border-radius: 0.5em; font-family: sans-serif; font-size: 88%; line-height: 1.6; box-shadow: 2px 2px 2px #ddd; padding: 0.5em; margin: 1em 0; }' +
'.wDiffSeparator { margin-bottom: 1em; }' +
'.wDiffOmittedChars { }' +
 
// Split into paragraphs, after double newlines
// newline
'paragraph': new RegExp(
'.wDiffNewline:before { content: "¶"; color: transparent; }' +
'(\\r\\n|\\n|\\r){2,}|[' +
'.wDiffBlockHighlight .wDiffNewline:before { color: transparent; }' +
this.config.regExpNewParagraph +
'.wDiffBlockHighlight:hover .wDiffNewline:before { color: #ccc; }' +
']',
'.wDiffBlockHighlight:hover .wDiffInsert .wDiffNewline:before, .wDiffInsert:hover .wDiffNewline:before { color: #999; }' +
'g'
'.wDiffBlockHighlight:hover .wDiffDelete .wDiffNewline:before, .wDiffDelete:hover .wDiffNewline:before { color: #aaa; }' +
),
 
// Split into lines
// tab
'line': new RegExp(
'.wDiffTab { position: relative; }' +
'\\r\\n|\\n|\\r|[' +
'.wDiffTabSymbol { position: absolute; top: -0.2em; }' +
this.config.regExpNewLinesAll +
'.wDiffTabSymbol:before { content: "→"; font-size: smaller; color: transparent; color: #ccc; }' +
']',
'.wDiffBlockLeft .wDiffTabSymbol:before, .wDiffBlockRight .wDiffTabSymbol:before { color: #aaa; }' +
'g'
'.wDiffBlockHighlight .wDiffTabSymbol:before { color: #aaa; }' +
),
'.wDiffInsert .wDiffTabSymbol:before { color: #aaa; }' +
'.wDiffDelete .wDiffTabSymbol:before { color: #bbb; }' +
 
// Split into sentences /[^ ].*?[.!?:;]+(?= |$)/
// space
'sentence': new RegExp(
'.wDiffSpace { position: relative; }' +
'[^' +
'.wDiffSpaceSymbol { position: absolute; top: -0.2em; left: -0.05em; }' +
this.config.regExpBlanks +
'.wDiffSpaceSymbol:before { content: "·"; color: transparent; }' +
'].*?[.!?:;' +
'.wDiffBlockHighlight .wDiffSpaceSymbol:before { color: transparent; }' +
this.config.regExpFullStops +
'.wDiffBlockHighlight:hover .wDiffSpaceSymbol:before { color: #ddd; }' +
this.config.regExpExclamationMarks +
'.wDiffBlockHighlight:hover .wDiffInsert .wDiffSpaceSymbol:before, .wDiffInsert:hover .wDiffSpaceSymbol:before { color: #888; }' +
this.config.regExpQuestionMarks +
'.wDiffBlockHighlight:hover .wDiffDelete .wDiffSpaceSymbol:before, .wDiffDelete:hover .wDiffSpaceSymbol:before { color: #999; }';
']+(?=[' +
}
this.config.regExpBlanks +
']|$)',
'g'
),
 
// Split into inline chunks
//
'chunk': new RegExp(
// css styles
'\\[\\[[^\\[\\]\\n]+\\]\\]|' + // [[wiki link]]
//
'\\{\\{[^\\{\\}\\n]+\\}\\}|' + // {{template}}
'\\[[^\\[\\]\\n]+\\]|' + // [ext. link]
'<\\/?[^<>\\[\\]\\{\\}\\n]+>|' + // <html>
'\\[\\[[^\\[\\]\\|\\n]+\\]\\]\\||' + // [[wiki link|
'\\{\\{[^\\{\\}\\|\\n]+\\||' + // {{template|
'\\b((https?:|)\\/\\/)[^\\x00-\\x20\\s"\\[\\]\\x7f]+', // link
'g'
),
 
// Split into words, multi-char markup, and chars
if (wDiff.styleInsert === undefined) { wDiff.styleInsert = ''; }
// regExpLetters speed-up: \\w+
if (wDiff.styleDelete === undefined) { wDiff.styleDelete = ''; }
'word': new RegExp(
if (wDiff.styleInsertBlank === undefined) { wDiff.styleInsertBlank = ''; }
'(\\w+|[_' +
if (wDiff.styleDeleteBlank === undefined) { wDiff.styleDeleteBlank = ''; }
this.config.regExpLetters +
if (wDiff.styleBlockLeft === undefined) { wDiff.styleBlockLeft = ''; }
'])+([\'’][_' +
if (wDiff.styleBlockRight === undefined) { wDiff.styleBlockRight = ''; }
this.config.regExpLetters +
if (wDiff.styleBlockHighlight === undefined) { wDiff.styleBlockHighlight = ''; }
']*)*|\\[\\[|\\]\\]|\\{\\{|\\}\\}|&\\w+;|\'\'\'|\'\'|==+|\\{\\||\\|\\}|\\|-|.',
if (wDiff.styleBlockColor === undefined) { wDiff.styleBlockColor = []; }
'g'
if (wDiff.styleMarkLeft === undefined) { wDiff.styleMarkLeft = ''; }
),
if (wDiff.styleMarkRight === undefined) { wDiff.styleMarkRight = ''; }
if (wDiff.styleMarkColor === undefined) { wDiff.styleMarkColor = []; }
if (wDiff.styleContainer === undefined) { wDiff.styleContainer = ''; }
if (wDiff.styleFragment === undefined) { wDiff.styleFragment = ''; }
if (wDiff.styleNoChange === undefined) { wDiff.styleNoChange = ''; }
if (wDiff.styleSeparator === undefined) { wDiff.styleSeparator = ''; }
if (wDiff.styleOmittedChars === undefined) { wDiff.styleOmittedChars = ''; }
if (wDiff.styleNewline === undefined) { wDiff.styleNewline = ''; }
if (wDiff.styleTab === undefined) { wDiff.styleTab = ''; }
if (wDiff.styleTabSymbol === undefined) { wDiff.styleTabSymbol = ''; }
if (wDiff.styleSpace === undefined) { wDiff.styleSpace = ''; }
if (wDiff.styleSpaceSymbol === undefined) { wDiff.styleSpaceSymbol = ''; }
 
// Split into chars
//
'character': /./g
// html for core diff
},
//
 
// RegExp to detect blank tokens
// dynamic replacements: {block}: block number style, {mark}: mark number style, {class}: class number, {number}: block number, {title}: title attribute (popup)
'blankOnlyToken': new RegExp(
// class plus html comment are required indicators for wDiff.ShortenOutput()
'[^' +
if (wDiff.blockEvent === undefined) { wDiff.blockEvent = ' onmouseover="wDiff.BlockHandler(undefined, this, \'mouseover\');"'; }
this.config.regExpBlanks +
this.config.regExpNewLinesAll +
this.config.regExpNewParagraph +
']'
),
 
// RegExps for sliding gaps: newlines and space/word breaks
if (wDiff.htmlContainerStart === undefined) { wDiff.htmlContainerStart = '<div class="wDiffContainer" id="wDiffContainer" style="' + wDiff.styleContainer + '">'; }
'slideStop': new RegExp(
if (wDiff.htmlContainerEnd === undefined) { wDiff.htmlContainerEnd = '</div>'; }
'[' +
this.config.regExpNewLinesAll +
this.config.regExpNewParagraph +
']$'
),
'slideBorder': new RegExp(
'[' +
this.config.regExpBlanks +
']$'
),
 
// RegExps for counting words
if (wDiff.htmlDeleteStart === undefined) { wDiff.htmlDeleteStart = '<span class="wDiffDelete" style="' + wDiff.styleDelete + '" title="−">'; }
'countWords': new RegExp(
if (wDiff.htmlDeleteStartBlank === undefined) { wDiff.htmlDeleteStartBlank = '<span class="wDiffDelete wDiffDeleteBlank" style="' + wDiff.styleDelete + ' ' + wDiff.styleDeleteBlank + '" title="−">'; }
'(\\w+|[_' +
if (wDiff.htmlDeleteEnd === undefined) { wDiff.htmlDeleteEnd = '</span><!--wDiffDelete-->'; }
this.config.regExpLetters +
'])+([\'’][_' +
this.config.regExpLetters +
']*)*',
'g'
),
'countChunks': new RegExp(
'\\[\\[[^\\[\\]\\n]+\\]\\]|' + // [[wiki link]]
'\\{\\{[^\\{\\}\\n]+\\}\\}|' + // {{template}}
'\\[[^\\[\\]\\n]+\\]|' + // [ext. link]
'<\\/?[^<>\\[\\]\\{\\}\\n]+>|' + // <html>
'\\[\\[[^\\[\\]\\|\\n]+\\]\\]\\||' + // [[wiki link|
'\\{\\{[^\\{\\}\\|\\n]+\\||' + // {{template|
'\\b((https?:|)\\/\\/)[^\\x00-\\x20\\s"\\[\\]\\x7f]+', // link
'g'
),
 
// RegExp detecting blank-only and single-char blocks
if (wDiff.htmlInsertStart === undefined) { wDiff.htmlInsertStart = '<span class="wDiffInsert" style="' + wDiff.styleInsert + '" title="+">'; }
'blankBlock': /^([^\t\S]+|[^\t])$/,
if (wDiff.htmlInsertStartBlank === undefined) { wDiff.htmlInsertStartBlank = '<span class="wDiffInsert wDiffInsertBlank" style="' + wDiff.styleInsert + ' ' + wDiff.styleInsertBlank + '" title="+">'; }
if (wDiff.htmlInsertEnd === undefined) { wDiff.htmlInsertEnd = '</span><!--wDiffInsert-->'; }
 
// RegExps for clipping
if (wDiff.htmlBlockLeftStart === undefined) { wDiff.htmlBlockLeftStart = '<span class="wDiffBlockLeft wDiffBlock{class}" style="' + wDiff.styleBlockLeft + '{block}" title="' + wDiff.symbolMarkLeft + '" id="wDiffBlock{number}"' + wDiff.blockEvent + '>'; }
'clipLine': new RegExp(
if (wDiff.htmlBlockLeftEnd === undefined) { wDiff.htmlBlockLeftEnd = '</span><!--wDiffBlockLeft-->'; }
'[' + this.config.regExpNewLinesAll +
this.config.regExpNewParagraph +
']+',
'g'
),
'clipHeading': new RegExp(
'( ^|\\n)(==+.+?==+|\\{\\||\\|\\}).*?(?=\\n|$)', 'g' ),
'clipParagraph': new RegExp(
'( (\\r\\n|\\n|\\r){2,}|[' +
this.config.regExpNewParagraph +
'])+',
'g'
),
'clipBlank': new RegExp(
'[' +
this.config.regExpBlanks + ']+',
'g'
),
'clipTrimNewLinesLeft': new RegExp(
'[' +
this.config.regExpNewLinesAll +
this.config.regExpNewParagraph +
']+$',
'g'
),
'clipTrimNewLinesRight': new RegExp(
'^[' +
this.config.regExpNewLinesAll +
this.config.regExpNewParagraph +
']+',
'g'
),
'clipTrimBlanksLeft': new RegExp(
'[' +
this.config.regExpBlanks +
this.config.regExpNewLinesAll +
this.config.regExpNewParagraph +
']+$',
'g'
),
'clipTrimBlanksRight': new RegExp(
'^[' +
this.config.regExpBlanks +
this.config.regExpNewLinesAll +
this.config.regExpNewParagraph +
']+',
'g'
)
};
 
/** Add messages to configuration settings. */
if (wDiff.htmlBlockRightStart === undefined) { wDiff.htmlBlockRightStart = '<span class="wDiffBlockRight wDiffBlock{class}" style="' + wDiff.styleBlockRight + '{block}" title="' + wDiff.symbolMarkRight + '" id="wDiffBlock{number}"' + wDiff.blockEvent + '>'; }
if (wDiff.htmlBlockRightEnd === undefined) { wDiff.htmlBlockRightEnd = '</span><!--wDiffBlockRight-->'; }
 
this.config.msg = {
if (wDiff.htmlMarkLeft === undefined) { wDiff.htmlMarkLeft = '<span class="wDiffMarkLeft wDiffMark{class}" style="' + wDiff.styleMarkLeft + '{mark}"{title} id="wDiffMark{number}"' + wDiff.blockEvent + '></span><!--wDiffMarkLeft-->'; }
'wiked-diff-empty': '(No difference)',
if (wDiff.htmlMarkRight === undefined) { wDiff.htmlMarkRight = '<span class="wDiffMarkRight wDiffMark{class}" style="' + wDiff.styleMarkRight + '{mark}"{title} id="wDiffMark{number}"' + wDiff.blockEvent + '></span><!--wDiffMarkRight-->'; }
'wiked-diff-same': '=',
'wiked-diff-ins': '+',
'wiked-diff-del': '-',
'wiked-diff-block-left': '◀',
'wiked-diff-block-right': '▶',
'wiked-diff-block-left-nounicode': '<',
'wiked-diff-block-right-nounicode': '>',
'wiked-diff-error': 'Error: diff not consistent with versions!'
};
 
/**
if (wDiff.htmlNewline === undefined) { wDiff.htmlNewline = '<span class="wDiffNewline" style="' + wDiff.styleNewline + '">\n</span>'; }
* Add output html fragments to configuration settings.
if (wDiff.htmlTab === undefined) { wDiff.htmlTab = '<span class="wDiffTab" style="' + wDiff.styleTab + '"><span class="wDiffTabSymbol" style="' + wDiff.styleTabSymbol + '"></span>\t</span>'; }
* Dynamic replacements:
if (wDiff.htmlSpace === undefined) { wDiff.htmlSpace = '<span class="wDiffSpace" style="' + wDiff.styleSpace + '"><span class="wDiffSpaceSymbol" style="' + wDiff.styleSpaceSymbol + '"></span> </span>'; }
* {number}: class/color/block/mark/id number
* {title}: title attribute (popup)
* {nounicode}: noUnicodeSymbols fallback
*/
this.config.htmlCode = {
'noChangeStart':
'<div class="wikEdDiffNoChange" title="' +
this.config.msg['wiked-diff-same'] +
'">',
'noChangeEnd': '</div>',
 
'containerStart': '<div class="wikEdDiffContainer" id="wikEdDiffContainer">',
//
'containerEnd': '</div>',
// html for shorten output
//
 
'fragmentStart': '<pre class="wikEdDiffFragment" style="white-space: pre-wrap;">',
if (wDiff.htmlFragmentStart === undefined) { wDiff.htmlFragmentStart = '<pre class="wDiffFragment" style="' + wDiff.styleFragment + '">'; }
'fragmentEnd': '</pre>',
if (wDiff.htmlFragmentEnd === undefined) { wDiff.htmlFragmentEnd = '</pre>'; }
'separator': '<div class="wikEdDiffSeparator"></div>',
 
'insertStart':
if (wDiff.htmlNoChange === undefined) { wDiff.htmlNoChange = '<pre class="wDiffNoChange" style="' + wDiff.styleNoChange + '" title="="></pre>'; }
'<span class="wikEdDiffInsert" title="' +
if (wDiff.htmlSeparator === undefined) { wDiff.htmlSeparator = '<div class="wDiffSeparator" style="' + wDiff.styleSeparator + '"></div>'; }
this.config.msg['wiked-diff-ins'] +
if (wDiff.htmlOmittedChars === undefined) { wDiff.htmlOmittedChars = '<span class="wDiffOmittedChars" style="' + wDiff.styleOmittedChars + '">…</span>'; }
'">',
'insertStartBlank':
'<span class="wikEdDiffInsert wikEdDiffInsertBlank" title="' +
this.config.msg['wiked-diff-ins'] +
'">',
'insertEnd': '</span>',
 
'deleteStart':
//
'<span class="wikEdDiffDelete" title="' +
// javascript handler for output code, compatible with IE 8
this.config.msg['wiked-diff-del'] +
//
'">',
'deleteStartBlank':
'<span class="wikEdDiffDelete wikEdDiffDeleteBlank" title="' +
this.config.msg['wiked-diff-del'] +
'">',
'deleteEnd': '</span>',
 
'blockStart':
// wDiff.BlockHandler: event handler for block and mark elements
'<span class="wikEdDiffBlock"' +
if (wDiff.BlockHandler === undefined) { wDiff.BlockHandler = function (event, element, type) {
'title="{title}" id="wikEdDiffBlock{number}"' +
'onmouseover="wikEdDiffBlockHandler(undefined, this, \'mouseover\');">',
'blockColoredStart':
'<span class="wikEdDiffBlock wikEdDiffBlock wikEdDiffBlock{number}"' +
'title="{title}" id="wikEdDiffBlock{number}"' +
'onmouseover="wikEdDiffBlockHandler(undefined, this, \'mouseover\');">',
'blockEnd': '</span>',
 
'markLeft':
// IE compatibility
'<span class="wikEdDiffMarkLeft{nounicode}"' +
if ( (event === undefined) && (window.event !== undefined) ) {
'title="{title}" id="wikEdDiffMark{number}"' +
event = window.event;
'onmouseover="wikEdDiffBlockHandler(undefined, this, \'mouseover\');"></span>',
}
'markLeftColored':
'<span class="wikEdDiffMarkLeft{nounicode} wikEdDiffMark wikEdDiffMark{number}"' +
'title="{title}" id="wikEdDiffMark{number}"' +
'onmouseover="wikEdDiffBlockHandler(undefined, this, \'mouseover\');"></span>',
 
'markRight':
// get mark/block elements
'<span class="wikEdDiffMarkRight{nounicode}"' +
var number = element.id.replace(/\D/g, '');
'title="{title}" id="wikEdDiffMark{number}"' +
var block = document.getElementById('wDiffBlock' + number);
'onmouseover="wikEdDiffBlockHandler(undefined, this, \'mouseover\');"></span>',
var mark = document.getElementById('wDiffMark' + number);
'markRightColored':
'<span class="wikEdDiffMarkRight{nounicode} wikEdDiffMark wikEdDiffMark{number}"' +
'title="{title}" id="wikEdDiffMark{number}"' +
'onmouseover="wikEdDiffBlockHandler(undefined, this, \'mouseover\');"></span>',
 
'newline': '<span class="wikEdDiffNewline">\n</span>',
// highlight corresponding mark/block pairs
'tab': '<span class="wikEdDiffTab"><span class="wikEdDiffTabSymbol"></span>\t</span>',
if (type == 'mouseover') {
'space': '<span class="wikEdDiffSpace"><span class="wikEdDiffSpaceSymbol"></span> </span>',
element.onmouseover = null;
element.onmouseout = function (event) { wDiff.BlockHandler(event, element, 'mouseout'); };
element.onclick = function (event) { wDiff.BlockHandler(event, element, 'click'); };
block.className += ' wDiffBlockHighlight';
mark.className += ' wDiffMarkHighlight';
}
 
'omittedChars': '<span class="wikEdDiffOmittedChars">…</span>',
// remove mark/block highlighting
if ( (type == 'mouseout') || (type == 'click') ) {
element.onmouseout = null;
element.onmouseover = function (event) { wDiff.BlockHandler(event, element, 'mouseover'); };
 
'errorStart': '<div class="wikEdDiffError" title="Error: diff not consistent with versions!">',
// getElementsByClassName
'errorEnd': '</div>'
var container = document.getElementById('wDiffContainer');
};
var spans = container.getElementsByTagName('span');
for (var i = 0; i < spans.length; i ++) {
if ( ( (spans[i] != block) && (spans[i] != mark) ) || (type != 'click') ) {
if (spans[i].className.indexOf(' wDiffBlockHighlight') != -1) {
spans[i].className = spans[i].className.replace(/ wDiffBlockHighlight/g, '');
}
else if (spans[i].className.indexOf(' wDiffMarkHighlight') != -1) {
spans[i].className = spans[i].className.replace(/ wDiffMarkHighlight/g, '');
}
}
}
}
 
/*
// scroll to corresponding mark/block element
* Add JavaScript event handler function to configuration settings
if (type == 'click') {
* Highlights corresponding block and mark elements on hover and jumps between them on click
* Code for use in non-jQuery environments and legacy browsers (at least IE 8 compatible)
*
* @option Event|undefined event Browser event if available
* @option element Node DOM node
* @option type string Event type
*/
this.config.blockHandler = function ( event, element, type ) {
 
// IE compatibility
// get corresponding element
if ( event === undefined && window.event !== undefined ) {
var corrElement;
event = window.event;
if (element == block) {
corrElement = mark;
}
else {
corrElement = block;
}
 
// Get mark/block elements
// getOffsetTop
var number = element.id.replace( /\D/g, '' );
var corrElementPos = 0;
var block = document.getElementById( 'wikEdDiffBlock' + number );
var node = corrElement;
var mark = document.getElementById( 'wikEdDiffMark' + number );
do {
if ( block === null || mark === null ) {
corrElementPos += node.offsetTop;
return;
} while ( (node = node.offsetParent) !== null );
 
// scroll element under mouse cursor
var top;
if (window.pageYOffset !== undefined) {
top = window.pageYOffset;
}
else {
top = document.documentElement.scrollTop;
}
 
// Highlight corresponding mark/block pairs
var cursor;
if (event.pageY !type === 'mouseover' undefined) {
element.onmouseover = null;
cursor = event.pageY;
element.onmouseout = function ( event ) {
}
window.wikEdDiffBlockHandler( event, element, 'mouseout' );
else if (event.clientY !== undefined) {
};
cursor = event.clientY + top;
element.onclick = function ( event ) {
window.wikEdDiffBlockHandler( event, element, 'click' );
};
block.className += ' wikEdDiffBlockHighlight';
mark.className += ' wikEdDiffMarkHighlight';
}
 
// Remove mark/block highlighting
var line = 12;
if ( type === 'mouseout' || type === 'click' ) {
if (window.getComputedStyle !== undefined) {
element.onmouseout = null;
line = parseInt(window.getComputedStyle(corrElement).getPropertyValue('line-height'));
element.onmouseover = function ( event ) {
window.wikEdDiffBlockHandler( event, element, 'mouseover' );
};
 
// Reset, allow outside container (e.g. legend)
if ( type !== 'click' ) {
block.className = block.className.replace( / wikEdDiffBlockHighlight/g, '' );
mark.className = mark.className.replace( / wikEdDiffMarkHighlight/g, '' );
 
// GetElementsByClassName
var container = document.getElementById( 'wikEdDiffContainer' );
if ( container !== null ) {
var spans = container.getElementsByTagName( 'span' );
var spansLength = spans.length;
for ( var i = 0; i < spansLength; i ++ ) {
if ( spans[i] !== block && spans[i] !== mark ) {
if ( spans[i].className.indexOf( ' wikEdDiffBlockHighlight' ) !== -1 ) {
spans[i].className = spans[i].className.replace( / wikEdDiffBlockHighlight/g, '' );
}
else if ( spans[i].className.indexOf( ' wikEdDiffMarkHighlight') !== -1 ) {
spans[i].className = spans[i].className.replace( / wikEdDiffMarkHighlight/g, '' );
}
}
}
}
}
}
 
// Scroll to corresponding mark/block element
window.scroll(0, corrElementPos + top - cursor + line / 2);
if ( type === 'click' ) {
}
return;
}; }
 
// Get corresponding element
//
var corrElement;
// start of diff code
if ( element === block ) {
//
corrElement = mark;
}
else {
corrElement = block;
}
 
// Get element height (getOffsetTop)
var corrElementPos = 0;
var node = corrElement;
do {
corrElementPos += node.offsetTop;
} while ( ( node = node.offsetParent ) !== null );
 
// Get scroll height
// wDiff.Init: initialize wDiff
var top;
// called from: on code load
if ( window.pageYOffset !== undefined ) {
// calls: wDiff.AddStyleSheet()
top = window.pageYOffset;
}
else {
top = document.documentElement.scrollTop;
}
 
// Get cursor pos
wDiff.Init = function () {
var cursor;
if ( event.pageY !== undefined ) {
cursor = event.pageY;
}
else if ( event.clientY !== undefined ) {
cursor = event.clientY + top;
}
 
// Get line height
// compatibility fixes for old names of functions
var line = 12;
window.StringDiff = wDiff.Diff;
if ( window.WDiffStringgetComputedStyle !== wDiff.Diff;undefined ) {
line = parseInt( window.getComputedStyle( corrElement ).getPropertyValue( 'line-height' ) );
window.WDiffShortenOutput = wDiff.ShortenOutput;
}
 
// Scroll element under mouse cursor
// shortcut to wikEd.Debug()
window.scroll( 0, corrElementPos + top - cursor + line / 2 );
if (WED === undefined) {
if (typeof console == 'object') {
WED = console.log;
}
else {return;
};
WED = window.alert;
}
}
 
/** Internal data structures. */
// add styles to head
wDiff.AddStyleSheet(wDiff.stylesheet);
 
/** @var WikEdDiffText newText New text version object with text and token list */
// add block handler to head if running under Greasemonkey
this.newText = null;
if (typeof GM_info == 'object') {
var script = 'var wDiff; if (wDiff === undefined) { wDiff = {}; } wDiff.BlockHandler = ' + wDiff.BlockHandler.toString();
wDiff.AddScript(script);
}
return;
};
 
/** @var WikEdDiffText oldText Old text version object with text and token list */
this.oldText = null;
 
/** @var object symbols Symbols table for whole text at all refinement levels */
// wDiff.Diff: main method
this.symbols = {
// input: oldString, newString, strings containing the texts to be diffed
token: [],
// called from: user code
hashTable: {},
// calls: wDiff.Split(), wDiff.SplitRefine(), wDiff.CalculateDiff(), wDiff.DetectBlocks(), wDiff.AssembleDiff()
linked: false
// returns: diff html code, call wDiff.ShortenOutput() for shortening this output
};
 
/** @var array bordersDown Matched region borders downwards */
wDiff.Diff = function (oldString, newString) {
this.bordersDown = [];
 
/** @var array bordersUp Matched region borders upwards */
var diff = '';
this.bordersUp = [];
 
/** @var array blocks Block data (consecutive text tokens) in new text order */
// wikEd.debugTimer.push(['diff?', new Date]);
this.blocks = [];
 
/** @var int maxWords Maximal detected word count of all linked blocks */
// IE / Mac fix
this.maxWords = 0;
oldString = oldString.replace(/\r\n?/g, '\n');
newString = newString.replace(/\r\n?/g, '\n');
 
/** @var array groups Section blocks that are consecutive in old text order */
// prepare text data object
var textthis.groups = {[];
newText: {
string: newString,
tokens: [],
first: null,
last: null,
words: {}
},
oldText: {
string: oldString,
tokens: [],
first: null,
last: null,
words: {}
},
diff: ''
};
 
/** @var array sections Block sections with no block move crosses outside a section */
// trap trivial changes: no change
this.sections = [];
if (oldString == newString) {
text.diff = wDiff.HtmlEscape(newString);
wDiff.HtmlFormat(text);
return text.diff;
}
 
/** @var object timer Debug timer array: string 'label' => float milliseconds. */
// trap trivial changes: old text deleted
this.timer = {};
if ( (oldString === null) || (oldString.length === 0) ) {
text.diff = wDiff.htmlInsertStart + wDiff.HtmlEscape(newString) + wDiff.htmlInsertEnd;
wDiff.HtmlFormat(text);
return text.diff;
}
 
/** @var array recursionTimer Count time spent in recursion level in milliseconds. */
// trap trivial changes: new text deleted
this.recursionTimer = [];
if ( (newString === null) || (newString.length === 0) ) {
text.diff = wDiff.htmlDeleteStart + wDiff.HtmlEscape(oldString) + wDiff.htmlDeleteEnd;
wDiff.HtmlFormat(text);
return text.diff;
}
 
/** Output data. */
// parse and count words in texts for later identification of unique words
wDiff.CountTextWords(text.newText);
wDiff.CountTextWords(text.oldText);
 
/** @var bool error Unit tests have detected a diff error */
// new symbols object
this.error = false;
var symbols = {
token: [],
hash: {},
linked: false
};
 
/** @var array fragments Diff fragment list for markup, abstraction layer for customization */
// split new and old text into paragraps
this.fragments = [];
wDiff.Split(text.newText, 'paragraph');
wDiff.Split(text.oldText, 'paragraph');
 
/** @var string html Html code of diff */
// calculate diff
this.html = '';
wDiff.CalculateDiff(text, symbols, 'paragraph');
 
// refine different paragraphs into sentences
wDiff.SplitRefine(text.newText, 'sentence');
wDiff.SplitRefine(text.oldText, 'sentence');
 
/**
// calculate refined diff
* Constructor, initialize settings, load js and css.
wDiff.CalculateDiff(text, symbols, 'sentence');
*
* @param[in] object wikEdDiffConfig Custom customization settings
* @param[out] object config Settings
*/
 
this.init = function () {
// refine different paragraphs into chunks
wDiff.SplitRefine(text.newText, 'chunk');
wDiff.SplitRefine(text.oldText, 'chunk');
 
// Import customizations from wikEdDiffConfig{}
// calculate refined diff
if ( typeof wikEdDiffConfig === 'object' ) {
wDiff.CalculateDiff(text, symbols, 'chunk');
this.deepCopy( wikEdDiffConfig, this.config );
}
 
// Add CSS stylescheet
// refine different sentences into words
this.addStyleSheet( this.config.stylesheet );
wDiff.SplitRefine(text.newText, 'word');
wDiff.SplitRefine(text.oldText, 'word');
 
// Load block handler script
// calculate refined diff information with recursion for unresolved gaps
if ( this.config.showBlockMoves === true ) {
wDiff.CalculateDiff(text, symbols, 'word', true);
 
// Add block handler to head if running under Greasemonkey
// slide up gaps
if ( typeof GM_info === 'object' ) {
wDiff.SlideGaps(text.newText, text.oldText);
var script = 'var wikEdDiffBlockHandler = ' + this.config.blockHandler.toString() + ';';
wDiff.SlideGaps(text.oldText, text.newText);
this.addScript( script );
}
else {
window.wikEdDiffBlockHandler = this.config.blockHandler;
}
}
return;
};
 
// split tokens into chars in selected unresolved gaps
if (wDiff.charDiff === true) {
wDiff.SplitRefineChars(text);
 
/**
// calculate refined diff information with recursion for unresolved gaps
* Main diff method.
wDiff.CalculateDiff(text, symbols, 'character', true);
*
* @param string oldString Old text version
* @param string newString New text version
* @param[out] array fragment
* Diff fragment list ready for markup, abstraction layer for customized diffs
* @param[out] string html Html code of diff
* @return string Html code of diff
*/
this.diff = function ( oldString, newString ) {
 
// slideStart uptotal gapstimer
if ( this.config.timer === true ) {
wDiff.SlideGaps(text.newText, text.oldText);
this.time( 'total' );
wDiff.SlideGaps(text.oldText, text.newText);
}
 
// Start diff timer
// enumerate tokens lists
if ( this.config.timer === true ) {
wDiff.EnumerateTokens(text.newText);
this.time( 'diff' );
wDiff.EnumerateTokens(text.oldText);
}
 
// detectReset movederror blocksflag
this.error = false;
var blocks = [];
var groups = [];
wDiff.DetectBlocks(text, blocks, groups);
 
// Strip trailing newline (.js only)
// assemble diff blocks into formatted html text
if ( this.config.stripTrailingNewline === true ) {
diff = wDiff.AssembleDiff(text, blocks, groups);
if ( newString.substr( -1 ) === '\n' && oldString.substr( -1 === '\n' ) ) {
newString = newString.substr( 0, newString.length - 1 );
oldString = oldString.substr( 0, oldString.length - 1 );
}
}
 
// Load version strings into WikEdDiffText objects
// wikEd.debugTimer.push(['diff=', new Date]);
this.newText = new WikEdDiff.WikEdDiffText( newString, this );
// wikEd.DebugTimer();
this.oldText = new WikEdDiff.WikEdDiffText( oldString, this );
 
// Trap trivial changes: no change
return diff;
if ( this.newText.text === this.oldText.text ) {
};
this.html =
this.config.htmlCode.containerStart +
this.config.htmlCode.noChangeStart +
this.htmlEscape( this.config.msg['wiked-diff-empty'] ) +
this.config.htmlCode.noChangeEnd +
this.config.htmlCode.containerEnd;
return this.html;
}
 
// Trap trivial changes: old text deleted
if (
this.oldText.text === '' || (
this.oldText.text === '\n' &&
( this.newText.text.charAt( this.newText.text.length - 1 ) === '\n' )
)
) {
this.html =
this.config.htmlCode.containerStart +
this.config.htmlCode.fragmentStart +
this.config.htmlCode.insertStart +
this.htmlEscape( this.newText.text ) +
this.config.htmlCode.insertEnd +
this.config.htmlCode.fragmentEnd +
this.config.htmlCode.containerEnd;
return this.html;
}
 
// Trap trivial changes: new text deleted
// wDiff.CountTextWords: parse and count words in text for later identification of unique words
if (
// changes: text (text.newText or text.oldText) .words
this.newText.text === '' || (
// called from: wDiff.Diff()
this.newText.text === '\n' &&
 
( this.oldText.text.charAt( this.oldText.text.length - 1 ) === '\n' )
wDiff.CountTextWords = function (text) {
)
) {
this.html =
this.config.htmlCode.containerStart +
this.config.htmlCode.fragmentStart +
this.config.htmlCode.deleteStart +
this.htmlEscape( this.oldText.text ) +
this.config.htmlCode.deleteEnd +
this.config.htmlCode.fragmentEnd +
this.config.htmlCode.containerEnd;
return this.html;
}
 
// Split new and old text into paragraps
var regExpMatch;
if ( this.config.timer === true ) {
while ( (regExpMatch = wDiff.regExpWordCount.exec(text.string)) !== null) {
this.time( 'paragraph split' );
var word = text.words[ regExpMatch[0] ];
if (word === undefined) {
word = 1;
}
this.newText.splitText( 'paragraph' );
else {
this.oldText.splitText( 'paragraph' );
word ++;
if ( this.config.timer === true ) {
this.timeEnd( 'paragraph split' );
}
}
return;
};
 
// Calculate diff
this.calculateDiff( 'line' );
 
// Refine different paragraphs into lines
// wDiff.Split: split text into paragraph, sentence, or word tokens
if ( this.config.timer === true ) {
// input: text (text.newText or text.oldText), object containing text data and strings; regExp, regular expression for splitting text into tokens; token, tokens index of token to be split
this.time( 'line split' );
// changes: text (text.newText or text.oldText): text.tokens list, text.first, text.last
}
// called from: wDiff.Diff()
this.newText.splitRefine( 'line' );
this.oldText.splitRefine( 'line' );
if ( this.config.timer === true ) {
this.timeEnd( 'line split' );
}
 
// Calculate refined diff
wDiff.Split = function (text, level, token) {
this.calculateDiff( 'line' );
 
// Refine different lines into sentences
var prev = null;
if ( this.config.timer === true ) {
var next = null;
this.time( 'sentence split' );
var current = text.tokens.length;
}
var first = current;
this.newText.splitRefine( 'sentence' );
var string = '';
this.oldText.splitRefine( 'sentence' );
if ( this.config.timer === true ) {
this.timeEnd( 'sentence split' );
}
 
// Calculate refined diff
// split full text or specified token
this.calculateDiff( 'sentence' );
if (token === undefined) {
string = text.string;
}
else {
prev = text.tokens[token].prev;
next = text.tokens[token].next;
string = text.tokens[token].token;
}
 
// Refine different sentences into chunks
// split text into tokens, regExp match as separator
if ( this.config.timer === true ) {
var number = 0;
this.time( 'chunk split' );
var split = [];
}
var regExpMatch;
this.newText.splitRefine( 'chunk' );
var lastIndex = 0;
this.oldText.splitRefine( 'chunk' );
while ( (regExpMatch = wDiff.regExpSplit[level].exec(string)) !== null) {
if (regExpMatch this.indexconfig.timer === >true lastIndex) {
this.timeEnd( 'chunk split' );
split.push(string.substring(lastIndex, regExpMatch.index));
}
split.push(regExpMatch[0]);
lastIndex = wDiff.regExpSplit[level].lastIndex;
}
if (lastIndex < string.length) {
split.push(string.substring(lastIndex));
}
 
// Calculate refined diff
// cycle trough new tokens
this.calculateDiff( 'chunk' );
for (var i = 0; i < split.length; i ++) {
 
// Refine different chunks into words
// insert current item, link to previous
if ( this.config.timer === true ) {
text.tokens[current] = {
token: this.time( 'word split[i],' );
prev: prev,
next: null,
link: null,
number: null,
parsed: false,
unique: false
};
number ++;
 
// link previous item to current
if (prev !== null) {
text.tokens[prev].next = current;
}
this.newText.splitRefine( 'word' );
prev = current;
this.oldText.splitRefine( 'word' );
current ++;
if ( this.config.timer === true ) {
}
this.timeEnd( 'word split' );
}
 
// Calculate refined diff information with recursion for unresolved gaps
this.calculateDiff( 'word', true );
 
// Slide gaps
// connect last new item and existing next item
if ( (numberthis.config.timer > 0) && (token !=== undefined)true ) {
this.time( 'word slide' );
if (prev !== null) {
text.tokens[prev].next = next;
}
this.slideGaps( this.newText, this.oldText );
if (next !== null) {
this.slideGaps( this.oldText, this.newText );
text.tokens[next].prev = prev;
if ( this.config.timer === true ) {
this.timeEnd( 'word slide' );
}
}
 
// Split tokens into chars
// set text first and last token index
if ( this.config.charDiff === true ) {
if (number > 0) {
 
// Split tokens into chars in selected unresolved gaps
// initial text split
if (token this.config.timer === undefinedtrue ) {
this.time( 'character split' );
text.first = 0;
}
text.last = prev;
this.splitRefineChars();
}
if ( this.config.timer === true ) {
this.timeEnd( 'character split' );
}
 
// Calculate refined diff information with recursion for unresolved gaps
// first or last token has been split
this.calculateDiff( 'character', true );
else {
 
if (token == text.first) {
// Slide gaps
text.first = first;
if ( this.config.timer === true ) {
this.time( 'character slide' );
}
this.slideGaps( this.newText, this.oldText );
if (token == text.last) {
this.slideGaps( this.oldText, this.newText );
text.last = prev;
if ( this.config.timer === true ) {
this.timeEnd( 'character slide' );
}
}
}
return;
};
 
// Free memory
this.symbols = undefined;
this.bordersDown = undefined;
this.bordersUp = undefined;
this.newText.words = undefined;
this.oldText.words = undefined;
 
// Enumerate token lists
// wDiff.SplitRefine: split unique unmatched tokens into smaller tokens
this.newText.enumerateTokens();
// changes: text (text.newText or text.oldText) .tokens list
this.oldText.enumerateTokens();
// called from: wDiff.Diff()
// calls: wDiff.Split()
 
// Detect moved blocks
wDiff.SplitRefine = function (text, regExp) {
if ( this.config.timer === true ) {
this.time( 'blocks' );
}
this.detectBlocks();
if ( this.config.timer === true ) {
this.timeEnd( 'blocks' );
}
 
// Free memory
// cycle through tokens list
this.newText.tokens = undefined;
var i = text.first;
this.oldText.tokens = undefined;
while ( (i !== null) && (text.tokens[i] !== null) ) {
 
// refineAssemble unique unmatched tokensblocks into smallerfragment tokenstable
this.getDiffFragments();
if (text.tokens[i].link === null) {
wDiff.Split(text, regExp, i);
}
i = text.tokens[i].next;
}
return;
};
 
// Free memory
this.blocks = undefined;
this.groups = undefined;
this.sections = undefined;
 
// Stop diff timer
// wDiff.SplitRefineChars: split tokens into chars in the following unresolved regions (gaps):
if ( this.config.timer === true ) {
// - one token became separated by space, dash, or any string
this.timeEnd( 'diff' );
// - same number of tokens in gap and strong similarity of all tokens:
}
// - addition or deletion of flanking strings in tokens
// - addition or deletion of internal string in tokens
// - same length and at least 50 % identity
// - same start or end, same text longer than different text
// - same length and at least 50 % identity
// identical tokens including space separators will be linked, resulting in word-wise char-level diffs
// changes: text (text.newText or text.oldText) .tokens list
// called from: wDiff.Diff()
// calls: wDiff.Split()
// steps:
// find corresponding gaps
// select gaps of identical token number and strong similarity in all tokens
// refine words into chars in selected gaps
 
// Unit tests
wDiff.SplitRefineChars = function (text) {
if ( this.config.unitTesting === true ) {
 
// Test diff to test consistency between input and output
//
if ( this.config.timer === true ) {
// find corresponding gaps
this.time( 'unit tests' );
//
}
this.unitTests();
if ( this.config.timer === true ) {
this.timeEnd( 'unit tests' );
}
}
 
// Clipping
// cycle trough new text tokens list
if ( this.config.fullDiff === false ) {
var gaps = [];
var gap = null;
var i = text.newText.first;
var j = text.oldText.first;
while ( (i !== null) && (text.newText.tokens[i] !== null) ) {
 
// Clipping unchanged sections from unmoved block text
// get token links
if ( this.config.timer === true ) {
var newLink = text.newText.tokens[i].link;
this.time( 'clip' );
var oldLink = null;
}
if (j !== null) {
this.clipDiffFragments();
oldLink = text.oldText.tokens[j].link;
if ( this.config.timer === true ) {
this.timeEnd( 'clip' );
}
}
 
// Create html formatted diff code from diff fragments
// start of gap in new and old
if ( this.config.timer === true ) {
if ( (gap === null) && (newLink === null) && (oldLink === null) ) {
this.time( 'html' );
gap = gaps.length;
}
gaps.push({
this.getDiffHtml();
newFirst: i,
if ( this.config.timer === true ) {
newLast: i,
this.timeEnd( 'html' );
newTokens: 1,
oldFirst: j,
oldLast: j,
oldTokens: null,
charSplit: null
});
}
 
// No change
// count chars and tokens in gap
else if ( (gap !== null) && (newLinkthis.html === null)'' ) {
gaps[gap]this.newLasthtml = i;
this.config.htmlCode.containerStart +
gaps[gap].newTokens ++;
this.config.htmlCode.noChangeStart +
this.htmlEscape( this.config.msg['wiked-diff-empty'] ) +
this.config.htmlCode.noChangeEnd +
this.config.htmlCode.containerEnd;
}
 
// gapAdd endederror indicator
else if ( (gapthis.error !== null) && (newLink !== null)true ) {
this.html = this.config.htmlCode.errorStart + this.html + this.config.htmlCode.errorEnd;
gap = null;
}
 
// nextStop listtotal elementstimer
if (newLink !this.config.timer === true null) {
this.timeEnd( 'total' );
j = text.oldText.tokens[newLink].next;
}
i = text.newText.tokens[i].next;
}
 
return this.html;
// cycle trough gaps and add old text gap data
};
for (var gap = 0; gap < gaps.length; gap ++) {
 
// cycle trough old text tokens list
var j = gaps[gap].oldFirst;
while ( (j !== null) && (text.oldText.tokens[j] !== null) && (text.oldText.tokens[j].link === null) ) {
 
/**
// count old chars and tokens in gap
* Split tokens into chars in the following unresolved regions (gaps):
gaps[gap].oldLast = j;
* - One token became connected or separated by space or dash (or any token)
gaps[gap].oldTokens ++;
* - Same number of tokens in gap and strong similarity of all tokens:
* - Addition or deletion of flanking strings in tokens
* - Addition or deletion of internal string in tokens
* - Same length and at least 50 % identity
* - Same start or end, same text longer than different text
* Identical tokens including space separators will be linked,
* resulting in word-wise char-level diffs
*
* @param[in/out] WikEdDiffText newText, oldText Text object tokens list
*/
this.splitRefineChars = function () {
 
/** Find corresponding gaps. */
j = text.oldText.tokens[j].next;
}
}
 
// Cycle through new text tokens list
//
var gaps = [];
// select gaps of identical token number and strong similarity of all tokens
var gap = null;
//
var i = this.newText.first;
var j = this.oldText.first;
while ( i !== null ) {
 
// Get token links
for (var gap = 0; gap < gaps.length; gap ++) {
var charSplitnewLink = truethis.newText.tokens[i].link;
var oldLink = null;
if ( j !== null ) {
oldLink = this.oldText.tokens[j].link;
}
 
// notStart sameof gap lengthin new and old
if ( gap === null && newLink === null && oldLink === null ) {
if (gaps[gap].newTokens != gaps[gap].oldTokens) {
gap = gaps.length;
gaps.push( {
newFirst: i,
newLast: i,
newTokens: 1,
oldFirst: j,
oldLast: j,
oldTokens: null,
charSplit: null
} );
}
 
// Count chars and tokens in gap
// one word became separated by space, dash, or any string
else if ( (gaps[gap].newTokens !== 1)null && (gaps[gap].oldTokensnewLink === 3)null ) {
gaps[gap].newLast = i;
if (text.newText.tokens[ gaps[gap].newFirst ].token != text.oldText.tokens[ gaps[gap].oldFirst ].token + text.oldText.tokens[ gaps[gap].oldLast ].token ) {
gaps[gap].newTokens ++;
continue;
}
}
 
else if ( (gaps[gap].oldTokens == 1) && (gaps[gap].newTokens == 3) ) {
// Gap ended
if (text.oldText.tokens[ gaps[gap].oldFirst ].token != text.newText.tokens[ gaps[gap].newFirst ].token + text.newText.tokens[ gaps[gap].newLast ].token ) {
else if ( gap !== null && newLink !== null ) {
continue;
}gap = null;
}
 
else {
// Next list elements
continue;
if ( newLink !== null ) {
j = this.oldText.tokens[newLink].next;
}
i = this.newText.tokens[i].next;
}
 
// cycleCycle troughthrough newgaps textand tokensadd listold andtext setgap charSplitdata
var igapsLength = gaps[gap].newFirstlength;
for ( var jgap = gaps[0; gap].oldFirst < gapsLength; gap ++ ) {
while (i !== null) {
var newToken = text.newText.tokens[i].token;
var oldToken = text.oldText.tokens[j].token;
 
// getCycle shorterthrough andold longertext tokentokens list
var shorterTokenj = gaps[gap].oldFirst;
while (
var longerToken;
j !== null &&
if (newToken.length < oldToken.length) {
this.oldText.tokens[j] !== null &&
shorterToken = newToken;
this.oldText.tokens[j].link === null
longerToken = oldToken;
}) {
 
else {
// Count old chars and tokens in gap
shorterToken = oldToken;
longerTokengaps[gap].oldLast = newTokenj;
gaps[gap].oldTokens ++;
 
j = this.oldText.tokens[j].next;
}
}
 
/** Select gaps of identical token number and strong similarity of all tokens. */
// not same token length
if (newToken.length != oldToken.length) {
 
var gapsLength = gaps.length;
// test for addition or deletion of internal string in tokens
for ( var gap = 0; gap < gapsLength; gap ++ ) {
var charSplit = true;
 
// Not same gap length
// find number of identical chars from left
if ( gaps[gap].newTokens !== gaps[gap].oldTokens ) {
var left = 0;
 
while (left < shorterToken.length) {
// One word became separated by space, dash, or any string
if (newToken.charAt(left) != oldToken.charAt(left)) {
if ( gaps[gap].newTokens === 1 && gaps[gap].oldTokens === 3 ) {
break;
var token = this.newText.tokens[ gaps[gap].newFirst ].token;
var tokenFirst = this.oldText.tokens[ gaps[gap].oldFirst ].token;
var tokenLast = this.oldText.tokens[ gaps[gap].oldLast ].token;
if (
token.indexOf( tokenFirst ) !== 0 ||
token.indexOf( tokenLast ) !== token.length - tokenLast.length
) {
continue;
}
left ++;
}
else if ( gaps[gap].oldTokens === 1 && gaps[gap].newTokens === 3 ) {
 
var token = this.oldText.tokens[ gaps[gap].oldFirst ].token;
// find number of identical chars from right
var tokenFirst = this.newText.tokens[ gaps[gap].newFirst ].token;
var right = 0;
var tokenLast = this.newText.tokens[ gaps[gap].newLast ].token;
while (right < shorterToken.length) {
if (
if (newToken.charAt(newToken.length - 1 - right) != oldToken.charAt(oldToken.length - 1 - right)) {
token.indexOf( tokenFirst ) !== 0 ||
break;
token.indexOf( tokenLast ) !== token.length - tokenLast.length
) {
continue;
}
right ++;
}
else {
continue;
}
gaps[gap].charSplit = true;
}
 
// Cycle through new text tokens list and set charSplit
// no simple insertion or deletion of internal string
else {
if (left + right != shorterToken.length) {
var i = gaps[gap].newFirst;
var j = gaps[gap].oldFirst;
while ( i !== null ) {
var newToken = this.newText.tokens[i].token;
var oldToken = this.oldText.tokens[j].token;
 
// Get shorter and longer token
// not addition or deletion of flanking strings in tokens (smaller token not part of larger token)
ifvar (longerToken.indexOf(shorterToken) == -1) {;
var longerToken;
if ( newToken.length < oldToken.length ) {
shorterToken = newToken;
longerToken = oldToken;
}
else {
shorterToken = oldToken;
longerToken = newToken;
}
 
// Not same text at start or end shorter than differenttoken textlength
if ( (left < shorterTokennewToken.length /!== 2) && (right < shorterTokenoldToken.length / 2) ) {
 
// doTest notfor splitaddition intoor charsdeletion thisof gapinternal string in tokens
 
// Find number of identical chars from left
var left = 0;
while ( left < shorterToken.length ) {
if ( newToken.charAt( left ) !== oldToken.charAt( left ) ) {
break;
}
left ++;
}
 
// Find number of identical chars from right
var right = 0;
while ( right < shorterToken.length ) {
if (
newToken.charAt( newToken.length - 1 - right ) !==
oldToken.charAt( oldToken.length - 1 - right )
) {
break;
}
right ++;
}
 
// No simple insertion or deletion of internal string
if ( left + right !== shorterToken.length ) {
 
// Not addition or deletion of flanking strings in tokens
// Smaller token not part of larger token
if ( longerToken.indexOf( shorterToken ) === -1 ) {
 
// Same text at start or end shorter than different text
if ( left < shorterToken.length / 2 && (right < shorterToken.length / 2) ) {
 
// Do not split into chars in this gap
charSplit = false;
break;
}
}
}
}
 
// Same token length
else if ( newToken !== oldToken ) {
 
// Tokens less than 50 % identical
var ident = 0;
var tokenLength = shorterToken.length;
for ( var pos = 0; pos < tokenLength; pos ++ ) {
if ( shorterToken.charAt( pos ) === longerToken.charAt( pos ) ) {
ident ++;
}
}
if ( ident / shorterToken.length < 0.49 ) {
 
// Do not split into chars this gap
charSplit = false;
break;
}
}
}
}
 
// sameNext tokenlist lengthelements
else if (newToken !i === gaps[gap].newLast oldToken) {
break;
 
// tokens less than 50 % identical
var ident = 0;
for (var pos = 0; pos < shorterToken.length; pos ++) {
if (shorterToken.charAt(pos) == longerToken.charAt(pos)) {
ident ++;
}
i = this.newText.tokens[i].next;
j = this.oldText.tokens[j].next;
}
gaps[gap].charSplit = charSplit;
if (ident/shorterToken.length < 0.49) {
 
// do not split into chars this gap
charSplit = false;
break;
}
}
 
// next list elements
if (i == gaps[gap].newLast) {
break;
}
i = text.newText.tokens[i].next;
j = text.oldText.tokens[j].next;
}
gaps[gap].charSplit = charSplit;
}
 
/** Refine words into chars in selected gaps. */
//
// refine words into chars in selected gaps
//
 
for ( var gapgapsLength = 0; gap < gaps.length; gap ++) {
iffor (gaps[ var gap].charSplit === true0; gap < gapsLength; gap ++ ) {
if ( gaps[gap].charSplit === true ) {
 
// cycleCycle troughthrough new text tokens list, link spaces, and split into chars
var i = gaps[gap].newFirst;
var j = gaps[gap].oldFirst;
var newGapLength = i - gaps[gap].newLast;
while (i !== null) {
var newTokenoldGapLength = text.newText.tokensj - gaps[igap].tokenoldLast;
while ( i !== null || j !== null ) {
var oldToken = text.oldText.tokens[j].token;
 
// linkLink identical tokens (spaces) to keep char refinement to words
if (newToken == oldToken) {
newGapLength === oldGapLength &&
text.newText.tokens[i].link = j;
text this.oldTextnewText.tokens[ji].linktoken === i;this.oldText.tokens[j].token
} ) {
this.newText.tokens[i].link = j;
this.oldText.tokens[j].link = i;
}
 
// refine differentRefine words into chars
else {
if ( i !== null ) {
wDiff.Split(text.newText, 'character', i);
wDiff this.Split(textnewText.oldText,splitText( 'character', ji );
}
if ( j !== null ) {
this.oldText.splitText( 'character', j );
}
}
 
// nextNext list elements
if ( i === gaps[gap].newLast ) {
break i = null;
}
if ( j === gaps[gap].oldLast ) {
j = null;
}
if ( i !== null ) {
i = this.newText.tokens[i].next;
}
if ( j !== null ) {
j = this.oldText.tokens[j].next;
}
}
i = text.newText.tokens[i].next;
j = text.oldText.tokens[j].next;
}
}
return;
}
};
 
// WED('Gap', wDiff.DebugGaps(gaps));
 
/**
return;
* Move gaps with ambiguous identical fronts to last newline border or otherwise last word border.
};
*
* @param[in/out] wikEdDiffText text, textLinked These two are newText and oldText
*/
this.slideGaps = function ( text, textLinked ) {
 
var regExpSlideBorder = this.config.regExp.slideBorder;
var regExpSlideStop = this.config.regExp.slideStop;
 
// Cycle through tokens list
// wDiff.SlideGaps: move gaps with ambiguous identical fronts to last newline or, if absent, last word border
var i = text.first;
// changes: text (text.newText or text.oldText) .tokens list
var gapStart = null;
// called from: wDiff.Diff()
while ( i !== null ) {
 
// Remember gap start
wDiff.SlideGaps = function (text, textLinked) {
if ( gapStart === null && text.tokens[i].link === null ) {
gapStart = i;
}
 
// Find gap end
// cycle through tokens list
else if ( gapStart !== null && text.tokens[i].link !== null ) {
var i = text.first;
var gapStartgapFront = nullgapStart;
while var (gapBack (i !== null) && (text.tokens[i] !== null) ) {.prev;
 
// Slide down as deep as possible
// remember gap start
var front = gapFront;
if ( (gapStart === null) && (text.tokens[i].link === null) ) {
var back = text.tokens[gapBack].next;
gapStart = i;
} if (
front !== null &&
back !== null &&
text.tokens[front].link === null &&
text.tokens[back].link !== null &&
text.tokens[front].token === text.tokens[back].token
) {
text.tokens[front].link = text.tokens[back].link;
textLinked.tokens[ text.tokens[front].link ].link = front;
text.tokens[back].link = null;
 
gapFront = text.tokens[gapFront].next;
// find gap end
else gapBack if ( (gapStart !== null) && (text.tokens[igapBack].link !== null) ) {next;
 
front = text.tokens[front].next;
// slide down as deep as possible
back = text.tokens[back].next;
var front = gapStart;
}
var back = i;
var frontTest = null;
var backTest = null;
while (
(front !== null) && (back !== null) &&
(text.tokens[front].link === null) && (text.tokens[back].link !== null) &&
(text.tokens[front].token === text.tokens[back].token)
) {
text.tokens[front].link = text.tokens[back].link;
textLinked.tokens[ text.tokens[front].link ].link = front;
text.tokens[back].link = null;
frontTest = front;
backTest = back;
front = text.tokens[front].next;
back = text.tokens[back].next;
}
 
// testTest slide up, remember last line break or word border
var frontStopfront = nulltext.tokens[gapFront].prev;
var back = gapBack;
while (
var gapFrontBlankTest = regExpSlideBorder.test( text.tokens[gapFront].token );
(frontTest !== null) && (backTest !== null) &&
var frontStop = front;
(text.tokens[frontTest].link !== null) && (text.tokens[backTest].link === null) &&
if ( text.tokens[frontTestback].tokenlink === text.tokens[backTest].tokennull ) {
) while {(
front !== null &&
if (wDiff.regExpSlideStop.test(text.tokens[frontTest].token) === true) {
frontStop back !== null frontTest;&&
text.tokens[front].link !== null &&
break;
text.tokens[front].token === text.tokens[back].token
) {
if ( front !== null ) {
 
// Stop at line break
if ( regExpSlideStop.test( text.tokens[front].token ) === true ) {
frontStop = front;
break;
}
 
// Stop at first word border (blank/word or word/blank)
if (
regExpSlideBorder.test( text.tokens[front].token ) !== gapFrontBlankTest ) {
frontStop = front;
}
}
front = text.tokens[front].prev;
back = text.tokens[back].prev;
}
}
else if ( (frontStop === null) && (wDiff.regExpSlideBorder.test(text.tokens[frontTest].token) === true) ) {
frontStop = frontTest;
}
frontTest = text.tokens[frontTest].prev;
backTest = text.tokens[backTest].prev;
}
 
// actuallyActually slide up to line break or, if absent, word borderstop
var front = text.tokens[gapFront].prev;
if (frontStop !== null) {
var back = gapBack;
while (
(front !== null) && (
back !== null) && (
front !== frontStop) &&
(text.tokens[front].link !== null) && (
text.tokens[back].link === null) &&
(text.tokens[front].token === text.tokens[back].token)
) {
text.tokens[back].link = text.tokens[front].link;
textLinked.tokens[ text.tokens[back].link ].link = back;
text.tokens[front].link = null;
 
front = text.tokens[front].prev;
back = text.tokens[back].prev;
}
gapStart = null;
}
gapStarti = nulltext.tokens[i].next;
}
i = text.tokens[i].next;
}
return;
};
 
 
// wDiff.EnumerateTokens: enumerate text token list
// changes: text (text.newText or text.oldText) .tokens list
// called from: wDiff.Diff()
 
wDiff.EnumerateTokens = function (text) {
 
// enumerate tokens list
var number = 0;
var i = text.first;
while ( (i !== null) && (text.tokens[i] !== null) ) {
text.tokens[i].number = number;
number ++;
i = text.tokens[i].next;
}
return;
};
 
 
// wDiff.CalculateDiff: calculate diff information, can be called repeatedly during refining
// input: text: object containing text data and tokens; level: 'paragraph', 'sentence', 'word', or 'character'
// optionally for recursive calls: newStart, newEnd, oldStart, oldEnd (tokens list indexes), recursionLevel
// changes: text.oldText/newText.tokens[].link, links corresponding tokens from old and new text
// steps:
// pass 1: parse new text into symbol table
// pass 2: parse old text into symbol table
// pass 3: connect unique matched tokens
// pass 4: connect adjacent identical tokens downwards
// pass 5: connect adjacent identical tokens upwards
// recursively diff still unresolved regions downwards
// recursively diff still unresolved regions upwards
 
wDiff.CalculateDiff = function (text, symbols, level, recurse, newStart, newEnd, oldStart, oldEnd, recursionLevel) {
 
// if (recursionLevel === undefined) { wikEd.debugTimer.push([level + '?', new Date]); }
 
// set defaults
if (newStart === undefined) { newStart = text.newText.first; }
if (newEnd === undefined) { newEnd = text.newText.last; }
if (oldStart === undefined) { oldStart = text.oldText.first; }
if (oldEnd === undefined) { oldEnd = text.oldText.last; }
if (recursionLevel === undefined) { recursionLevel = 0; }
 
// limit recursion depth
if (recursionLevel > 10) {
return;
};
 
//
// pass 1: parse new text into symbol table
//
 
/**
// cycle trough new text tokens list
* Calculate diff information, can be called repeatedly during refining.
var i = newStart;
* Links corresponding tokens from old and new text.
while ( (i !== null) && (text.newText.tokens[i] !== null) ) {
* Steps:
* Pass 1: parse new text into symbol table
* Pass 2: parse old text into symbol table
* Pass 3: connect unique matching tokens
* Pass 4: connect adjacent identical tokens downwards
* Pass 5: connect adjacent identical tokens upwards
* Repeat with empty symbol table (against crossed-over gaps)
* Recursively diff still unresolved regions downwards with empty symbol table
* Recursively diff still unresolved regions upwards with empty symbol table
*
* @param array symbols Symbol table object
* @param string level Split level: 'paragraph', 'line', 'sentence', 'chunk', 'word', 'character'
*
* Optionally for recursive or repeated calls:
* @param bool repeating Currently repeating with empty symbol table
* @param bool recurse Enable recursion
* @param int newStart, newEnd, oldStart, oldEnd Text object tokens indices
* @param int recursionLevel Recursion level
* @param[in/out] WikEdDiffText newText, oldText Text object, tokens list link property
*/
this.calculateDiff = function (
level,
recurse,
repeating,
newStart,
oldStart,
up,
recursionLevel
) {
 
// Set defaults
// add new entry to symbol table
if ( repeating === undefined ) { repeating = false; }
var token = text.newText.tokens[i].token;
if ( recurse === undefined ) { recurse = false; }
if (Object.prototype.hasOwnProperty.call(symbols.hash, token) === false) {
if ( newStart === undefined ) { newStart = this.newText.first; }
var current = symbols.token.length;
if ( oldStart === undefined ) { oldStart = this.oldText.first; }
symbols.hash[token] = current;
if ( up === undefined ) { up = false; }
symbols.token[current] = {
if ( recursionLevel === undefined ) { recursionLevel = 0; }
newCount: 1,
oldCount: 0,
newToken: i,
oldToken: null
};
}
 
// Start timers
// or update existing entry
if ( this.config.timer === true && repeating === false && recursionLevel === 0 ) {
else {
this.time( level );
 
// increment token counter for new text
var hashToArray = symbols.hash[token];
symbols.token[hashToArray].newCount ++;
}
if ( this.config.timer === true && repeating === false ) {
 
this.time( level + recursionLevel );
// next list element
if (i == newEnd) {
break;
}
i = text.newText.tokens[i].next;
}
 
// Get object symbols table and linked region borders
//
var symbols;
// pass 2: parse old text into symbol table
var bordersDown;
//
var bordersUp;
 
if ( recursionLevel === 0 && repeating === false ) {
// cycle trough old text tokens list
symbols = this.symbols;
var j = oldStart;
bordersDown = this.bordersDown;
while ( (j !== null) && (text.oldText.tokens[j] !== null) ) {
bordersUp = this.bordersUp;
 
// add new entry to symbol table
var token = text.oldText.tokens[j].token;
if (Object.prototype.hasOwnProperty.call(symbols.hash, token) === false) {
var current = symbols.token.length;
symbols.hash[token] = current;
symbols.token[current] = {
newCount: 0,
oldCount: 1,
newToken: null,
oldToken: j
};
}
 
// Create empty local symbols table and linked region borders arrays
// or update existing entry
else {
symbols = {
 
// increment token: counter for old text[],
hashTable: {},
var hashToArray = symbols.hash[token];
linked: false
symbols.token[hashToArray].oldCount ++;
};
 
bordersDown = [];
// add token number for old text
bordersUp = [];
symbols.token[hashToArray].oldToken = j;
}
 
// next list element
if (j === oldEnd) {
break;
}
j = text.oldText.tokens[j].next;
}
 
// Updated versions of linked region borders
//
var bordersUpNext = [];
// pass 3: connect unique tokens
var bordersDownNext = [];
//
 
/**
// cycle trough symbol array
* Pass 1: parse new text into symbol table.
for (var i = 0; i < symbols.token.length; i ++) {
*/
 
// Cycle through new text tokens list
// find tokens in the symbol table that occur only once in both versions
var i = newStart;
if ( (symbols.token[i].newCount == 1) && (symbols.token[i].oldCount == 1) ) {
while ( i !== null ) {
var newToken = symbols.token[i].newToken;
if ( this.newText.tokens[i].link === null ) {
var oldToken = symbols.token[i].oldToken;
 
// doAdd notnew useentry spacesto assymbol unique markerstable
if var (/^\s+$/.test(texttoken = this.newText.tokens[newTokeni].token) === false) {;
if ( Object.prototype.hasOwnProperty.call( symbols.hashTable, token ) === false ) {
symbols.hashTable[token] = symbols.token.length;
symbols.token.push( {
newCount: 1,
oldCount: 0,
newToken: i,
oldToken: null
} );
}
 
// Or update existing entry
// connect from new to old and from old to new
else {
if (text.newText.tokens[newToken].link === null) {
text.newText.tokens[newToken].link = oldToken;
text.oldText.tokens[oldToken].link = newToken;
symbols.linked = true;
 
// checkIncrement iftoken uniquecounter wordfor new text
var hashToArray = symbols.hashTable[token];
if ( (level == 'word') && (recursionLevel === 0) ) {
var symbols.token = text.newText.tokens[newTokenhashToArray].tokennewCount ++;
if ( (text.oldText.words[token] == 1) && (text.newText.words[token] == 1) ) {
text.newText.tokens[newToken].unique = true;
text.oldText.tokens[oldToken].unique = true;
}
}
}
}
}
}
 
// Stop after gap if recursing
// continue only if unique tokens have been linked previously
else if ( recursionLevel > 0 ) {
if (symbols.linked === true) {
break;
 
//
// pass 4: connect adjacent identical tokens downwards
//
 
// get surrounding connected tokens
var i = newStart;
if (text.newText.tokens[i].prev !== null) {
i = text.newText.tokens[i].prev;
}
var iStop = newEnd;
if (text.newText.tokens[iStop].next !== null) {
iStop = text.newText.tokens[iStop].next;
}
var j = null;
 
// cycle trough new text tokens list down
do {
 
// connected pair
var link = text.newText.tokens[i].link;
if (link !== null) {
j = text.oldText.tokens[link].next;
}
 
// connectGet ifnext tokens are the sametoken
if ( up === false ) {
else if ( (j !== null) && (text.oldText.tokens[j].link === null) && (text.newText.tokens[i].token == text.oldText.tokens[j].token) ) {
texti = this.newText.tokens[i].link = jnext;
text.oldText.tokens[j].link = i;
j = text.oldText.tokens[j].next;
}
 
// not same
else {
ji = nullthis.newText.tokens[i].prev;
}
}
i = text.newText.tokens[i].next;
} while (i !== iStop);
 
//**
* Pass 2: parse old text into symbol table.
// pass 5: connect adjacent identical tokens upwards
/ */
 
// getCycle surroundingthrough connectedold text tokens list
var ij = newEndoldStart;
ifwhile (text.newText.tokens[i].next j !== null ) {
iif =( textthis.newTextoldText.tokens[ij].next;link === null ) {
}
var iStop = newStart;
if (text.newText.tokens[iStop].prev !== null) {
iStop = text.newText.tokens[iStop].prev;
}
var j = null;
 
// cycle troughAdd new textentry tokensto listsymbol uptable
var token = this.oldText.tokens[j].token;
do {
if ( Object.prototype.hasOwnProperty.call( symbols.hashTable, token ) === false ) {
symbols.hashTable[token] = symbols.token.length;
symbols.token.push( {
newCount: 0,
oldCount: 1,
newToken: null,
oldToken: j
} );
}
 
// Or update existing entry
// connected pair
else {
var link = text.newText.tokens[i].link;
 
if (link !== null) {
// Increment token counter for old text
j = text.oldText.tokens[link].prev;
var hashToArray = symbols.hashTable[token];
symbols.token[hashToArray].oldCount ++;
 
// Add token number for old text
symbols.token[hashToArray].oldToken = j;
}
}
 
// connectStop ifafter tokensgap are theif samerecursing
else if ( recursionLevel > 0 ) {
else if ( (j !== null) && (text.oldText.tokens[j].link === null) && (text.newText.tokens[i].token == text.oldText.tokens[j].token) ) {
break;
text.newText.tokens[i].link = j;
text.oldText.tokens[j].link = i;
j = text.oldText.tokens[j].prev;
}
 
// notGet samenext token
if ( up === false ) {
j = this.oldText.tokens[j].next;
}
else {
j = nullthis.oldText.tokens[j].prev;
}
}
i = text.newText.tokens[i].prev;
} while (i !== iStop);
 
//**
* Pass 3: connect unique tokens.
// connect adjacent identical tokens downwards from text start, treat boundary as connected, stop after first connected token
/ */
 
// onlyCycle forthrough fullsymbol text diffarray
var symbolsLength = symbols.token.length;
if ( (newStart == text.newText.first) && (newEnd == text.newText.last) ) {
for ( var i = 0; i < symbolsLength; i ++ ) {
 
// Find tokens in the symbol table that occur only once in both versions
// from start
if ( symbols.token[i].newCount === 1 && symbols.token[i].oldCount === 1 ) {
var i = text.newText.first;
var jnewToken = textsymbols.oldTexttoken[i].firstnewToken;
var oldToken = symbols.token[i].oldToken;
var newTokenObj = this.newText.tokens[newToken];
var oldTokenObj = this.oldText.tokens[oldToken];
 
// Connect from new to old and from old to new
// cycle trough new text tokens list down, connect identical tokens, stop after first connected token
if ( newTokenObj.link === null ) {
while ( (i !== null) && (j !== null) && (text.newText.tokens[i].link === null) && (text.oldText.tokens[j].link === null) && (text.newText.tokens[i].token == text.oldText.tokens[j].token) ) {
text.newText.tokens[i].link = j;
text.oldText.tokens[j].link = i;
j = text.oldText.tokens[j].next;
i = text.newText.tokens[i].next;
}
 
// Do not use spaces as unique markers
// from end
if (
var i = text.newText.last;
this.config.regExp.blankOnlyToken.test( newTokenObj.token ) === true
var j = text.oldText.last;
) {
 
// Link new and old tokens
// cycle trough old text tokens list up, connect identical tokens, stop after first connected token
newTokenObj.link = oldToken;
while ( (i !== null) && (j !== null) && (text.newText.tokens[i].link === null) && (text.oldText.tokens[j].link === null) && (text.newText.tokens[i].token == text.oldText.tokens[j].token) ) {
text.newText.tokens[i] oldTokenObj.link = jnewToken;
text symbols.oldText.tokens[j].linklinked = itrue;
j = text.oldText.tokens[j].prev;
i = text.newText.tokens[i].prev;
}
}
 
// Save linked region borders
//
bordersDown.push( [newToken, oldToken] );
// refine by recursively diffing unresolved regions caused by addition of common tokens around sequences of common tokens, only at word level split
bordersUp.push( [newToken, oldToken] );
//
 
// Check if token contains unique word
if ( (recurse === true) && (wDiff.recursiveDiff === true) ) {
if ( recursionLevel === 0 ) {
var unique = false;
if ( level === 'character' ) {
unique = true;
}
else {
var token = newTokenObj.token;
var words =
( token.match( this.config.regExp.countWords ) || [] ).concat(
( token.match( this.config.regExp.countChunks ) || [] )
);
 
// Unique if longer than min block length
//
var wordsLength = words.length;
// recursively diff still unresolved regions downwards
if ( wordsLength >= this.config.blockMinLength ) {
//
unique = true;
}
 
// Unique if it contains at least one unique word
// cycle trough new text tokens list
else {
var i = newStart;
for ( var i = 0;i < wordsLength; i ++ ) {
var j = oldStart;
var word = words[i];
if (
this.oldText.words[word] === 1 &&
this.newText.words[word] === 1 &&
Object.prototype.hasOwnProperty.call( this.oldText.words, word ) === true &&
Object.prototype.hasOwnProperty.call( this.newText.words, word ) === true
) {
unique = true;
break;
}
}
}
}
 
// Set unique
while ( (i !== null) && (text.newText.tokens[i] !== null) ) {
if ( unique === true ) {
 
newTokenObj.unique = true;
// get j from previous tokens match
oldTokenObj.unique = true;
var iPrev = text.newText.tokens[i].prev;
}
if (iPrev !== null) {
}
var jPrev = text.newText.tokens[iPrev].link;
if (jPrev !== null) {
j = text.oldText.tokens[jPrev].next;
}
}
}
}
 
// Continue passes only if unique tokens have been linked previously
// check for the start of an unresolved sequence
if ( symbols.linked === true ) {
if ( (j !== null) && (text.oldText.tokens[j] !== null) && (text.newText.tokens[i].link === null) && (text.oldText.tokens[j].link === null) ) {
 
/**
// determine the limits of the unresolved new sequence
* Pass 4: connect adjacent identical tokens downwards.
var iStart = i;
*/
var iEnd = null;
var iLength = 0;
var iNext = i;
while ( (iNext !== null) && (text.newText.tokens[iNext].link === null) ) {
iEnd = iNext;
iLength ++;
if (iEnd == newEnd) {
break;
}
iNext = text.newText.tokens[iNext].next;
}
 
// determineCycle thethrough limitslist of thelinked unresolvednew oldtext sequencetokens
var jStartbordersLength = jbordersDown.length;
for ( var match = 0; match < bordersLength; match ++ ) {
var jEnd = null;
var jLengthi = bordersDown[match][0];
var jNextj = jbordersDown[match][1];
while ( (jNext !== null) && (text.oldText.tokens[jNext].link === null) ) {
jEnd = jNext;
jLength ++;
if (jEnd == oldEnd) {
break;
}
jNext = text.oldText.tokens[jNext].next;
}
 
// Next down
// recursively diff the unresolved sequence
var iMatch = i;
if ( (iLength > 1) || (jLength > 1) ) {
var jMatch = j;
i = this.newText.tokens[i].next;
j = this.oldText.tokens[j].next;
 
// Cycle through new symbolstext objectlist forgap sub-region downwards
while (
var symbolsRecurse = {
token:i !== [],null &&
hash:j !== null {},&&
this.newText.tokens[i].link === null &&
linked: false
this.oldText.tokens[j].link === null
};
) {
wDiff.CalculateDiff(text, symbolsRecurse, level, true, iStart, iEnd, jStart, jEnd, recursionLevel + 1);
 
// Connect if same token
if ( this.newText.tokens[i].token === this.oldText.tokens[j].token ) {
this.newText.tokens[i].link = j;
this.oldText.tokens[j].link = i;
}
i = iEnd;
}
 
// Not a match yet, maybe in next listrefinement elementlevel
if (i == newEnd) else {
bordersDownNext.push( [iMatch, jMatch] );
break;
break;
}
 
// Next token down
iMatch = i;
jMatch = j;
i = this.newText.tokens[i].next;
j = this.oldText.tokens[j].next;
}
i = text.newText.tokens[i].next;
}
 
//**
// recursively* diffPass still5: unresolvedconnect regionsadjacent identical tokens upwards.
/ */
 
// cycleCycle troughthrough list of connected new text tokens list
var ibordersLength = newEndbordersUp.length;
for ( var match = 0; match < bordersLength; match ++ ) {
var j = oldEnd;
var i = bordersUp[match][0];
while ( (i !== null) && (text.newText.tokens[i] !== null) ) {
var j = bordersUp[match][1];
 
// getNext j from next matched tokensup
var iPreviMatch = text.newText.tokens[i].next;
ifvar (iPrevjMatch !== null) {j;
var jPrevi = textthis.newText.tokens[iPrevi].linkprev;
j = this.oldText.tokens[j].prev;
if (jPrev !== null) {
j = text.oldText.tokens[jPrev].prev;
}
}
 
// checkCycle forthrough thenew starttext ofgap anregion unresolved sequenceupwards
while (
if ( (j !== null) && (text.oldText.tokens[j] !== null) && (text.newText.tokens[i].link === null) && (text.oldText.tokens[j].link === null) ) {
i !== null &&
j !== null &&
this.newText.tokens[i].link === null &&
this.oldText.tokens[j].link === null
) {
 
// Connect if same token
// determine the limits of the unresolved new sequence
if ( this.newText.tokens[i].token === this.oldText.tokens[j].token ) {
var iStart = null;
var iEnd this.newText.tokens[i].link = ij;
var iLength this.oldText.tokens[j].link = 0i;
var iNext = i;
while ( (iNext !== null) && (text.newText.tokens[iNext].link === null) ) {
iStart = iNext;
iLength ++;
if (iStart == newStart) {
break;
}
iNext = text.newText.tokens[iNext].prev;
}
 
// determineNot thea limitsmatch ofyet, themaybe unresolvedin oldnext sequencerefinement level
varelse jStart = null;{
bordersUpNext.push( [iMatch, jMatch] );
var jEnd = j;
var jLength = 0 break;
var jNext = j;
while ( (jNext !== null) && (text.oldText.tokens[jNext].link === null) ) {
jStart = jNext;
jLength ++;
if (jStart == oldStart) {
break;
}
jNext = text.oldText.tokens[jNext].prev;
}
 
// recursivelyNext difftoken the unresolved sequenceup
iMatch = i;
if ( (iLength > 1) || (jLength > 1) ) {
jMatch = j;
i = this.newText.tokens[i].prev;
j = this.oldText.tokens[j].prev;
}
}
 
/**
// new symbols object for sub-region
* Connect adjacent identical tokens downwards from text start.
var symbolsRecurse = {
* Treat boundary as connected, stop after first connected token.
token: [],
hash: {},*/
 
linked: false
// Only for full text diff
};
if ( recursionLevel === 0 && repeating === false ) {
wDiff.CalculateDiff(text, symbolsRecurse, level, true, iStart, iEnd, jStart, jEnd, recursionLevel + 1);
 
}
i// =From iStart;start
var i = this.newText.first;
var j = this.oldText.first;
var iMatch = null;
var jMatch = null;
 
// Cycle through old text tokens down
// Connect identical tokens, stop after first connected token
while (
i !== null &&
j !== null &&
this.newText.tokens[i].link === null &&
this.oldText.tokens[j].link === null &&
this.newText.tokens[i].token === this.oldText.tokens[j].token
) {
this.newText.tokens[i].link = j;
this.oldText.tokens[j].link = i;
iMatch = i;
jMatch = j;
i = this.newText.tokens[i].next;
j = this.oldText.tokens[j].next;
}
if ( iMatch !== null ) {
bordersDownNext.push( [iMatch, jMatch] );
}
 
// nextFrom list elementend
i = this.newText.last;
if (i == newStart) {
j = this.oldText.last;
break;
iMatch = null;
jMatch = null;
 
// Cycle through old text tokens up
// Connect identical tokens, stop after first connected token
while (
i !== null &&
j !== null &&
this.newText.tokens[i].link === null &&
this.oldText.tokens[j].link === null &&
this.newText.tokens[i].token === this.oldText.tokens[j].token
) {
this.newText.tokens[i].link = j;
this.oldText.tokens[j].link = i;
iMatch = i;
jMatch = j;
i = this.newText.tokens[i].prev;
j = this.oldText.tokens[j].prev;
}
if ( iMatch !== null ) {
bordersUpNext.push( [iMatch, jMatch] );
}
i = text.newText.tokens[i].prev;
}
}
}
 
// Save updated linked region borders to object
// if (recursionLevel === 0) { wikEd.debugTimer.push([level + '=', new Date]); }
if ( recursionLevel === 0 && repeating === false ) {
this.bordersDown = bordersDownNext;
this.bordersUp = bordersUpNext;
}
 
// Merge local updated linked region borders into object
return;
else {
};
this.bordersDown = this.bordersDown.concat( bordersDownNext );
this.bordersUp = this.bordersUp.concat( bordersUpNext );
}
 
 
/**
// wDiff.DetectBlocks: extract block data for inserted, deleted, or moved blocks from diff data in text object
* Repeat once with empty symbol table to link hidden unresolved common tokens in cross-overs.
// input:
* ("and" in "and this a and b that" -> "and this a and b that")
// text: object containing text tokens list
*/
// blocks: empty array for block data
// groups: empty array for group data
// changes: text, blocks, groups
// called from: wDiff.Diff()
// scheme of blocks, sections, and groups (old block numbers):
// old: 1 2 3D4 5E6 7 8 9 10 11
// | ‾/-/_ X | >|< |
// new: 1 I 3D4 2 E6 5 N 7 10 9 8 11
// section: 0 0 0 1 1 2 2 2
// group: 0 10 111 2 33 4 11 5 6 7 8 9
// fixed: + +++ - ++ - + + - - +
// type: = + =-= = -= = + = = = = =
 
if ( repeating === false && this.config.repeatedDiff === true ) {
wDiff.DetectBlocks = function (text, blocks, groups) {
var repeat = true;
this.calculateDiff( level, recurse, repeat, newStart, oldStart, up, recursionLevel );
}
 
/**
// WED('text.oldText', wDiff.DebugText(text.oldText));
* Refine by recursively diffing not linked regions with new symbol table.
// WED('text.newText', wDiff.DebugText(text.newText));
* At word and character level only.
* Helps against gaps caused by addition of common tokens around sequences of common tokens.
*/
 
if (
// collect identical corresponding ('same') blocks from old text and sort by new text
recurse === true &&
wDiff.GetSameBlocks(text, blocks);
this.config['recursiveDiff'] === true &&
recursionLevel < this.config.recursionMax
) {
 
/**
// collect independent block sections (no old/new crosses outside section) for per-section determination of non-moving (fixed) groups
* Recursively diff gap downwards.
var sections = [];
*/
wDiff.GetSections(blocks, sections);
 
// Cycle through list of linked region borders
// find groups of continuous old text blocks
var bordersLength = bordersDownNext.length;
wDiff.GetGroups(blocks, groups);
for ( match = 0; match < bordersLength; match ++ ) {
var i = bordersDownNext[match][0];
var j = bordersDownNext[match][1];
 
// Next token down
// set longest sequence of increasing groups in sections as fixed (not moved)
i = this.newText.tokens[i].next;
wDiff.SetFixed(blocks, groups, sections);
j = this.oldText.tokens[j].next;
 
// Start recursion at first gap token pair
// collect deletion ('del') blocks from old text
if (
wDiff.GetDelBlocks(text, blocks);
i !== null &&
j !== null &&
this.newText.tokens[i].link === null &&
this.oldText.tokens[j].link === null
) {
var repeat = false;
var dirUp = false;
this.calculateDiff( level, recurse, repeat, i, j, dirUp, recursionLevel + 1 );
}
}
 
/**
// position 'del' blocks into new text order
* Recursively diff gap upwards.
wDiff.PositionDelBlocks(blocks);
*/
 
// Cycle through list of linked region borders
// sort blocks by new text token number and update groups
var bordersLength = bordersUpNext.length;
wDiff.SortBlocks(blocks, groups);
for ( match = 0; match < bordersLength; match ++ ) {
var i = bordersUpNext[match][0];
var j = bordersUpNext[match][1];
 
// Next token up
// convert groups to insertions/deletions if maximal block length is too short
i = this.newText.tokens[i].prev;
if (wDiff.blockMinLength > 0) {
j = this.oldText.tokens[j].prev;
var unlinked = wDiff.UnlinkBlocks(text, blocks, groups);
 
// Start recursion at first gap token pair
// repeat from start after conversion
if (unlinked === true) {
i !== null &&
wDiff.SlideGaps(text.newText, text.oldText);
j !== null &&
wDiff.SlideGaps(text.oldText, text.newText);
this.newText.tokens[i].link === null &&
this.oldText.tokens[j].link === null
) {
var repeat = false;
var dirUp = true;
this.calculateDiff( level, recurse, repeat, i, j, dirUp, recursionLevel + 1 );
}
}
}
}
 
// Stop timers
// repeat block detection from start
if ( this.config.timer === true && repeating === false ) {
wDiff.GetSameBlocks(text, blocks);
if ( this.recursionTimer[recursionLevel] === undefined ) {
wDiff.GetSections(blocks, sections);
this.recursionTimer[recursionLevel] = 0;
wDiff.GetGroups(blocks, groups);
}
wDiff.SetFixed(blocks, groups, sections);
this.recursionTimer[recursionLevel] += this.timeEnd( level + recursionLevel, true );
wDiff.GetDelBlocks(text, blocks);
}
wDiff.PositionDelBlocks(blocks);
if ( this.config.timer === true && repeating === false && recursionLevel === 0 ) {
this.timeRecursionEnd( level );
this.timeEnd( level );
}
}
 
return;
// collect insertion ('ins') blocks from new text
};
wDiff.GetInsBlocks(text, blocks);
 
// sort blocks by new text token number and update groups
wDiff.SortBlocks(blocks, groups);
 
/**
// set group numbers of 'ins' and 'del' blocks
* Main method for processing raw diff data, extracting deleted, inserted, and moved blocks.
wDiff.SetInsDelGroups(blocks, groups);
*
* Scheme of blocks, sections, and groups (old block numbers):
* Old: 1 2 3D4 5E6 7 8 9 10 11
* | ‾/-/_ X | >|< |
* New: 1 I 3D4 2 E6 5 N 7 10 9 8 11
* Section: 0 0 0 1 1 2 2 2
* Group: 0 10 111 2 33 4 11 5 6 7 8 9
* Fixed: . +++ - ++ - . . - - +
* Type: = . =-= = -= = . = = = = =
*
* @param[out] array groups Groups table object
* @param[out] array blocks Blocks table object
* @param[in/out] WikEdDiffText newText, oldText Text object tokens list
*/
this.detectBlocks = function () {
 
// Debug log
// mark original positions of moved groups
if ( this.config.debug === true ) {
wDiff.MarkMoved(groups);
this.oldText.debugText( 'Old text' );
this.newText.debugText( 'New text' );
}
 
// Collect identical corresponding ('=') blocks from old text and sort by new text
// set moved block colors
this.getSameBlocks();
wDiff.ColorMoved(groups);
 
// Collect independent block sections with no block move crosses outside a section
// WED('Groups', wDiff.DebugGroups(groups));
this.getSections();
// WED('Blocks', wDiff.DebugBlocks(blocks));
 
// Find groups of continuous old text blocks
return;
this.getGroups();
};
 
// Set longest sequence of increasing groups in sections as fixed (not moved)
this.setFixed();
 
// Convert groups to insertions/deletions if maximum block length is too short
// wDiff.GetSameBlocks: collect identical corresponding ('same') blocks from old text and sort by new text
// Only for more complex texts that actually have blocks of minimum block length
// called from: DetectBlocks()
var unlinkCount = 0;
// changes: creates blocks
if (
this.config.unlinkBlocks === true &&
this.config.blockMinLength > 0 &&
this.maxWords >= this.config.blockMinLength
) {
if ( this.config.timer === true ) {
this.time( 'total unlinking' );
}
 
// Repeat as long as unlinking is possible
wDiff.GetSameBlocks = function (text, blocks) {
var unlinked = true;
while ( unlinked === true && unlinkCount < this.config.unlinkMax ) {
 
// Convert '=' to '+'/'-' pairs
// clear blocks array
unlinked = this.unlinkBlocks();
blocks.splice(0);
 
// Start over after conversion
// cycle through old text to find matched (linked) blocks
if ( unlinked === true ) {
var j = text.oldText.first;
unlinkCount ++;
var i = null;
this.slideGaps( this.newText, this.oldText );
while (j !== null) {
this.slideGaps( this.oldText, this.newText );
 
// Repeat block detection from start
// skip 'del' blocks
this.maxWords = 0;
while ( (j !== null) && (text.oldText.tokens[j].link === null) ) {
this.getSameBlocks();
j = text.oldText.tokens[j].next;
this.getSections();
}
this.getGroups();
 
this.setFixed();
// get 'same' block
if (j !== null) {
i = text.oldText.tokens[j].link;
var iStart = i;
var jStart = j;
 
// detect matching blocks ('same')
var count = 0;
var unique = false;
var chars = 0;
var string = '';
while ( (i !== null) && (j !== null) && (text.oldText.tokens[j].link == i) ) {
var token = text.oldText.tokens[j].token;
count ++;
if (text.newText.tokens[i].unique === true) {
unique = true;
}
chars += token.length;
string += token;
i = text.newText.tokens[i].next;
j = text.oldText.tokens[j].next;
}
if ( this.config.timer === true ) {
 
this.timeEnd( 'total unlinking' );
// save old text 'same' block
}
blocks.push({
oldBlock: blocks.length,
newBlock: null,
oldNumber: text.oldText.tokens[jStart].number,
newNumber: text.newText.tokens[iStart].number,
oldStart: jStart,
count: count,
unique: unique,
words: wDiff.WordCount(string),
chars: chars,
type: 'same',
section: null,
group: null,
fixed: null,
string: string
});
}
}
 
// Collect deletion ('-') blocks from old text
// sort blocks by new text token number
this.getDelBlocks();
blocks.sort(function(a, b) {
return a.newNumber - b.newNumber;
});
 
// numberPosition '-' blocks ininto new text order
this.positionDelBlocks();
for (var block = 0; block < blocks.length; block ++) {
blocks[block].newBlock = block;
}
return;
};
 
// Collect insertion ('+') blocks from new text
this.getInsBlocks();
 
// Set group numbers of '+' blocks
// wDiff.GetSections: collect independent block sections (no old/new crosses outside section) for per-section determination of non-moving (fixed) groups
this.setInsGroups();
// called from: DetectBlocks()
// changes: creates sections, blocks[].section
 
// Mark original positions of moved groups
wDiff.GetSections = function (blocks, sections) {
this.insertMarks();
 
// Debug log
// clear sections array
if ( this.config.timer === true || this.config.debug === true ) {
sections.splice(0);
console.log( 'Unlink count: ', unlinkCount );
}
if ( this.config.debug === true ) {
this.debugGroups( 'Groups' );
this.debugBlocks( 'Blocks' );
}
return;
};
 
// cycle through blocks
for (var block = 0; block < blocks.length; block ++) {
 
/**
var sectionStart = block;
* Collect identical corresponding matching ('=') blocks from old text and sort by new text.
var sectionEnd = block;
*
* @param[in] WikEdDiffText newText, oldText Text objects
* @param[in/out] array blocks Blocks table object
*/
this.getSameBlocks = function () {
 
if ( this.config.timer === true ) {
var oldMax = blocks[sectionStart].oldNumber;
this.time( 'getSameBlocks' );
var sectionOldMax = oldMax;
}
 
var blocks = this.blocks;
// check right
for (var j = sectionStart + 1; j < blocks.length; j ++) {
 
// Clear blocks array
// check for crossing over to the left
if (blocks[j].oldNumbersplice( >0 oldMax) {;
oldMax = blocks[j].oldNumber;
}
else if (blocks[j].oldNumber < sectionOldMax) {
sectionEnd = j;
sectionOldMax = oldMax;
}
}
 
// Cycle through old text to find connected (linked, matched) blocks
// save crossing sections
var j = this.oldText.first;
if (sectionEnd > sectionStart) {
var i = null;
while ( j !== null ) {
 
// saveSkip section'-' to blockblocks
while ( j !== null && this.oldText.tokens[j].link === null ) {
for (var i = sectionStart; i <= sectionEnd; i ++) {
j = this.oldText.tokens[j].next;
blocks[i].section = sections.length;
}
 
// saveGet section'=' block
if ( j !== null ) {
sections.push({
i = this.oldText.tokens[j].link;
blockStart: sectionStart,
var iStart = i;
blockEnd: sectionEnd,
deleted:var jStart = falsej;
});
block = sectionEnd;
}
}
return;
};
 
// Detect matching blocks ('=')
var count = 0;
var unique = false;
var text = '';
while ( i !== null && j !== null && this.oldText.tokens[j].link === i ) {
text += this.oldText.tokens[j].token;
count ++;
if ( this.newText.tokens[i].unique === true ) {
unique = true;
}
i = this.newText.tokens[i].next;
j = this.oldText.tokens[j].next;
}
 
// Save old text '=' block
// wDiff.GetGroups: find groups of continuous old text blocks
blocks.push( {
// called from: DetectBlocks()
// changes oldBlock: creates groups, blocks[].grouplength,
newBlock: null,
oldNumber: this.oldText.tokens[jStart].number,
newNumber: this.newText.tokens[iStart].number,
oldStart: jStart,
count: count,
unique: unique,
words: this.wordCount( text ),
chars: text.length,
type: '=',
section: null,
group: null,
fixed: null,
moved: null,
text: text
} );
}
}
 
// Sort blocks by new text token number
wDiff.GetGroups = function (blocks, groups) {
blocks.sort( function( a, b ) {
return a.newNumber - b.newNumber;
} );
 
// Number blocks in new text order
// clear groups array
var blocksLength = blocks.length;
groups.splice(0);
for ( var block = 0; block < blocksLength; block ++ ) {
blocks[block].newBlock = block;
}
 
if ( this.config.timer === true ) {
// cycle through blocks
this.timeEnd( 'getSameBlocks' );
for (var block = 0; block < blocks.length; block ++) {
if (blocks[block].deleted === true) {
continue;
}
return;
var groupStart = block;
};
var groupEnd = block;
var oldBlock = blocks[groupStart].oldBlock;
 
// get word and char count of block
var words = wDiff.WordCount(blocks[block].string);
var maxWords = words;
var unique = false;
var chars = blocks[block].chars;
 
/**
// check right
* Collect independent block sections with no block move crosses
for (var i = groupEnd + 1; i < blocks.length; i ++) {
* outside a section for per-section determination of non-moving fixed groups.
*
* @param[out] array sections Sections table object
* @param[in/out] array blocks Blocks table object, section property
*/
this.getSections = function () {
 
if ( this.config.timer === true ) {
// check for crossing over to the left
this.time( 'getSections' );
if (blocks[i].oldBlock != oldBlock + 1) {
break;
}
oldBlock = blocks[i].oldBlock;
 
// get word and char count of block
if (blocks[i].words > maxWords) {
maxWords = blocks[i].words;
}
if (blocks[i].unique === true) {
unique = true;
}
words += blocks[i].words;
chars += blocks[i].chars;
groupEnd = i;
}
 
var blocks = this.blocks;
// save crossing group
var sections = this.sections;
if (groupEnd >= groupStart) {
 
// set groups outsideClear sections as fixedarray
sections.splice( 0 );
var fixed = false;
if (blocks[groupStart].section === null) {
fixed = true;
}
 
// saveCycle groupthrough to blockblocks
var blocksLength = blocks.length;
for (var i = groupStart; i <= groupEnd; i ++) {
for ( var block = 0; block < blocksLength; block ++ ) {
blocks[i].group = groups.length;
blocks[i].fixed = fixed;
}
 
var sectionStart = block;
// save group
var sectionEnd = block;
groups.push({
oldNumber: blocks[groupStart].oldNumber,
blockStart: groupStart,
blockEnd: groupEnd,
unique: unique,
maxWords: maxWords,
words: words,
chars: chars,
fixed: fixed,
moved: [],
movedFrom: null,
color: null,
diff: ''
});
block = groupEnd;
}
}
return;
};
 
var oldMax = blocks[sectionStart].oldNumber;
var sectionOldMax = oldMax;
 
// Check right
// wDiff.SetFixed: set longest sequence of increasing groups in sections as fixed (not moved)
for ( var j = sectionStart + 1; j < blocksLength; j ++ ) {
// called from: DetectBlocks()
// calls: wDiff.FindMaxPath()
// changes: groups[].fixed, blocks[].fixed
 
// Check for crossing over to the left
wDiff.SetFixed = function (blocks, groups, sections) {
if ( blocks[j].oldNumber > oldMax ) {
oldMax = blocks[j].oldNumber;
}
else if ( blocks[j].oldNumber < sectionOldMax ) {
sectionEnd = j;
sectionOldMax = oldMax;
}
}
 
// cycleSave throughcrossing sections
if ( sectionEnd > sectionStart ) {
for (var section = 0; section < sections.length; section ++) {
var blockStart = sections[section].blockStart;
var blockEnd = sections[section].blockEnd;
 
// Save section to block
var groupStart = blocks[blockStart].group;
for ( var i = sectionStart; i <= sectionEnd; i ++ ) {
var groupEnd = blocks[blockEnd].group;
blocks[i].section = sections.length;
 
}
// recusively find path of groups in increasing old group order with longest char length
 
// start at each group ofSave section
sections.push( {
var cache = [];
blockStart: sectionStart,
var maxChars = 0;
blockEnd: sectionEnd
var maxPath = null;
} );
for (var i = groupStart; i <= groupEnd; i ++) {
block = sectionEnd;
var pathObj = wDiff.FindMaxPath(i, [], 0, cache, groups, groupEnd);
if (pathObj.chars > maxChars) {
maxPath = pathObj.path;
maxChars = pathObj.chars;
}
}
if ( this.config.timer === true ) {
this.timeEnd( 'getSections' );
}
return;
};
 
// mark fixed groups
for (var i = 0; i < maxPath.length; i ++) {
var group = maxPath[i];
groups[group].fixed = true;
 
/**
// mark fixed blocks
* Find groups of continuous old text blocks.
for (var block = groups[group].blockStart; block <= groups[group].blockEnd; block ++) {
*
blocks[block].fixed = true;
* @param[out] array groups Groups table object
}
* @param[in/out] array blocks Blocks table object, group property
*/
this.getGroups = function () {
 
if ( this.config.timer === true ) {
this.time( 'getGroups' );
}
}
return;
};
 
var blocks = this.blocks;
var groups = this.groups;
 
// Clear groups array
// wDiff.FindMaxPath: recusively find path of groups in increasing old group order with longest char length
groups.splice( 0 );
// input: start, path start group; path, array of path groups; chars, char count of path; cache, cached sub-path lengths; groups, groups, group object; groupEnd, last group
// returns: returnObj, contains path and length
// called from: wDiff.SetFixed()
// calls: itself recursively
 
// Cycle through blocks
wDiff.FindMaxPath = function (start, path, chars, cache, groups, groupEnd) {
var blocksLength = blocks.length;
for ( var block = 0; block < blocksLength; block ++ ) {
var groupStart = block;
var groupEnd = block;
var oldBlock = blocks[groupStart].oldBlock;
 
// Get word and char count of block
// add current path point
var words = this.wordCount( blocks[block].text );
var pathLocal = path.slice();
var maxWords = words;
pathLocal.push(start);
var unique = blocks[block].unique;
chars = chars + groups[start].chars;
var chars = blocks[block].chars;
 
// Check right
// last group, terminate recursion
for ( var i = groupEnd + 1; i < blocksLength; i ++ ) {
var returnObj = { path: pathLocal, chars: chars };
if (start == groupEnd) {
return returnObj;
}
 
// Check for crossing over to the left
// find longest sub-path
if ( blocks[i].oldBlock !== oldBlock + 1 ) {
var maxChars = 0;
break;
var oldNumber = groups[start].oldNumber;
}
for (var i = start + 1; i <= groupEnd; i ++) {
oldBlock = blocks[i].oldBlock;
 
// onlyGet inword increasingand oldchar groupcount orderof block
if (groups blocks[i].oldNumberwords > <maxWords oldNumber) {
maxWords = blocks[i].words;
continue;
}
if ( blocks[i].unique === true ) {
unique = true;
}
words += blocks[i].words;
chars += blocks[i].chars;
groupEnd = i;
}
 
// Save crossing group
// get longest sub-path from cache
if (cache[start] !=groupEnd >= undefinedgroupStart ) {
returnObj = cache[start];
}
 
// Set groups outside sections as fixed
// get longest sub-path by recursion
var fixed = false;
else {
if ( blocks[groupStart].section === null ) {
var pathObj = wDiff.FindMaxPath(i, pathLocal, chars, cache, groups, groupEnd);
fixed = true;
}
 
// selectSave longestgroup sub-pathto block
for ( var i = groupStart; i <= groupEnd; i ++ ) {
if (pathObj.chars > maxChars) {
returnObj blocks[i].group = pathObjgroups.length;
blocks[i].fixed = fixed;
}
 
// Save group
groups.push( {
oldNumber: blocks[groupStart].oldNumber,
blockStart: groupStart,
blockEnd: groupEnd,
unique: unique,
maxWords: maxWords,
words: words,
chars: chars,
fixed: fixed,
movedFrom: null,
color: null
} );
block = groupEnd;
 
// Set global word count of longest linked block
if ( maxWords > this.maxWords ) {
this.maxWords = maxWords;
}
}
}
if ( this.config.timer === true ) {
}
this.timeEnd( 'getGroups' );
}
return;
};
 
// save longest path to cache
if (cache[i] === undefined) {
cache[start] = returnObj;
}
return returnObj;
};
 
/**
* Set longest sequence of increasing groups in sections as fixed (not moved).
*
* @param[in] array sections Sections table object
* @param[in/out] array groups Groups table object, fixed property
* @param[in/out] array blocks Blocks table object, fixed property
*/
this.setFixed = function () {
 
if ( this.config.timer === true ) {
// wDiff.GetDelBlocks: collect deletion ('del') blocks from old text
this.time( 'setFixed' );
// called from: DetectBlocks()
}
// changes: blocks
 
var blocks = this.blocks;
wDiff.GetDelBlocks = function (text, blocks) {
var groups = this.groups;
var sections = this.sections;
 
// cycleCycle through old text to find matched (linked) blockssections
var sectionsLength = sections.length;
var j = text.oldText.first;
for ( var section = 0; section < sectionsLength; section ++ ) {
var i = null;
var blockStart = sections[section].blockStart;
while (j !== null) {
var blockEnd = sections[section].blockEnd;
 
// var collectgroupStart 'del'= blocks[blockStart].group;
var oldStartgroupEnd = jblocks[blockEnd].group;
var count = 0;
var string = '';
while ( (j !== null) && (text.oldText.tokens[j].link === null) ) {
count ++;
string += text.oldText.tokens[j].token;
j = text.oldText.tokens[j].next;
}
 
// Recusively find path of groups in increasing old group order with longest char length
// save old text 'del' block
if var (countcache !== 0) {[];
var maxChars = 0;
blocks.push({
oldBlock:var maxPath = null,;
newBlock: null,
oldNumber: text.oldText.tokens[oldStart].number,
newNumber: null,
oldStart: oldStart,
count: count,
unique: false,
words: null,
chars: null,
type: 'del',
section: null,
group: null,
fixed: null,
string: string
});
}
 
// Start at each group of section
// skip 'same' block
if for (j !var i = groupStart; i <= nullgroupEnd; i ++ ) {
var pathObj = this.findMaxPath( i, groupEnd, cache );
i = text.oldText.tokens[j].link;
if ( pathObj.chars > maxChars ) {
while ( (i !== null) && (j !== null) && (text.oldText.tokens[j].link == i) ) {
maxPath = pathObj.path;
i = text.newText.tokens[i].next;
maxChars = pathObj.chars;
j = text.oldText.tokens[j].next;
}
}
}
}
return;
};
 
// Mark fixed groups
var maxPathLength = maxPath.length;
for ( var i = 0; i < maxPathLength; i ++ ) {
var group = maxPath[i];
groups[group].fixed = true;
 
// Mark fixed blocks
// wDiff.PositionDelBlocks: position 'del' blocks into new text order
for ( var block = groups[group].blockStart; block <= groups[group].blockEnd; block ++ ) {
// called from: DetectBlocks()
// changes: blocks[block].section/group/fixed/newNumber = true;
}
//
}
// deletion blocks move with fixed neighbor (new number +/- 0.1):
}
// old: 1 D 2 1 D 2
if ( this.config.timer === true ) {
// / / \ / \ \
this.timeEnd( 'setFixed' );
// new: 1 D 2 1 D 2
}
// fixed: * *
return;
// new number: 1 1.1 1.9 2
};
 
wDiff.PositionDelBlocks = function (blocks) {
 
/**
// sort shallow copy of blocks by oldNumber
* Recusively find path of groups in increasing old group order with longest char length.
var blocksOld = blocks.slice();
*
blocksOld.sort(function(a, b) {
* @param int start Path start group
return a.oldNumber - b.oldNumber;
* @param int groupEnd Path last group
});
* @param array cache Cache object, contains returnObj for start
* @return array returnObj Contains path and char length
*/
this.findMaxPath = function ( start, groupEnd, cache ) {
 
var groups = this.groups;
// cycle through 'del' blocks in old text order
for (var blockOld = 0; blockOld < blocksOld.length; blockOld ++) {
var delBlock = blocksOld[blockOld];
if (delBlock.type != 'del') {
continue;
}
 
// getFind oldlongest text prev blocksub-path
var prevBlockmaxChars = 0;
var oldNumber = groups[start].oldNumber;
if (blockOld > 0) {
var returnObj = { path: [], chars: 0};
prevBlock = blocks[ blocksOld[blockOld - 1].newBlock ];
for ( var i = start + 1; i <= groupEnd; i ++ ) {
}
 
// getOnly oldin textincreasing old nextgroup blockorder
if ( groups[i].oldNumber < oldNumber ) {
var nextBlock;
continue;
if (blockOld < blocksOld.length - 1) {
}
nextBlock = blocks[ blocksOld[blockOld + 1].newBlock ];
}
 
// Get longest sub-path from cache (deep copy)
// move after prev block if fixed
var neighborpathObj;
if ( (prevBlockcache[i] !== undefined) && (prevBlock.fixed === true) ) {
pathObj = { path: cache[i].path.slice(), chars: cache[i].chars };
neighbor = prevBlock;
}
delBlock.newNumber = neighbor.newNumber + 0.1;
}
 
// moveGet beforelongest nextsub-path blockby if fixedrecursion
else {
else if ( (nextBlock !== undefined) && (nextBlock.fixed === true) ) {
pathObj = this.findMaxPath( i, groupEnd, cache );
neighbor = nextBlock;
}
delBlock.newNumber = neighbor.newNumber - 0.1;
}
 
// Select longest sub-path
// move after prev block if existent
if ( pathObj.chars > maxChars ) {
else if (prevBlock !== undefined) {
neighbor maxChars = prevBlockpathObj.chars;
returnObj = pathObj;
delBlock.newNumber = neighbor.newNumber + 0.1;
}
}
 
// moveAdd beforecurrent nextstart blockto path
returnObj.path.unshift( start );
else if (nextBlock !== undefined) {
returnObj.chars += groups[start].chars;
neighbor = nextBlock;
delBlock.newNumber = neighbor.newNumber - 0.1;
}
 
// Save path to cache (deep copy)
// move before first block
if ( cache[start] === undefined ) {
else {
cache[start] = { path: returnObj.path.slice(), chars: returnObj.chars };
delBlock.newNumber = -0.1;
}
 
return returnObj;
// update 'del' block with neighbor data
};
if (neighbor !== undefined) {
delBlock.section = neighbor.section;
delBlock.group = neighbor.group;
delBlock.fixed = neighbor.fixed;
}
}
return;
};
 
 
/**
// wDiff.UnlinkBlocks: convert 'same' blocks in groups into 'ins'/'del' pairs if too short
* Convert matching '=' blocks in groups into insertion/deletion ('+'/'-') pairs
// called from: DetectBlocks()
* if too short and too common.
// changes: text.newText/oldText[].link
* Prevents fragmentated diffs for very different versions.
// returns: true if text tokens were unlinked
*
* @param[in] array blocks Blocks table object
* @param[in/out] WikEdDiffText newText, oldText Text object, linked property
* @param[in/out] array groups Groups table object
* @return bool True if text tokens were unlinked
*/
this.unlinkBlocks = function () {
 
var blocks = this.blocks;
wDiff.UnlinkBlocks = function (text, blocks, groups) {
var groups = this.groups;
 
// Cycle through groups
var unlinked = false;
var unlinked = false;
var groupsLength = groups.length;
for ( var group = 0; group < groupsLength; group ++ ) {
var blockStart = groups[group].blockStart;
var blockEnd = groups[group].blockEnd;
 
// Unlink whole group if no block is at least blockMinLength words long and unique
// cycle through groups
for if (var groups[group].maxWords =< 0;this.config.blockMinLength group <&& groups[group].length;unique group=== false ++) {
for ( var blockStartblock = groups[group].blockStart; block <= blockEnd; block ++ ) {
if ( blocks[block].type === '=' ) {
var blockEnd = groups[group].blockEnd;
this.unlinkSingleBlock( blocks[block] );
 
// no block in group is at least blockMinLength words long
if (groups[group].maxWords < wDiff.blockMinLength) {
 
// unlink whole moved group if it contains no unique matched token
if ( (groups[group].fixed === false) && (groups[group].unique === false) ) {
 
for (var block = blockStart; block <= blockEnd; block ++) {
if (blocks[block].type == 'same') {
wDiff.UnlinkSingleBlock(blocks[block], text);
unlinked = true;
}
Line 2,054 ⟶ 2,562:
}
 
// Otherwise unlink block flanks
else {
 
// unlinkUnlink blocks from start if preceded by 'del'
for ( var block = blockStart; block <= blockEnd; block ++ ) {
if ( (block > 0) && (blocks[block - 1].type === 'del') && (blocks[block].type == 'same') ) {
 
// stopStop unlinking if more than one word or a unique word
if ( (blocks[block].words > 1) || ( (blocks[block].words == 1) && (blocks[block].unique === true) ) ) {
break;
}
wDiffthis.UnlinkSingleBlockunlinkSingleBlock( blocks[block], text);
unlinked = true;
blockStart = block;
Line 2,071 ⟶ 2,579:
}
 
// unlinkUnlink blocks from end if followed by 'del'
for ( var block = blockEnd; block > blockStart; block -- ) {
if ( (blockEnd < blocks.length - 1) && (blocks[block + 1].type === 'del') && (blocks[block].type == 'same') ) {
 
// stopStop unlinking if more than one word or a unique word
if (
if ( (blocks[block].words > 1) || ( (blocks[block].words == 1) && (blocks[block].unique === true) ) ) {
blocks[block].words > 1 ||
( blocks[block].words === 1 && blocks[block].unique === true )
) {
break;
}
wDiffthis.UnlinkSingleBlockunlinkSingleBlock( blocks[block], text);
unlinked = true;
}
Line 2,085 ⟶ 2,596:
}
}
return unlinked;
}
};
return unlinked;
};
 
 
/**
// wDiff.UnlinkBlock: un-link text tokens of single block, converting them into 'ins'/'del' pairs
* Unlink text tokens of single block, convert them into into insertion/deletion ('+'/'-') pairs.
// called from: wDiff.UnlinkBlocks()
*
// changes: text.newText/oldText[].link
* @param[in] array blocks Blocks table object
* @param[out] WikEdDiffText newText, oldText Text objects, link property
*/
this.unlinkSingleBlock = function ( block ) {
 
// Cycle through old text
wDiff.UnlinkSingleBlock = function (block, text) {
var j = block.oldStart;
for ( var count = 0; count < block.count; count ++ ) {
 
// Unlink tokens
// cycle through old text
this.newText.tokens[ this.oldText.tokens[j].link ].link = null;
var j = block.oldStart;
this.oldText.tokens[j].link = null;
for (var count = 0; count < block.count; count ++) {
j = this.oldText.tokens[j].next;
}
return;
};
 
// unlink tokens
text.newText.tokens[ text.oldText.tokens[j].link ].link = null;
text.oldText.tokens[j].link = null;
j = text.oldText.tokens[j].next;
}
return;
};
 
/**
* Collect deletion ('-') blocks from old text.
*
* @param[in] WikEdDiffText oldText Old Text object
* @param[out] array blocks Blocks table object
*/
this.getDelBlocks = function () {
 
if ( this.config.timer === true ) {
// wDiff.GetInsBlocks: collect insertion ('ins') blocks from new text
this.time( 'getDelBlocks' );
// called from: DetectBlocks()
}
// changes: blocks
 
var blocks = this.blocks;
wDiff.GetInsBlocks = function (text, blocks) {
 
// cycleCycle through newold text to find insertionconnected (linked, matched) blocks
var ij = textthis.newTextoldText.first;
while var (i !== null) {;
while ( j !== null ) {
 
// jump over linked (matched) block
while ( (i !== null) && (text.newText.tokens[i].link !== null) ) {
i = text.newText.tokens[i].next;
}
 
// detectCollect insertion'-' blocks ('ins')
if var (ioldStart !== null) {j;
var iStart = i;
var count = 0;
var stringtext = '';
while ( (ij !== null) && (textthis.newTextoldText.tokens[ij].link === null) ) {
count ++;
stringtext += textthis.newTextoldText.tokens[ij].token;
ij = textthis.newTextoldText.tokens[ij].next;
}
 
// saveSave newold text 'ins-' block
if ( count !== 0 ) {
blocks.push({
oldBlock:blocks.push( null,{
newBlock oldBlock: null,
oldNumber newBlock: null,
newNumber oldNumber: textthis.newTextoldText.tokens[iStartoldStart].number,
oldStart newNumber: null,
count oldStart: countoldStart,
unique count: false count,
words unique: nullfalse,
chars words: null,
type chars: 'ins'text.length,
section type: null '-',
group section: null,
fixed group: null,
string fixed: string null,
moved: null,
});
text: text
} );
}
 
// Skip '=' blocks
if ( j !== null ) {
i = this.oldText.tokens[j].link;
while ( i !== null && j !== null && this.oldText.tokens[j].link === i ) {
i = this.newText.tokens[i].next;
j = this.oldText.tokens[j].next;
}
}
}
if ( this.config.timer === true ) {
}
this.timeEnd( 'getDelBlocks' );
return;
};
return;
};
 
 
/**
// wDiff.SortBlocks: sort blocks by new text token number and update groups
* Position deletion '-' blocks into new text order.
// called from: DetectBlocks()
* Deletion blocks move with fixed reference:
// changes: blocks
* Old: 1 D 2 1 D 2
* / \ / \ \
* New: 1 D 2 1 D 2
* Fixed: * *
* newNumber: 1 1 2 2
*
* Marks '|' and deletions '-' get newNumber of reference block
* and are sorted around it by old text number.
*
* @param[in/out] array blocks Blocks table, newNumber, section, group, and fixed properties
*
*/
this.positionDelBlocks = function () {
 
if ( this.config.timer === true ) {
wDiff.SortBlocks = function (blocks, groups) {
this.time( 'positionDelBlocks' );
}
 
var blocks = this.blocks;
// sort by newNumber
var groups = this.groups;
blocks.sort(function(a, b) {
return a.newNumber - b.newNumber;
});
 
// Sort shallow copy of blocks by oldNumber
// cycle through blocks and update groups with new block numbers
var groupblocksOld = nullblocks.slice();
blocksOld.sort( function( a, b ) {
for (var block = 0; block < blocks.length; block ++) {
return a.oldNumber - b.oldNumber;
var blockGroup = blocks[block].group;
} );
if (blockGroup !== null) {
 
if (blockGroup != group) {
group// =Cycle through blocks[block].group; in old text order
var blocksOldLength = blocksOld.length;
groups[group].blockStart = block;
for ( var block = 0; block < blocksOldLength; block ++ ) {
groups[group].oldNumber = blocks[block].oldNumber;
var delBlock = blocksOld[block];
 
// '-' block only
if ( delBlock.type !== '-' ) {
continue;
}
groups[blockGroup].blockEnd = block;
}
}
return;
};
 
// Find fixed '=' reference block from original block position to position '-' block
// Similar to position marks '|' code
 
// Get old text prev block
// wDiff.SetInsDelGroups: set group numbers of 'ins' and 'del' blocks
var prevBlockNumber = null;
// called from: DetectBlocks()
var prevBlock = null;
// changes: groups, blocks[].fixed/group
if ( block > 0 ) {
prevBlockNumber = blocksOld[block - 1].newBlock;
prevBlock = blocks[prevBlockNumber];
}
 
// Get old text next block
wDiff.SetInsDelGroups = function (blocks, groups) {
var nextBlockNumber = null;
var nextBlock = null;
if ( block < blocksOld.length - 1 ) {
nextBlockNumber = blocksOld[block + 1].newBlock;
nextBlock = blocks[nextBlockNumber];
}
 
// Move after prev block if fixed
// set group numbers of 'ins' and 'del' blocks inside existing groups
var refBlock = null;
for (var group = 0; group < groups.length; group ++) {
if ( prevBlock !== null && prevBlock.type === '=' && prevBlock.fixed === true ) {
var fixed = groups[group].fixed;
refBlock = prevBlock;
for (var block = groups[group].blockStart; block <= groups[group].blockEnd; block ++) {
if (blocks[block].group === null) {
blocks[block].group = group;
blocks[block].fixed = fixed;
}
}
}
 
// Move before next block if fixed
// add remaining 'ins' and 'del' blocks to groups
else if ( nextBlock !== null && nextBlock.type === '=' && nextBlock.fixed === true ) {
refBlock = nextBlock;
}
 
// Move after prev block if not start of group
// cycle through blocks
else if (
for (var block = 0; block < blocks.length; block ++) {
prevBlock !== null &&
prevBlock.type === '=' &&
prevBlockNumber !== groups[ prevBlock.group ].blockEnd
) {
refBlock = prevBlock;
}
 
// Move before next block if not start of group
// skip existing groups
else if (
if (blocks[block].group === null) {
nextBlock !== null &&
blocks[block].group = groups.length;
nextBlock.type === '=' &&
var fixed = blocks[block].fixed;
nextBlockNumber !== groups[ nextBlock.group ].blockStart
) {
refBlock = nextBlock;
}
 
// Move after closest previous fixed block
// save group
groups.push(else {
for ( var fixed = block; fixed >= 0; fixed -- ) {
oldNumber: blocks[block].oldNumber,
if ( blocksOld[fixed].type === '=' && blocksOld[fixed].fixed === true ) {
blockStart: block,
refBlock = blocksOld[fixed];
blockEnd: block,
break;
unique: false,
}
maxWords: null,
}
words: null,
}
chars: null,
 
fixed: fixed,
moved:// Move before first [],block
movedFrom:if ( refBlock === null, ) {
color:delBlock.newNumber = null,-1;
}
diff: ''
 
});
// Update '-' block data
else {
delBlock.newNumber = refBlock.newNumber;
delBlock.section = refBlock.section;
delBlock.group = refBlock.group;
delBlock.fixed = refBlock.fixed;
}
}
}
return;
};
 
// Sort '-' blocks in and update groups
this.sortBlocks();
 
if ( this.config.timer === true ) {
// wDiff.MarkMoved: mark original positions of moved groups
this.timeEnd( 'positionDelBlocks' );
// called from: DetectBlocks()
}
// changes: groups[].moved/movedFrom
return;
// moved block marks at original positions relative to fixed groups:
};
// groups: 3 7
// 1 <| | (no next smaller fixed)
// 5 |< |
// |> 5 |
// | 5 <|
// | >| 5
// | |> 9 (no next larger fixed)
// fixed: * *
// mark direction: groups[movedGroup].blockStart < groups[group].blockStart
// group side: groups[movedGroup].oldNumber < groups[group].oldNumber
 
wDiff.MarkMoved = function (groups) {
 
/**
// cycle through groups (moved group)
* Collect insertion ('+') blocks from new text.
for (var movedGroup = 0; movedGroup < groups.length; movedGroup ++) {
*
if (groups[movedGroup].fixed !== false) {
* @param[in] WikEdDiffText newText New Text object
continue;
* @param[out] array blocks Blocks table object
*/
this.getInsBlocks = function () {
 
if ( this.config.timer === true ) {
this.time( 'getInsBlocks' );
}
var movedOldNumber = groups[movedGroup].oldNumber;
 
var blocks = this.blocks;
// find closest fixed groups
var nextSmallerNumber = null;
var nextSmallerGroup = null;
var nextLargerNumber = null;
var nextLargerGroup = null;
 
// cycleCycle through groupsnew (originaltext positions)to find insertion blocks
var i = this.newText.first;
for (var group = 0; group < groups.length; group ++) {
ifwhile ( (groups[group].fixedi !== true) || (group == movedGroup)null ) {
continue;
}
 
// Jump over linked (matched) block
// find fixed group with closest smaller oldNumber
while ( i !== null && this.newText.tokens[i].link !== null ) {
var oldNumber = groups[group].oldNumber;
i = this.newText.tokens[i].next;
if ( (oldNumber < movedOldNumber) && ( (nextSmallerNumber === null) || (oldNumber > nextSmallerNumber) ) ) {
nextSmallerNumber = oldNumber;
nextSmallerGroup = group;
}
 
// Detect insertion blocks ('+')
// find fixed group with closest larger oldNumber
if ( i !== null ) {
if ( (oldNumber > movedOldNumber) && ( (nextLargerNumber === null) || (oldNumber < nextLargerNumber) ) ) {
nextLargerNumbervar iStart = oldNumberi;
nextLargerGroupvar count = group0;
var text = '';
while ( i !== null && this.newText.tokens[i].link === null ) {
count ++;
text += this.newText.tokens[i].token;
i = this.newText.tokens[i].next;
}
 
// Save new text '+' block
blocks.push( {
oldBlock: null,
newBlock: null,
oldNumber: null,
newNumber: this.newText.tokens[iStart].number,
oldStart: null,
count: count,
unique: false,
words: null,
chars: text.length,
type: '+',
section: null,
group: null,
fixed: null,
moved: null,
text: text
} );
}
}
 
// Sort '+' blocks in and update groups
// no larger fixed group, moved right
this.sortBlocks();
var movedFrom = '';
if (nextLargerGroup === null) {
movedFrom = 'left';
}
 
if ( this.config.timer === true ) {
// no smaller fixed group, moved right
this.timeEnd( 'getInsBlocks' );
else if (nextSmallerGroup === null) {
movedFrom = 'right';
}
return;
};
 
// group moved from between two closest fixed neighbors, moved left or right depending on char distance
else {
var rightChars = 0;
for (var group = nextSmallerGroup + 1; group < movedGroup; group ++) {
rightChars += groups[group].chars;
}
var leftChars = 0;
for (var group = movedGroup + 1; group < nextLargerGroup; group ++) {
leftChars += groups[group].chars;
}
 
/**
// moved right
* Sort blocks by new text token number and update groups.
if (rightChars <= leftChars) {
*
movedFrom = 'left';
* @param[in/out] array groups Groups table object
* @param[in/out] array blocks Blocks table object
*/
this.sortBlocks = function () {
 
var blocks = this.blocks;
var groups = this.groups;
 
// Sort by newNumber, then by old number
blocks.sort( function( a, b ) {
var comp = a.newNumber - b.newNumber;
if ( comp === 0 ) {
comp = a.oldNumber - b.oldNumber;
}
return comp;
} );
 
// Cycle through blocks and update groups with new block numbers
// moved left
var group = null;
else {
var blocksLength = blocks.length;
movedFrom = 'right';
for ( var block = 0; block < blocksLength; block ++ ) {
var blockGroup = blocks[block].group;
if ( blockGroup !== null ) {
if ( blockGroup !== group ) {
group = blocks[block].group;
groups[group].blockStart = block;
groups[group].oldNumber = blocks[block].oldNumber;
}
groups[blockGroup].blockEnd = block;
}
}
return;
};
 
 
// check for null-moves
/**
if (movedFrom == 'left') {
* Set group numbers of insertion '+' blocks.
if (groups[nextSmallerGroup].blockEnd + 1 != groups[movedGroup].blockStart) {
*
groups[nextSmallerGroup].moved.push(movedGroup);
* @param[in/out] array groups Groups table object
groups[movedGroup].movedFrom = nextSmallerGroup;
* @param[in/out] array blocks Blocks table object, fixed and group properties
}
*/
this.setInsGroups = function () {
 
if ( this.config.timer === true ) {
this.time( 'setInsGroups' );
}
 
else if (movedFrom == 'right') {
var blocks = this.blocks;
if (groups[movedGroup].blockEnd + 1 != groups[nextLargerGroup].blockStart) {
var groups = this.groups;
groups[nextLargerGroup].moved.push(movedGroup);
 
groups[movedGroup].movedFrom = nextLargerGroup;
// Set group numbers of '+' blocks inside existing groups
var groupsLength = groups.length;
for ( var group = 0; group < groupsLength; group ++ ) {
var fixed = groups[group].fixed;
for ( var block = groups[group].blockStart; block <= groups[group].blockEnd; block ++ ) {
if ( blocks[block].group === null ) {
blocks[block].group = group;
blocks[block].fixed = fixed;
}
}
}
}
 
// cycleAdd throughremaining groups, sort'+' blocks movedto fromnew here by old numbergroups
 
for (var group = 0; group < groups.length; group ++) {
// Cycle through blocks
var moved = groups[group].moved;
var blocksLength = blocks.length;
if (moved !== null) {
for ( var block = 0; block < blocksLength; block ++ ) {
moved.sort(function(a, b) {
 
return groups[a].oldNumber - groups[b].oldNumber;
// Skip existing groups
});
if ( blocks[block].group === null ) {
blocks[block].group = groups.length;
 
// Save new single-block group
groups.push( {
oldNumber: blocks[block].oldNumber,
blockStart: block,
blockEnd: block,
unique: blocks[block].unique,
maxWords: blocks[block].words,
words: blocks[block].words,
chars: blocks[block].chars,
fixed: blocks[block].fixed,
movedFrom: null,
color: null
} );
}
}
if ( this.config.timer === true ) {
}
this.timeEnd( 'setInsGroups' );
return;
};
return;
};
 
 
/**
// wDiff.ColorMoved: set moved block color numbers
* Mark original positions of moved groups.
// called from: DetectBlocks()
* Scheme: moved block marks at original positions relative to fixed groups:
// changes: groups[].color
* Groups: 3 7
* 1 <| | (no next smaller fixed)
* 5 |< |
* |> 5 |
* | 5 <|
* | >| 5
* | |> 9 (no next larger fixed)
* Fixed: * *
*
* Mark direction: groups.movedGroup.blockStart < groups.group.blockStart
* Group side: groups.movedGroup.oldNumber < groups.group.oldNumber
*
* Marks '|' and deletions '-' get newNumber of reference block
* and are sorted around it by old text number.
*
* @param[in/out] array groups Groups table object, movedFrom property
* @param[in/out] array blocks Blocks table object
*/
this.insertMarks = function () {
 
if ( this.config.timer === true ) {
wDiff.ColorMoved = function (groups) {
this.time( 'insertMarks' );
}
 
var blocks = this.blocks;
// cycle through groups
var movedgroups = []this.groups;
var moved = [];
for (var group = 0; group < groups.length; group ++) {
var color = 1;
moved = moved.concat(groups[group].moved);
}
 
// Make shallow copy of blocks
// sort moved array by old number
var blocksOld = blocks.slice();
moved.sort(function(a, b) {
return groups[a].oldNumber - groups[b].oldNumber;
});
 
// Enumerate copy
// set color
var blocksOldLength = blocksOld.length;
var color = 0;
for ( var i = 0; i < moved.lengthblocksOldLength; i ++ ) {
var movedGroup blocksOld[i].number = moved[i];
if (wDiff.showBlockMoves === true) {
groups[movedGroup].color = color;
color ++;
}
}
return;
};
 
// Sort copy by oldNumber
blocksOld.sort( function( a, b ) {
var comp = a.oldNumber - b.oldNumber;
if ( comp === 0 ) {
comp = a.newNumber - b.newNumber;
}
return comp;
} );
 
// Create lookup table: original to sorted
// wDiff.AssembleDiff: process diff data into formatted html text
var lookupSorted = [];
// input: text, object containing text tokens list; blocks, array containing block type; groups, array containing fixed (not moved), color, and moved mark data
for ( var i = 0; i < blocksOldLength; i ++ ) {
// returns: diff html string
lookupSorted[ blocksOld[i].number ] = i;
// called from: wDiff.Diff()
}
// calls: wDiff.HtmlCustomize(), wDiff.HtmlFormat()
 
// Cycle through groups (moved group)
wDiff.AssembleDiff = function (text, blocks, groups) {
var groupsLength = groups.length;
for ( var moved = 0; moved < groupsLength; moved ++ ) {
var movedGroup = groups[moved];
if ( movedGroup.fixed !== false ) {
continue;
}
var movedOldNumber = movedGroup.oldNumber;
 
// Find fixed '=' reference block from original block position to position '|' block
//
// Similar to position deletions '-' code
// create group diffs
//
 
// Get old text prev block
// cycle through groups
var prevBlock = null;
for (var group = 0; group < groups.length; group ++) {
var colorblock = groupslookupSorted[group] movedGroup.colorblockStart ];
if ( block > 0 ) {
var blockStart = groups[group].blockStart;
prevBlock = blocksOld[block - 1];
var blockEnd = groups[group].blockEnd;
}
var diff = '';
 
// checkGet forold coloredtext next block and move direction
var blockFromnextBlock = null;
var block = lookupSorted[ movedGroup.blockEnd ];
if (color !== null) {
if ( block < blocksOld.length - 1 ) {
if (groups[ groups[group].movedFrom ].blockStart < blockStart) {
blockFromnextBlock = 'left'blocksOld[block + 1];
}
 
// Move after prev block if fixed
var refBlock = null;
if ( prevBlock !== null && prevBlock.type === '=' && prevBlock.fixed === true ) {
refBlock = prevBlock;
}
 
// Move before next block if fixed
else if ( nextBlock !== null && nextBlock.type === '=' && nextBlock.fixed === true ) {
refBlock = nextBlock;
}
 
// Find closest fixed block to the left
else {
for ( var fixed = lookupSorted[ movedGroup.blockStart ] - 1; fixed >= 0; fixed -- ) {
blockFrom = 'right';
if ( blocksOld[fixed].type === '=' && blocksOld[fixed].fixed === true ) {
refBlock = blocksOld[fixed];
break;
}
}
}
 
// Get position of new mark block
var newNumber;
var markGroup;
 
// No smaller fixed block, moved right from before first block
if ( refBlock === null ) {
newNumber = -1;
markGroup = groups.length;
 
// Save new single-mark-block group
groups.push( {
oldNumber: 0,
blockStart: blocks.length,
blockEnd: blocks.length,
unique: false,
maxWords: null,
words: null,
chars: 0,
fixed: null,
movedFrom: null,
color: null
} );
}
else {
newNumber = refBlock.newNumber;
markGroup = refBlock.group;
}
 
// Insert '|' block
blocks.push( {
oldBlock: null,
newBlock: null,
oldNumber: movedOldNumber,
newNumber: newNumber,
oldStart: null,
count: null,
unique: null,
words: null,
chars: 0,
type: '|',
section: null,
group: markGroup,
fixed: true,
moved: moved,
text: ''
} );
 
// Set group color
movedGroup.color = color;
movedGroup.movedFrom = markGroup;
color ++;
}
 
// Sort '|' blocks in and update groups
// add colored block start markup
this.sortBlocks();
if (blockFrom == 'left') {
 
diff += wDiff.HtmlCustomize(wDiff.htmlBlockLeftStart, color);
if ( this.config.timer === true ) {
}
this.timeEnd( 'insertMarks' );
else if (blockFrom == 'right') {
diff += wDiff.HtmlCustomize(wDiff.htmlBlockRightStart, color);
}
return;
};
 
 
/**
* Collect diff fragment list for markup, create abstraction layer for customized diffs.
* Adds the following fagment types:
* '=', '-', '+' same, deletion, insertion
* '<', '>' mark left, mark right
* '(<', '(>', ')' block start and end
* '[', ']' fragment start and end
* '{', '}' container start and end
*
* @param[in] array groups Groups table object
* @param[in] array blocks Blocks table object
* @param[out] array fragments Fragments array, abstraction layer for diff code
*/
this.getDiffFragments = function () {
 
var blocks = this.blocks;
var groups = this.groups;
var fragments = this.fragments;
 
// Make shallow copy of groups and sort by blockStart
// cycle through blocks
var groupsSort = groups.slice();
for (var block = blockStart; block <= blockEnd; block ++) {
groupsSort.sort( function( a, b ) {
var type = blocks[block].type;
return a.blockStart - b.blockStart;
var string = blocks[block].string;
} );
 
// htmlCycle escapethrough text stringgroups
var groupsSortLength = groupsSort.length;
string = wDiff.HtmlEscape(string);
for ( var group = 0; group < groupsSortLength; group ++ ) {
var blockStart = groupsSort[group].blockStart;
var blockEnd = groupsSort[group].blockEnd;
 
// add 'same' (unchanged) text andAdd moved block start
var color = groupsSort[group].color;
if (type == 'same') {
if ( color !== null ) {
var type;
diff += wDiff.HtmlFormatBlock(string);
if ( groupsSort[group].movedFrom < blocks[ blockStart ].group ) {
type = '(<';
}
else {
difftype += string'(>';
}
fragments.push( {
text: '',
type: type,
color: color
} );
}
 
// addCycle 'del'through textblocks
for ( var block = blockStart; block <= blockEnd; block ++ ) {
else if (type == 'del') {
var type = blocks[block].type;
if (wDiff.regExpBlankBlock.test(string) === true) {
 
diff += wDiff.htmlDeleteStartBlank;
// Add '=' unchanged text and moved block
if ( type === '=' || type === '-' || type === '+' ) {
fragments.push( {
text: blocks[block].text,
type: type,
color: color
} );
}
 
else {
// Add '<' and '>' marks
diff += wDiff.htmlDeleteStart;
else if ( type === '|' ) {
var movedGroup = groups[ blocks[block].moved ];
 
// Get mark text
var markText = '';
for (
var movedBlock = movedGroup.blockStart;
movedBlock <= movedGroup.blockEnd;
movedBlock ++
) {
if ( blocks[movedBlock].type === '=' || blocks[movedBlock].type === '-' ) {
markText += blocks[movedBlock].text;
}
}
 
// Get mark direction
var markType;
if ( movedGroup.blockStart < blockStart ) {
markType = '<';
}
else {
markType = '>';
}
 
// Add mark
fragments.push( {
text: markText,
type: markType,
color: movedGroup.color
} );
}
diff += wDiff.HtmlFormatBlock(string) + wDiff.htmlDeleteEnd;
}
 
// addAdd 'ins'moved textblock end
else if (type color !== 'ins'null ) {
fragments.push( {
if (wDiff.regExpBlankBlock.test(string) === true) {
text: '',
diff += wDiff.htmlInsertStartBlank;
} type: ' )',
else color: {color
} );
diff += wDiff.htmlInsertStart;
}
diff += wDiff.HtmlFormatBlock(string) + wDiff.htmlInsertEnd;
}
}
 
// Cycle through fragments, join consecutive fragments of same type (i.e. '-' blocks)
// add colored block end markup
var fragmentsLength = fragments.length;
if (blockFrom == 'left') {
for ( var fragment = 1; fragment < fragmentsLength; fragment ++ ) {
diff += wDiff.htmlBlockLeftEnd;
 
// Check if joinable
if (
fragments[fragment].type === fragments[fragment - 1].type &&
fragments[fragment].color === fragments[fragment - 1].color &&
fragments[fragment].text !== '' && fragments[fragment - 1].text !== ''
) {
 
// Join and splice
fragments[fragment - 1].text += fragments[fragment].text;
fragments.splice( fragment, 1 );
fragment --;
}
}
 
else if (blockFrom == 'right') {
// Enclose in containers
diff += wDiff.htmlBlockRightEnd;
fragments.unshift( { text: '', type: '{', color: null }, { text: '', type: '[', color: null } );
fragments.push( { text: '', type: ']', color: null }, { text: '', type: '}', color: null } );
 
return;
};
 
 
/**
* Clip unchanged sections from unmoved block text.
* Adds the following fagment types:
* '~', ' ~', '~ ' omission indicators
* '[', ']', ',' fragment start and end, fragment separator
*
* @param[in/out] array fragments Fragments array, abstraction layer for diff code
*/
this.clipDiffFragments = function () {
 
var fragments = this.fragments;
 
// Skip if only one fragment in containers, no change
if ( fragments.length === 5 ) {
return;
}
 
// Min length for clipping right
groups[group].diff = diff;
var minRight = this.config.clipHeadingRight;
}
if ( this.config.clipParagraphRightMin < minRight ) {
minRight = this.config.clipParagraphRightMin;
}
if ( this.config.clipLineRightMin < minRight ) {
minRight = this.config.clipLineRightMin;
}
if ( this.config.clipBlankRightMin < minRight ) {
minRight = this.config.clipBlankRightMin;
}
if ( this.config.clipCharsRight < minRight ) {
minRight = this.config.clipCharsRight;
}
 
// Min length for clipping left
//
var minLeft = this.config.clipHeadingLeft;
// mark original block positions
if ( this.config.clipParagraphLeftMin < minLeft ) {
//
minLeft = this.config.clipParagraphLeftMin;
}
if ( this.config.clipLineLeftMin < minLeft ) {
minLeft = this.config.clipLineLeftMin;
}
if ( this.config.clipBlankLeftMin < minLeft ) {
minLeft = this.config.clipBlankLeftMin;
}
if ( this.config.clipCharsLeft < minLeft ) {
minLeft = this.config.clipCharsLeft;
}
 
// cycleCycle through groupsfragments
for ( var groupfragmentsLength = 0; group < groupsfragments.length; group ++) {
for ( var fragment = 0; fragment < fragmentsLength; fragment ++ ) {
var moved = groups[group].moved;
 
// cycleSkip throughif listnot ofan groupsunmoved movedand fromunchanged hereblock
var leftMarkstype = ''fragments[fragment].type;
var rightMarkscolor = ''fragments[fragment].color;
for if (var itype !== 0;'=' i|| <color moved.length;!== inull ++) {
continue;
var movedGroup = moved[i];
}
var markColor = groups[movedGroup].color;
var mark;
 
// getSkip movedif blocktoo textshort for clipping
var stringtext = ''fragments[fragment].text;
var textLength = text.length;
for (var block = groups[movedGroup].blockStart; block <= groups[movedGroup].blockEnd; block ++) {
if ( textLength < minRight && textLength < minLeft ) {
if (blocks[block].type != 'ins') {
continue;
string += blocks[block].string;
}
}
 
// Get line positions including start and end
// display as deletion at original position
var lines = [];
if (wDiff.showBlockMoves === false) {
var lastIndex = null;
string = wDiff.HtmlEscape(string);
var regExpMatch;
if (wDiff.regExpBlankBlock.test(string) === true) {
while ( ( regExpMatch = this.config.regExp.clipLine.exec( text ) ) !== null ) {
mark = wDiff.htmlDeleteStartBlank;
lines.push( regExpMatch.index );
}
lastIndex = this.config.regExp.clipLine.lastIndex;
else {
}
mark = wDiff.htmlDeleteStart;
if ( lines[0] !== 0 ) {
}
lines.unshift( 0 );
mark += wDiff.HtmlFormatBlock(string) + wDiff.htmlDeleteEnd;
}
if ( lastIndex !== textLength ) {
lines.push( textLength );
}
 
// getGet markheading directionpositions
var headings = [];
else {
var headingsEnd = [];
if (groups[movedGroup].blockStart < groups[group].blockStart) {
while ( ( regExpMatch = this.config.regExp.clipHeading.exec( text ) ) !== null ) {
mark = wDiff.htmlMarkLeft;
headings.push( regExpMatch.index );
}
headingsEnd.push( regExpMatch.index + regExpMatch[0].length );
else {
mark = wDiff.htmlMarkRight;
}
mark = wDiff.HtmlCustomize(mark, markColor, string);
}
 
// getGet sideparagraph ofpositions groupincluding tostart markand end
var paragraphs = [];
if (groups[movedGroup].oldNumber < groups[group].oldNumber) {
leftMarksvar lastIndex += marknull;
while ( ( regExpMatch = this.config.regExp.clipParagraph.exec( text ) ) !== null ) {
paragraphs.push( regExpMatch.index );
lastIndex = this.config.regExp.clipParagraph.lastIndex;
}
if ( paragraphs[0] !== 0 ) {
else {
rightMarksparagraphs.unshift( +=0 mark);
}
if ( lastIndex !== textLength ) {
paragraphs.push( textLength );
}
}
groups[group].diff = leftMarks + groups[group].diff + rightMarks;
}
 
// Determine ranges to keep on left and right side
//
var rangeRight = null;
// join diffs
var rangeLeft = null;
//
var rangeRightType = '';
var rangeLeftType = '';
 
// Find clip pos from left, skip for first non-container block
// make shallow copy of groups and sort by blockStart
if ( fragment !== 2 ) {
var groupsSort = groups.slice();
groupsSort.sort(function(a, b) {
return a.blockStart - b.blockStart;
});
 
// Maximum lines to search from left
// cycle through sorted groups and assemble diffs
var rangeLeftMax = textLength;
for (var group = 0; group < groupsSort.length; group ++) {
if ( this.config.clipLinesLeftMax < lines.length ) {
text.diff += groupsSort[group].diff;
rangeLeftMax = lines[this.config.clipLinesLeftMax];
}
}
 
// Find first heading from left
// WED('Groups', wDiff.DebugGroups(groups));
if ( rangeLeft === null ) {
var headingsLength = headingsEnd.length;
for ( var j = 0; j < headingsLength; j ++ ) {
if ( headingsEnd[j] > this.config.clipHeadingLeft || headingsEnd[j] > rangeLeftMax ) {
break;
}
rangeLeft = headingsEnd[j];
rangeLeftType = 'heading';
break;
}
}
 
// Find first paragraph from left
// keep newlines and multiple spaces
if ( rangeLeft === null ) {
wDiff.HtmlFormat(text);
var paragraphsLength = paragraphs.length;
for ( var j = 0; j < paragraphsLength; j ++ ) {
if (
paragraphs[j] > this.config.clipParagraphLeftMax ||
paragraphs[j] > rangeLeftMax
) {
break;
}
if ( paragraphs[j] > this.config.clipParagraphLeftMin ) {
rangeLeft = paragraphs[j];
rangeLeftType = 'paragraph';
break;
}
}
}
 
// Find first line break from left
// WED('text.diff', text.diff);
if ( rangeLeft === null ) {
var linesLength = lines.length;
for ( var j = 0; j < linesLength; j ++ ) {
if ( lines[j] > this.config.clipLineLeftMax || lines[j] > rangeLeftMax ) {
break;
}
if ( lines[j] > this.config.clipLineLeftMin ) {
rangeLeft = lines[j];
rangeLeftType = 'line';
break;
}
}
}
 
// Find first blank from left
return text.diff;
if ( rangeLeft === null ) {
};
this.config.regExp.clipBlank.lastIndex = this.config.clipBlankLeftMin;
if ( ( regExpMatch = this.config.regExp.clipBlank.exec( text ) ) !== null ) {
if (
regExpMatch.index < this.config.clipBlankLeftMax &&
regExpMatch.index < rangeLeftMax
) {
rangeLeft = regExpMatch.index;
rangeLeftType = 'blank';
}
}
}
 
// Fixed number of chars from left
if ( rangeLeft === null ) {
if ( this.config.clipCharsLeft < rangeLeftMax ) {
rangeLeft = this.config.clipCharsLeft;
rangeLeftType = 'chars';
}
}
 
// Fixed number of lines from left
//
if ( rangeLeft === null ) {
// wDiff.HtmlCustomize: customize move indicator html: {block}: block number style, {mark}: mark number style, {class}: class number, {number}: block number, {title}: title attribute (popup)
rangeLeft = rangeLeftMax;
// input: text (html or css code)
rangeLeftType = 'fixed';
// returns: customized text
}
// called from: wDiff.AssembleDiff()
}
 
// Find clip pos from right, skip for last non-container block
wDiff.HtmlCustomize = function (text, number, title) {
if ( fragment !== fragments.length - 3 ) {
 
// Maximum lines to search from right
if (wDiff.coloredBlocks === true) {
var rangeRightMin = 0;
var blockStyle = wDiff.styleBlockColor[number];
if ( lines.length >= this.config.clipLinesRightMax ) {
if (blockStyle === undefined) {
rangeRightMin = lines[lines.length - this.config.clipLinesRightMax];
blockStyle = '';
}
var markStyle = wDiff.styleMarkColor[number];
if (markStyle === undefined) {
markStyle = '';
}
text = text.replace(/\{block\}/g, ' ' + blockStyle);
text = text.replace(/\{mark\}/g, ' ' + markStyle);
text = text.replace(/\{class\}/g, number);
}
else {
text = text.replace(/\{block\}|\{mark\}|\{class\}/g, '');
}
text = text.replace(/\{number\}/g, number);
 
// Find last heading from right
// shorten title text, replace {title}
if ( (titlerangeRight !== undefined) && (title !== '')null ) {
for ( var j = headings.length - 1; j >= 0; j -- ) {
var max = 512;
if (
var end = 128;
headings[j] < textLength - this.config.clipHeadingRight ||
var gapMark = ' [...] ';
headings[j] < rangeRightMin
if (title.length > max) {
) {
title = title.substr(0, max - gapMark.length - end) + gapMark + title.substr(title.length - end);
break;
}
}
title = wDiff.HtmlEscape(title);
rangeRight = headings[j];
title = title.replace(/\t/g, '&nbsp;&nbsp;');
rangeRightType = 'heading';
title = title.replace(/ /g, '&nbsp;&nbsp;');
break;
text = text.replace(/\{title\}/, ' title="' + title + '"');
}
}
}
else {
text = text.replace(/\{title\}/, '');
}
return text;
};
 
// Find last paragraph from right
if ( rangeRight === null ) {
for ( var j = paragraphs.length - 1; j >= 0 ; j -- ) {
if (
paragraphs[j] < textLength - this.config.clipParagraphRightMax ||
paragraphs[j] < rangeRightMin
) {
break;
}
if ( paragraphs[j] < textLength - this.config.clipParagraphRightMin ) {
rangeRight = paragraphs[j];
rangeRightType = 'paragraph';
break;
}
}
}
 
// Find last line break from right
// wDiff.HtmlEscape: replace html-sensitive characters in output text with character entities
if ( rangeRight === null ) {
// input: text
for ( var j = lines.length - 1; j >= 0; j -- ) {
// returns: escaped text
if (
// called from: wDiff.Diff(), wDiff.AssembleDiff()
lines[j] < textLength - this.config.clipLineRightMax ||
lines[j] < rangeRightMin
) {
break;
}
if ( lines[j] < textLength - this.config.clipLineRightMin ) {
rangeRight = lines[j];
rangeRightType = 'line';
break;
}
}
}
 
// Find last blank from right
wDiff.HtmlEscape = function (text) {
if ( rangeRight === null ) {
var startPos = textLength - this.config.clipBlankRightMax;
if ( startPos < rangeRightMin ) {
startPos = rangeRightMin;
}
this.config.regExp.clipBlank.lastIndex = startPos;
var lastPos = null;
while ( ( regExpMatch = this.config.regExp.clipBlank.exec( text ) ) !== null ) {
if ( regExpMatch.index > textLength - this.config.clipBlankRightMin ) {
if ( lastPos !== null ) {
rangeRight = lastPos;
rangeRightType = 'blank';
}
break;
}
lastPos = regExpMatch.index;
}
}
 
// Fixed number of chars from right
text = text.replace(/&/g, '&amp;');
if ( rangeRight === null ) {
text = text.replace(/</g, '&lt;');
if ( textLength - this.config.clipCharsRight > rangeRightMin ) {
text = text.replace(/>/g, '&gt;');
rangeRight = textLength - this.config.clipCharsRight;
text = text.replace(/"/g, '&quot;');
rangeRightType = 'chars';
return (text);
}
};
}
 
// Fixed number of lines from right
if ( rangeRight === null ) {
rangeRight = rangeRightMin;
rangeRightType = 'fixed';
}
}
 
// Check if we skip clipping if ranges are close together
// wDiff.HtmlFormatBlock: markup newlines and spaces in blocks
if ( rangeLeft !== null && rangeRight !== null ) {
// called from: wDiff.Diff(), wDiff.AssembleDiff()
//
 
// Skip if overlapping ranges
wDiff.HtmlFormatBlock = function (string) {
if ( rangeLeft > rangeRight ) {
continue;
// spare blanks in tags
}
string = string.replace(/(<[^>]*>)|( )/g, function (p, p1, p2) {
if (p2 == ' ') {
return wDiff.htmlSpace;
}
return p1;
});
string = string.replace(/\n/g, wDiff.htmlNewline);
return string;
};
 
// Skip if chars too close
var skipChars = rangeRight - rangeLeft;
if ( skipChars < this.config.clipSkipChars ) {
continue;
}
 
// Skip if lines too close
// wDiff.HtmlFormat: tidy html, join chained markup, markup tabs, add container
var skipLines = 0;
// changes: text.diff
var linesLength = lines.length;
// called from: wDiff.Diff(), wDiff.AssembleDiff()
for ( var j = 0; j < linesLength; j ++ ) {
if ( lines[j] > rangeRight || skipLines > this.config.clipSkipLines ) {
break;
}
if ( lines[j] > rangeLeft ) {
skipLines ++;
}
}
if ( skipLines < this.config.clipSkipLines ) {
continue;
}
}
 
// Skip if nothing to clip
wDiff.HtmlFormat = function (text) {
if ( rangeLeft === null && rangeRight === null ) {
continue;
}
 
// Split left text
text.diff = text.diff.replace(/<\/(\w+)><!--wDiff(Delete|Insert)--><\1\b[^>]*\bclass="wDiff\2"[^>]*>/g, '');
var textLeft = null;
text.diff = text.diff.replace(/\t/g, wDiff.htmlTab);
var omittedLeft = null;
text.diff = wDiff.htmlContainerStart + wDiff.htmlFragmentStart + text.diff + wDiff.htmlFragmentEnd + wDiff.htmlContainerEnd;
if ( rangeLeft !== null ) {
return;
textLeft = text.slice( 0, rangeLeft );
};
 
// Remove trailing empty lines
textLeft = textLeft.replace( this.config.regExp.clipTrimNewLinesLeft, '' );
 
// Get omission indicators, remove trailing blanks
// wDiff.ShortenOutput: shorten diff html by removing unchanged parts
if ( rangeLeftType === 'chars' ) {
// input: diff html string from wDiff.Diff()
omittedLeft = '~';
// returns: shortened html with removed unchanged passages indicated by (...) or separator
textLeft = textLeft.replace( this.config.regExp.clipTrimBlanksLeft, '' );
}
else if ( rangeLeftType === 'blank' ) {
omittedLeft = ' ~';
textLeft = textLeft.replace( this.config.regExp.clipTrimBlanksLeft, '' );
}
}
 
// Split right text
wDiff.ShortenOutput = function (html) {
var textRight = null;
var omittedRight = null;
if ( rangeRight !== null ) {
textRight = text.slice( rangeRight );
 
// Remove leading empty lines
var diff = '';
textRight = textRight.replace( this.config.regExp.clipTrimNewLinesRight, '' );
 
// Get omission indicators, remove leading blanks
// wikEd.debugTimer.push(['shorten?', new Date]);
if ( rangeRightType === 'chars' ) {
omittedRight = '~';
textRight = textRight.replace( this.config.regExp.clipTrimBlanksRight, '' );
}
else if ( rangeRightType === 'blank' ) {
omittedRight = '~ ';
textRight = textRight.replace( this.config.regExp.clipTrimBlanksRight, '' );
}
}
 
// Remove split element
// empty text
fragments.splice( fragment, 1 );
if ( (html === undefined) || (html === '') ) {
fragmentsLength --;
return '';
}
 
// Add left text to fragments list
// remove container by non-regExp replace
if ( rangeLeft !== null ) {
html = html.replace(wDiff.htmlContainerStart, '');
fragments.splice( fragment ++, 0, { text: textLeft, type: '=', color: null } );
html = html.replace(wDiff.htmlFragmentStart, '');
fragmentsLength ++;
html = html.replace(wDiff.htmlFragmentEnd, '');
if ( omittedLeft !== null ) {
html = html.replace(wDiff.htmlContainerEnd, '');
fragments.splice( fragment ++, 0, { text: '', type: omittedLeft, color: null } );
fragmentsLength ++;
}
}
 
// Add fragment container and separator to list
// scan for diff html tags
if ( rangeLeft !== null && rangeRight !== null ) {
var regExpDiff = /<\w+\b[^>]*\bclass="[^">]*?\bwDiff(MarkLeft|MarkRight|BlockLeft|BlockRight|Delete|Insert)\b[^">]*"[^>]*>(.|\n)*?<!--wDiff\1-->/g;
fragments.splice( fragment ++, 0, { text: '', type: ']', color: null } );
var tagsStart = [];
fragments.splice( fragment ++, 0, { text: '', type: ',', color: null } );
var tagsEnd = [];
fragments.splice( fragment ++, 0, { text: '', type: '[', color: null } );
var i = 0;
fragmentsLength += 3;
var regExpMatch;
}
 
// Add right text to fragments list
// save tag positions
while if ( (regExpMatch = regExpDiff.exec(html))rangeRight !== null ) {
if ( omittedRight !== null ) {
fragments.splice( fragment ++, 0, { text: '', type: omittedRight, color: null } );
fragmentsLength ++;
}
fragments.splice( fragment ++, 0, { text: textRight, type: '=', color: null } );
fragmentsLength ++;
}
}
 
// Debug log
// combine consecutive diff tags
if ( (i > 0) && (tagsEnd[i - 1]this.config.debug === regExpMatch.index)true ) {
this.debugFragments( 'Fragments' );
tagsEnd[i - 1] = regExpMatch.index + regExpMatch[0].length;
}
 
else {
return;
tagsStart[i] = regExpMatch.index;
};
tagsEnd[i] = regExpMatch.index + regExpMatch[0].length;
 
i ++;
 
/**
* Create html formatted diff code from diff fragments.
*
* @param[in] array fragments Fragments array, abstraction layer for diff code
* @param string|undefined version
* Output version: 'new' or 'old': only text from new or old version, used for unit tests
* @param[out] string html Html code of diff
*/
this.getDiffHtml = function ( version ) {
 
var fragments = this.fragments;
 
// No change, only one unchanged block in containers
if ( fragments.length === 5 && fragments[2].type === '=' ) {
this.html = '';
return;
}
}
 
// Cycle through fragments
// no diff tags detected
var htmlFragments = [];
if (tagsStart.length === 0) {
var fragmentsLength = fragments.length;
return wDiff.htmlNoChange;
for ( var fragment = 0; fragment < fragmentsLength; fragment ++ ) {
}
var text = fragments[fragment].text;
var type = fragments[fragment].type;
var color = fragments[fragment].color;
var html = '';
 
// Test if text is blanks-only or a single character
// define regexps
var blank = false;
var regExpLine = /^(\n+|.)|(\n+|.)$|\n+/g;
if ( text !== '' ) {
var regExpHeading = /(^|\n)(<[^>]+>)*(==+.+?==+|\{\||\|\}).*?\n?/g;
blank = this.config.regExp.blankBlock.test( text );
var regExpParagraph = /^(\n\n+|.)|(\n\n+|.)$|\n\n+/g;
}
var regExpBlank = /(<[^>]+>)*\s+/g;
 
// Add container start markup
// get line positions
if ( type === '{' ) {
var regExpMatch;
html = this.config.htmlCode.containerStart;
var lines = [];
}
while ( (regExpMatch = regExpLine.exec(html)) !== null) {
lines.push(regExpMatch.index);
}
 
// Add container end markup
// get heading positions
else if ( type === '}' ) {
var headings = [];
html = this.config.htmlCode.containerEnd;
var headingsEnd = [];
}
while ( (regExpMatch = regExpHeading.exec(html)) !== null ) {
headings.push(regExpMatch.index);
headingsEnd.push(regExpMatch.index + regExpMatch[0].length);
}
 
// Add fragment start markup
// get paragraph positions
if ( type === '[' ) {
var paragraphs = [];
html = this.config.htmlCode.fragmentStart;
while ( (regExpMatch = regExpParagraph.exec(html)) !== null ) {
}
paragraphs.push(regExpMatch.index);
}
 
// determineAdd fragment borderend positions around diff tagsmarkup
else if ( type === ']' ) {
var lineMaxBefore = 0;
html = this.config.htmlCode.fragmentEnd;
var headingBefore = 0;
}
var paragraphBefore = 0;
var lineBefore = 0;
 
// Add fragment separator markup
var lineMaxAfter = 0;
else if ( type === ',' ) {
var headingAfter = 0;
html = this.config.htmlCode.separator;
var paragraphAfter = 0;
}
var lineAfter = 0;
 
// Add omission markup
var rangeStart = [];
if ( type === '~' ) {
var rangeEnd = [];
html = this.config.htmlCode.omittedChars;
var rangeStartType = [];
}
var rangeEndType = [];
 
// Add omission markup
// cycle through diff tag start positions
for if (var itype === 0; i < tagsStart.length;' i~' ++) {
html = ' ' + this.config.htmlCode.omittedChars;
var tagStart = tagsStart[i];
}
var tagEnd = tagsEnd[i];
 
// Add omission markup
// maximal lines to search before diff tag
if ( type === '~ ' ) {
var rangeStartMin = 0;
html = this.config.htmlCode.omittedChars + ' ';
for (var j = lineMaxBefore; j < lines.length - 1; j ++) {
if (tagStart < lines[j + 1]) {
if (j - wDiff.linesBeforeMax >= 0) {
rangeStartMin = lines[j - wDiff.linesBeforeMax];
}
lineMaxBefore = j;
break;
}
}
 
// Add colored left-pointing block start markup
// find last heading before diff tag
else if (rangeStart[i] type === undefined'(<' ) {
if ( version !== 'old' ) {
for (var j = headingBefore; j < headings.length - 1; j ++) {
 
if (headings[j] > tagStart) {
break;// Get title
var title;
if ( this.config.noUnicodeSymbols === true ) {
title = this.config.msg['wiked-diff-block-left-nounicode'];
}
else {
title = this.config.msg['wiked-diff-block-left'];
}
 
// Get html
if ( this.config.coloredBlocks === true ) {
html = this.config.htmlCode.blockColoredStart;
}
else {
html = this.config.htmlCode.blockStart;
}
html = this.htmlCustomize( html, color, title );
}
}
if (headings[j + 1] > tagStart) {
 
if ( (headings[j] > tagStart - wDiff.headingBefore) && (headings[j] > rangeStartMin) ) {
// Add colored right-pointing block start markup
rangeStart[i] = headings[j];
else if ( type === '(>' ) {
rangeStartType[i] = 'heading';
if ( version !== 'old' ) {
headingBefore = j;
 
// Get title
var title;
if ( this.config.noUnicodeSymbols === true ) {
title = this.config.msg['wiked-diff-block-right-nounicode'];
}
break;else {
title = this.config.msg['wiked-diff-block-right'];
}
 
// Get html
if ( this.config.coloredBlocks === true ) {
html = this.config.htmlCode.blockColoredStart;
}
else {
html = this.config.htmlCode.blockStart;
}
html = this.htmlCustomize( html, color, title );
}
}
}
 
// findAdd lastcolored paragraphblock beforeend diff tagmarkup
else if (rangeStart[i] type === undefined' )' ) {
if ( version !== 'old' ) {
for (var j = paragraphBefore; j < paragraphs.length - 1; j ++) {
html = this.config.htmlCode.blockEnd;
if (paragraphs[j] > tagStart) {
break;
}
}
if (paragraphs[j + 1] > tagStart - wDiff.paragraphBeforeMin) {
 
if ( (paragraphs[j] > tagStart - wDiff.paragraphBeforeMax) && (paragraphs[j] > rangeStartMin) ) {
// Add '=' (unchanged) text and moved block
rangeStart[i] = paragraphs[j];
if ( type === '=' ) {
rangeStartType[i] = 'paragraph';
text = this.htmlEscape( text );
paragraphBefore = j;
if ( color !== null ) {
if ( version !== 'old' ) {
html = this.markupBlanks( text, true );
}
break;}
else {
html = this.markupBlanks( text );
}
}
}
 
// Add '-' text
// find last line break before diff tag
else if (rangeStart[i] type === undefined'-' ) {
if ( version !== 'new' ) {
for (var j = lineBefore; j < lines.length - 1; j ++) {
 
if (lines[j + 1] > tagStart - wDiff.lineBeforeMin) {
// For old version skip '-' inside moved group
if ( (lines[j] > tagStart - wDiff.lineBeforeMax) && (lines[j] > rangeStartMin) ) {
if ( version !== 'old' || color === null ) {
rangeStart[i] = lines[j];
text = this.htmlEscape( text );
rangeStartType[i] = 'line';
text = this.markupBlanks( text, true );
lineBefore = j;
if ( blank === true ) {
html = this.config.htmlCode.deleteStartBlank;
}
else {
html = this.config.htmlCode.deleteStart;
}
html += text + this.config.htmlCode.deleteEnd;
}
break;
}
}
}
 
// Add '+' text
// find last blank before diff tag
else if (rangeStart[i] type === undefined'+' ) {
if ( version !== 'old' ) {
var lastPos = tagStart - wDiff.blankBeforeMax;
text = this.htmlEscape( text );
if (lastPos < rangeStartMin) {
text = this.markupBlanks( text, true );
lastPos = rangeStartMin;
if ( blank === true ) {
html = this.config.htmlCode.insertStartBlank;
}
else {
html = this.config.htmlCode.insertStart;
}
html += text + this.config.htmlCode.insertEnd;
}
}
 
regExpBlank.lastIndex = lastPos;
// Add '<' and '>' code
while ( (regExpMatch = regExpBlank.exec(html)) !== null ) {
else if ( type === '<' || type === '>' ) {
if (regExpMatch.index > tagStart - wDiff.blankBeforeMin) {
if ( version !== 'new' ) {
rangeStart[i] = lastPos;
 
rangeStartType[i] = 'blank';
// Display as deletion at original position
break;
if ( this.config.showBlockMoves === false || version === 'old' ) {
text = this.htmlEscape( text );
text = this.markupBlanks( text, true );
if ( version === 'old' ) {
if ( this.config.coloredBlocks === true ) {
html =
this.htmlCustomize( this.config.htmlCode.blockColoredStart, color ) +
text +
this.config.htmlCode.blockEnd;
}
else {
html =
this.htmlCustomize( this.config.htmlCode.blockStart, color ) +
text +
this.config.htmlCode.blockEnd;
}
}
else {
if ( blank === true ) {
html =
this.config.htmlCode.deleteStartBlank +
text +
this.config.htmlCode.deleteEnd;
}
else {
html = this.config.htmlCode.deleteStart + text + this.config.htmlCode.deleteEnd;
}
}
}
 
// Display as mark
else {
if ( type === '<' ) {
if ( this.config.coloredBlocks === true ) {
html = this.htmlCustomize( this.config.htmlCode.markLeftColored, color, text );
}
else {
html = this.htmlCustomize( this.config.htmlCode.markLeft, color, text );
}
}
else {
if ( this.config.coloredBlocks === true ) {
html = this.htmlCustomize( this.config.htmlCode.markRightColored, color, text );
}
else {
html = this.htmlCustomize( this.config.htmlCode.markRight, color, text );
}
}
}
}
lastPos = regExpMatch.index;
}
htmlFragments.push( html );
}
 
// Join fragments
// fixed number of chars before diff tag
this.html = htmlFragments.join( '' );
if (rangeStart[i] === undefined) {
 
if (tagStart - wDiff.charsBefore > rangeStartMin) {
return;
rangeStart[i] = tagStart - wDiff.charsBefore;
};
rangeStartType[i] = 'chars';
 
 
/**
* Customize html code fragments.
* Replaces:
* {number}: class/color/block/mark/id number
* {title}: title attribute (popup)
* {nounicode}: noUnicodeSymbols fallback
* input: html, number: block number, title: title attribute (popup) text
*
* @param string html Html code to be customized
* @return string Customized html code
*/
this.htmlCustomize = function ( html, number, title ) {
 
// Replace {number} with class/color/block/mark/id number
html = html.replace( /\{number\}/g, number);
 
// Replace {nounicode} with wikEdDiffNoUnicode class name
if ( this.config.noUnicodeSymbols === true ) {
html = html.replace( /\{nounicode\}/g, ' wikEdDiffNoUnicode');
}
else {
html = html.replace( /\{nounicode\}/g, '');
}
 
// Shorten title text, replace {title}
if ( title !== undefined ) {
var max = 512;
var end = 128;
var gapMark = ' [...] ';
if ( title.length > max ) {
title =
title.substr( 0, max - gapMark.length - end ) +
gapMark +
title.substr( title.length - end );
}
title = this.htmlEscape( title );
title = title.replace( /\t/g, '&nbsp;&nbsp;');
title = title.replace( / /g, '&nbsp;&nbsp;');
html = html.replace( /\{title\}/, title);
}
return html;
};
 
 
// fixed number of lines before diff tag
/**
if (rangeStart[i] === undefined) {
* Replace html-sensitive characters in output text with character entities.
rangeStart[i] = rangeStartMin;
*
rangeStartType[i] = 'lines';
* @param string html Html code to be escaped
* @return string Escaped html code
*/
this.htmlEscape = function ( html ) {
 
html = html.replace( /&/g, '&amp;');
html = html.replace( /</g, '&lt;');
html = html.replace( />/g, '&gt;');
html = html.replace( /"/g, '&quot;');
return html;
};
 
 
/**
* Markup tabs, newlines, and spaces in diff fragment text.
*
* @param bool highlight Highlight newlines and spaces in addition to tabs
* @param string html Text code to be marked-up
* @return string Marked-up text
*/
this.markupBlanks = function ( html, highlight ) {
 
if ( highlight === true ) {
html = html.replace( / /g, this.config.htmlCode.space);
html = html.replace( /\n/g, this.config.htmlCode.newline);
}
html = html.replace( /\t/g, this.config.htmlCode.tab);
return html;
};
 
 
// maximal lines to search after diff tag
/**
var rangeEndMax = html.length;
* Count real words in text.
for (var j = lineMaxAfter; j < lines.length; j ++) {
*
if (lines[j] > tagEnd) {
* @param string text Text for word counting
if (j + wDiff.linesAfterMax < lines.length) {
* @return int Number of words in text
rangeEndMax = lines[j + wDiff.linesAfterMax];
*/
}
this.wordCount = function ( text ) {
lineMaxAfter = j;
 
break;
return ( text.match( this.config.regExp.countWords ) || [] ).length;
}
};
 
 
/**
* Test diff code for consistency with input versions.
* Prints results to debug console.
*
* @param[in] WikEdDiffText newText, oldText Text objects
*/
this.unitTests = function () {
 
// Check if output is consistent with new text
this.getDiffHtml( 'new' );
var diff = this.html.replace( /<[^>]*>/g, '');
var text = this.htmlEscape( this.newText.text );
if ( diff !== text ) {
console.log(
'Error: wikEdDiff unit test failure: diff not consistent with new text version!'
);
this.error = true;
console.log( 'new text:\n', text );
console.log( 'new diff:\n', diff );
}
else {
console.log( 'OK: wikEdDiff unit test passed: diff consistent with new text.' );
}
 
// Check if output is consistent with old text
// find first heading after diff tag
this.getDiffHtml( 'old' );
if (rangeEnd[i] === undefined) {
var diff = this.html.replace( /<[^>]*>/g, '');
for (var j = headingAfter; j < headingsEnd.length; j ++) {
var text = this.htmlEscape( this.oldText.text );
if (headingsEnd[j] > tagEnd) {
if ( diff !== text ) {
if ( (headingsEnd[j] < tagEnd + wDiff.headingAfter) && (headingsEnd[j] < rangeEndMax) ) {
console.log(
rangeEnd[i] = headingsEnd[j];
'Error: wikEdDiff unit test failure: diff not consistent with old text version!'
rangeEndType[i] = 'heading';
);
paragraphAfter = j;
this.error = true;
}
console.log( 'old text:\n', text );
break;
console.log( 'old diff:\n', diff );
}
}
else {
console.log( 'OK: wikEdDiff unit test passed: diff consistent with old text.' );
}
 
return;
// find first paragraph after diff tag
};
if (rangeEnd[i] === undefined) {
 
for (var j = paragraphAfter; j < paragraphs.length; j ++) {
 
if (paragraphs[j] > tagEnd + wDiff.paragraphAfterMin) {
/**
if ( (paragraphs[j] < tagEnd + wDiff.paragraphAfterMax) && (paragraphs[j] < rangeEndMax) ) {
* Dump blocks object to browser console.
rangeEnd[i] = paragraphs[j];
*
rangeEndType[i] = 'paragraph';
* @param string name Block name
paragraphAfter = j;
* @param[in] array blocks Blocks table object
}
*/
break;
this.debugBlocks = function ( name, blocks ) {
}
 
}
if ( blocks === undefined ) {
blocks = this.blocks;
}
var dump =
'\ni \toldBl \tnewBl \toldNm \tnewNm \toldSt \tcount \tuniq' +
'\twords \tchars \ttype \tsect \tgroup \tfixed \tmoved \ttext\n';
var blocksLength = blocks.length;
for ( var i = 0; i < blocksLength; i ++ ) {
dump +=
i + ' \t' + blocks[i].oldBlock + ' \t' + blocks[i].newBlock + ' \t' +
blocks[i].oldNumber + ' \t' + blocks[i].newNumber + ' \t' + blocks[i].oldStart + ' \t' +
blocks[i].count + ' \t' + blocks[i].unique + ' \t' + blocks[i].words + ' \t' +
blocks[i].chars + ' \t' + blocks[i].type + ' \t' + blocks[i].section + ' \t' +
blocks[i].group + ' \t' + blocks[i].fixed + ' \t' + blocks[i].moved + ' \t' +
this.debugShortenText( blocks[i].text ) + '\n';
}
console.log( name + ':\n' + dump );
};
 
 
// find first line break after diff tag
/**
if (rangeEnd[i] === undefined) {
* Dump groups object to browser console.
for (var j = lineAfter; j < lines.length; j ++) {
*
if (lines[j] > tagEnd + wDiff.lineAfterMin) {
* @param string name Group name
if ( (lines[j] < tagEnd + wDiff.lineAfterMax) && (lines[j] < rangeEndMax) ) {
* @param[in] array groups Groups table object
rangeEnd[i] = lines[j];
*/
rangeEndType[i] = 'line';
this.debugGroups = function ( name, groups ) {
lineAfter = j;
 
}
if ( groups === undefined ) {
break;
groups = this.groups;
}
}
}
var dump =
'\ni \toldNm \tblSta \tblEnd \tuniq \tmaxWo' +
'\twords \tchars \tfixed \toldNm \tmFrom \tcolor\n';
var groupsLength = groupsLength;
for ( var i = 0; i < groups.length; i ++ ) {
dump +=
i + ' \t' + groups[i].oldNumber + ' \t' + groups[i].blockStart + ' \t' +
groups[i].blockEnd + ' \t' + groups[i].unique + ' \t' + groups[i].maxWords + ' \t' +
groups[i].words + ' \t' + groups[i].chars + ' \t' + groups[i].fixed + ' \t' +
groups[i].oldNumber + ' \t' + groups[i].movedFrom + ' \t' + groups[i].color + '\n';
}
console.log( name + ':\n' + dump );
};
 
 
// find blank after diff tag
/**
if (rangeEnd[i] === undefined) {
* Dump fragments array to browser console.
regExpBlank.lastIndex = tagEnd + wDiff.blankAfterMin;
*
if ( (regExpMatch = regExpBlank.exec(html)) !== null ) {
* @param string name Fragments name
if ( (regExpMatch.index < tagEnd + wDiff.blankAfterMax) && (regExpMatch.index < rangeEndMax) ) {
* @param[in] array fragments Fragments array
rangeEnd[i] = regExpMatch.index;
*/
rangeEndType[i] = 'blank';
this.debugFragments = function ( name ) {
}
 
}
var fragments = this.fragments;
var dump = '\ni \ttype \tcolor \ttext\n';
var fragmentsLength = fragments.length;
for ( var i = 0; i < fragmentsLength; i ++ ) {
dump +=
i + ' \t"' + fragments[i].type + '" \t' + fragments[i].color + ' \t' +
this.debugShortenText( fragments[i].text, 120, 40 ) + '\n';
}
console.log( name + ':\n' + dump );
};
 
 
// fixed number of chars after diff tag
/**
if (rangeEnd[i] === undefined) {
* Dump borders array to browser console.
if (tagEnd + wDiff.charsAfter < rangeEndMax) {
*
rangeEnd[i] = tagEnd + wDiff.charsAfter;
* @param string name Arrays name
rangeEndType[i] = 'chars';
* @param[in] array border Match border array
}
*/
this.debugBorders = function ( name, borders ) {
 
var dump = '\ni \t[ new \told ]\n';
var bordersLength = borders.length;
for ( var i = 0; i < bordersLength; i ++ ) {
dump += i + ' \t[ ' + borders[i][0] + ' \t' + borders[i][1] + ' ]\n';
}
console.log( name, dump );
};
 
 
// fixed number of lines after diff tag
/**
if (rangeEnd[i] === undefined) {
* Shorten text for dumping.
rangeEnd[i] = rangeEndMax;
*
rangeEndType[i] = 'lines';
* @param string text Text to be shortened
* @param int max Max length of (shortened) text
* @param int end Length of trailing fragment of shortened text
* @return string Shortened text
*/
this.debugShortenText = function ( text, max, end ) {
 
if ( typeof text !== 'string' ) {
text = text.toString();
}
text = text.replace( /\n/g, '\\n');
}
text = text.replace( /\t/g, ' ');
if ( max === undefined ) {
max = 50;
}
if ( end === undefined ) {
end = 15;
}
if ( text.length > max ) {
text = text.substr( 0, max - 1 - end ) + '…' + text.substr( text.length - end );
}
return '"' + text + '"';
};
 
// remove overlaps, join close fragments
var fragmentStart = [];
var fragmentEnd = [];
var fragmentStartType = [];
var fragmentEndType = [];
fragmentStart[0] = rangeStart[0];
fragmentEnd[0] = rangeEnd[0];
fragmentStartType[0] = rangeStartType[0];
fragmentEndType[0] = rangeEndType[0];
var j = 1;
for (var i = 1; i < rangeStart.length; i ++) {
 
/**
// get lines between fragments
* Start timer 'label', analogous to JavaScript console timer.
var lines = 0;
* Usage: this.time( 'label' );
if (fragmentEnd[j - 1] < rangeStart[i]) {
*
var join = html.substring(fragmentEnd[j - 1], rangeStart[i]);
* @param string label Timer label
lines = (join.match(/\n/g) || []).length;
* @param[out] array timer Current time in milliseconds (float)
*/
this.time = function ( label ) {
 
this.timer[label] = new Date().getTime();
return;
};
 
 
/**
* Stop timer 'label', analogous to JavaScript console timer.
* Logs time in milliseconds since start to browser console.
* Usage: this.timeEnd( 'label' );
*
* @param string label Timer label
* @param bool noLog Do not log result
* @return float Time in milliseconds
*/
this.timeEnd = function ( label, noLog ) {
 
var diff = 0;
if ( this.timer[label] !== undefined ) {
var start = this.timer[label];
var stop = new Date().getTime();
diff = stop - start;
this.timer[label] = undefined;
if ( noLog !== true ) {
console.log( label + ': ' + diff.toFixed( 2 ) + ' ms' );
}
}
return diff;
};
 
 
if ( (rangeStart[i] > fragmentEnd[j - 1] + wDiff.fragmentJoinChars) || (lines > wDiff.fragmentJoinLines) ) {
/**
fragmentStart[j] = rangeStart[i];
* Log recursion timer results to browser console.
fragmentEnd[j] = rangeEnd[i];
* Usage: this.timeRecursionEnd();
fragmentStartType[j] = rangeStartType[i];
*
fragmentEndType[j] = rangeEndType[i];
* @param string text Text label for output
j ++;
* @param[in] array recursionTimer Accumulated recursion times
*/
this.timeRecursionEnd = function ( text ) {
 
if ( this.recursionTimer.length > 1 ) {
 
// Subtract times spent in deeper recursions
var timerEnd = this.recursionTimer.length - 1;
for ( var i = 0; i < timerEnd; i ++ ) {
this.recursionTimer[i] -= this.recursionTimer[i + 1];
}
 
// Log recursion times
var timerLength = this.recursionTimer.length;
for ( var i = 0; i < timerLength; i ++ ) {
console.log( text + ' recursion ' + i + ': ' + this.recursionTimer[i].toFixed( 2 ) + ' ms' );
}
}
this.recursionTimer = [];
return;
};
 
 
/**
* Log variable values to debug console.
* Usage: this.debug( 'var', var );
*
* @param string name Object identifier
* @param mixed|undefined name Object to be logged
*/
this.debug = function ( name, object ) {
 
if ( object === undefined ) {
console.log( name );
}
else {
console.log( name + ': ' + object );
fragmentEnd[j - 1] = rangeEnd[i];
fragmentEndType[j - 1] = rangeEndType[i];
}
return;
}
};
 
// assemble the fragments
for (var i = 0; i < fragmentStart.length; i ++) {
 
/**
// get text fragment
* Add script to document head.
var fragment = html.substring(fragmentStart[i], fragmentEnd[i]);
*
fragment = fragment.replace(/^\n+|\n+$/g, '');
* @param string code JavaScript code
*/
this.addScript = function ( code ) {
 
if ( document.getElementById( 'wikEdDiffBlockHandler' ) === null ) {
// add inline marks for omitted chars and words
var script = document.createElement( 'script' );
if (fragmentStart[i] > 0) {
script.id = 'wikEdDiffBlockHandler';
if (fragmentStartType[i] == 'chars') {
if ( script.innerText !== undefined ) {
fragment = wDiff.htmlOmittedChars + fragment;
script.innerText = code;
}
else {
else if (fragmentStartType[i] == 'blank') {
script.textContent = code;
fragment = wDiff.htmlOmittedChars + ' ' + fragment;
}
document.getElementsByTagName( 'head' )[0].appendChild( script );
}
return;
if (fragmentEnd[i] < html.length) {
};
if (fragmentStartType[i] == 'chars') {
 
fragment = fragment + wDiff.htmlOmittedChars;
 
/**
* Add stylesheet to document head, cross-browser >= IE6.
*
* @param string css CSS code
*/
this.addStyleSheet = function ( css ) {
 
if ( document.getElementById( 'wikEdDiffStyles' ) === null ) {
 
// Replace mark symbols
css = css.replace( /\{cssMarkLeft\}/g, this.config.cssMarkLeft);
css = css.replace( /\{cssMarkRight\}/g, this.config.cssMarkRight);
 
var style = document.createElement( 'style' );
style.id = 'wikEdDiffStyles';
style.type = 'text/css';
if ( style.styleSheet !== undefined ) {
style.styleSheet.cssText = css;
}
else {
else if (fragmentStartType[i] == 'blank') {
style.appendChild( document.createTextNode( css ) );
fragment = fragment + ' ' + wDiff.htmlOmittedChars;
}
document.getElementsByTagName( 'head' )[0].appendChild( style );
}
return;
};
 
// remove leading and trailing empty lines
fragment = fragment.replace(/^\n+|\n+$/g, '');
 
/**
// add fragment separator
* Recursive deep copy from target over source for customization import.
if (i > 0) {
*
diff += wDiff.htmlSeparator;
* @param object source Source object
* @param object target Target object
*/
this.deepCopy = function ( source, target ) {
 
for ( var key in source ) {
if ( Object.prototype.hasOwnProperty.call( source, key ) === true ) {
if ( typeof source[key] === 'object' ) {
this.deepCopy( source[key], target[key] );
}
else {
target[key] = source[key];
}
}
}
return;
};
 
// Initialze WikEdDiff object
// encapsulate span errors
this.init();
diff += wDiff.htmlFragmentStart + fragment + wDiff.htmlFragmentEnd;
};
 
// add to container
diff = wDiff.htmlContainerStart + diff + wDiff.htmlContainerEnd;
 
/**
// WED('diff', diff);
* Data and methods for single text version (old or new one).
*
* @class WikEdDiffText
*/
WikEdDiff.WikEdDiffText = function ( text, parent ) {
 
/** @var WikEdDiff parent Parent object for configuration settings and debugging methods */
// wikEd.debugTimer.push(['shorten=', new Date]);
this.parent = parent;
// wikEd.DebugTimer();
 
/** @var string text Text of this version */
return diff;
this.text = null;
};
 
/** @var array tokens Tokens list */
this.tokens = [];
 
/** @var int first, last First and last index of tokens list */
//
this.first = null;
// wDiff.AddScript: add script to head
this.last = null;
//
 
/** @var array words Word counts for version text */
wDiff.AddScript = function (code) {
this.words = {};
 
var script = document.createElement('script');
script.id = 'wDiffBlockHandler';
if (script.innerText !== undefined) {
script.innerText = code;
}
else {
script.textContent = code;
}
document.getElementsByTagName('head')[0].appendChild(script);
return;
};
 
/**
* Constructor, initialize text object.
*
* @param string text Text of version
* @param WikEdDiff parent Parent, for configuration settings and debugging methods
*/
this.init = function () {
 
if ( typeof text !== 'string' ) {
//
text = text.toString();
// wDiff.AddStyleSheet: add CSS rules to new style sheet, cross-browser >= IE6
}
//
 
// IE / Mac fix
wDiff.AddStyleSheet = function (css) {
this.text = text.replace( /\r\n?/g, '\n');
 
// Parse and count words and chunks for identification of unique real words
var style = document.createElement('style');
if ( this.parent.config.timer === true ) {
style.type = 'text/css';
this.parent.time( 'wordParse' );
if (style.styleSheet !== undefined) {
}
style.styleSheet.cssText = css;
this.wordParse( this.parent.config.regExp.countWords );
}
this.wordParse( this.parent.config.regExp.countChunks );
else {
if ( this.parent.config.timer === true ) {
style.appendChild( document.createTextNode(css) );
this.parent.timeEnd( 'wordParse' );
}
}
document.getElementsByTagName('head')[0].appendChild(style);
return;
};
 
 
//**
* Parse and count words and chunks for identification of unique words.
// wDiff.WordCount: count words in string
*
//
* @param string regExp Regular expression for counting words
* @param[in] string text Text of version
* @param[out] array words Number of word occurrences
*/
this.wordParse = function ( regExp ) {
 
var regExpMatch = this.text.match( regExp );
wDiff.WordCount = function (string) {
if ( regExpMatch !== null ) {
var matchLength = regExpMatch.length;
for (var i = 0; i < matchLength; i ++) {
var word = regExpMatch[i];
if ( Object.prototype.hasOwnProperty.call( this.words, word ) === false ) {
this.words[word] = 1;
}
else {
this.words[word] ++;
}
}
}
return;
};
 
return (string.match(wDiff.regExpWordCount) || []).length;
};
 
/**
* Split text into paragraph, line, sentence, chunk, word, or character tokens.
*
* @param string level Level of splitting: paragraph, line, sentence, chunk, word, or character
* @param int|null token Index of token to be split, otherwise uses full text
* @param[in] string text Full text to be split
* @param[out] array tokens Tokens list
* @param[out] int first, last First and last index of tokens list
*/
this.splitText = function ( level, token ) {
 
var prev = null;
//
var next = null;
// wDiff.DebugText: dump text (text.oldText or text.newText) object
var current = this.tokens.length;
//
var first = current;
var text = '';
 
// Split full text or specified token
wDiff.DebugText = function (text) {
if ( token === undefined ) {
var dump = 'first: ' + text.first + '\tlast: ' + text.last + '\n';
text = this.text;
dump += '\ni \tlink \t(prev \tnext) \t#num \t"token"\n';
}
var i = text.first;
else {
while ( (i !== null) && (text.tokens[i] !== null) ) {
prev = this.tokens[token].prev;
dump += i + ' \t' + text.tokens[i].link + ' \t(' + text.tokens[i].prev + ' \t' + text.tokens[i].next + ') \t#' + text.tokens[i].number + ' \t' + wDiff.DebugShortenString(text.tokens[i].token) + '\n';
i next = textthis.tokens[itoken].next;
text = this.tokens[token].token;
}
}
return dump;
};
 
// Split text into tokens, regExp match as separator
var number = 0;
var split = [];
var regExpMatch;
var lastIndex = 0;
var regExp = this.parent.config.regExp.split[level];
while ( ( regExpMatch = regExp.exec( text ) ) !== null ) {
if ( regExpMatch.index > lastIndex ) {
split.push( text.substring( lastIndex, regExpMatch.index ) );
}
split.push( regExpMatch[0] );
lastIndex = regExp.lastIndex;
}
if ( lastIndex < text.length ) {
split.push( text.substring( lastIndex ) );
}
 
// Cycle through new tokens
//
var splitLength = split.length;
// wDiff.DebugBlocks: dump blocks object
for ( var i = 0; i < splitLength; i ++ ) {
//
 
// Insert current item, link to previous
wDiff.DebugBlocks = function (blocks) {
this.tokens.push( {
var dump = '\ni \toldBl \tnewBl \toldNm \tnewNm \toldSt \tcount \tuniq \twords \tchars \ttype \tsect \tgroup \tfixed \tstring\n';
token: split[i],
for (var i = 0; i < blocks.length; i ++) {
prev: prev,
dump += i + ' \t' + blocks[i].oldBlock + ' \t' + blocks[i].newBlock + ' \t' + blocks[i].oldNumber + ' \t' + blocks[i].newNumber + ' \t' + blocks[i].oldStart + ' \t' + blocks[i].count + ' \t' + blocks[i].unique + ' \t' + blocks[i].words + ' \t' + blocks[i].chars + ' \t' + blocks[i].type + ' \t' + blocks[i].section + ' \t' + blocks[i].group + ' \t' + blocks[i].fixed + ' \t' + wDiff.DebugShortenString(blocks[i].string) + '\n';
next: null,
}
link: null,
return dump;
number: null,
};
unique: false
} );
number ++;
 
// Link previous item to current
if ( prev !== null ) {
this.tokens[prev].next = current;
}
prev = current;
current ++;
}
 
// Connect last new item and existing next item
//
if ( number > 0 && token !== undefined ) {
// wDiff.DebugGroups: dump groups object
if ( prev !== null ) {
//
this.tokens[prev].next = next;
}
if ( next !== null ) {
this.tokens[next].prev = prev;
}
}
 
// Set text first and last token index
wDiff.DebugGroups = function (groups) {
if ( number > 0 ) {
var dump = '\ni \tblSta \tblEnd \tuniq \tmaxWo \twords \tchars \tfixed \toldNm \tmFrom \tcolor \tmoved \tdiff\n';
for (var i = 0; i < groups.length; i ++) {
dump += i + ' \t' + groups[i].blockStart + ' \t' + groups[i].blockEnd + ' \t' + groups[i].unique + ' \t' + groups[i].maxWords + ' \t' + groups[i].words + ' \t' + groups[i].chars + ' \t' + groups[i].fixed + ' \t' + groups[i].oldNumber + ' \t' + groups[i].movedFrom + ' \t' + groups[i].color + ' \t' + groups[i].moved.toString() + ' \t' + wDiff.DebugShortenString(groups[i].diff) + '\n';
}
return dump;
};
 
// Initial text split
if ( token === undefined ) {
this.first = 0;
this.last = prev;
}
 
// First or last token has been split
//
else {
// wDiff.DebugShortenString: shorten string for debugging
if ( token === this.first ) {
//
this.first = first;
}
if ( token === this.last ) {
this.last = prev;
}
}
}
return;
};
 
wDiff.DebugShortenString = function (string) {
if (string === null) {
return 'null';
}
string = string.replace(/\n/g, '\\n');
string = string.replace(/\t/g, ' ');
var max = 100;
if (string.length > max) {
string = string.substr(0, max - 1 - 30) + '…' + string.substr(string.length - 30);
}
return '"' + string + '"';
};
 
/**
* Split unique unmatched tokens into smaller tokens.
*
* @param string level Level of splitting: line, sentence, chunk, or word
* @param[in] array tokens Tokens list
*/
this.splitRefine = function ( regExp ) {
 
// Cycle through tokens list
// initialize wDiff
var i = this.first;
wDiff.Init();
while ( i !== null ) {
 
// Refine unique unmatched tokens into smaller tokens
if ( this.tokens[i].link === null ) {
this.splitText( regExp, i );
}
i = this.tokens[i].next;
}
return;
};
 
 
/**
* Enumerate text token list before detecting blocks.
*
* @param[out] array tokens Tokens list
*/
this.enumerateTokens = function () {
 
// Enumerate tokens list
var number = 0;
var i = this.first;
while ( i !== null ) {
this.tokens[i].number = number;
number ++;
i = this.tokens[i].next;
}
return;
};
 
 
/**
* Dump tokens object to browser console.
*
* @param string name Text name
* @param[in] int first, last First and last index of tokens list
* @param[in] array tokens Tokens list
*/
this.debugText = function ( name ) {
 
var tokens = this.tokens;
var dump = 'first: ' + this.first + '\tlast: ' + this.last + '\n';
dump += '\ni \tlink \t(prev \tnext) \tuniq \t#num \t"token"\n';
var i = this.first;
while ( i !== null ) {
dump +=
i + ' \t' + tokens[i].link + ' \t(' + tokens[i].prev + ' \t' + tokens[i].next + ') \t' +
tokens[i].unique + ' \t#' + tokens[i].number + ' \t' +
parent.debugShortenText( tokens[i].token ) + '\n';
i = tokens[i].next;
}
console.log( name + ':\n' + dump );
return;
};
 
 
// Initialize WikEdDiffText object
this.init();
};
 
// </syntaxhighlight>