Skip to content

Commit

Permalink
Fix compatibility issue with Mail 2.7.0
Browse files Browse the repository at this point in the history
In Mail 2.7.0 in an actual Rails application, HTML part of the body now
returns an `ActiveSupport::SafeBuffer` object instead of a String
object. This causes a problem as there is a known issue of how
`SafeBuffer#gsub` with a block form is broken in Rails[1], and that
`HTMLEntities#decode` actually performs a `gsub` with a block
internally[2].

Upon further investigation, however, it seems like the root cause of
this issue might not be on Mail gem, but actually on `ERB::Util.h`
returning `ActiveSupport::SafeBuffer` object instead of `String`[3].

This commit changes `default_part_body` method to call `to_s.to_str` on
the message body so that we'll be able to pass a String object, which
always works with `gsub`, to `HTMLEntities#decode`.

Please note that we need to call `to_s.to_str` on the object because
`ActiveSupport::SafeBuffer` actually overrides `to_s` to return itself
and not the underlying `String` object.

I believe this PR should fix issue #202, #204, and #205, and it's better
to fix the issue here than in `HTMLEntities`.

[1]: rails/rails#1555
[2]: https://github.com/threedaymonk/htmlentities/blob/v4.3.3/lib/htmlentities/decoder.rb#L10-L20
[3]: threedaymonk/htmlentities#33 (comment)
  • Loading branch information
sikachu committed Dec 30, 2017
1 parent 24f694f commit de08b27
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/email_spec/mail_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ def default_part
end

def default_part_body
HTMLEntities.new.decode(default_part.body)
# Calling to_str as we want the actual String object
HTMLEntities.new.decode(default_part.body.to_s.to_str)
end

def html
Expand Down
5 changes: 5 additions & 0 deletions spec/email_spec/mail_ext_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
email = Mail.new(:body => "hi")
expect(email.default_part.body).to eq(email.default_part_body)
end

it "compatible with ActiveSupport::SafeBuffer" do
email = Mail.new(:body => ActiveSupport::SafeBuffer.new("bacon & pancake"))
expect(email.default_part_body).to eq ("bacon & pancake")
end
end

describe "#html" do
Expand Down

0 comments on commit de08b27

Please sign in to comment.