Skip to content

Commit

Permalink
Merge pull request #118 from alphagov/fix-button-linebreak-rendering
Browse files Browse the repository at this point in the history
Fix buttons rendering incorrectly in linebreaks
  • Loading branch information
NickColley authored Nov 21, 2017
2 parents 3a9f553 + d495c4e commit 40dfb3a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/govspeak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def insert_strong_inside_p(body, parser=Govspeak::Document)
end

extension('button', %r{
^ # Match start of line only, allows for indenting code examples
(?:\r|\n|^) # non-capturing match to make sure start of line and linebreak
{button(.*?)} # match opening bracket and capture attributes
\s* # any whitespace between opening bracket and link
\[ # match start of link markdown
Expand All @@ -147,7 +147,7 @@ def insert_strong_inside_p(body, parser=Govspeak::Document)
\) # match end of link text markdown
\s* # any whitespace between opening bracket and link
{\/button} # match ending bracket
$ # Match end of line only, allows for indenting code examples
(?:\r|\n|$) # non-capturing match to make sure end of line and linebreak
}x) { |attributes, text, href|
button_classes = "button"
button_classes << " button-start" if attributes =~ /start/
Expand Down
49 changes: 49 additions & 0 deletions test/govspeak_button_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,53 @@ class GovspeakTest < Minitest::Test
</code></pre>
}
end

# Make sure button renders when typical linebreaks are before it, seen in publishing applications
test_given_govspeak "{button}[Line breaks](https://gov.uk/random){/button}\r\n\r\n{button}[Continue](https://gov.uk/random){/button}\r\n\r\n{button}[Continue](https://gov.uk/random){/button}" do
assert_html_output %{
<p><a role="button" class="button" href="https://gov.uk/random">Line breaks</a></p>
<p><a role="button" class="button" href="https://gov.uk/random">Continue</a></p>
<p><a role="button" class="button" href="https://gov.uk/random">Continue</a></p>
}
end

test_given_govspeak "{button}[More line breaks](https://gov.uk/random){/button}\n\n{button}[Continue](https://gov.uk/random){/button}\n\n{button}[Continue](https://gov.uk/random){/button}" do
assert_html_output %{
<p><a role="button" class="button" href="https://gov.uk/random">More line breaks</a></p>
<p><a role="button" class="button" href="https://gov.uk/random">Continue</a></p>
<p><a role="button" class="button" href="https://gov.uk/random">Continue</a></p>
}
end

test_given_govspeak %{
## Register to vote
Introduction text about the service.
lorem lorem lorem
lorem lorem lorem
{button start}[Start Now](https://gov.uk/random){/button}
lorem lorem lorem
lorem lorem lorem
} do
assert_html_output %{
<h2 id="register-to-vote">Register to vote</h2>
<p>Introduction text about the service.</p>
<p>lorem lorem lorem
lorem lorem lorem</p>
<p><a role="button" class="button button-start" href="https://gov.uk/random">Start Now</a></p>
<p>lorem lorem lorem
lorem lorem lorem</p>
}
end
end

0 comments on commit 40dfb3a

Please sign in to comment.