mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Remove 1.8 non-encoding branches
This commit is contained in:
parent
a058a89fef
commit
d3398065d3
5 changed files with 48 additions and 86 deletions
|
@ -1,32 +1,25 @@
|
|||
module ExecJS
|
||||
# Encodes strings as UTF-8
|
||||
module Encoding
|
||||
if "".respond_to?(:encode)
|
||||
if RUBY_ENGINE == 'jruby' || RUBY_ENGINE == 'rbx'
|
||||
# workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588
|
||||
# workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729
|
||||
def encode(string)
|
||||
if string.encoding.name == 'ASCII-8BIT'
|
||||
data = string.dup
|
||||
data.force_encoding('UTF-8')
|
||||
if RUBY_ENGINE == 'jruby' || RUBY_ENGINE == 'rbx'
|
||||
# workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588
|
||||
# workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729
|
||||
def encode(string)
|
||||
if string.encoding.name == 'ASCII-8BIT'
|
||||
data = string.dup
|
||||
data.force_encoding('UTF-8')
|
||||
|
||||
unless data.valid_encoding?
|
||||
raise ::Encoding::UndefinedConversionError, "Could not encode ASCII-8BIT data #{string.dump} as UTF-8"
|
||||
end
|
||||
else
|
||||
data = string.encode('UTF-8')
|
||||
unless data.valid_encoding?
|
||||
raise ::Encoding::UndefinedConversionError, "Could not encode ASCII-8BIT data #{string.dump} as UTF-8"
|
||||
end
|
||||
data
|
||||
end
|
||||
else
|
||||
def encode(string)
|
||||
string.encode('UTF-8')
|
||||
else
|
||||
data = string.encode('UTF-8')
|
||||
end
|
||||
data
|
||||
end
|
||||
else
|
||||
# Define no-op on 1.8
|
||||
def encode(string)
|
||||
string
|
||||
string.encode('UTF-8')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -69,19 +69,9 @@ module ExecJS
|
|||
end
|
||||
end
|
||||
|
||||
if "".respond_to?(:codepoints)
|
||||
def encode_unicode_codepoints(str)
|
||||
str.gsub(/[\u0080-\uffff]/) do |ch|
|
||||
"\\u%04x" % ch.codepoints.to_a
|
||||
end
|
||||
end
|
||||
else
|
||||
def encode_unicode_codepoints(str)
|
||||
str.gsub(/([\xC0-\xDF][\x80-\xBF]|
|
||||
[\xE0-\xEF][\x80-\xBF]{2}|
|
||||
[\xF0-\xF7][\x80-\xBF]{3})+/nx) do |ch|
|
||||
"\\u%04x" % ch.unpack("U*")
|
||||
end
|
||||
def encode_unicode_codepoints(str)
|
||||
str.gsub(/[\u0080-\uffff]/) do |ch|
|
||||
"\\u%04x" % ch.codepoints.to_a
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -165,27 +155,12 @@ module ExecJS
|
|||
end
|
||||
end
|
||||
|
||||
if "".respond_to?(:force_encoding)
|
||||
def sh(command)
|
||||
output, options = nil, {}
|
||||
options[:external_encoding] = @encoding if @encoding
|
||||
options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8'
|
||||
IO.popen(command, options) { |f| output = f.read }
|
||||
output
|
||||
end
|
||||
else
|
||||
require "iconv"
|
||||
|
||||
def sh(command)
|
||||
output = nil
|
||||
IO.popen(command) { |f| output = f.read }
|
||||
|
||||
if @encoding
|
||||
Iconv.new('UTF-8', @encoding).iconv(output)
|
||||
else
|
||||
output
|
||||
end
|
||||
end
|
||||
def sh(command)
|
||||
output, options = nil, {}
|
||||
options[:external_encoding] = @encoding if @encoding
|
||||
options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8'
|
||||
IO.popen(command, options) { |f| output = f.read }
|
||||
output
|
||||
end
|
||||
|
||||
if ExecJS.windows?
|
||||
|
|
|
@ -47,9 +47,7 @@ module ExecJS
|
|||
when function?(value)
|
||||
nil
|
||||
when string?(value)
|
||||
value.respond_to?(:force_encoding) ?
|
||||
value.force_encoding('UTF-8') :
|
||||
value
|
||||
value.force_encoding('UTF-8')
|
||||
when array?(value)
|
||||
value.map { |v| unbox(v) }
|
||||
when object?(value)
|
||||
|
|
|
@ -64,9 +64,7 @@ module ExecJS
|
|||
vs
|
||||
end
|
||||
when String
|
||||
value.respond_to?(:force_encoding) ?
|
||||
value.force_encoding('UTF-8') :
|
||||
value
|
||||
value.force_encoding('UTF-8')
|
||||
else
|
||||
value
|
||||
end
|
||||
|
|
|
@ -83,41 +83,39 @@ class TestExecJS < Test::Unit::TestCase
|
|||
assert_equal "\\", ExecJS.eval('"\\\\"')
|
||||
end
|
||||
|
||||
if defined? Encoding
|
||||
def test_encoding
|
||||
utf8 = Encoding.find('UTF-8')
|
||||
def test_encoding
|
||||
utf8 = Encoding.find('UTF-8')
|
||||
|
||||
assert_equal utf8, ExecJS.exec("return 'hello'").encoding
|
||||
assert_equal utf8, ExecJS.eval("'☃'").encoding
|
||||
assert_equal utf8, ExecJS.exec("return 'hello'").encoding
|
||||
assert_equal utf8, ExecJS.eval("'☃'").encoding
|
||||
|
||||
ascii = "'hello'".encode('US-ASCII')
|
||||
result = ExecJS.eval(ascii)
|
||||
assert_equal "hello", result
|
||||
assert_equal utf8, result.encoding
|
||||
ascii = "'hello'".encode('US-ASCII')
|
||||
result = ExecJS.eval(ascii)
|
||||
assert_equal "hello", result
|
||||
assert_equal utf8, result.encoding
|
||||
|
||||
assert_raise Encoding::UndefinedConversionError do
|
||||
binary = "\xde\xad\xbe\xef".force_encoding("BINARY")
|
||||
ExecJS.eval(binary)
|
||||
end
|
||||
assert_raise Encoding::UndefinedConversionError do
|
||||
binary = "\xde\xad\xbe\xef".force_encoding("BINARY")
|
||||
ExecJS.eval(binary)
|
||||
end
|
||||
end
|
||||
|
||||
def test_encoding_compile
|
||||
utf8 = Encoding.find('UTF-8')
|
||||
def test_encoding_compile
|
||||
utf8 = Encoding.find('UTF-8')
|
||||
|
||||
context = ExecJS.compile("foo = function(v) { return '¶' + v; }".encode("ISO8859-15"))
|
||||
context = ExecJS.compile("foo = function(v) { return '¶' + v; }".encode("ISO8859-15"))
|
||||
|
||||
assert_equal utf8, context.exec("return foo('hello')").encoding
|
||||
assert_equal utf8, context.eval("foo('☃')").encoding
|
||||
assert_equal utf8, context.exec("return foo('hello')").encoding
|
||||
assert_equal utf8, context.eval("foo('☃')").encoding
|
||||
|
||||
ascii = "foo('hello')".encode('US-ASCII')
|
||||
result = context.eval(ascii)
|
||||
assert_equal "¶hello", result
|
||||
assert_equal utf8, result.encoding
|
||||
ascii = "foo('hello')".encode('US-ASCII')
|
||||
result = context.eval(ascii)
|
||||
assert_equal "¶hello", result
|
||||
assert_equal utf8, result.encoding
|
||||
|
||||
assert_raise Encoding::UndefinedConversionError do
|
||||
binary = "\xde\xad\xbe\xef".force_encoding("BINARY")
|
||||
context.eval(binary)
|
||||
end
|
||||
assert_raise Encoding::UndefinedConversionError do
|
||||
binary = "\xde\xad\xbe\xef".force_encoding("BINARY")
|
||||
context.eval(binary)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue