User:Cacycle/diff.js: Difference between revisions

Content deleted Content added
1.1.3a (September 23, 2014) bugfix slideGaps
1.1.4 (1.1.4) fix tab highlighting, update highlight handler for wikEd diff tool
Line 2:
 
// ==UserScript==
// @name wDiffwikEd diff
// @version 1.1.3a4
// @date September 2325, 2014
// @description improved word-based diff library with block move detection
// @homepage https://en.wikipedia.org/wiki/User:Cacycle/diff
Line 108:
 
// JSHint options: W004: is already defined, W100: character may get silently deleted
/* jshint -W004, -W100, newcap: falsetrue, browser: true, jquery: true, sub: true, bitwise: true, curly: true, evil: true, forin: true, freeze: true, globalstrict: true, immed: true, latedef: true, loopfunc: true, quotmark: single, strict: true, undef: true */
/* global console */
 
Line 125:
// core diff settings
//
 
// Show complete unshortened diff text
if (wDiff.fullDiff === undefined) { wDiff.fullDiff = false; }
 
// enable block move layout with highlighted blocks and marks at their original positions
if (wDiff.showBlockMoves === undefined) { wDiff.showBlockMoves = true; }
 
// furtherrefine resolveword-based replacementsto character-wise from start andbased enddiff
if (wDiff.charDiff === undefined) { wDiff.charDiff = true; }
 
Line 135 ⟶ 138:
if (wDiff.recursiveDiff === undefined) { wDiff.recursiveDiff = true; }
 
// unlinkreject blocks if they are too short and tootheir words are not commonunique
if (wDiff.unlinkBlocks === undefined) { wDiff.unlinkBlocks = true; }
 
// do not reject blocks if longer than this number of real words
// minimal number of real words for a moved block
if (wDiff.blockMinLength === undefined) { wDiff.blockMinLength = 3; }
 
// display blocks in differentdiffering colors (rainbow color scheme)
if (wDiff.coloredBlocks === undefined) { wDiff.coloredBlocks = false; }
 
// show debug infos and stats (block and group data object) in browser console
if (wDiff.debug === undefined) { wDiff.debug = false; }
 
// show debug timing results in browser console
if (wDiff.debugTimetimer === undefined) { wDiff.debugTimetimer = false; }
 
// run unit tests to prove correct working, display results in browser console
if (wDiff.unitTesting === undefined) { wDiff.unitTesting = false; }
 
Line 303 ⟶ 306:
'.wDiffTab { position: relative; }' +
'.wDiffTabSymbol { position: absolute; top: -0.2em; }' +
'.wDiffTabSymbol:before { content: "→"; font-size: smaller; color: transparent; color: #ccc; }' +
'.wDiffBlockLeft .wDiffTabSymbol:before, .wDiffBlockRight .wDiffTabSymbol:before { color: #aaa; }' +
'.wDiffBlockHighlight .wDiffTabSymbol:before { color: #aaa; }' +
Line 437 ⟶ 440:
 
// get mark/block elements
if (wDiff.showBlockMoves === false) {
return;
};
var number = element.id.replace(/\D/g, '');
var block = document.getElementById('wDiffBlock' + number);
Line 455 ⟶ 461:
element.onmouseover = function (event) { wDiff.blockHandler(event, element, 'mouseover'); };
 
