From fa2a35ab714d5bf3064dc64f465b17ff4d94f85c Mon Sep 17 00:00:00 2001 From: Joseph Kempster Date: Wed, 18 Oct 2023 13:13:23 +0100 Subject: [PATCH] Fix issues with block elements inside CTA and LegislativeList 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 --- lib/govspeak.rb | 10 ++++------ test/govspeak_test.rb | 36 ++++++++++++++++++++++++++++++++++++ test/govspeak_test_helper.rb | 7 +++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/lib/govspeak.rb b/lib/govspeak.rb index 982facb1..a46b5b68 100644 --- a/lib/govspeak.rb +++ b/lib/govspeak.rb @@ -278,9 +278,7 @@ def render_image(image) extension("call-to-action", surrounded_by("$CTA")) do |body| <<~BODY - {::options parse_block_html=\"true\" /} -
#{body}
- {::options parse_block_html=\"false\" /} +
#{body}
BODY end @@ -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\" /} -
#{body}
- {::options parse_block_html=\"false\" ordered_lists_disabled=\"false\" /} + {::options ordered_lists_disabled=\"true\" /} +
#{body}
+ {::options ordered_lists_disabled=\"false\" /} BODY end diff --git a/test/govspeak_test.rb b/test/govspeak_test.rb index 7accde6c..ae50516e 100644 --- a/test/govspeak_test.rb +++ b/test/govspeak_test.rb @@ -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 %( +
+
my alt
+
+ +

Some text

+ ) + end + end + test_given_govspeak " 1. rod 2. jane @@ -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 %( +
+
my alt
+
+ +

Some text

+ ) + 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 diff --git a/test/govspeak_test_helper.rb b/test/govspeak_test_helper.rb index 4e3c1ec3..5dc3cdfe 100644 --- a/test/govspeak_test_helper.rb +++ b/test/govspeak_test_helper.rb @@ -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