Skip to content

Commit 3225942

Browse files
TimothyGuaddaleax
authored andcommitted
url: update sort() behavior with no params
PR-URL: #14185 Refs: whatwg/url#336 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent cfed48e commit 3225942

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

lib/internal/url.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1076,12 +1076,11 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
10761076
sort() {
10771077
const a = this[searchParams];
10781078
const len = a.length;
1079-
if (len <= 2) {
1080-
return;
1081-
}
10821079

1083-
// arbitrary number found through testing
1084-
if (len < 100) {
1080+
if (len <= 2) {
1081+
// Nothing needs to be done.
1082+
} else if (len < 100) {
1083+
// 100 is found through testing.
10851084
// Simple stable in-place insertion sort
10861085
// Derived from v8/src/js/array.js
10871086
for (var i = 2; i < len; i += 2) {

test/parallel/test-whatwg-url-searchparams-delete.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { test, assert_equals, assert_true, assert_false } =
88

99
/* The following tests are copied from WPT. Modifications to them should be
1010
upstreamed first. Refs:
11-
https://github.com/w3c/web-platform-tests/blob/8791bed/url/urlsearchparams-delete.html
11+
https://github.com/w3c/web-platform-tests/blob/70a0898763/url/urlsearchparams-delete.html
1212
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
1313
*/
1414
/* eslint-disable */
@@ -42,6 +42,21 @@ test(function() {
4242
params.delete('first');
4343
assert_false(params.has('first'), 'Search params object has no "first" name');
4444
}, 'Deleting appended multiple');
45+
46+
test(function() {
47+
var url = new URL('http://example.com/?param1&param2');
48+
url.searchParams.delete('param1');
49+
url.searchParams.delete('param2');
50+
assert_equals(url.href, 'http://example.com/', 'url.href does not have ?');
51+
assert_equals(url.search, '', 'url.search does not have ?');
52+
}, 'Deleting all params removes ? from URL');
53+
54+
test(function() {
55+
var url = new URL('http://example.com/?');
56+
url.searchParams.delete('param1');
57+
assert_equals(url.href, 'http://example.com/', 'url.href does not have ?');
58+
assert_equals(url.search, '', 'url.search does not have ?');
59+
}, 'Removing non-existent param removes ? from URL');
4560
/* eslint-enable */
4661

4762
// Tests below are not from WPT.

test/parallel/test-whatwg-url-searchparams-sort.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
require('../common');
44
const { URL, URLSearchParams } = require('url');
5-
const { test, assert_array_equals } = require('../common/wpt');
5+
const { test, assert_equals, assert_array_equals } = require('../common/wpt');
66

77
/* The following tests are copied from WPT. Modifications to them should be
88
upstreamed first. Refs:
9-
https://github.com/w3c/web-platform-tests/blob/5903e00e77e85f8bcb21c73d1d7819fcd04763bd/url/urlsearchparams-sort.html
9+
https://github.com/w3c/web-platform-tests/blob/70a0898763/url/urlsearchparams-sort.html
1010
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
1111
*/
1212
/* eslint-disable */
@@ -53,6 +53,13 @@ const { test, assert_array_equals } = require('../common/wpt');
5353
}
5454
}, "URL parse and sort: " + val.input)
5555
})
56+
57+
test(function() {
58+
const url = new URL("http://example.com/?")
59+
url.searchParams.sort()
60+
assert_equals(url.href, "http://example.com/")
61+
assert_equals(url.search, "")
62+
}, "Sorting non-existent params removes ? from URL")
5663
/* eslint-enable */
5764

5865
// Tests below are not from WPT.

0 commit comments

Comments
 (0)