Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/iruby/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ def clear_output(wait = false)
private

def protect(mime, data)
MIME::Type.new(mime).ascii? ? data.to_s : [data.to_s].pack('m0')
ascii?(mime) ? data.to_s : [data.to_s].pack('m0')
end

def ascii?(mime)
case mime
when "application/javascript"
# Special case for application/javascript.
# This needs because mime-types tells us application/javascript a non-text type.
true
else
MIME::Type.new(mime).ascii?
end
end

def render(data, obj, exact_mime, fuzzy_mime)
Expand Down
25 changes: 18 additions & 7 deletions test/iruby/mime_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
class IRubyTest::MimeTest < IRubyTest::TestBase
sub_test_case("IRuby::Display") do
def test_display_with_mime_type
html = "<b>Bold Text</b>"
sub_test_case(".display") do
sub_test_case("with mime type") do
test("text/html") do
html = "<b>Bold Text</b>"

obj = Object.new
obj.define_singleton_method(:to_s) { html }
obj = Object.new
obj.define_singleton_method(:to_s) { html }

res = IRuby::Display.display(obj, mime: "text/html")
assert_equal({ plain: obj.inspect, html: html },
{ plain: res["text/plain"], html: res["text/html"] })
res = IRuby::Display.display(obj, mime: "text/html")
assert_equal({ plain: obj.inspect, html: html },
{ plain: res["text/plain"], html: res["text/html"] })
end

test("application/javascript") do
data = "alert('Hello World!')"
res = IRuby::Display.display(data, mime: "application/javascript")
assert_equal(data,
res["application/javascript"])
end
end
end
end

Expand Down