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
14 changes: 10 additions & 4 deletions lib/iruby/display.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "set"

module IRuby
module Display
class << self
Expand Down Expand Up @@ -50,11 +52,15 @@ def protect(mime, data)
ascii?(mime) ? data.to_s : [data.to_s].pack('m0')
end

# Each of the following mime types must be a text type,
# but mime-types library tells us it is a non-text type.
FORCE_TEXT_TYPES = Set[
"application/javascript",
"image/svg+xml"
].freeze

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.
if FORCE_TEXT_TYPES.include?(mime)
true
else
MIME::Type.new(mime).ascii?
Expand Down
7 changes: 7 additions & 0 deletions test/iruby/mime_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class IRubyTest::MimeTest < IRubyTest::TestBase
assert_equal(data,
res["application/javascript"])
end

test("image/svg+xml") do
data = '<svg height="30" width="100"><text x="0" y="15" fill="red">SVG</text></svg>'
res = IRuby::Display.display(data, mime: "image/svg+xml")
assert_equal(data,
res["image/svg+xml"])
end
end
end
end
Expand Down