Skip to content

Commit

Permalink
Fix issues with block elements inside CTA and LegislativeList
Browse files Browse the repository at this point in the history
In #291, we made a change to how the call to action and legislative list
components are parsed. This involved the use of the `parse_block_html` option,
which enables parsing in HTML blocks until it is toggled off.

Since this change, we have received Zendesk tickets regarding the use of the
image component inside a call to action. Because we toggle off the parsing of
HTML blocks entirely within call to actions, the HTML of the image is not
parsed correctly.

To fix this, we can enable parsing of the surrounding divs of the call to
action and legislative list components only. This means that any inner blocks
(such as the `figure` tag surrounding an image) are parsed using their default
mechanism [1].

[1]: https://kramdown.gettalong.org/syntax.html#html-blocks
  • Loading branch information
jkempster34 committed Oct 18, 2023
1 parent 58b4417 commit fa2a35a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/govspeak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ def render_image(image)

extension("call-to-action", surrounded_by("$CTA")) do |body|
<<~BODY
{::options parse_block_html=\"true\" /}
<div class="call-to-action">#{body}</div>
{::options parse_block_html=\"false\" /}
<div class="call-to-action" markdown="1">#{body}</div>
BODY
end

Expand All @@ -303,9 +301,9 @@ def render_image(image)
# The surrounding div is neccessary to control flow in `parse_block_html` and
# maintain the same functionality as a previous version of this extension.
<<~BODY
{::options parse_block_html=\"true\" ordered_lists_disabled=\"true\" /}
<div class="legislative-list-wrapper">#{body}</div>
{::options parse_block_html=\"false\" ordered_lists_disabled=\"false\" /}
{::options ordered_lists_disabled=\"true\" /}
<div class="legislative-list-wrapper" markdown="1">#{body}</div>
{::options ordered_lists_disabled=\"false\" /}
BODY
end

Expand Down
36 changes: 36 additions & 0 deletions test/govspeak_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,24 @@ class GovspeakTest < Minitest::Test
)
end

test "CTA with image" do
given_govspeak "
$CTA
[Image:image-id]
$CTA
Some text
", images: [build_image] do
assert_html_output %(
<div class="call-to-action">
<figure class="image embedded"><div class="img"><img src="http://example.com/image.jpg" alt="my alt"></div></figure>
</div>
<p>Some text</p>
)
end
end

test_given_govspeak "
1. rod
2. jane
Expand Down Expand Up @@ -1268,6 +1286,24 @@ class GovspeakTest < Minitest::Test
)
end

test "LegislativeList with image" do
given_govspeak "
$LegislativeList
[Image:image-id]
$EndLegislativeList
Some text
", images: [build_image] do
assert_html_output %(
<div class="legislative-list-wrapper">
<figure class="image embedded"><div class="img"><img src="http://example.com/image.jpg" alt="my alt"></div></figure>
</div>
<p>Some text</p>
)
end
end

test_given_govspeak "
Zippy, Bungle and George did not qualify for the tax exemption in s428. They filled in their tax return accordingly.
" do
Expand Down
7 changes: 7 additions & 0 deletions test/govspeak_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ def deobfuscate_mailto(html)
coder.decode(html)
end

def build_image(attrs = {})
attrs[:alt_text] ||= "my alt"
attrs[:url] ||= "http://example.com/image.jpg"
attrs[:id] ||= "image-id"
attrs
end

module ClassMethods
def test_given_govspeak(govspeak, options = {}, &block)
test "Given #{govspeak}" do
Expand Down

0 comments on commit fa2a35a

Please sign in to comment.