diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index c83bd59d7..c3555c656 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -208,12 +208,20 @@ def map_user_field_options(value) def interpolate(string, opts = { user: true, wizard: true, value: true, template: false }) return string if string.blank? || string.frozen? - string.gsub!(/u\{(.*?)\}/) { |match| map_user_field($1) || "" } if opts[:user] && @user.present? + # Braces may arrive URL-encoded (%7B / %7D), e.g. when a placeholder is + # used inside a URL, so both forms are accepted. + if opts[:user] && @user.present? + string.gsub!(/u(?:\{|%7[Bb])(.*?)(?:\}|%7[Dd])/) { |match| map_user_field($1) || "" } + end - string.gsub!(/w\{(.*?)\}/) { |match| recurse(data, [*$1.split(".")]) || "" } if opts[:wizard] + if opts[:wizard] + string.gsub!(/w(?:\{|%7[Bb])(.*?)(?:\}|%7[Dd])/) do |match| + recurse(data, [*$1.split(".")]) || "" + end + end if opts[:value] - string.gsub!(/v\{(.*?)\}/) do |match| + string.gsub!(/v(?:\{|%7[Bb])(.*?)(?:\}|%7[Dd])/) do |match| attrs = $1.split(":") key = attrs.first format = attrs.last if attrs.length > 1 diff --git a/plugin.rb b/plugin.rb index b113e3548..4c00af195 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.18.6 +# version: 2.18.7 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Marcos Gutierrez # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech diff --git a/spec/components/custom_wizard/mapper_spec.rb b/spec/components/custom_wizard/mapper_spec.rb index 20cd8a448..93ca9037d 100644 --- a/spec/components/custom_wizard/mapper_spec.rb +++ b/spec/components/custom_wizard/mapper_spec.rb @@ -287,6 +287,19 @@ def create_template_mapper(data, user) CustomWizard::Mapper.new(inputs: avatar_inputs, data: data, user: user1).perform, ).to eq("Avatar: ![avatar](#{user1.avatar_template_url.gsub("{size}", "144")})") end + + it "placeholders with url-encoded braces" do + freeze_time + mapper = create_template_mapper(template_params, user1) + result = + mapper.interpolate( + "/new?name=u%7Bname%7D&field=w%7bstep_1_field_1%7d&date=v%7Btime%7D".dup, + user: true, + wizard: true, + value: true, + ) + expect(result).to eq("/new?name=Angus&field=Hello&date=#{Time.now.strftime("%B %-d, %Y")}") + end end it "handles not equal pairs" do