CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
escape strings for javascript?

 



leptogenesis
Novice

Jul 15, 2009, 12:46 PM

Post #1 of 12 (1277 views)
escape strings for javascript? Can't Post

I'm writing a perl script that will write some javascript code. Specifically, I have a string in perl that I need to end up in a javascript variable (inside html), so I need to write this string into a javascript string literal. Unfortunately, this string contains quotes, newlines, unicode characters...probably some other stuff too if anything else could possibly cause problems.

So, is there a way to encode the string into javascript, and be sure I've escaped everything that needs to be escaped? I've been looking for a module that can do it for me, but I haven't yet found one...


FishMonger
Veteran / Moderator

Jul 16, 2009, 5:30 AM

Post #2 of 12 (1270 views)
Re: [leptogenesis] escape strings for javascript? [In reply to] Can't Post

Simple solution, you need to learn about Perl's quote-like operators.

http://perldoc.perl.org/perlop.html#Quote-Like-Operators


leptogenesis
Novice

Jul 16, 2009, 7:43 AM

Post #3 of 12 (1264 views)
Re: [FishMonger] escape strings for javascript? [In reply to] Can't Post

Thanks for the reply, but I don't see how quote-like operators will help me. The string is already correctly loaded into a perl variable (it's actually read from a file). I just need something that will let me write the perl variable into javascript code.


FishMonger
Veteran / Moderator

Jul 16, 2009, 7:52 AM

Post #4 of 12 (1263 views)
Re: [leptogenesis] escape strings for javascript? [In reply to] Can't Post


In Reply To
I just need something that will let me write the perl variable into javascript code.


What do you mean by that?

If you already have the string in a perl variable, then simply use the print function to output that string.

Can you post your code and the string in question and point out where you're having the problem?


leptogenesis
Novice

Jul 16, 2009, 10:37 AM

Post #5 of 12 (1258 views)
Re: [FishMonger] escape strings for javascript? [In reply to] Can't Post

The real string is thousands of lines long, so I'll make up a simple example. So imagine the code looks like this:

my $str = "hello\nworld"; #want this value in a javascript variable
my $javascript = "var mystring = \'$str\'";
print JAVSCRIPTFILE $javascript;

What ends up in the javascript is:

var mystring = 'hello
world'

This is incorrect javascript syntax--you can't have multiline strings in javascript. Similarly, if you had

$str = "hello sam\'s world";

Your javascript would look like:

var mystring = 'hello sam's world'

The browser would see the apostrophe in sam's as terminating the string. It would therefore cause an error.

Unfortunately, I don't know enough about javascript to know whether I've handled everything that could cause problems like this--I was hoping that there would be a perl module that would handle the escaping so I didn't have to worry about it.


FishMonger
Veteran / Moderator

Jul 16, 2009, 11:13 AM

Post #6 of 12 (1257 views)
Re: [leptogenesis] escape strings for javascript? [In reply to] Can't Post

Your first example is easily fixed with a simple regex

Code
my $str = "hello\nworld"; 
$str =~ s/\n/ /;
my $javascript = "var mystring = '$str'"; # note that there is no reason to escape the single quotes
print JAVSCRIPTFILE $javascript;


Your second example is easily fixed by using the proper quoting operator.

Code
$str = "hello sam's world";  
my $javascript = qq(var mystring = "$str");
print JAVSCRIPTFILE $javascript;

Note that you can use either single or double quotes when quoting the value of the javascript var.


leptogenesis
Novice

Jul 16, 2009, 11:24 AM

Post #7 of 12 (1255 views)
Re: [FishMonger] escape strings for javascript? [In reply to] Can't Post

I know I can do it the naive way by trying my hardest to think of every character that can show up in this string and thinking about whether each one needs to be escaped. However, I'm relatively new to javascript--I don't trust myself to list off every character that could cause a problem in the javascript. Even if it's only a few lines of perl regex code, I'd much rather have the javascript-escape code be written by somebody who's a javascript expert than have it written by somebody like me.

