Skip to content

Fixes issues that caused IE7/8 to fail #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ var hasOwnProperty = Object.prototype.hasOwnProperty;
var indexOf = typeof Array.prototype.indexOf === 'function'
? function(arr, el) { return arr.indexOf(el); }
: function(arr, el) {
if (!isArray(arr)) {
arr = arr.split('');
}
for (var i = 0; i < arr.length; i++) {
if (arr[i] === el) return i;
}
Expand Down Expand Up @@ -341,6 +344,9 @@ function lastBraceInKey(str) {
var len = str.length
, brace
, c;
if (!isArray(str)) {
str = str.split('');
}
for (var i = 0; i < len; ++i) {
c = str[i];
if (']' == c) brace = false;
Expand All @@ -363,4 +369,4 @@ function decode(str) {
} catch (err) {
return str;
}
}
}
68 changes: 62 additions & 6 deletions test/browser/qs.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,22 @@ require.register("querystring", function(module, exports, require){

var toString = Object.prototype.toString;

/**
* Object#hasOwnProperty ref
*/

var hasOwnProperty = Object.prototype.hasOwnProperty;

/**
* Array#indexOf shim.
*/

var indexOf = typeof Array.prototype.indexOf === 'function'
? function(arr, el) { return arr.indexOf(el); }
: function(arr, el) {
if (!isArray(arr)) {
arr = arr.split('');
}
for (var i = 0; i < arr.length; i++) {
if (arr[i] === el) return i;
}
Expand All @@ -129,7 +138,11 @@ var isArray = Array.isArray || function(arr) {

var objectKeys = Object.keys || function(obj) {
var ret = [];
for (var key in obj) ret.push(key);
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
ret.push(key);
}
}
return ret;
};

Expand Down Expand Up @@ -161,15 +174,23 @@ var reduce = function(arr, fn, initial) {
var isint = /^[0-9]+$/;

function promote(parent, key) {
if (parent[key].length == 0) return parent[key] = {};
if (parent[key].length == 0) return parent[key] = {}
var t = {};
for (var i in parent[key]) t[i] = parent[key][i];
for (var i in parent[key]) {
if (hasOwnProperty.call(parent[key], i)) {
t[i] = parent[key][i];
}
}
parent[key] = t;
return t;
}

function parse(parts, parent, key, val) {
var part = parts.shift();

// illegal
if (hasOwnProperty.call(Object.prototype, key)) return;

// end
if (!part) {
if (isArray(parent[key])) {
Expand Down Expand Up @@ -228,24 +249,52 @@ function merge(parent, key, val){
return parent;
}

/**
* Compact sparse arrays.
*/

function compact(obj) {
if ('object' != typeof obj) return obj;

if (isArray(obj)) {
var ret = [];

for (var i in obj) {
if (hasOwnProperty.call(obj, i)) {
ret.push(obj[i]);
}
}

return ret;
}

for (var key in obj) {
obj[key] = compact(obj[key]);
}

return obj;
}

/**
* Parse the given obj.
*/

function parseObject(obj){
var ret = { base: {} };

forEach(objectKeys(obj), function(name){
merge(ret, name, obj[name]);
});
return ret.base;

return compact(ret.base);
}

/**
* Parse the given str.
*/

function parseString(str){
return reduce(String(str).split('&'), function(ret, pair){
var ret = reduce(String(str).split('&'), function(ret, pair){
var eql = indexOf(pair, '=')
, brace = lastBraceInKey(pair)
, key = pair.substr(0, brace || eql)
Expand All @@ -258,6 +307,8 @@ function parseString(str){

return merge(ret, decode(key), decode(val));
}, { base: {} }).base;

return compact(ret);
}

/**
Expand Down Expand Up @@ -343,6 +394,7 @@ function stringifyObject(obj, prefix) {

for (var i = 0, len = keys.length; i < len; ++i) {
key = keys[i];
if ('' == key) continue;
if (null == obj[key]) {
ret.push(encodeURIComponent(key) + '=');
} else {
Expand All @@ -368,6 +420,7 @@ function stringifyObject(obj, prefix) {

function set(obj, key, val) {
var v = obj[key];
if (hasOwnProperty.call(Object.prototype, key)) return;
if (undefined === v) {
obj[key] = val;
} else if (isArray(v)) {
Expand All @@ -389,6 +442,9 @@ function lastBraceInKey(str) {
var len = str.length
, brace
, c;
if (!isArray(str)) {
str = str.split('');
}
for (var i = 0; i < len; ++i) {
c = str[i];
if (']' == c) brace = false;
Expand All @@ -413,4 +469,4 @@ function decode(str) {
}
}
})();
});
});
2 changes: 1 addition & 1 deletion test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,4 @@ describe('qs.parse()', function(){
Object.prototype.crash = '';
expect(qs.parse.bind(null, 'test')).to.not.throwException();
})
})
})
3 changes: 1 addition & 2 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,4 @@ describe('qs.stringify', function(){
expect(qs.stringify(obj)).to.eql(str);
});
});
});

});