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.
This commit is contained in:
Jacques Distler 2009-12-01 12:03:15 -06:00
parent e3832c6f79
commit 34b63a8375
3 changed files with 16 additions and 4 deletions

View file

@ -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

View file

@ -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

12
test/functional/wiki_controller_test.rb Executable file → Normal file
View file

@ -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'