To put it more plainly, there are exactly two answers I can think of that would solve my problem:

1. a *complete* list of characters that would need to be escaped in order to print the string into javascript.

2. a perl module that already knows this list internally, so I don't have to think about it.

I would be astonished if such a module doesn't already exist in perl. It exists in Java, and Java doesn't claim to be a string processing language:

http://commons.apache.org/lang/api-2.3/org/apache/commons/lang/StringEscapeUtils.html


KevinR
Veteran


Jul 16, 2009, 11:44 AM

Post #8 of 12 (1253 views)
Re: [leptogenesis] escape strings for javascript? [In reply to] Can't Post

Search CPAN then:

http://search.cpan.org/
-------------------------------------------------


FishMonger
Veteran / Moderator

Jul 16, 2009, 11:49 AM

Post #9 of 12 (1253 views)
Re: [leptogenesis] escape strings for javascript? [In reply to] Can't Post

Based on the limited info you've given, it appears that your perl script reads in a large block of javascript code that may not have been well written, which is why you need to escape portions of it.

Is that correct?

Can you post your actual perl code and a more realistic example of the data that needs to be escaped?

Have you looked at any of the javascript modules on CPAN?
http://search.cpan.org/search?query=javascript&mode=all


leptogenesis
Novice

Jul 16, 2009, 12:06 PM

Post #10 of 12 (1251 views)
Re: [FishMonger] escape strings for javascript? [In reply to] Can't Post

I've tried CPAN...no luck, although there were a couple of modules (e.g. Encode::Escape) which look like they might do close to the right thing. I can't figure out exactly what Encode::Escape actually does.

Anyway, you asked for it...I've attached an actual file that I need to embed in javascript. They're characters and bounding boxes for an OCR translation of a document, and will eventually allow people to review the OCR program's translations over mechanical turk. Unfortunately, because of mechanical turk's restrictions, I need to have the entire file in a single variable, since I need to be able to tell mechanical turk beforehand how many variables I have. The perl code that actually creates the .csv files for mechanical turk is below, with the code for finding the prop_info files stripped:


Code
  my $csv = Text::CSV->new({binary => 1}); 
...
open PROPINF, "<", "$file/$pagefile/prop_info.txt";
my @propinfolines = <PROPINF>;
for(my $i=0; $i<$#propinfolines+1; $i++){
$propinfolines[$i]=~s/\\/\\\\/g;
$propinfolines[$i]=~s/\'/\\u0027/g;
$propinfolines[$i]=~s/\"/\\\"/g;
$propinfolines[$i]=~s/\t/\\t/g;
$propinfolines[$i]=~s/\n/\\n/g;
$propinfolines[$i]=~s/\r/\\r/g;
}
my $propinfo = join('',@propinfolines);
if(! $csv->combine($docname,$docpage,$propinfo)){
print "can't combine";
exit
}
my $str = $csv->string();
chomp $str;
print FILE $str;


Let me know if any more details would be useful.
Attachments: prop_info.txt (65.5 KB)


FishMonger
Veteran / Moderator

Jul 16, 2009, 1:02 PM

Post #11 of 12 (1249 views)
Re: [leptogenesis] escape strings for javascript? [In reply to] Can't Post

Well, that's not what I thought your doing.

I haven't used it myself, but you could try:
URI::Escape::JavaScript - A perl implementation of JavaScript's escape() and unescape() functions
http://search.cpan.org/~taniguchi/URI-Escape-JavaScript-0.03/lib/URI/Escape/JavaScript.pm


leptogenesis
Novice

Jul 16, 2009, 2:09 PM

Post #12 of 12 (1247 views)
Re: [FishMonger] escape strings for javascript? [In reply to] Can't Post

I'm starting to think this is a dead end for me. For posterity, I'm going to say that URI::Escape::JavaScript isn't the right thing--it's for turning strings into something that could be passed in a URL: e.g. it turns "My Documents" into "My%20Documents". It's called "URI::Escape::JavaScript" because it's supposed to function exactly the same as the Javascript escape function.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives