Skip to content
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
14 changes: 11 additions & 3 deletions lib/custom_wizard/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 13 additions & 0 deletions spec/components/custom_wizard/mapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading