Skip to content
This repository
Browse code

Broaden URI.unescape fix to all affected 1.9.x by checking for broken…

… behavior instead of specific patchlevel
  • Loading branch information...
commit 69bbbe9068edb79f914e18940af9021e9da437a4 1 parent bfe7d51
Jeremy Kemper authored February 20, 2009
18  activesupport/lib/active_support/core_ext/uri.rb
... ...
@@ -1,10 +1,16 @@
1  
-if RUBY_VERSION == "1.9.1" && defined?(RUBY_PATCHLEVEL) && RUBY_PATCHLEVEL == 0
  1
+if RUBY_VERSION >= '1.9'
2 2
   require 'uri'
3  
-  URI::Parser.class_eval do
4  
-    remove_method :unescape
5  
-    def unescape(str, escaped = @regexp[:ESCAPED])
6  
-      enc = (str.encoding == Encoding::US_ASCII) ? Encoding::UTF_8 : str.encoding
7  
-      str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(enc)
  3
+
  4
+  str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese.
  5
+  str.force_encoding(Encoding::UTF_8) if str.respond_to?(:force_encoding)
  6
+
  7
+  unless str == URI.unescape(URI.escape(str))
  8
+    URI::Parser.class_eval do
  9
+      remove_method :unescape
  10
+      def unescape(str, escaped = @regexp[:ESCAPED])
  11
+        enc = (str.encoding == Encoding::US_ASCII) ? Encoding::UTF_8 : str.encoding
  12
+        str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(enc)
  13
+      end
8 14
     end
9 15
   end
10 16
 end
11  activesupport/test/core_ext/uri_ext_test.rb
... ...
@@ -1,11 +1,12 @@
1 1
 require 'abstract_unit'
  2
+require 'uri'
2 3
 
3  
-class URITest < Test::Unit::TestCase
  4
+class URIExtTest < Test::Unit::TestCase
4 5
   def test_uri_decode_handle_multibyte
5  
-    str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8,  means Japanese.
6  
-    str.force_encoding(Encoding::UTF_8) if(defined? Encoding::UTF_8)
  6
+    str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese.
  7
+    str.force_encoding(Encoding::UTF_8) if str.respond_to?(:force_encoding)
7 8
 
8  
-    assert_equal str, ::URI.unescape( ::URI.escape(str) )
9  
-    assert_equal str, ::URI.decode( ::URI.escape(str) )
  9
+    assert_equal str, URI.unescape(URI.escape(str))
  10
+    assert_equal str, URI.decode(URI.escape(str))
10 11
   end
11 12
 end

0 notes on commit 69bbbe9

Please sign in to comment.
Something went wrong with that request. Please try again.