Make sure that the Encoding check does not modify the given string

this partially reverts 6ce26c83ad
see: 6ce26c83ad (commitcomment-2586688)
This commit is contained in:
Akira Matsuda 2013-02-09 00:23:34 +09:00
parent 190532b40a
commit 924f950f45
3 changed files with 17 additions and 5 deletions

View File

@ -103,8 +103,7 @@ module Haml
# @return [String] `str`, potentially with encoding gotchas like BOMs removed
if RUBY_VERSION < "1.9"
def check_encoding(str)
str.gsub!(/\A\xEF\xBB\xBF/, '') # Get rid of the UTF-8 BOM
str
str.gsub(/\A\xEF\xBB\xBF/, '') # Get rid of the UTF-8 BOM
end
else
@ -113,11 +112,12 @@ module Haml
# Get rid of the Unicode BOM if possible
# Shortcut for UTF-8 which might be the majority case
if str.encoding == Encoding::UTF_8
str.gsub!(/\A\uFEFF/, '')
return str.gsub(/\A\uFEFF/, '')
elsif str.encoding.name =~ /^UTF-(16|32)(BE|LE)?$/
str.gsub!(Regexp.new("\\A\uFEFF".encode(str.encoding)), '')
return str.gsub(Regexp.new("\\A\uFEFF".encode(str.encoding)), '')
else
return str
end
return str
end
encoding = str.encoding

View File

@ -0,0 +1 @@
BOMG

View File

@ -60,4 +60,15 @@ RUBY
c.send(static_method_name(:static_method, false, false),
"brush your teeth", "play with fire"))
end
def test_check_encoding_does_not_destoy_the_given_string
string_with_bom = if RUBY_VERSION > '1.9'
File.read(File.dirname(__FILE__) + '/templates/with_bom.haml', :encoding => Encoding::UTF_8)
else
File.read(File.dirname(__FILE__) + '/templates/with_bom.haml')
end
original = string_with_bom.dup
check_encoding(string_with_bom)
assert_equal(original, string_with_bom)
end
end