Content deleted Content added
Polygnotus (talk | contribs) ←Created page with ' $.ajax('https://cdnjs.cloudflare.com/ajax/libs/diff-match-patch/20121119/diff_match_patch.min.js', { dataType: 'script', cache: true }).then(function () { function showDiff(text1, text2) { const dmp = new diff_match_patch(); const diffs = dmp.diff_main(text1, text2); dmp.diff_cleanupSemantic(diffs); let html = ''; for (let i = 0; i < diffs.length; i++) { const [op, data] = diffs[i];...' |
Polygnotus (talk | contribs) No edit summary |
||
(11 intermediate revisions by the same user not shown) | |||
Line 1:
// https://github.com/google/diff-match-patch
$.ajax('https://
dataType: 'script',
cache: true
}).then(function () {
const DEBUG = false;
function debug(...args) {
if (DEBUG) {
debug('[DiffCompare]', ...args);
}▼
}▼
debug('diff_match_patch library loaded successfully');
if (typeof diff_match_patch === 'undefined') {
console.error('diff_match_patch is undefined after loading');
return;
}
function showDiff(text1, text2) {
debug('showDiff called with:', { text1, text2 });
const dmp = new diff_match_patch();
debug('diff_match_patch instance created');
const diffs = dmp.diff_main(text1, text2);
debug('diffs created:', diffs);
dmp.diff_cleanupSemantic(diffs);
debug('diffs after cleanup:', diffs);
let html = '';
for (let i = 0; i < diffs.length; i++) {
const op = diffs[i][0];
const data = diffs[i][1];
switch (op) {
case
html += '<
break;
case
html += '<
break;
case
html += '<span>' + escapeHtml(data) + '</span>';
break;
default:
console.error(`Unknown diff operation: ${op}`);
}
}
debug('Final HTML:', html);
return html;
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
Line 31 ⟶ 69:
const newText = "The fast brown fox leaps over the lazy cat.";
try {
debug('Attempting to generate diff');
debug('Diff generation successful');
▲ console.log('Diff result:', diffHtml);
const diffContainer = $('<div>')
.attr('id', 'diff-output')
.css({
'border': '1px solid #a2a9b1',
'padding': '10px',
'margin-bottom': '20px',
'background-color': '#f8f9fa'
})
.html('<strong>Diff Result:</strong><br>' + diffHtml);
$('#bodyContent').prepend(diffContainer);
} catch (error) {
console.error('Error generating diff:', error);
}
}).catch(function(error) {
console.error('Error loading diff_match_patch library:', error);
});
|