Skip to content

Commit 38a5536

Browse files
mrknkojix2
authored andcommitted
Make application/javascript a text type
While mime-types handles application/javascript as a non-text, IRuby needs to handle it as a text. The new `ascii?` private method judges whether the given mime type is text. This method handles application/javascript as a text in the special case. [Fix GH-292]
1 parent 082eb9a commit 38a5536

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

lib/iruby/display.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,18 @@ def clear_output(wait = false)
4444
private
4545

4646
def protect(mime, data)
47-
MIME::Type.new(mime).ascii? ? data.to_s : [data.to_s].pack('m0')
47+
ascii?(mime) ? data.to_s : [data.to_s].pack('m0')
48+
end
49+
50+
def ascii?(mime)
51+
case mime
52+
when "application/javascript"
53+
# Special case for application/javascript.
54+
# This needs because mime-types tells us application/javascript a non-text type.
55+
true
56+
else
57+
MIME::Type.new(mime).ascii?
58+
end
4859
end
4960

5061
def render(data, obj, exact_mime, fuzzy_mime)

test/iruby/mime_test.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
class IRubyTest::MimeTest < IRubyTest::TestBase
22
sub_test_case("IRuby::Display") do
3-
def test_display_with_mime_type
4-
html = "<b>Bold Text</b>"
3+
sub_test_case(".display") do
4+
sub_test_case("with mime type") do
5+
test("text/html") do
6+
html = "<b>Bold Text</b>"
57

6-
obj = Object.new
7-
obj.define_singleton_method(:to_s) { html }
8+
obj = Object.new
9+
obj.define_singleton_method(:to_s) { html }
810

9-
res = IRuby::Display.display(obj, mime: "text/html")
10-
assert_equal({ plain: obj.inspect, html: html },
11-
{ plain: res["text/plain"], html: res["text/html"] })
11+
res = IRuby::Display.display(obj, mime: "text/html")
12+
assert_equal({ plain: obj.inspect, html: html },
13+
{ plain: res["text/plain"], html: res["text/html"] })
14+
end
15+
16+
test("application/javascript") do
17+
data = "alert('Hello World!')"
18+
res = IRuby::Display.display(data, mime: "application/javascript")
19+
assert_equal(data,
20+
res["application/javascript"])
21+
end
22+
end
1223
end
1324
end
1425

0 commit comments

Comments
 (0)