// reset, allow outside container (e.g. legend)
// getElementsByClassName
if (fulltype !== true'click') {
var container = document.getElementById('wDiffContainer');
spans[i]block.className = spans[i]block.className.replace(/ wDiffBlockHighlight/g, '');
var spans = container.getElementsByTagName('span');
spans[i]mark.className = spans[i]mark.className.replace(/ wDiffMarkHighlight/g, '');
for (var i = 0; i < spans.length; i ++) {
 
if ( ( (spans[i] != block) && (spans[i] != mark) ) || (type != 'click') ) {
// getElementsByClassName
if (spans[i].className.indexOf(' wDiffBlockHighlight') != -1) {
var container = document.getElementById('wDiffContainer');
spans[i].className = spans[i].className.replace(/ wDiffBlockHighlight/g, '');
if (container !== null) {
}
var spans = container.getElementsByTagName('span');
else if (spans[i].className.indexOf(' wDiffMarkHighlight') != -1) {
for (var i = 0; i < spans.length; i ++) {
spans[i].className = spans[i].className.replace(/ wDiffMarkHighlight/g, '');
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, '');
}
}
}
}
Line 550 ⟶ 564:
// wDiff.diff(): main method of wDiff, runs the diff and shortens the output
// called from: user land
// calls: new TextDiff, TextDiff.shortenOutput(), TextDiff.htmlFormat(), this.unitTests()
 
wDiff.diff = function (oldString, newString, full) {
Line 564 ⟶ 578:
 
// start timer
if (wDiff.debugTimetimer === true) {
console.time('diff');
}
Line 572 ⟶ 586:
 
// start timer
if (wDiff.debugTimetimer === true) {
console.timeEnd('diff');
}
 
// shorten output
if ( ( (full !== undefined) && (full !== true) ) || ( (full === undefined) && (wDiff.fullDiff !== true) ) ) {
if (full !== true) {
 
// start timer
if (wDiff.debugTimetimer === true) {
console.time('shorten');
}
Line 587 ⟶ 601:
 
// stop timer
if (wDiff.debugTimetimer === true) {
console.timeEnd('shorten');
}
}
 
// TextDiff.htmlFormat(): markup tabs, add container
else {
thistextDiff.htmlFormat();
}
 
// stop timer
if (wDiff.debugTimetimer === true) {
console.timeEnd('diff');
}
Line 612 ⟶ 631:
 
// start timer
if (wDiff.debugTimetimer === true) {
console.time('unit tests');
}
Line 652 ⟶ 671:
 
// stop timer
if (wDiff.debugTimetimer === true) {
console.timeEnd('unit tests');
}
Line 1,355 ⟶ 1,374:
 
// start timer
if ( (wDiff.debugTimetimer === true) && (repeat !== true) && (recursionLevel === undefined) ) {
console.time(level);
}
Line 1,784 ⟶ 1,803:
 
// stop timer
if ( (wDiff.debugTimetimer === true) && (repeat !== true) && (recursionLevel === 0) ) {
console.timeEnd(level);
}
Line 2,887 ⟶ 2,906:
// join fragments
this.html = htmlFrags.join('');
 
// markup newlines and spaces in blocks
this.htmlFormat();
 
return;
Line 2,972 ⟶ 2,988:
string = string.replace(/\n/g, wDiff.htmlNewline);
return string;
};
 
 
// TextDiff.htmlFormat(): markup tabs, add container
// changes: .diff
// called from: .diff(), .assembleDiff()
 
this.htmlFormat = function () {
 
this.html = this.html.replace(/\t/g, wDiff.htmlTab);
this.html = wDiff.htmlContainerStart + wDiff.htmlFragmentStart + this.html + wDiff.htmlFragmentEnd + wDiff.htmlContainerEnd;
return;
};
 
Line 2,995 ⟶ 2,999:
var html = this.html;
var diff = '';
 
// remove container by non-regExp replace
html = html.replace(wDiff.htmlContainerStart, '');
html = html.replace(wDiff.htmlFragmentStart, '');
html = html.replace(wDiff.htmlFragmentEnd, '');
html = html.replace(wDiff.htmlContainerEnd, '');
 
// scan for diff html tags
Line 3,316 ⟶ 3,314:
diff += wDiff.htmlFragmentStart + fragment + wDiff.htmlFragmentEnd;
}
this.html = diff;
 
// addmarkup difftabs, add wrappercontainer
this.htmlFormat();
diff = wDiff.htmlContainerStart + diff + wDiff.htmlContainerEnd;
 
return;
this.html = diff;
};
 
 
// TextDiff.htmlFormat(): markup tabs, add container
// changes: .diff
// called from: .diff(), .assembleDiffshortenOutput()
 
this.htmlFormat = function () {
 
this.html = this.html.replace(/\t/g, wDiff.htmlTab);
this.html = wDiff.htmlContainerStart + wDiff.htmlFragmentStart + this.html + wDiff.htmlFragmentEnd + wDiff.htmlContainerEnd;
return;
};