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

Fix UrlParams parsing when URL has no parameters. Add remove() and isEmpty() methods. #6246

Merged
merged 4 commits into from
Dec 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions src/utils/UrlParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,30 @@ define(function (require, exports, module) {
* @param {string} url
*/
UrlParams.prototype.parse = function (url) {
if (url) {
url = url.substring(url.indexOf("?") + 1);
} else {
url = window.document.location.search.substring(1);
}

var urlParams = url.split("&"),
var queryString = "",
urlParams,
p,
self = this;

urlParams.forEach(function (param) {
p = param.split("=");
self._store[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
});
self._store = {};

if (!url) {
queryString = window.document.location.search.substring(1);
} else if (url.indexOf("?") !== -1) {
queryString = url.substring(url.indexOf("?") + 1);
}

queryString = queryString.trimRight();

if (queryString) {
urlParams = queryString.split("&");

urlParams.forEach(function (param) {
p = param.split("=");
p[1] = p[1] || "";
self._store[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it's possible for this code could provide unexpected results for self._store. For example, if url ends with a ?. Also, if all search parameters are integers, then self._store would be an array.

I think it would be safest to initialize it to an empty object in all cases:

        self._store = {};

        if (searchString) {
            urlParams = searchString.split("&");

            urlParams.forEach(function (param) {
                p = param.split("=");
                self._store[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
            });
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@redmunds, I believe this is already addressed in the URLParams constructor, which initializes self._store to an empty object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the constructor made the call to parse() and parse() was private method, then I would agree. But parse() is public and can be called multiple times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I missed comment until after my last commit. Let me look into it.

};

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

/**
* Retreive a value by name
* Retrieve a value by name
* @param {!string} name
* @return {string}
*/
UrlParams.prototype.get = function (name) {
return this._store[name];
};

/**
* Remove a name/value string pair
* @param {!string} name
*/
UrlParams.prototype.remove = function (name) {
delete this._store[name];
};

/**
* Returns true if the parameter list is empty, else returns false.
* @return {boolean}
*/
UrlParams.prototype.isEmpty = function (name) {
return _.isEmpty(this._store);
};

/**
* Encode name/value pairs as URI components.
* @return {string}
*/
UrlParams.prototype.toString = function () {
var strs = [],
Expand All @@ -88,7 +116,7 @@ define(function (require, exports, module) {

return strs.join("&");
};

// Define public API
exports.UrlParams = UrlParams;
});
1 change: 1 addition & 0 deletions test/UnitTestSuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ define(function (require, exports, module) {
require("spec/StringUtils-test");
require("spec/TextRange-test");
require("spec/UpdateNotification-test");
require("spec/UrlParams-test");
require("spec/ViewCommandHandlers-test");
require("spec/ViewUtils-test");
require("spec/WorkingSetView-test");
Expand Down
196 changes: 196 additions & 0 deletions test/spec/UrlParams-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/


/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, describe, it, expect */

define(function (require, exports, module) {
'use strict';

var UrlParams = require("utils/UrlParams").UrlParams;

describe("UrlParams", function () {
describe("Test for Empty parameter list", function () {
var params = new UrlParams();

it("should show that the parameter object is empty", function () {
params.parse("http://www.brackets.io");

expect(params.isEmpty()).toBeTruthy();
expect(params.toString()).toEqual("");
});

it("should show that the parameter object is NOT empty", function () {
params.parse("http://www.brackets.io?one=1&two=true&three=foobar");

expect(params.isEmpty()).toBeFalsy();
expect(params.toString()).toNotEqual("");
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add some tests for some edge cases:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@redmunds, yes, this sounds good. I'm going to put these test cases in and see if I can break the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@redmunds, one nit I ran into while describing these tests. I am now calling the portion of the URL after the question mark the "query string" instead of the "search string" naming convention I used in my original code. Even though we use window.document.location.search to find the string, the term "search string" is too narrow a descriptor and much less commonly used than "query string". My next commit will reflect this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-paired values: http://www.brackets.io?one&two&three

Fixed. Parsing this URL now produces an object with properties "one", "two", and "three", each with the value of an empty string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty params with and without whitespace at the end: http://www.brackets.io?

Fixed. Parsing these URLs now produce an empty object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random number used to circumvent browser cache: http://www.brackets.io?28945893575608

Fixed by the non-paired values fix above. Parsing this URL now produces an object with one property "28945893575608" that has the value of empty string.


describe("Parse and Get URL parameters", function () {
var params = new UrlParams();

it("should create a parameter object and get three parameters", function () {
params.parse("http://www.brackets.io?one=1&two=true&three=foobar");

expect(params.get("one")).toEqual("1");
expect(params.get("two")).toEqual("true");
expect(params.get("three")).toEqual("foobar");
});

it("should create a parameter object with three parameters with empty string values", function () {
params.parse("http://www.brackets.io?one&two&three");

expect(params.get("one")).toEqual("");
expect(params.get("two")).toEqual("");
expect(params.get("three")).toEqual("");
});

it("should create a parameter object with no parameters", function () {
params.parse("http://www.brackets.io");

expect(params.get("one")).toBeUndefined();
expect(params.get("two")).toBeUndefined();
expect(params.get("three")).toBeUndefined();
});
});

describe("Put and Remove URL parameters", function () {
var params = new UrlParams();

it("should put a new parameter three in the list", function () {
params.parse("http://www.brackets.io?one=1&two=true");

expect(params.get("one")).toEqual("1");
expect(params.get("two")).toEqual("true");
expect(params.get("three")).toBeUndefined();

params.put("three", "foobar");

expect(params.get("one")).toEqual("1");
expect(params.get("two")).toEqual("true");
expect(params.get("three")).toEqual("foobar");
});

it("should change the value of parameter two", function () {
params.parse("http://www.brackets.io?one=1&two=true&three=foobar");

expect(params.get("one")).toEqual("1");
expect(params.get("two")).toEqual("true");
expect(params.get("three")).toEqual("foobar");

params.put("two", "false");

expect(params.get("one")).toEqual("1");
expect(params.get("two")).toEqual("false");
expect(params.get("three")).toEqual("foobar");
});

it("should remove parameter one", function () {
params.parse("http://www.brackets.io?one=1&two=true&three=foobar");

expect(params.get("one")).toEqual("1");
expect(params.get("two")).toEqual("true");
expect(params.get("three")).toEqual("foobar");

params.remove("one");

expect(params.get("one")).toBeUndefined();
expect(params.get("two")).toEqual("true");
expect(params.get("three")).toEqual("foobar");
});

it("should remove three parameters, leaving an empty list", function () {
params.parse("http://www.brackets.io?one=1&two=true&three=foobar");

expect(params.get("one")).toEqual("1");
expect(params.get("two")).toEqual("true");
expect(params.get("three")).toEqual("foobar");

expect(params.isEmpty()).toBeFalsy();
expect(params.toString()).toNotEqual("");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably makes more sense to test:

        expect(params.toString()).toEqual("one=1&two=true&three=foobar");

Or is order not guaranteed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@redmunds, correct, the order is not guaranteed, and I couldn't think of a better test for toString() than checking for an empty string. Is there a way to test a toString() value of an object more accurately?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My knee-jerk expectation was that the UrlParams object would maintain order when converting back to string, but I can't think of a scenario where it's important. I guess this test is ok like it is.


params.remove("one");
params.remove("two");
params.remove("three");

expect(params.get("one")).toBeUndefined();
expect(params.get("two")).toBeUndefined();
expect(params.get("three")).toBeUndefined();

expect(params.isEmpty()).toBeTruthy();
expect(params.toString()).toEqual("");
});

it("should add three parameters to an empty list", function () {
params.parse("http://www.brackets.io");

expect(params.get("one")).toBeUndefined();
expect(params.get("two")).toBeUndefined();
expect(params.get("three")).toBeUndefined();

expect(params.isEmpty()).toBeTruthy();
expect(params.toString()).toEqual("");

params.put("one", "1");
params.put("two", "true");
params.put("three", "foobar");

expect(params.get("one")).toEqual("1");
expect(params.get("two")).toEqual("true");
expect(params.get("three")).toEqual("foobar");

expect(params.isEmpty()).toBeFalsy();
expect(params.toString()).toNotEqual("");
});
});

describe("Test for malformed or unusual query strings", function () {
var params = new UrlParams();

it("should parse a missing query string as an empty object", function () {
params.parse("http://www.brackets.io?");

expect(params.isEmpty()).toBeTruthy();
expect(params.toString()).toEqual("");
});

it("should parse a query string of whitespace as an empty object", function () {
params.parse("http://www.brackets.io? ");

expect(params.isEmpty()).toBeTruthy();
expect(params.toString()).toEqual("");
});

it("should parse a random number (used to circumvent browser cache)", function () {
params.parse("http://www.brackets.io?28945893575608");

expect(params.get("28945893575608")).toEqual("");
expect(params.isEmpty()).toBeFalsy();
expect(params.toString()).toEqual("28945893575608=");
});
});
});
});