Skip to content

Commit

Permalink
Update markup improver to support empty string (Mange#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkiczula authored and Mange committed Oct 2, 2019
1 parent afda57d commit ab45606
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[full changelog](https://github.com/Mange/roadie/compare/v3.4.0...master)

Nothing yet.
* Gracefully handle empty string email body, such as those provided by `ActionMailer::Base::NullMail` objects

### 3.5.0

Expand Down
7 changes: 7 additions & 0 deletions lib/roadie/markup_improver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def initialize(dom, original_html)
# @return [nil] passed DOM will be mutated
def improve
ensure_doctype_present
ensure_html_element_present
head = ensure_head_element_present
ensure_declared_charset head
end
Expand All @@ -48,6 +49,12 @@ def uses_buggy_jruby?
defined?(JRuby)
end

def ensure_html_element_present
return if dom.at_xpath('html')
html = Nokogiri::XML::Node.new 'html', dom
dom << html
end

def ensure_head_element_present
if (head = dom.at_xpath('html/head'))
head
Expand Down
1 change: 1 addition & 0 deletions spec/lib/roadie/markup_improver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def pending_for_buggy_jruby

describe "basic HTML structure" do
it "inserts a <html> element as the root" do
expect(improve("")).to have_selector("html")
expect(improve("<h1>Hey!</h1>")).to have_selector("html h1")
expect(improve("<html></html>").css('html').size).to eq(1)
end
Expand Down

0 comments on commit ab45606

Please sign in to comment.