Skip to content

Commit bc45059

Browse files
committed
Other Fix for rowanj#310
Fix for rowanj#310. ``` ... message = "TypeError: 'undefined' is not an object (evaluating 'ns[ n[i] ].rows.push')"; sourceURL = "file:///Applications/GitX.app/Contents/Resources/html/lib/diffHighlighter.js"; ``` I turns out that the `inlinediff.diff()` method had issues caused by the use of Objects as HashMap. That's why @dgreensp added a fix using `Object.prototype.hasOwnProperty` (see rowanj#320). However this solution still fails when the key becomes `"__proto__"`, for some reason. A simpler, saver and yet more efficient fix to the root issue is to just prefix the keys. e.g. `'"' + key`.
1 parent 7ed6a85 commit bc45059

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

html/lib/diffHighlighter.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -465,30 +465,27 @@ var inlinediff = (function () {
465465
];
466466
}
467467

468-
function hasProp(obj, x) {
469-
return Object.prototype.hasOwnProperty.call(obj, x);
470-
}
471-
472468
function diff( o, n ) {
473-
var ns = new Object();
474-
var os = new Object();
469+
var ns = {}, os = {}, k = null, i = 0;
475470

476471
for ( var i = 0; i < n.length; i++ ) {
477-
if ( ! hasProp(ns, n[i]) )
478-
ns[ n[i] ] = { rows: new Array(), o: null };
479-
ns[ n[i] ].rows.push( i );
472+
k = '"' + n[i]; // prefix keys with a quote to not collide with Object's internal keys, e.g. '__proto__' or 'constructor'
473+
if ( ns[k] === undefined )
474+
ns[k] = { rows: [], o: null };
475+
ns[k].rows.push( i );
480476
}
481477

482478
for ( var i = 0; i < o.length; i++ ) {
483-
if ( ! hasProp(os, o[i]) )
484-
os[ o[i] ] = { rows: new Array(), n: null };
485-
os[ o[i] ].rows.push( i );
479+
k = '"' + o[i]
480+
if ( os[k] === undefined )
481+
os[k] = { rows: [], n: null };
482+
os[k].rows.push( i );
486483
}
487484

488-
for ( var i in ns ) {
489-
if ( ns[i].rows.length == 1 && hasProp(os, i) && os[i].rows.length == 1 ) {
490-
n[ ns[i].rows[0] ] = { text: n[ ns[i].rows[0] ], row: os[i].rows[0] };
491-
o[ os[i].rows[0] ] = { text: o[ os[i].rows[0] ], row: ns[i].rows[0] };
485+
for ( var k in ns ) {
486+
if ( ns[k].rows.length == 1 && os[k] !== undefined && os[k].rows.length == 1 ) {
487+
n[ ns[k].rows[0] ] = { text: n[ ns[k].rows[0] ], row: os[k].rows[0] };
488+
o[ os[k].rows[0] ] = { text: o[ os[k].rows[0] ], row: ns[k].rows[0] };
492489
}
493490
}
494491

0 commit comments

Comments
 (0)