Skip to content

Commit 7d25a56

Browse files
author
Elusive
committed
- Fixed data handling for GET requests in AjaxRequest.
- Updated README.
1 parent ae8e9aa commit 7d25a56

File tree

11 files changed

+272
-245
lines changed

11 files changed

+272
-245
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,9 +1221,9 @@ Create a new DOM element.
12211221
- `html` is a string that will become the HTML contents of the node.
12221222
- `text` is a string that will become the text contents of the node.
12231223
- `class` is an array of classes, or a space seperated string of class names.
1224-
- `styles` is an object containing style values to set.
1224+
- `style` is an object containing style values to set.
12251225
- `value` is a string that will become the value of the node.
1226-
- `attribute` is an object containing attribute values to set.
1226+
- `attributes` is an object containing attribute values to set.
12271227
- `properties` is an object containing property values to set.
12281228
- `dataset` is an object containing dataset values to set.
12291229

dist/frost-dom-bundle.js

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@
11051105
});
11061106

11071107
/**
1108-
* FrostDOM v2.1.2
1108+
* FrostDOM v2.1.3
11091109
* https://github.com/elusivecodes/FrostDOM
11101110
*/
11111111
(function(global, factory) {
@@ -1399,6 +1399,30 @@
13991399
new MockXMLHttpRequest :
14001400
new XMLHttpRequest;
14011401

1402+
if (this._options.data) {
1403+
if (this._options.processData && Core.isObject(this._options.data)) {
1404+
if (this._options.contentType === 'application/json') {
1405+
this._options.data = JSON.stringify(this._options.data);
1406+
} else if (this._options.contentType === 'application/x-www-form-urlencoded') {
1407+
this._options.data = this.constructor._parseParams(this._options.data);
1408+
} else {
1409+
this._options.data = this.constructor._parseFormData(this._options.data);
1410+
}
1411+
}
1412+
1413+
if (this._options.method === 'GET') {
1414+
const dataParams = new URLSearchParams(this._options.data);
1415+
1416+
const searchParams = this.constructor.getSearchParams(this._options.url);
1417+
for (const [key, value] of dataParams.entries()) {
1418+
searchParams.append(key, value);
1419+
}
1420+
1421+
this._options.url = this.constructor.setSearchParams(this._options.url, searchParams);
1422+
this._options.data = null;
1423+
}
1424+
}
1425+
14021426
this._xhr.open(this._options.method, this._options.url, true, this._options.username, this._options.password);
14031427

14041428
for (const key in this._options.headers) {
@@ -1466,16 +1490,6 @@
14661490
this._options.beforeSend(this._xhr);
14671491
}
14681492

1469-
if (this._options.data && this._options.processData && Core.isObject(this._options.data)) {
1470-
if (this._options.contentType === 'application/json') {
1471-
this._options.data = JSON.stringify(this._options.data);
1472-
} else if (this._options.contentType === 'application/x-www-form-urlencoded') {
1473-
this._options.data = this.constructor._parseParams(this._options.data);
1474-
} else {
1475-
this._options.data = this.constructor._parseFormData(this._options.data);
1476-
}
1477-
}
1478-
14791493
this._xhr.send(this._options.data);
14801494

14811495
if (this._options.afterSend) {
@@ -1500,15 +1514,62 @@
15001514
* @returns {string} The new URL.
15011515
*/
15021516
appendQueryString(url, key, value) {
1517+
const searchParams = this.getSearchParams(url);
1518+
1519+
searchParams.append(key, value);
1520+
1521+
return this.setSearchParams(url, searchParams);
1522+
},
1523+
1524+
/**
1525+
* Get the URL from a URL string.
1526+
* @param {string} url The URL.
1527+
* @returns {URL} The URL.
1528+
*/
1529+
getURL(url) {
15031530
const baseHref = (window.location.origin + window.location.pathname).replace(/\/$/, '');
1504-
const urlData = new URL(url, baseHref);
1505-
urlData.searchParams.append(key, value);
1506-
let newUrl = urlData.toString();
15071531

1508-
if (newUrl.substring(0, url.length) === url) {
1509-
return newUrl;
1532+
return new URL(url, baseHref);
1533+
},
1534+
1535+
/**
1536+
* Get the URLSearchParams from a URL string.
1537+
* @param {string} url The URL.
1538+
* @returns {URLSearchParams} The URLSearchParams.
1539+
*/
1540+
getSearchParams(url) {
1541+
return this.getURL(url).searchParams;
1542+
},
1543+
1544+
/**
1545+
* Merge two or more URLSearchParams.
1546+
* @param {...URLSearchParams} params The URLSearchParms.
1547+
* @returns {URLSearchParams} The new URLSearchParams.
1548+
*/
1549+
mergeSearchParams(...params) {
1550+
const searchParams = new URLSearchParams('');
1551+
for (const param of params) {
1552+
for (const [key, value] of param.entries()) {
1553+
searchParams.set(key, value);
1554+
}
15101555
}
15111556

1557+
return searchParams;
1558+
},
1559+
1560+
/**
1561+
* Set the URLSearchParams for a URL string.
1562+
* @param {string} url The URL.
1563+
* @param {URLSearchParams} searchParams The URLSearchParams.
1564+
* @returns The new URL string.
1565+
*/
1566+
setSearchParams(url, searchParams) {
1567+
const urlData = this.getURL(url);
1568+
1569+
urlData.search = searchParams.toString();
1570+
1571+
const newUrl = urlData.toString();
1572+
15121573
const pos = newUrl.indexOf(url);
15131574
return newUrl.substring(pos);
15141575
},

dist/frost-dom-bundle.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.

dist/frost-dom.js

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* FrostDOM v2.1.2
2+
* FrostDOM v2.1.3
33
* https://github.com/elusivecodes/FrostDOM
44
*/
55
(function(global, factory) {
@@ -293,6 +293,30 @@
293293
new MockXMLHttpRequest :
294294
new XMLHttpRequest;
295295

296+
if (this._options.data) {
297+
if (this._options.processData && Core.isObject(this._options.data)) {
298+
if (this._options.contentType === 'application/json') {
299+
this._options.data = JSON.stringify(this._options.data);
300+
} else if (this._options.contentType === 'application/x-www-form-urlencoded') {
301+
this._options.data = this.constructor._parseParams(this._options.data);
302+
} else {
303+
this._options.data = this.constructor._parseFormData(this._options.data);
304+
}
305+
}
306+
307+
if (this._options.method === 'GET') {
308+
const dataParams = new URLSearchParams(this._options.data);
309+
310+
const searchParams = this.constructor.getSearchParams(this._options.url);
311+
for (const [key, value] of dataParams.entries()) {
312+
searchParams.append(key, value);
313+
}
314+
315+
this._options.url = this.constructor.setSearchParams(this._options.url, searchParams);
316+
this._options.data = null;
317+
}
318+
}
319+
296320
this._xhr.open(this._options.method, this._options.url, true, this._options.username, this._options.password);
297321

298322
for (const key in this._options.headers) {
@@ -360,16 +384,6 @@
360384
this._options.beforeSend(this._xhr);
361385
}
362386

363-
if (this._options.data && this._options.processData && Core.isObject(this._options.data)) {
364-
if (this._options.contentType === 'application/json') {
365-
this._options.data = JSON.stringify(this._options.data);
366-
} else if (this._options.contentType === 'application/x-www-form-urlencoded') {
367-
this._options.data = this.constructor._parseParams(this._options.data);
368-
} else {
369-
this._options.data = this.constructor._parseFormData(this._options.data);
370-
}
371-
}
372-
373387
this._xhr.send(this._options.data);
374388

375389
if (this._options.afterSend) {
@@ -394,15 +408,62 @@
394408
* @returns {string} The new URL.
395409
*/
396410
appendQueryString(url, key, value) {
411+
const searchParams = this.getSearchParams(url);
412+
413+
searchParams.append(key, value);
414+
415+
return this.setSearchParams(url, searchParams);
416+
},
417+
418+
/**
419+
* Get the URL from a URL string.
420+
* @param {string} url The URL.
421+
* @returns {URL} The URL.
422+
*/
423+
getURL(url) {
397424
const baseHref = (window.location.origin + window.location.pathname).replace(/\/$/, '');
398-
const urlData = new URL(url, baseHref);
399-
urlData.searchParams.append(key, value);
400-
let newUrl = urlData.toString();
401425

402-
if (newUrl.substring(0, url.length) === url) {
403-
return newUrl;
426+
return new URL(url, baseHref);
427+
},
428+
429+
/**
430+
* Get the URLSearchParams from a URL string.
431+
* @param {string} url The URL.
432+
* @returns {URLSearchParams} The URLSearchParams.
433+
*/
434+
getSearchParams(url) {
435+
return this.getURL(url).searchParams;
436+
},
437+
438+
/**
439+
* Merge two or more URLSearchParams.
440+
* @param {...URLSearchParams} params The URLSearchParms.
441+
* @returns {URLSearchParams} The new URLSearchParams.
442+
*/
443+
mergeSearchParams(...params) {
444+
const searchParams = new URLSearchParams('');
445+
for (const param of params) {
446+
for (const [key, value] of param.entries()) {
447+
searchParams.set(key, value);
448+
}
404449
}
405450

451+
return searchParams;
452+
},
453+
454+
/**
455+
* Set the URLSearchParams for a URL string.
456+
* @param {string} url The URL.
457+
* @param {URLSearchParams} searchParams The URLSearchParams.
458+
* @returns The new URL string.
459+
*/
460+
setSearchParams(url, searchParams) {
461+
const urlData = this.getURL(url);
462+
463+
urlData.search = searchParams.toString();
464+
465+
const newUrl = urlData.toString();
466+
406467
const pos = newUrl.indexOf(url);
407468
return newUrl.substring(pos);
408469
},

dist/frost-dom.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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frostdom",
3-
"version": "2.1.2",
3+
"version": "2.1.3",
44
"description": "FrostDOM is a free, open-source DOM manipulation library for JavaScript.",
55
"keywords": [
66
"dom",

src/AjaxRequest/helpers.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@ Object.assign(AjaxRequest.prototype, {
1212
new MockXMLHttpRequest :
1313
new XMLHttpRequest;
1414

15+
if (this._options.data) {
16+
if (this._options.processData && Core.isObject(this._options.data)) {
17+
if (this._options.contentType === 'application/json') {
18+
this._options.data = JSON.stringify(this._options.data);
19+
} else if (this._options.contentType === 'application/x-www-form-urlencoded') {
20+
this._options.data = this.constructor._parseParams(this._options.data);
21+
} else {
22+
this._options.data = this.constructor._parseFormData(this._options.data);
23+
}
24+
}
25+
26+
if (this._options.method === 'GET') {
27+
const dataParams = new URLSearchParams(this._options.data);
28+
29+
const searchParams = this.constructor.getSearchParams(this._options.url);
30+
for (const [key, value] of dataParams.entries()) {
31+
searchParams.append(key, value);
32+
}
33+
34+
this._options.url = this.constructor.setSearchParams(this._options.url, searchParams);
35+
this._options.data = null;
36+
}
37+
}
38+
1539
this._xhr.open(this._options.method, this._options.url, true, this._options.username, this._options.password);
1640

1741
for (const key in this._options.headers) {
@@ -79,16 +103,6 @@ Object.assign(AjaxRequest.prototype, {
79103
this._options.beforeSend(this._xhr);
80104
}
81105

82-
if (this._options.data && this._options.processData && Core.isObject(this._options.data)) {
83-
if (this._options.contentType === 'application/json') {
84-
this._options.data = JSON.stringify(this._options.data);
85-
} else if (this._options.contentType === 'application/x-www-form-urlencoded') {
86-
this._options.data = this.constructor._parseParams(this._options.data);
87-
} else {
88-
this._options.data = this.constructor._parseFormData(this._options.data);
89-
}
90-
}
91-
92106
this._xhr.send(this._options.data);
93107

94108
if (this._options.afterSend) {

src/AjaxRequest/static.js

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,62 @@ Object.assign(AjaxRequest, {
1212
* @returns {string} The new URL.
1313
*/
1414
appendQueryString(url, key, value) {
15+
const searchParams = this.getSearchParams(url);
16+
17+
searchParams.append(key, value);
18+
19+
return this.setSearchParams(url, searchParams);
20+
},
21+
22+
/**
23+
* Get the URL from a URL string.
24+
* @param {string} url The URL.
25+
* @returns {URL} The URL.
26+
*/
27+
getURL(url) {
1528
const baseHref = (window.location.origin + window.location.pathname).replace(/\/$/, '');
16-
const urlData = new URL(url, baseHref);
17-
urlData.searchParams.append(key, value);
18-
let newUrl = urlData.toString();
1929

20-
if (newUrl.substring(0, url.length) === url) {
21-
return newUrl;
30+
return new URL(url, baseHref);
31+
},
32+
33+
/**
34+
* Get the URLSearchParams from a URL string.
35+
* @param {string} url The URL.
36+
* @returns {URLSearchParams} The URLSearchParams.
37+
*/
38+
getSearchParams(url) {
39+
return this.getURL(url).searchParams;
40+
},
41+
42+
/**
43+
* Merge two or more URLSearchParams.
44+
* @param {...URLSearchParams} params The URLSearchParms.
45+
* @returns {URLSearchParams} The new URLSearchParams.
46+
*/
47+
mergeSearchParams(...params) {
48+
const searchParams = new URLSearchParams('');
49+
for (const param of params) {
50+
for (const [key, value] of param.entries()) {
51+
searchParams.set(key, value);
52+
}
2253
}
2354

55+
return searchParams;
56+
},
57+
58+
/**
59+
* Set the URLSearchParams for a URL string.
60+
* @param {string} url The URL.
61+
* @param {URLSearchParams} searchParams The URLSearchParams.
62+
* @returns The new URL string.
63+
*/
64+
setSearchParams(url, searchParams) {
65+
const urlData = this.getURL(url);
66+
67+
urlData.search = searchParams.toString();
68+
69+
const newUrl = urlData.toString();
70+
2471
const pos = newUrl.indexOf(url);
2572
return newUrl.substring(pos);
2673
},

src/wrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* FrostDOM v2.1.2
2+
* FrostDOM v2.1.3
33
* https://github.com/elusivecodes/FrostDOM
44
*/
55
(function(global, factory) {

0 commit comments

Comments
 (0)