From 34b63a8375985486c2b4deeac2fced2c20e482d5 Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Tue, 1 Dec 2009 12:03:15 -0600 Subject: [PATCH] Fix a Ruby 1.9 Character Encoding Bug Wow, this stuff is complicated! Some things really want to be UTF-8; others really want to be byte strings. --- app/controllers/wiki_controller.rb | 2 +- lib/stringsupport.rb | 6 +++--- test/functional/wiki_controller_test.rb | 12 ++++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) mode change 100755 => 100644 test/functional/wiki_controller_test.rb diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 2e41b6ce..23b1ec7e 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -270,7 +270,7 @@ class WikiController < ApplicationController prev_content = '' filter_spam(the_content) raise Instiki::ValidationError.new('Your name cannot contain a "."') if author_name.include? '.' - cookies['author'] = { :value => author_name, :expires => Time.utc(2030) } + cookies['author'] = { :value => author_name.dup.as_bytes, :expires => Time.utc(2030) } if @page new_name = params['new_name'] ? params['new_name'].purify : @page_name prev_content = @page.current_revision.content diff --git a/lib/stringsupport.rb b/lib/stringsupport.rb index 731a1f95..459ca752 100644 --- a/lib/stringsupport.rb +++ b/lib/stringsupport.rb @@ -30,9 +30,9 @@ class String # returns a valid utf-8 string, purged of any subsequences of illegal bytes. #-- def purify - text = check_ncrs - if text.respond_to?(:encoding) - text.split(//).collect{|c| c.as_bytes}.grep(UTF8_REGEX).join.as_utf8 + text = self.dup.check_ncrs.as_utf8 + if text.respond_to?(:force_encoding) + text.chars.collect{|c| c.as_bytes}.grep(UTF8_REGEX).join.as_utf8 else text.split(//u).grep(UTF8_REGEX).join end diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb old mode 100755 new mode 100644 index 8676b1ac..de43c267 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -653,6 +653,18 @@ class WikiControllerTest < ActionController::TestCase assert_equal 'AuthorOfNewPage', new_page.author end + def test_save_astral_plane_characters + r = process 'save', 'web' => 'wiki1', 'id' => 'NewPage', 'content' => "Double-struck A: \xF0\x9D\x94\xB8", + 'author' => "\xF0\x9D\x94\xB8\xC3\xBCthorOfNewPage" + + assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'show', :id => 'NewPage' + assert_match @eternity, r.headers["Set-Cookie"][0] + new_page = @wiki.read_page('wiki1', 'NewPage') + assert_equal "Double-struck A: \360\235\224\270", new_page.content + assert_equal "\360\235\224\270\303\274thorOfNewPage", new_page.author + assert_equal "\360\235\224\270\303\274thorOfNewPage", r.cookies['author'] + end + def test_save_not_utf8 r = process 'save', 'web' => 'wiki1', 'id' => 'NewPage', 'content' => "Cont\000ents of a new page\r\n\000", 'author' => 'AuthorOfNewPage'