Skip to content

Commit

Permalink
Don't die when an attachment is missing a title
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindew committed Sep 29, 2016
1 parent d0894c4 commit a436909
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/govspeak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ def insert_strong_inside_p(body, parser=Govspeak::Document)
attachment = AttachmentPresenter.new(attachment)
span_id = attachment.id ? %{ id="attachment_#{attachment.id}"} : ""
# new lines inside our title cause problems with govspeak rendering as this is expected to be on one line.
link = attachment.link(attachment.title.tr("\n", " "), attachment.url)
title = (attachment.title || "").tr("\n", " ")
link = attachment.link(title, attachment.url)
attributes = attachment.attachment_attributes.empty? ? "" : " (#{attachment.attachment_attributes})"
%{<span#{span_id} class="attachment-inline">#{link}#{attributes}</span>}
end
Expand All @@ -212,7 +213,8 @@ def insert_strong_inside_p(body, parser=Govspeak::Document)
attachment = attachments.detect { |a| a[:content_id].match(content_id) }
next "" unless attachment
attachment = AttachmentPresenter.new(attachment)
render_image(attachment.url, attachment.title.tr("\n", " "), nil, attachment.id)
title = (attachment.title || "").tr("\n", " ")
render_image(attachment.url, title, nil, attachment.id)
end

# As of version 1.12.0 of Kramdown the block elements (div & figcaption)
Expand Down
8 changes: 8 additions & 0 deletions test/govspeak_attachments_image_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def compress_html(html)
assert_match(%r{<img.*alt="My Title"}, rendered)
end

test "can render a nil image title" do
rendered = render_govspeak(
"[embed:attachments:image:1fe8]",
[build_attachment(id: nil, title: nil, content_id: "1fe8")]
)
assert_match(%r{<img.*alt=""}, rendered)
end

test "a full image attachment rendering looks correct" do
rendered = render_govspeak(
"[embed:attachments:image:1fe8]",
Expand Down
8 changes: 8 additions & 0 deletions test/govspeak_attachments_inline_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ def render_govspeak(govspeak, attachments = [])
assert_match(%r{<a href="http://a.b/f.pdf">My Pdf</a>}, rendered)
end

test "renders with a nil title" do
rendered = render_govspeak(
"[embed:attachments:inline:1fe8]",
[build_attachment(content_id: "1fe8", url: "http://a.b/f.pdf", title: nil)]
)
assert_match(%r{<a href="http://a.b/f.pdf"></a>}, rendered)
end

test "renders on a single line" do
rendered = render_govspeak(
"[embed:attachments:inline:2bc1]",
Expand Down

0 comments on commit a436909

Please sign in to comment.