Subject: | Use single quotes for strings |
Data::JavaScript::Anon uses double quotes for constructing strings. This makes it very hard to embed into onclick attributes and the like. Luckily, double quotes and single quotes do the exact same thing in javascript. As I see no advantage to using double quotes, I've attached is a patch which switches to using single quotes when strings are needed.
Only in Data-JavaScript-Anon-singlequote/: Makefile
Only in Data-JavaScript-Anon-singlequote/: blib
diff -ru Data-JavaScript-Anon-0.6/lib/Data/JavaScript/Anon.pm Data-JavaScript-Anon-singlequote/lib/Data/JavaScript/Anon.pm
--- Data-JavaScript-Anon-0.6/lib/Data/JavaScript/Anon.pm 2005-07-26 04:48:34.000000000 -0400
+++ Data-JavaScript-Anon-singlequote/lib/Data/JavaScript/Anon.pm 2005-12-14 16:49:45.000000000 -0500
@@ -170,7 +170,7 @@
return $value if $value =~ /$RE_NUMERIC/;
# Escape and quote
- '"' . _escape($value) . '"';
+ "'" . _escape($value) . "'";
}
# Turn a single perl value into a javascript hash key
@@ -186,7 +186,7 @@
return $value if $value =~ /$RE_NUMERIC_HASHKEY/;
# Escape and quote
- '"' . _escape($value) . '"';
+ "'" . _escape($value) . "'";
}
# Create a Javascript array given the javascript array name
@@ -222,7 +222,7 @@
sub _escape {
my $text = shift;
- $text =~ s/(\"|\\)/\\$1/g; # Escape quotes and backslashes
+ $text =~ s/(\'|\\)/\\$1/g; # Escape quotes and backslashes
$text =~ s/\n/\\n/g; # Escape newlines in a readable way
$text =~ s/\r/\\r/g; # Escape CRs in a readable way
$text =~ s/\t/\\t/g; # Escape tabs in a readable way
Only in Data-JavaScript-Anon-singlequote/: pm_to_blib
diff -ru Data-JavaScript-Anon-0.6/t/02_results.t Data-JavaScript-Anon-singlequote/t/02_results.t
--- Data-JavaScript-Anon-0.6/t/02_results.t 2005-07-26 04:48:34.000000000 -0400
+++ Data-JavaScript-Anon-singlequote/t/02_results.t 2005-12-14 16:55:35.000000000 -0500
@@ -32,16 +32,16 @@
3com 2131.231fd2132 +0x21x
};
-my @keywords = qw{
- abstract boolean break byte case catch char class const continue
- debugger default delete do double else enum export extends false final
- finally float for function goto if implements import in instanceof int
- interface long native new null package private protected public return
- short static super switch synchronized this throw throws transient true
- try typeof var void volatile while with
-};
-
-plan tests => (@numbers + @not_numbers + @keywords + 5);
+my @keywords = qw{
+ abstract boolean break byte case catch char class const continue
+ debugger default delete do double else enum export extends false final
+ finally float for function goto if implements import in instanceof int
+ interface long native new null package private protected public return
+ short static super switch synchronized this throw throws transient true
+ try typeof var void volatile while with
+};
+
+plan tests => (@numbers + @not_numbers + @keywords + 6);
foreach ( @numbers ) {
ok( Data::JavaScript::Anon->is_a_number( $_ ), "$_ is a number" );
@@ -50,34 +50,38 @@
ok( ! Data::JavaScript::Anon->is_a_number( $_ ), "$_ is not a number" );
}
-# Test that keywords come out quoted
-foreach ( @keywords ) {
- is( Data::JavaScript::Anon->anon_hash_key($_), '"' . $_ . '"',
- "anon_hash_key correctly quotes keyword $_ used as hash key" );
+# Test that keywords come out quoted
+foreach ( @keywords ) {
+ is( Data::JavaScript::Anon->anon_hash_key($_), '"' . $_ . '"',
+ "anon_hash_key correctly quotes keyword $_ used as hash key" );
}
# Do a simple test of most of the code in a single go
my $rv = Data::JavaScript::Anon->anon_dump( [ 'a', 1, { a => { a => 1, } }, \"foo" ] );
-is( $rv, '[ "a", 1, { a: { a: 1 } }, "foo" ]',
+is( $rv, q|[ 'a', 1, { a: { a: 1 } }, 'foo' ]|,
'Generates expected output for simple combination struct' );
# Test for CPAN bug #7183
-is( Data::JavaScript::Anon->anon_hash_key( "0596000278" ), '"0596000278"',
+is( Data::JavaScript::Anon->anon_hash_key( "0596000278" ), q|'0596000278'|,
'anon_hash_key correctly escapes 0-leading non-octal' );
# Test for CPAN bug #11882 (forward slash not being escaped)
-is( Data::JavaScript::Anon->anon_scalar( 'C:\\devel' ), '"C:\\\\devel"',
+is( Data::JavaScript::Anon->anon_scalar( 'C:\\devel' ), q|'C:\\\\devel'|,
'anon_scalar correctly escapes forward slashes' );
-# Also make sure double quotes are escaped
-is( Data::JavaScript::Anon->anon_scalar( 'foo"bar' ), '"foo\\"bar"',
- 'anon_scalar correctly escapes double quotes' );
+# Make sure double quotes are not escaped
+is( Data::JavaScript::Anon->anon_scalar( 'foo"bar' ), q|'foo"bar'|,
+ "anon_scalar correctly doesn't escape double quotes" );
+
+# Also make sure single quotes are escaped
+is( Data::JavaScript::Anon->anon_scalar( "foo'bar" ), q|'foo\\'bar'|,
+ 'anon_scalar correctly escapes single quotes' );
# Test for generalised case of CPAN bug # (newline not being escaped)
-$rv = Data::JavaScript::Anon->anon_dump( [ "a\nb", "a\rb", "a b", "a\"b", "a\bb" ] );
-is( $rv, '[ "a\nb", "a\rb", "a\tb", "a\\"b", "a\010b" ]', 'escape tabs, newlines, CRs and control chars');
+$rv = Data::JavaScript::Anon->anon_dump( [ "a\nb", "a\rb", "a b", "a'b", "a\bb" ] );
+is( $rv, q|[ 'a\nb', 'a\rb', 'a\tb', 'a\\'b', 'a\010b' ]|, 'escape tabs, newlines, CRs and control chars');
1;