Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit b8d6411

Browse files
committed
Merge pull request #6246 from lkcampbell/fix-6008
Fix UrlParams parsing when URL has no parameters. Add remove() and isEmpty() methods.
2 parents 831a49a + 44d8b9d commit b8d6411

3 files changed

Lines changed: 238 additions & 13 deletions

File tree

src/utils/UrlParams.js

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,30 @@ define(function (require, exports, module) {
4242
* @param {string} url
4343
*/
4444
UrlParams.prototype.parse = function (url) {
45-
if (url) {
46-
url = url.substring(url.indexOf("?") + 1);
47-
} else {
48-
url = window.document.location.search.substring(1);
49-
}
50-
51-
var urlParams = url.split("&"),
45+
var queryString = "",
46+
urlParams,
5247
p,
5348
self = this;
5449

55-
urlParams.forEach(function (param) {
56-
p = param.split("=");
57-
self._store[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
58-
});
50+
self._store = {};
51+
52+
if (!url) {
53+
queryString = window.document.location.search.substring(1);
54+
} else if (url.indexOf("?") !== -1) {
55+
queryString = url.substring(url.indexOf("?") + 1);
56+
}
57+
58+
queryString = queryString.trimRight();
59+
60+
if (queryString) {
61+
urlParams = queryString.split("&");
62+
63+
urlParams.forEach(function (param) {
64+
p = param.split("=");
65+
p[1] = p[1] || "";
66+
self._store[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
67+
});
68+
}
5969
};
6070

6171
/**
@@ -68,15 +78,33 @@ define(function (require, exports, module) {
6878
};
6979

7080
/**
71-
* Retreive a value by name
81+
* Retrieve a value by name
7282
* @param {!string} name
83+
* @return {string}
7384
*/
7485
UrlParams.prototype.get = function (name) {
7586
return this._store[name];
7687
};
7788

89+
/**
90+
* Remove a name/value string pair
91+
* @param {!string} name
92+
*/
93+
UrlParams.prototype.remove = function (name) {
94+
delete this._store[name];
95+
};
96+
97+
/**
98+
* Returns true if the parameter list is empty, else returns false.
99+
* @return {boolean}
100+
*/
101+
UrlParams.prototype.isEmpty = function (name) {
102+
return _.isEmpty(this._store);
103+
};
104+
78105
/**
79106
* Encode name/value pairs as URI components.
107+
* @return {string}
80108
*/
81109
UrlParams.prototype.toString = function () {
82110
var strs = [],
@@ -88,7 +116,7 @@ define(function (require, exports, module) {
88116

89117
return strs.join("&");
90118
};
91-
119+
92120
// Define public API
93121
exports.UrlParams = UrlParams;
94122
});

test/UnitTestSuite.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ define(function (require, exports, module) {
6868
require("spec/StringUtils-test");
6969
require("spec/TextRange-test");
7070
require("spec/UpdateNotification-test");
71+
require("spec/UrlParams-test");
7172
require("spec/ViewCommandHandlers-test");
7273
require("spec/ViewUtils-test");
7374
require("spec/WorkingSetView-test");

test/spec/UrlParams-test.js

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
* DEALINGS IN THE SOFTWARE.
21+
*
22+
*/
23+
24+
25+
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
26+
/*global define, describe, it, expect */
27+
28+
define(function (require, exports, module) {
29+
'use strict';
30+
31+
var UrlParams = require("utils/UrlParams").UrlParams;
32+
33+
describe("UrlParams", function () {
34+
describe("Test for Empty parameter list", function () {
35+
var params = new UrlParams();
36+
37+
it("should show that the parameter object is empty", function () {
38+
params.parse("http://www.brackets.io");
39+
40+
expect(params.isEmpty()).toBeTruthy();
41+
expect(params.toString()).toEqual("");
42+
});
43+
44+
it("should show that the parameter object is NOT empty", function () {
45+
params.parse("http://www.brackets.io?one=1&two=true&three=foobar");
46+
47+
expect(params.isEmpty()).toBeFalsy();
48+
expect(params.toString()).toNotEqual("");
49+
});
50+
});
51+
52+
describe("Parse and Get URL parameters", function () {
53+
var params = new UrlParams();
54+
55+
it("should create a parameter object and get three parameters", function () {
56+
params.parse("http://www.brackets.io?one=1&two=true&three=foobar");
57+
58+
expect(params.get("one")).toEqual("1");
59+
expect(params.get("two")).toEqual("true");
60+
expect(params.get("three")).toEqual("foobar");
61+
});
62+
63+
it("should create a parameter object with three parameters with empty string values", function () {
64+
params.parse("http://www.brackets.io?one&two&three");
65+
66+
expect(params.get("one")).toEqual("");
67+
expect(params.get("two")).toEqual("");
68+
expect(params.get("three")).toEqual("");
69+
});
70+
71+
it("should create a parameter object with no parameters", function () {
72+
params.parse("http://www.brackets.io");
73+
74+
expect(params.get("one")).toBeUndefined();
75+
expect(params.get("two")).toBeUndefined();
76+
expect(params.get("three")).toBeUndefined();
77+
});
78+
});
79+
80+
describe("Put and Remove URL parameters", function () {
81+
var params = new UrlParams();
82+
83+
it("should put a new parameter three in the list", function () {
84+
params.parse("http://www.brackets.io?one=1&two=true");
85+
86+
expect(params.get("one")).toEqual("1");
87+
expect(params.get("two")).toEqual("true");
88+
expect(params.get("three")).toBeUndefined();
89+
90+
params.put("three", "foobar");
91+
92+
expect(params.get("one")).toEqual("1");
93+
expect(params.get("two")).toEqual("true");
94+
expect(params.get("three")).toEqual("foobar");
95+
});
96+
97+
it("should change the value of parameter two", function () {
98+
params.parse("http://www.brackets.io?one=1&two=true&three=foobar");
99+
100+
expect(params.get("one")).toEqual("1");
101+
expect(params.get("two")).toEqual("true");
102+
expect(params.get("three")).toEqual("foobar");
103+
104+
params.put("two", "false");
105+
106+
expect(params.get("one")).toEqual("1");
107+
expect(params.get("two")).toEqual("false");
108+
expect(params.get("three")).toEqual("foobar");
109+
});
110+
111+
it("should remove parameter one", function () {
112+
params.parse("http://www.brackets.io?one=1&two=true&three=foobar");
113+
114+
expect(params.get("one")).toEqual("1");
115+
expect(params.get("two")).toEqual("true");
116+
expect(params.get("three")).toEqual("foobar");
117+
118+
params.remove("one");
119+
120+
expect(params.get("one")).toBeUndefined();
121+
expect(params.get("two")).toEqual("true");
122+
expect(params.get("three")).toEqual("foobar");
123+
});
124+
125+
it("should remove three parameters, leaving an empty list", function () {
126+
params.parse("http://www.brackets.io?one=1&two=true&three=foobar");
127+
128+
expect(params.get("one")).toEqual("1");
129+
expect(params.get("two")).toEqual("true");
130+
expect(params.get("three")).toEqual("foobar");
131+
132+
expect(params.isEmpty()).toBeFalsy();
133+
expect(params.toString()).toNotEqual("");
134+
135+
params.remove("one");
136+
params.remove("two");
137+
params.remove("three");
138+
139+
expect(params.get("one")).toBeUndefined();
140+
expect(params.get("two")).toBeUndefined();
141+
expect(params.get("three")).toBeUndefined();
142+
143+
expect(params.isEmpty()).toBeTruthy();
144+
expect(params.toString()).toEqual("");
145+
});
146+
147+
it("should add three parameters to an empty list", function () {
148+
params.parse("http://www.brackets.io");
149+
150+
expect(params.get("one")).toBeUndefined();
151+
expect(params.get("two")).toBeUndefined();
152+
expect(params.get("three")).toBeUndefined();
153+
154+
expect(params.isEmpty()).toBeTruthy();
155+
expect(params.toString()).toEqual("");
156+
157+
params.put("one", "1");
158+
params.put("two", "true");
159+
params.put("three", "foobar");
160+
161+
expect(params.get("one")).toEqual("1");
162+
expect(params.get("two")).toEqual("true");
163+
expect(params.get("three")).toEqual("foobar");
164+
165+
expect(params.isEmpty()).toBeFalsy();
166+
expect(params.toString()).toNotEqual("");
167+
});
168+
});
169+
170+
describe("Test for malformed or unusual query strings", function () {
171+
var params = new UrlParams();
172+
173+
it("should parse a missing query string as an empty object", function () {
174+
params.parse("http://www.brackets.io?");
175+
176+
expect(params.isEmpty()).toBeTruthy();
177+
expect(params.toString()).toEqual("");
178+
});
179+
180+
it("should parse a query string of whitespace as an empty object", function () {
181+
params.parse("http://www.brackets.io? ");
182+
183+
expect(params.isEmpty()).toBeTruthy();
184+
expect(params.toString()).toEqual("");
185+
});
186+
187+
it("should parse a random number (used to circumvent browser cache)", function () {
188+
params.parse("http://www.brackets.io?28945893575608");
189+
190+
expect(params.get("28945893575608")).toEqual("");
191+
expect(params.isEmpty()).toBeFalsy();
192+
expect(params.toString()).toEqual("28945893575608=");
193+
});
194+
});
195+
});
196+
});

0 commit comments

Comments
 (0)