From c009d780a1ac41ca000d5ff0510ca4a59c2788f6 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 29 Apr 2022 12:18:47 +0200 Subject: [PATCH] Replace curly braces with braces in user entered content --- lib/custom_wizard/mapper.rb | 7 ++++++- spec/components/custom_wizard/mapper_spec.rb | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index 0ded76cf..89718f7b 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -232,7 +232,12 @@ class CustomWizard::Mapper end if opts[:wizard] - string.gsub!(/w\{(.*?)\}/) { |match| recurse(data, [*$1.split('.')]) || '' } + string.gsub!(/w\{(.*?)\}/) do |match| + content = recurse(data, [*$1.split('.')]) || '' + # Curly braces are used by liquid, so they can't be part of the content. + # TODO: Ideally these would be escaped somehow. + content.gsub(/{/, "(").gsub(/}/, ")") if opts[:template] + end end if opts[:value] diff --git a/spec/components/custom_wizard/mapper_spec.rb b/spec/components/custom_wizard/mapper_spec.rb index 422ffbf5..a23fb4fc 100644 --- a/spec/components/custom_wizard/mapper_spec.rb +++ b/spec/components/custom_wizard/mapper_spec.rb @@ -459,5 +459,20 @@ describe CustomWizard::Mapper do expect(result).to eq("") end end + + it "replaces curly braces in user content with braces" do + params_with_curly_braces = { + "step_1_field_1" => "Some code with braces {%s}" + } + template = "w{step_1_field_1}" + mapper = create_template_mapper(params_with_curly_braces, user1) + result = mapper.interpolate( + template.dup, + template: true, + user: true, + wizard: true + ) + expect(result).to eq("Some code with braces (%s)") + end end end