Skip to content

Commit 158b5df

Browse files
committed
support for encoding/decoding url values and testing json objects as url query values
1 parent 20b591f commit 158b5df

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

test_unit.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const ExampleValues = {
7070
let t_InitForm = {
7171
"name": "Initialize Form",
7272
"func": test_Init,
73-
"golden": `https://localhost:8082/?first_name=Bob&last_name=Smith&email_address=bob%40something.com&phone_number=1234567890&subscribe_latest_news=true&country_select=1`
73+
"golden": `https://localhost:8082/?first_name=Bob&last_name=Smith&email_address=bob%40something.com&phone_number=1234567890&subscribe_latest_news=true&country_select=1&json_payload=%7B%22e%22%3A%22ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%21%23%24%25%26%28%29*%2B%2C.%2F%3A%3B%3C%3D%3E%3F%40%5B%5D%5E_%60%7B%7C%7D%7E%22%7D`
7474
};
7575

7676
/**@type {Test} */
@@ -201,6 +201,11 @@ function test_Init() {
201201
url.searchParams.set('phone_number', 1234567890);
202202
url.searchParams.set('subscribe_latest_news', true);
203203
url.searchParams.set('country_select', "1");
204+
// Tests JSON objects/escaping as URL values.
205+
url.searchParams.set('json_payload', JSON.stringify({
206+
"e": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"
207+
}));
208+
204209
// Push new state that updates query params without reloading the page.
205210
window.history.pushState({}, '', url);
206211
initedFormOptions = URLForm.Init(FormOptions);
@@ -264,8 +269,8 @@ function test_GetFormElements() {
264269
// Tests retrieval of key:value pairs from the URL.
265270
function test_GetURLKeyValue() {
266271
let pairs = URLForm.GetURLKeyValue(initedFormOptions);
267-
let golden = `{"first_name":"Bob","last_name":"Smith","email_address":"bob@something.com","phone_number":"1234567890","subscribe_latest_news":"true","country_select":"1"}`
268-
return JSON.stringify(pairs) === golden;
272+
let golden = {"first_name":"Bob","last_name":"Smith","email_address":"bob@something.com","phone_number":"1234567890","subscribe_latest_news":"true","country_select":"1","json_payload":"{\"e\":\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~\"}"}
273+
return JSON.stringify(pairs) === JSON.stringify(golden);
269274
}
270275

271276
// Tests Serialize().

urlform.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ function getFragmentString() {
386386
if (fParts.length == 1) { // only "#"
387387
return "";
388388
}
389-
return fParts[1];
389+
// Always decode URL, even if not URL encoded.
390+
return decodeURIComponent(fParts[1]);
390391
}
391392

392393

@@ -571,7 +572,7 @@ function sanitizeFormOptions(formOptions) {
571572

572573
/**
573574
* Generates a share URL from the current URL and form, populates the GUI with
574-
* share links, and returns the URL.
575+
* share links, and returns the URL encoded URL.
575576
*
576577
* Fragment query parameters take precedence over query parameters.
577578
*
@@ -644,7 +645,7 @@ function setShareURL(href, formOptions) {
644645

645646

646647
/**
647-
* Generates a fragment string from Fragment.
648+
* Generates a URL encoded fragment string from Fragment.
648649
*
649650
* @param {Fragment} fragment
650651
* @param {FormOptions} formOptions
@@ -685,7 +686,7 @@ function quagPartsToURLHash(fragment, formOptions) {
685686

686687
// After.
687688
fqs += fragment.after;
688-
return fqs;
689+
return encodeURIComponent(fqs);
689690
}
690691

691692

@@ -773,7 +774,7 @@ function GetQuagParts(formOptions) {
773774

774775
let qp = {
775776
query: {
776-
string: window.location.search.substring(1), // substring removes "?"
777+
string: decodeURIComponent(window.location.search.substring(1)), // substring removes "?"
777778
pairs: getPairs(window.location.search.substring(1)),
778779
extras: {},
779780
},

urlform.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

urlform.min.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)