From c9061ae66a8d6fe2eae14ef5e6442e69b6ec703a Mon Sep 17 00:00:00 2001 From: TEC Date: Tue, 25 Oct 2022 00:23:29 +0800 Subject: [PATCH] Avoid mapping different options to the same ID Replacing non alphanumeric characters is fragile, e.g. "C++" and "C#" both become "C-". Instead, we can use base64 encoding. --- Manifest.toml | 2 +- Project.toml | 1 + lib/Surveys.jl | 9 +++++---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Manifest.toml b/Manifest.toml index 07979e2..4e368ae 100755 --- a/Manifest.toml +++ b/Manifest.toml @@ -2,7 +2,7 @@ julia_version = "1.8.2" manifest_format = "2.0" -project_hash = "c18c5eaab58f1afb699d93a8a6f512a73acf8b7c" +project_hash = "813d458aeace91ef95ce5c748cf72ad43f943461" [[deps.ArgParse]] deps = ["Logging", "TextWrap"] diff --git a/Project.toml b/Project.toml index 30dbb34..481f083 100755 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ authors = ["TEC "] version = "0.1.0" [deps] +Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/lib/Surveys.jl b/lib/Surveys.jl index 4170788..0923c62 100644 --- a/lib/Surveys.jl +++ b/lib/Surveys.jl @@ -1,6 +1,7 @@ module Surveys using Dates +using Base64 using Genie.Renderers.Html import Base: show, isvalid, isempty @@ -574,13 +575,13 @@ function htmlrender(q::Union{<:Question{RadioSelect}, Question{MultiSelect}}, if nonempty in q.validators "true" else false end), '\n', join(map(q.field.options.options |> enumerate) do (i, opt) - id = string("qn-", q.id, "-", replace(opt.second, r"[^A-Za-z0-9]+" => '-')) + id = string("qn-", q.id, "-", base64encode(opt.second)) elem("label", elem("input", :type => type, :id => id, :name => string(q.id, "[]"), :value => html_escape(opt.second), :checked => (!ismissing(value) && opt.second ∈ value), if type == "radio" && q.field.other - [:oninput => "document.getElementById('$(string("qn-", q.id, "---other-input"))').value = ''"] + [:oninput => "document.getElementById('$(string("qn-", q.id, "--other-input"))').value = ''"] else [] end...) * opt.first, :for => id) @@ -598,10 +599,10 @@ function htmlrender(q::Union{<:Question{RadioSelect}, Question{MultiSelect}}, :id => string("qn-", q.id, "--other"), :value => "", :checked => length(othervals) > 0, if type == "checkbox" - [:oninput => "if (!this.checked) { document.getElementById('$(string("qn-", q.id, "---other-input"))').value = '' }"] + [:oninput => "if (!this.checked) { document.getElementById('$(string("qn-", q.id, "--other-input"))').value = '' }"] else [] end...), elem("input", :type => "text", - :id => string("qn-", q.id, "---other-input"), + :id => string("qn-", q.id, "--other-input"), :class => "other", :placeholder => "Other", :name => string(q.id, "[]"), :value => html_escape(join(othervals, ", ")), :oninput => "document.getElementById('$(string("qn-", q.id, "--other"))').checked = this.value.length > 0"