From 9941b2a4ced755ee0601ff5c49f244774d26882c Mon Sep 17 00:00:00 2001 From: Jeremy Judeaux Date: Tue, 14 Aug 2018 16:59:21 +0800 Subject: [PATCH 1/2] Use polyfill when browser doesn't remove trailing question mark See https://bugs.webkit.org/show_bug.cgi?id=162345 --- url.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/url.js b/url.js index b1bd3db..f3fc00a 100644 --- a/url.js +++ b/url.js @@ -31,11 +31,15 @@ var nativeURL; try { if (origURL) { - nativeURL = new global.URL('http://example.com'); - if ('searchParams' in nativeURL) - return; - if (!('href' in nativeURL)) + nativeURL = new global.URL('http://example.com/?a=1'); + // Test for some version of Safari not removing trailing `?` + // In this case, apply polyfill + // See https://bugs.webkit.org/show_bug.cgi?id=162345 + nativeURL.search = ''; + if (!('href' in nativeURL) || nativeURL.href !== 'http://example.com/') nativeURL = undefined; + else if ('searchParams' in nativeURL) + return; } } catch (_) {} From eed72acb3b5ab8874a4318a2ccee9f4fbabcd305 Mon Sep 17 00:00:00 2001 From: Jeremy Judeaux Date: Wed, 15 Aug 2018 14:29:25 +0800 Subject: [PATCH 2/2] Ensure (new URLSearchParams()) instanceof URLSearchParams is true, fixes #150 --- tests/url.js | 4 ++++ url.js | 21 ++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/url.js b/tests/url.js index 1b6cffc..9693e8c 100644 --- a/tests/url.js +++ b/tests/url.js @@ -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); diff --git a/url.js b/url.js index f3fc00a..9a3ec9d 100644 --- a/url.js +++ b/url.js @@ -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));