Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #310 - diff highlighter breaks on some strings #320

Merged
merged 1 commit into from
May 16, 2014

Conversation

dgreensp
Copy link

Strings like “constructor” occur as properties on JavaScript objects by default:

var x = new Object();
x[“constructor”] // Object

Strings like “constructor” occur as properties on JavaScript objects by default:

```
var x = new Object();
x[“constructor”] // Object
```
rowanj added a commit that referenced this pull request May 16, 2014
Fix #310 - diff highlighter breaks on some strings
@rowanj rowanj merged commit 8ed1286 into rowanj:master May 16, 2014
@muhqu
Copy link

muhqu commented May 16, 2014

@dgreensp absolutely the right fix for this issue! 👍

I wonder why I have not ran into that issue since I already use this since 9 month or so. But I'm not that much into JS any more...

@dgreensp
Copy link
Author

Happy to help! I write JavaScript code all day, so I ran into this bug on
my first commit :)

The cool thing was I didn't even have to rebuild GitX to test the fix, I
just fixed the JS in the application bundle.

It's amusing that John Resig himself wrote this bug:
http://ejohn.org/projects/javascript-diff-algorithm/

On Thursday, May 15, 2014, Mathias Leppich notifications@github.com wrote:

@dgreensp https://github.com/dgreensp absolutely the right fix for this
issue! [image: 👍]

I wonder why I have not ran into that issue since I already use this since
9 month or so. But I'm not that much into JS any more...


Reply to this email directly or view it on GitHubhttps://github.com//pull/320#issuecomment-43299467
.

@muhqu
Copy link

muhqu commented May 16, 2014

Maybe @jeresig wants to know about this – eh?

The first thing I did was searching for a diff algorithm already implemented in JavaScript and John's implementation was a perfect starting point. Even-though it was written almost 9 years ago... ;-)

@dgreensp
Copy link
Author

There's still a bug, an even more subtle one.

The word "proto" in the input string will cause the assignment
ns.proto = { rows: [...], ... }. This actually changes the prototype
of "ns" and puts a "rows" property in the prototype chain! Then the code
crashes when iterating over the properties of ns, because ns["rows"] is an
array and doesn't have a "rows" property like all the other ns[i].

The solution is to put a hasProp check at the top of the for (var i in ns)
loop. I'm on my phone or I would make a PR.

On Friday, May 16, 2014, Mathias Leppich notifications@github.com wrote:

Maybe @jeresig https://github.com/jeresig wants to know about this – eh?

The first thing I did was searching for a diff algorithm already
implemented in JavaScript and John's implementationhttp://ejohn.org/projects/javascript-diff-algorithm/was a perfect starting point. Even-though it was written almost 9 years
ago... ;-)


Reply to this email directly or view it on GitHubhttps://github.com//pull/320#issuecomment-43374324
.

muhqu added a commit to muhqu/gitx that referenced this pull request May 19, 2014
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`.
@muhqu muhqu mentioned this pull request May 19, 2014
@muhqu
Copy link

muhqu commented May 19, 2014

@dgreensp I addressed the issue with PR #333

@dgreensp
Copy link
Author

Sounds good!

On Sun, May 18, 2014 at 11:53 PM, Mathias Leppich
notifications@github.comwrote:

@dgreensp https://github.com/dgreensp I addressed the issue with PR #333#333


Reply to this email directly or view it on GitHubhttps://github.com//pull/320#issuecomment-43470896
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants