Skip to content

Commit

Permalink
Merge pull request #190 from ayumin/kill-ruby18
Browse files Browse the repository at this point in the history
Use String#encode instead NKF
  • Loading branch information
onk committed Oct 4, 2014
2 parents 29af6f8 + 835f7bf commit f0d7eb0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/string_utils.rb
@@ -1,6 +1,5 @@
require "erb"
#require "jcode"
require "nkf"
require "uri"

module ActionView::Helpers::SanitizeHelper
Expand All @@ -14,7 +13,7 @@ class String
LAME_CHARS = [0x200c, 0x200d, 0x200e, 0x200f, 0x2028, 0x2029, 0x202a, 0x202b, 0x202c,0x202d, 0x202e, 0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0xfeff].pack("U*")

def utf8_roundtrip
NKF.nkf("-W -w -m0 -x", self).delete(LAME_CHARS)
self.encode('UTF-8').delete(LAME_CHARS)
end

def sanitize_uri
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/string_utils_spec.rb
Expand Up @@ -3,6 +3,24 @@
require 'spec_helper'

describe :string_utils do

describe 'utf8_roundtrip' do
context 'turn encode to utf-8' do
it 'utf-8 turns utf-8' do
str = 'Ruby1.8をつかっている人はもういない'
expect(str.utf8_roundtrip).to eq('Ruby1.8をつかっている人はもういない')
end
it 'euc-jp turns utf-8' do
str = 'Ruby1.8をつかっている人はもういない'.encode('EUC-JP')
expect(str.utf8_roundtrip).to eq('Ruby1.8をつかっている人はもういない')
end
it 'Shif-JIS turns utf-8' do
str = 'Ruby1.8をつかっている人はもういない'.encode('Shift_JIS')
expect(str.utf8_roundtrip).to eq('Ruby1.8をつかっている人はもういない')
end
end
end

describe '.scrub_html' do
context 'strip danger element' do
it 'tag' do
Expand Down

0 comments on commit f0d7eb0

Please sign in to comment.