Skip to content

Commit bcad298

Browse files
committed
fix possible removal of extra entries in URLSearchParam.prototype.delete polyfill with second argument
1 parent a1f9a2f commit bcad298

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Fixed handling of some line terminators in case of `multiline` + `sticky` mode in `RegExp` polyfill
2323
- Fixed handling of empty groups with `global` and `unicode` flags in polyfills
2424
- Fixed `URLSearchParam.prototype.delete` polyfill with duplicate key-value pairs
25+
- Fixed possible removal of unnecessary entries in `URLSearchParam.prototype.delete` polyfill with second argument
2526
- Fixed an error in some cases of non-special URLs without a path in the `URL` polyfill
2627
- Fixed some percent encode cases / character sets in the `URL` polyfill
2728
- Fixed host parsing with `hostname = host:port` in the `URL` polyfill

packages/core-js/modules/web.url-search-params.delete.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,16 @@ if (params + '' !== 'a=2') {
3030
var key = toString(name);
3131
var value = toString($value);
3232
var index = 0;
33-
var dindex = 0;
34-
var found = false;
3533
var entriesLength = entries.length;
3634
var entry;
3735
while (index < entriesLength) {
38-
entry = entries[index++];
39-
if (found || entry.key === key) {
40-
found = true;
41-
$delete(this, entry.key);
42-
} else dindex++;
36+
entry = entries[index];
37+
$delete(this, entry.key);
38+
index++;
4339
}
44-
while (dindex < entriesLength) {
45-
entry = entries[dindex++];
40+
index = 0;
41+
while (index < entriesLength) {
42+
entry = entries[index++];
4643
if (!(entry.key === key && entry.value === value)) append(this, entry.key, entry.value);
4744
}
4845
}, { enumerable: true, unsafe: true });

tests/unit-global/web.url-search-params.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,19 @@ QUnit.test('URLSearchParams#delete', assert => {
295295
params.delete('a', undefined);
296296
assert.same(String(params), 'b=4');
297297

298+
// delete with value should not drop entries with the same key before the target
299+
params = new URLSearchParams('b=1&a=2&b=3');
300+
params.delete('a', '2');
301+
assert.same(String(params), 'b=1&b=3', 'entries before target with same key preserved');
302+
303+
params = new URLSearchParams('a=1&a=2&b=3');
304+
params.delete('a', '1');
305+
assert.same(String(params), 'a=2&b=3', 'only matching name+value pairs removed, rest preserved');
306+
307+
params = new URLSearchParams('a=1&b=2');
308+
params.delete('a', '999');
309+
assert.same(String(params), 'a=1&b=2', 'no match leaves all entries intact');
310+
298311
if (DESCRIPTORS) {
299312
let url = new URL('http://example.com/?param1&param2');
300313
url.searchParams.delete('param1');

tests/unit-pure/web.url-search-params.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,19 @@ QUnit.test('URLSearchParams#delete', assert => {
295295
params.delete('a', undefined);
296296
assert.same(String(params), 'b=4');
297297

298+
// delete with value should not drop entries with the same key before the target
299+
params = new URLSearchParams('b=1&a=2&b=3');
300+
params.delete('a', '2');
301+
assert.same(String(params), 'b=1&b=3', 'entries before target with same key preserved');
302+
303+
params = new URLSearchParams('a=1&a=2&b=3');
304+
params.delete('a', '1');
305+
assert.same(String(params), 'a=2&b=3', 'only matching name+value pairs removed, rest preserved');
306+
307+
params = new URLSearchParams('a=1&b=2');
308+
params.delete('a', '999');
309+
assert.same(String(params), 'a=1&b=2', 'no match leaves all entries intact');
310+
298311
if (DESCRIPTORS) {
299312
let url = new URL('http://example.com/?param1&param2');
300313
url.searchParams.delete('param1');

0 commit comments

Comments
 (0)