Skip to content

Commit 71ecaaa

Browse files
author
David Greenspan
committed
Fix #310 - diff highlighter breaks on some strings
Strings like “constructor” occur as properties on JavaScript objects by default: ``` var x = new Object(); x[“constructor”] // Object ```
1 parent dc8fa78 commit 71ecaaa

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

html/lib/diffHighlighter.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,24 +460,28 @@ var inlinediff = (function () {
460460
];
461461
}
462462

463+
function hasProp(obj, x) {
464+
return Object.prototype.hasOwnProperty.call(obj, x);
465+
}
466+
463467
function diff( o, n ) {
464468
var ns = new Object();
465469
var os = new Object();
466470

467471
for ( var i = 0; i < n.length; i++ ) {
468-
if ( ns[ n[i] ] == null )
472+
if ( ! hasProp(ns, n[i]) )
469473
ns[ n[i] ] = { rows: new Array(), o: null };
470474
ns[ n[i] ].rows.push( i );
471475
}
472476

473477
for ( var i = 0; i < o.length; i++ ) {
474-
if ( os[ o[i] ] == null )
478+
if ( ! hasProp(os, o[i]) )
475479
os[ o[i] ] = { rows: new Array(), n: null };
476480
os[ o[i] ].rows.push( i );
477481
}
478482

479483
for ( var i in ns ) {
480-
if ( ns[i].rows.length == 1 && typeof(os[i]) != "undefined" && os[i].rows.length == 1 ) {
484+
if ( ns[i].rows.length == 1 && hasProp(os, i) && os[i].rows.length == 1 ) {
481485
n[ ns[i].rows[0] ] = { text: n[ ns[i].rows[0] ], row: os[i].rows[0] };
482486
o[ os[i].rows[0] ] = { text: o[ os[i].rows[0] ], row: ns[i].rows[0] };
483487
}

0 commit comments

Comments
 (0)