Skip to content

Commit

Permalink
Ensure (new URLSearchParams()) instanceof URLSearchParams is true, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Judeaux authored and Jeremy Judeaux committed Aug 15, 2018
1 parent 9941b2a commit eed72ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions tests/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ QUnit.test('URLSearchParams', function(assert) {
assert.equal(String(new URLSearchParams(new URLSearchParams('?a=1&b&a'))), 'a=1&b=&a=');
});

QUnit.test('instanceof URLSearchParams', function(assert) {
assert.ok((new URLSearchParams()) instanceof URLSearchParams);
});

QUnit.test('URLSearchParams mutation', function(assert) {
var p = new URLSearchParams();
assert.equal(p.get('a'), null);
Expand Down
21 changes: 16 additions & 5 deletions url.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,27 +460,38 @@
if (new global.URLSearchParams([['a', 1]]).get('a') === '1' &&
new global.URLSearchParams({a: 1}).get('a') === '1')
return;

var orig = global.URLSearchParams;
global.URLSearchParams = function(init) {
var URLSearchParams = function(init) {
var o;
if (init && typeof init === 'object' && isSequence(init)) {
var o = new orig();
o = new orig();
toArray(init).forEach(function(e) {
if (!isSequence(e)) throw TypeError();
var nv = toArray(e);
if (nv.length !== 2) throw TypeError();
o.append(nv[0], nv[1]);
});
return o;
} else if (init && typeof init === 'object') {
o = new orig();
Object.keys(init).forEach(function(key) {
o.set(key, init[key]);
});
return o;
} else {
return new orig(init);
o = new orig(init);
}
if (Object.setPrototypeOf) {
Object.setPrototypeOf(o, URLSearchParams.prototype);
} else {
o.__proto__ = URLSearchParams.prototype;
}
return o;
};

URLSearchParams.prototype = Object.create(orig.prototype);
URLSearchParams.prototype.constructor = URLSearchParams;

global.URLSearchParams = URLSearchParams;
}());

}(self));

0 comments on commit eed72ac

Please sign in to comment.