Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix buttons rendering incorrectly in linebreaks #118

Merged
merged 1 commit into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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