Skip to content

Commit

Permalink
Strip spaces between elements on inline attachments
Browse files Browse the repository at this point in the history
Outputting <span class="attachment-inline"> <a href="">test</a> </span>
will output as ` test ` which is often not what an author wants.

This is a rather ugly fix for it.
  • Loading branch information
kevindew committed Sep 28, 2016
1 parent 809d8be commit d54a058
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/govspeak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,11 @@ 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)
content = File.read(__dir__ + '/templates/inline_attachment.html.erb')
ERB.new(content).result(binding).gsub(/\n/, "")
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.
title = attachment.link(attachment.title.gsub("\n", " "), attachment.url)
attributes = attachment.attachment_attributes.empty? ? " (#{attachment.attachment_attributes})" : ""
%{<span#{span_id} class="attachment-inline">#{link}#{attributes}</span>}
end

def render_image(url, alt_text, caption = nil)
Expand Down
10 changes: 9 additions & 1 deletion test/govspeak_attachments_inline_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def render_govspeak(govspeak, attachments = [])
assert_match(%r{<span id="attachment[^\n]*</span>}, rendered)
end

test "doesn't have spaces between the span and the link" do
rendered = render_govspeak(
"[embed:attachments:inline:2bc1]",
[build_attachment(content_id: "2bc1", id: nil)]
)
assert_match(%r{<span class="attachment-inline"><a href=".*">.*</a></span>}, rendered)
end

test "will show HTML type (in brackets) if file_extension is specified as html" do
rendered = render_govspeak(
"[embed:attachments:inline:1fe8]",
Expand Down Expand Up @@ -121,7 +129,7 @@ def render_govspeak(govspeak, attachments = [])
type = %{<span class="type">Plain text</span>}
file_size = %{<span class="file-size">2 KB</span>}
pages = %{<span class="page-length">2 pages</span>}
assert_match(/#{link}\s*\(#{type}, #{file_size}, #{pages}\)/, rendered)
assert_match(/#{link}\s+\(#{type}, #{file_size}, #{pages}\)/, rendered)
end

test "can render two inline attachments on the same line" do
Expand Down

0 comments on commit d54a058

Please sign in to comment.