|
<!DOCTYPE html> |
|
<style type="text/css"> |
|
* { border-collapse: collapse; font-family: sans-serif; } |
|
th, td { border-bottom: 1px solid #BBB; padding: .5em; } |
|
.changed { background: #FFD; } |
|
.changed * + * { background: #EFE; } |
|
strong { background: #FEE; border: 1px solid #FBB; border-radius: 2px; color: #E00; } |
|
</style> |
|
<table> |
|
<tr><th>Old version</th><th>Current version</th></tr> |
|
<tr> |
|
<td>This is a simple diff algorithm.</td> |
|
<td>This is a tiny diff algorithm.</td> |
|
</tr> |
|
<tr> |
|
<td>It's 140 bytes only.</td> |
|
<td>It's 126 bytes only.</td> |
|
</tr> |
|
<tr> |
|
<td>It's made for database records to find the location and length of a single change per field.</td> |
|
<td>It's made for database records to find the position and length of a single change per field.</td> |
|
</tr> |
|
<tr> |
|
<td>It works bets with short values since it can't separate multiple changes.</td> |
|
<td>It works best with short values since it can't seperate multiple changes.</td> |
|
</tr> |
|
<tr> |
|
<td>It should not highlight lines that did not changed.</td> |
|
<td>It should not highlight lines that did not changed.</td> |
|
</tr> |
|
<tr> |
|
<td>It should not fail in confusing confusing cases.</td> |
|
<td>It should not fail in confusing cases.</td> |
|
</tr> |
|
</table> |
|
<script type="text/javascript"> |
|
var diff = |
|
{ |
|
// 124 bytes |
|
findDiffBetweenStrings: function(a, b) //compare these two strings |
|
{ |
|
for (var c = 0, //start from the first character |
|
d = a.length, e = b.length; //and from the last characters of both strings |
|
a[c] && //if not at the end of the string and |
|
a[c] == b[c]; //if both strings are equal at this position |
|
c++); //go forward |
|
for (; d > c & e > c & //stop at the position found by the first loop |
|
a[d - 1] == b[e - 1]; //if both strings are equal at this position |
|
d--) e--; //go backward |
|
return[c, d - c, e - c] //return position and lengths of the two substrings found |
|
}, |
|
highlightDiff: function(a, b, c) //element, position and length of the substring found |
|
{ |
|
if (c) //skip if nothing was found |
|
with (document) |
|
{ |
|
var d = createElement('STRONG'); |
|
d.appendChild(createTextNode(a.data.substr(b, c))); |
|
with (a.parentNode) |
|
appendChild(d), |
|
appendChild(createTextNode(a.data.slice(b + c))), |
|
parentNode.className = 'changed'; |
|
a.data = a.data.slice(0, b); |
|
} |
|
}, |
|
highlight: function(a) //requires a table element |
|
{ |
|
a = a.getElementsByTagName('TR'); |
|
for (var b = a.length; b--; ) //iterate all rows in the table |
|
{ |
|
var c = a[b].getElementsByTagName('TD'); |
|
if (c[1]) |
|
{ |
|
var d = c[0].firstChild, e = c[1].firstChild; |
|
c = this.findDiffBetweenStrings(d.data, e.data); |
|
this.highlightDiff(d, c[0], c[1]); |
|
this.highlightDiff(e, c[0], c[2]); |
|
} |
|
} |
|
} |
|
} |
|
diff.highlight(document.getElementsByTagName('TABLE')[0]); |
|
</script> |
cool!
what is below this line is wrong:
you could save 2 bytes using a plceholder for the 'c' variable inside the for loop