Skip to content

Commit dd1b59f

Browse files
committed
url escape values only
1 parent 515d61f commit dd1b59f

File tree

3 files changed

+31
-32
lines changed

3 files changed

+31
-32
lines changed

urlform.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// URLFormJS is used for sticky forms and sharable URL links. See README.
22
'use strict';
33

4-
5-
6-
74
/**
85
* FormParameter are the options for a form's field/parameter.
96
*
@@ -15,32 +12,31 @@
1512
* "funcTrue": ()=> ToggleVisible(document.querySelector("#advancedOptions"));
1613
* }
1714
*
18-
* - name: Parameter name in the URI. Is used as the default value for
15+
* - name Parameter name in the URI. Is used as the default value for
1916
* id.
2017
*
21-
* - id: Id of the html element if it differs from the name. Example,
22-
* URI parameter "retrieve" and html id "Retrieve"
18+
* - id Id of the HTML element if it differs from the name. Example,
19+
* URI parameter "retrieve" and HTML id "Retrieve".
2320
*
24-
* - type: Type of the parameter (bool/string/number). Defaults to
21+
* - type Type of the parameter (bool/string/number). Defaults to
2522
* string. For 'bool', if the parameter is present in the URL
2623
* and has a function set in 'funcTrue', the function will be
2724
* executed. (e.g. https://localhost/?send_news_and_updates)
2825
* using the example above will execute the 'ToggleVisible'
2926
* function.
3027
*
31-
* - func: Called if set on each call to SetForm
28+
* - func Called if set on each call to SetForm
3229
* (Populate and PopulateFromValues).
3330
*
34-
* - funcTrue: Execute if param is true. e.g. `"funcTrue": ()
35-
* => {
31+
* - funcTrue Execute if param is true. e.g. `"funcTrue": () => {
3632
* ToggleVisible(document.querySelector("#advancedOptions"))};`
3733
*
38-
* - queryLocation: Option for overriding the param in the URL link to either
34+
* - queryLocation Option for overriding the param in the URL link to either
3935
* be a query parameter, or a fragment query. Defaults to empty
4036
* string, which will inherit the 'defaultQueryLocation' from
4137
* the form wide options.
4238
*
43-
* - saveSetting: Save and use this setting from local storage. Will be
39+
* - saveSetting (Bool) Save and use this setting from local storage. Will be
4440
* overwritten by URL flag values if present.
4541
* @typedef {Object} FormParameter
4642
* @property {String} name
@@ -97,10 +93,10 @@
9793
* - clearBtn: Button element for clearing the form and
9894
* queries/fragments in the URL.
9995
*
100-
* - shareURLBtn: Button element triggers generating share link.
101-
*
10296
* - shareURL: Element ID of <a> for share link.
10397
*
98+
* - shareURLBtn: Button element triggers generating share link.
99+
*
104100
* - shareURLArea: Element ID of text area for share link.
105101
*
106102
* - defaultQueryLocation: Link sets parameter in query or fragment query.
@@ -586,7 +582,7 @@ function ShareURI(formOptions) {
586582
var u = new URL(window.location.origin + window.location.pathname);
587583

588584
for (let fp of formOptions.FormParameters) {
589-
let value = formPairs[fp.name];
585+
let value = encodeURIComponent(formPairs[fp.name]);
590586
// console.log(fp, value);
591587
if (isEmpty(value)) {
592588
// Sets value if populated. Otherwise removes from the query/fragment. (A
@@ -661,7 +657,7 @@ function quagPartsToURLHash(fragment, formOptions) {
661657
fqs += "?"; //start fragment query delimiter ("?")
662658
for (let key in fragment.pairs) {
663659
i--;
664-
fqs += key + "=" + fragment.pairs[key]
660+
fqs += key + "=" + encodeURIComponent(fragment.pairs[key]);
665661
if (i > 0) {
666662
fqs += "&"; // Add separator on everything except the last.
667663
}
@@ -677,7 +673,7 @@ function quagPartsToURLHash(fragment, formOptions) {
677673
if (j > 0 && !formOptions.cleanURL) {
678674
for (let e in fragment.extras) {
679675
j--;
680-
fqs += e + "=" + fragment.extras[e]
676+
fqs += e + "=" + encodeURIComponent(fragment.extras[e]);
681677
if (j > 0) {
682678
fqs += "&";
683679
}
@@ -686,12 +682,12 @@ function quagPartsToURLHash(fragment, formOptions) {
686682

687683
// After.
688684
fqs += fragment.after;
689-
return encodeURIComponent(fqs);
685+
return fqs;
690686
}
691687

692688

693689
/**
694-
* Returns from `key=value` string a `key:value` object.
690+
* Helper that returns a `k:v,k:v` object from a `k=v&k=v` string.
695691
*
696692
* @param {String} s e.g. `key=value&key=value`.
697693
* @returns {QuagPairs} {key:value}
@@ -730,6 +726,8 @@ function getPairs(s) {
730726
* form, and puts values into the correct object based on formOptions.
731727
* Includes extras. See docs on `QuagParts`.
732728
*
729+
* On duplicate the default behavior overwrites query pairs with fragment pairs.
730+
*
733731
* @param {FormOptions} formOptions
734732
* @returns {QuagParts}
735733
*/
@@ -813,8 +811,9 @@ function GetQuagParts(formOptions) {
813811
}
814812

815813
/**
816-
* GetURLKeyValue is a helper func that returns the key:value pairs from the
817-
* URL. Default behavior overwrites query pairs with fragment pairs.
814+
* GetURLKeyValue is a helper that returns k:v from the current URL.
815+
*
816+
* On duplicate the default behavior overwrites query pairs with fragment pairs.
818817
*
819818
* @param {FormOptions} formOptions
820819
* @returns {QuagParts.pairs}
@@ -853,7 +852,7 @@ function GetForm(formOptions, ReturnPairOnZero) {
853852
}
854853

855854
let pairs = {};
856-
// Normal usage, Not FormMode. On individual ID's, not in a <form>.
855+
// Normal usage, not FormMode (not in a <form>), select individual ID's.
857856
if (!formOptions.FormMode) {
858857
for (let fp of formOptions.FormParameters) {
859858
let value;
@@ -919,19 +918,19 @@ function GetForm(formOptions, ReturnPairOnZero) {
919918
};
920919

921920
/**
922-
* GetFormElements will return a key:value object, where the key's are the given
923-
* form parameters, and the values are the elements that hold the form
924-
* parameters' values.
921+
* GetFormElements will return a key:value object from teh GUI form using form
922+
* parameters, and the values are the elements that hold the form parameters'
923+
* values.
925924
*
926925
* @param {FormOptions} formOptions
927926
* @returns {QuagPairs} key/value (where value is an HTML Element)
928927
*/
929928
function GetFormElements(formOptions) {
930-
let elems = {};
929+
let kv = {};
931930
for (let param of formOptions.FormParameters) {
932-
elems[param.name] = document.getElementById(formOptions.prefix + param.name);
931+
kv[param.name] = document.getElementById(formOptions.prefix + param.name);
933932
}
934-
return elems;
933+
return kv;
935934
};
936935

937936

0 commit comments

Comments
 (0)