Permalink
Browse files

Use octets in string literals when translating U+2028 and U+2029 if u…

…sing a Ruby version less than 1.9.

Previously, this would translate "u2028" to "\\u2028", because the \u escape character doesn't exist in 1.8.
  • Loading branch information...
1 parent eff0146 commit 4122df8ca7fcd646db7adc6a33e3d5288079879c @benmanns benmanns committed Sep 25, 2011
Showing with 11 additions and 1 deletion.
  1. +11 −1 lib/rack/contrib/jsonp.rb
View
@@ -83,7 +83,17 @@ def pad(callback, response, body = "")
# replacing them with the escaped version. This should be safe because
# according to the JSON spec, these characters are *only* valid inside
# a string and should therefore not be present any other places.
- body << s.to_s.gsub("\u2028", '\u2028').gsub("\u2029", '\u2029')
+ body << if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('1.9')
+ s.to_s.gsub("\u2028", '\u2028').gsub("\u2029", '\u2029')
+ else
+ # In 1.8
+ # "\u2028" # => "u2028"
+ # "\u2029" # => "u2029"
+ # In 1.9
+ # "\342\200\250" # => "\u2028"
+ # "\342\200\251" # => "\u2029"
+ s.to_s.gsub("\342\200\250", '\u2028').gsub("\342\200\251", '\u2029')
+ end
end
["#{callback}(#{body})"]

0 comments on commit 4122df8

Please sign in to comment.