Scaping quotes and backslashes (I hope you understand my pro

Hi you all, I have a weird problem escaping characters.

The user types in a text area the following:
Hello

When the page is rendered with the .to_html of RedCloth this works.

(I’m using a Javscript code called CoolTip, that, when the mouse is over
a word or words with this javascript, a tip appears. In this tip, a link
can be written. The whole sentence about is the called to the
javascript, but this is not important)

I don’t want the user to type this stuff. So, I write that in rails, in
a string:

semantic_link = ‘Hello

But this does not work; what rails renders is a mess. Am I doing
something wrong escaping the rare characters? Cause the code at the very
beginning does work, so the problem isn’t RedCloth or the javascript
call…

Thanks :frowning:

Can you maybe boil it down to a smaller example? The only thing I can
think of is maybe it’s un-escaping the characters too many times, or
not enough times.

Andrew R. wrote:

Can you maybe boil it down to a smaller example?

Yes; I was kind of desperate and I just threw the whole stuff :smiley:

I have to write this code, for a Javscript function:
onmouseover=“coolTip(’<a
href=’/wiki/show_appears?value=Fidel+Castro&name=primaryCharacter’>Appears…</a>’,
STICKY,MOUSEOFF);”

If I write this in a text area, as it is, and then I render the page,
everything is ok. However, this whoole string will be given as a
variable in my app. So I tried with this:
semantic_link = ‘onmouseover="coolTip(’<a
href=\’/wiki/show_appears?value=Fidel+Castro&name=primaryCharacter\’
>Appears…</a>’, STICKY, MOUSEOFF);"’

The problem is in the a href=\’/wiki/ …
If i put 2 backslashes (to return a backslash), and another one to
return the ',
what rails render is a dot. I ve tried with other combinations but…
they didn’t work! How can I do it?

If I hardcode the anchor tag in my rhtml file as follows it works fine
for me. I did have to dispense with the \ though. However, I get the
impression you were not hardcoding this in your view. If your still
having trouble with it, post your controller and view code.

Hello

-Paul

Ok, I understand you need to change it programmatically. I added the \
back in, put it in a string assigned to an instance variable and did a
simple render in the view. It seems to work just fine for me. Here is
the string in my controller:

@testvar = ‘Hello

Here is the rhtml code:

<%= @testvar %>

And here is the html as it appears in the browser when I do view
source:

Hello

By the way I am using Firefox 1.0.5.9 and IE 6.

-Paul

Paul C. wrote:

Ok, I understand you need to change it programmatically. I added the \
back in, put it in a string assigned to an instance variable and did a
simple render in the view. It seems to work just fine for me. Here is
the string in my controller:

@testvar = ‘Hello

Here is the rhtml code:

<%= @testvar %>

And here is the html as it appears in the browser when I do view
source:

Hello

By the way I am using Firefox 1.0.5.9 and IE 6.

-Paul

No, sorry :frowning: It does not work for me. The three backslashes turned into
a dot.
Dunno why :frowning: I’m not using RedCloth at all, I just throw the variable as
you in the rhtml. Can it be the Rails version? I’m using rails 1.1.4 :frowning:

Thanks a lot

Paul C. wrote:

If I hardcode the anchor tag in my rhtml file as follows it works fine
for me. I did have to dispense with the \ though. However, I get the
impression you were not hardcoding this in your view. If your still
having trouble with it, post your controller and view code.

Hello

-Paul

This anchor tag also works for me if I write it as it is in a rhtml
file. But I have to generate it from the application. So I have to write
the anchor inside a string (cause the link /wiki/show… will change
depending on several things). My problem is having the anchor tag inside
a string variable in rails.

I’m narrowing the error…

I have test the variable
var = ‘\’’

and when this var is rendered in an rhtml, it works fine, as you’ve told
me. The render symbols are ’

Buuuuut, what i have to do is programatically substitute a pattern for
the link tag. So when I do:

semantic_body.gsub!(‘test’,var)… this does not work!! It adds a dot.

Why this behaviour???

(in fact, it not only returns a dot, but the following paragrpahs are
mixed in a mess…living the dot a paragraph away instead!! dunno)

I was using rails v1.1.2 but I just upgraded to v1.1.6 and it still
works fine for me. I am using Ruby v1.8.4 on a Windows XP box. I guess
at this point I would check the development.log to see if there is
something in there that might give you a clue. I suppose it’s also
possible a plug-in might be causing the problem Otherwise, I think your
only recourse is to rebuild the environment by installing a clean
version of ruby on rails. Don’t install any plug-ins and give it
another shot.

-Paul

PS:

Damaris - if you can post an example of working code that fails I think
we can figure this out. Show me a real code fragment, something I can
recreate on my end.

Thanks,
Paul

Ok, instead of giving my rails stuff code, try this in a simple Ruby
file:

var = ‘\’’
varTest1 = ‘hola’
print “ARG” + var
print “ERG” + varTest1.gsub!(‘hola’, var)

The two “prints” should return the same, should’n they? But they don’t
:frowning:
The first “print” returns my so lovely ’ but the second returns
nothing.

Could you please try if you obtain the same results? Cause if I have to
uninstall everything… pfff, I’m gonna die :(((

(I have ruby version 1.8.2)

Thanks for your help, really :frowning:

It seems we need to add another set of double backslashes to get the
desired results. I don’t understand this as the documentation says that
on a single quoted string that a double backslash converts to a single
backslash.

puts “ERG” + varTest1.gsub!(‘hola’, ‘\\’’) --> ERG’

-Paul

Paul C. wrote:

It seems we need to add another set of double backslashes to get the
desired results. I don’t understand this as the documentation says that
on a single quoted string that a double backslash converts to a single
backslash.

puts “ERG” + varTest1.gsub!(‘hola’, ‘\\’’) --> ERG’

-Paul

Wow!! It works!! You’ve saved my life!!!
Thank you really much.

I don’t understand either, cause all works fine according to the
documentation but the “gsub” function.

Thanks again, thanks for your help, and patience.

Glad to see it fixed the problem. I’ve added this to my growing list of
gotcha’s. :slight_smile:

Damaris F. wrote:

Paul C. wrote:

It seems we need to add another set of double backslashes to get the
desired results. I don’t understand this as the documentation says that
on a single quoted string that a double backslash converts to a single
backslash.

puts “ERG” + varTest1.gsub!(‘hola’, ‘\\’’) --> ERG’

-Paul

Wow!! It works!! You’ve saved my life!!!
Thank you really much.

I don’t understand either, cause all works fine according to the
documentation but the “gsub” function.

Thanks again, thanks for your help, and patience.

Its because the string is going to the regexp replacement engine.
If you do:

a = ‘\1’

that gives you a slash and a 1 in a string. however:

b = ‘blah’
b.gsub! /(l)a/, a
b

=> ‘blh’, no slash 1 in sight.

Thats because [0-9] is used for the back-references.
A (probably slower) solution, is to avoid this by using the block
approach:

b.gsub!(/(l)a/) { a }

=> “b\1h”