User:Polygnotus/Scripts/DiffCompare.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 3:
cache: true
}).then(function () {
console.log('diff_match_patch library loaded successfully');
 
if (typeof diff_match_patch === 'undefined') {
console.error('diff_match_patch libraryis notundefined loadedafter loading');
return;
}
 
function showDiff(text1, text2) {
console.log('showDiff called with:', { text1, text2 });
 
const dmp = new diff_match_patch();
console.log('diff_match_patch instance created');
 
const diffs = dmp.diff_main(text1, text2);
console.log('diffs created:', diffs);
 
dmp.diff_cleanupSemantic(diffs);
console.log('diffs after cleanup:', diffs);
 
let html = '';
for (let i = 0; i < diffs.length; i++) {
console.log(`Processing diff ${i}:`, diffs[i]);
if (!Array.isArray(diffs[i]) || diffs[i].length !== 2) {
console.error(`Invalid diff at index ${i}:`, diffs[i]);
continue;
}
const [op, data] = diffs[i];
switch (op) {
Line 26 ⟶ 40:
html += '<span>' + escapeHtml(data) + '</span>';
break;
default:
console.error(`Unknown diff operation: ${op}`);
}
}
console.log('Final HTML:', html);
return html;
}
Line 44 ⟶ 61:
 
try {
console.log('Attempting to generate diff');
const diffHtml = showDiff(oldText, newText);
console.log('Diff generation successful');
console.log('Diff result:', diffHtml);
// $('#diff-output').html(diffHtml);