rainbow icon indicating copy to clipboard operation
rainbow copied to clipboard

Problem with the replace function

Open ewino opened this issue 11 years ago • 2 comments

When marking code that has special characters Rainbow sometimes break. In my case I have a matlab code (I created a simple parser for it) and I have a line in my code that starts like this: xlabel('$x$', .... When trying to parse '$x$' as a string, rainbow tries to replace '$x$' with <span class=...>'$x$'</span>. In doing so, _replaceAtPosition calls string.replace, but in JavaScript's replace function "$'" (dollar + single quote) is a special combination (you can read about it here). Why is there a need for the replace call there? When I removed it I couldn't see any negative behavior... I simply replaced _replaceAtPosition like this:

position = Number(position)
return code.substr(0, position) + replace_with + code.substr(position + replace.length);

Is there any problem with this? Maybe this can be added to the next release?

ewino avatar Jul 30 '13 11:07 ewino

Can you give me a simple example of how to reproduce this? Just a Rainbow.extend() call with the pattern and the input code.

I tried using your code for _replaceAtPosition, but that causes a bunch of the unit tests to fail so it might work in your case, but I don't think it will in all cases.

ccampbell avatar Jul 31 '13 01:07 ccampbell

Here's a code that demonstrates this behavior: http://jsfiddle.net/tkdbm/ I put the replacement code here from memory, maybe the indexes are off by one somewhere... What test cases fail?

(sorry for the close/reopen, it's my first time reporting an issue in github and I got confused by the "close" button)

ewino avatar Aug 01 '13 18:08 ewino