Escape \u2028 and \u2029 for 1.8 #44

merged 4 commits into from Dec 10, 2012


None yet

3 participants


In 1.8, the string "\u2028" is converted to "u2028", so any JSON containing "u2028" would be replaced with "\u2028", and any string literals "\u2028" would not get escaped. This patch uses octet literals if the Ruby version is less than 1.9, and Unicode literals if the Ruby version is 1.9 or greater.

If anyone doesn't like the Ruby version comparison within the code, I can remove the comparison and use the octet literal for all versions of Ruby, because 1.9 interprets "\342\200\250" the same as "\u2028".

benmanns added some commits Sep 25, 2011
@benmanns benmanns Split "should not allow literal U+2028 or U+2029" assigns and tests f…
…or ">= 1.9" and "< 1.9".
@benmanns benmanns 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.
@benmanns benmanns Replace Gem::Version comparison with "\u2028" == 'u2028' comparison.
This ensures that the Unicode character is properly translated without relying on Gem::Version. Thanks to @judofyr.
@benmanns benmanns Move U+2028 and U+2029 into constants.
This allows the Ruby compatibility check to be run once on startup rather than for every request. Thanks to @judofyr.

The tests are a little hard to understand unless you've read the comments on this PR. A comment there or more descriptive naming would help.

+1 otherwise.

@rkh rkh merged commit 42b92b7 into rack:master Dec 10, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment