Skip to content

Commit

Permalink
Fixes #9263 (#9266)
Browse files Browse the repository at this point in the history
* Fixes #9283

* Update URLSearchParams.test.ts

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
  • Loading branch information
Jarred-Sumner and Jarred-Sumner committed Mar 6, 2024
1 parent 12c257a commit edeeffc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
15 changes: 7 additions & 8 deletions src/bun.js/bindings/URLSearchParams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ String URLSearchParams::get(const String& name) const
return String();
}

bool URLSearchParams::has(const String& name) const
bool URLSearchParams::has(const String& name, const String& value) const
{
for (const auto& pair : m_pairs) {
if (pair.key == name)
if (pair.key == name && (value.isNull() || pair.value == value))
return true;
}
return false;
Expand All @@ -101,6 +101,7 @@ void URLSearchParams::sort()
return WTF::codePointCompareLessThan(a.key, b.key);
});
updateURL();
needsSorting = false;
}

void URLSearchParams::set(const String& name, const String& value)
Expand Down Expand Up @@ -147,13 +148,11 @@ Vector<String> URLSearchParams::getAll(const String& name) const
return values;
}

void URLSearchParams::remove(const String& name)
void URLSearchParams::remove(const String& name, const String& value)
{
if (!m_pairs.removeAllMatching([&](const auto& pair) {
return pair.key == name;
})
&& m_pairs.size() > 0)
return;
m_pairs.removeAllMatching([&](const auto& pair) {
return pair.key == name && (value.isNull() || pair.value == value);
});
updateURL();
needsSorting = true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/bindings/URLSearchParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class URLSearchParams : public RefCounted<URLSearchParams> {
}

void append(const String& name, const String& value);
void remove(const String& name);
void remove(const String& name, const String& value = {});
String get(const String& name) const;
Vector<String> getAll(const String& name) const;
bool has(const String& name) const;
bool has(const String& name, const String& value = {}) const;
void set(const String& name, const String& value);
String toString() const;
void updateFromAssociatedURL();
Expand Down
21 changes: 19 additions & 2 deletions src/bun.js/bindings/webcore/JSURLSearchParams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include <wtf/Vector.h>
#include <variant>
#include "GCDefferalContext.h"
#include "wtf/StdLibExtras.h"

namespace WebCore {
using namespace JSC;
Expand Down Expand Up @@ -278,7 +279,15 @@ static inline JSC::EncodedJSValue jsURLSearchParamsPrototypeFunction_deleteBody(
EnsureStillAliveScope argument0 = callFrame->uncheckedArgument(0);
auto name = convert<IDLUSVString>(*lexicalGlobalObject, argument0.value());
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
RELEASE_AND_RETURN(throwScope, JSValue::encode(toJS<IDLUndefined>(*lexicalGlobalObject, throwScope, [&]() -> decltype(auto) { return impl.remove(WTFMove(name)); })));

String value;
EnsureStillAliveScope argument1 = callFrame->argument(1);
if (!argument1.value().isUndefined()) {
value = convert<IDLUSVString>(*lexicalGlobalObject, argument1.value());
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
}

RELEASE_AND_RETURN(throwScope, JSValue::encode(toJS<IDLUndefined>(*lexicalGlobalObject, throwScope, [&]() -> decltype(auto) { return impl.remove(WTFMove(name), WTFMove(value)); })));
}

JSC_DEFINE_HOST_FUNCTION(jsURLSearchParamsPrototypeFunction_delete, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame))
Expand Down Expand Up @@ -338,7 +347,15 @@ static inline JSC::EncodedJSValue jsURLSearchParamsPrototypeFunction_hasBody(JSC
EnsureStillAliveScope argument0 = callFrame->uncheckedArgument(0);
auto name = convert<IDLUSVString>(*lexicalGlobalObject, argument0.value());
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
RELEASE_AND_RETURN(throwScope, JSValue::encode(toJS<IDLBoolean>(*lexicalGlobalObject, throwScope, impl.has(WTFMove(name)))));

String value;
EnsureStillAliveScope argument1 = callFrame->argument(1);
if (!argument1.value().isUndefined()) {
value = convert<IDLUSVString>(*lexicalGlobalObject, argument1.value());
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
}

RELEASE_AND_RETURN(throwScope, JSValue::encode(toJS<IDLBoolean>(*lexicalGlobalObject, throwScope, impl.has(WTFMove(name), WTFMove(value)))));
}

JSC_DEFINE_HOST_FUNCTION(jsURLSearchParamsPrototypeFunction_has, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame))
Expand Down
16 changes: 16 additions & 0 deletions test/js/web/html/URLSearchParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,19 @@ describe("URLSearchParams", () => {
});
});
});

it(".delete second argument", () => {
const params = new URLSearchParams("a=1&a=2&b=3");
params.delete("a", 1);
params.delete("b", undefined);
expect(params + "").toBe("a=2");
});

it(".has second argument", () => {
const params = new URLSearchParams("a=1&a=2&b=3");
expect(params.has("a", 1)).toBe(true);
expect(params.has("a", 2)).toBe(true);
expect(params.has("a", 3)).toBe(false);
expect(params.has("b", 3)).toBe(true);
expect(params.has("b", 4)).toBe(false);
});

0 comments on commit edeeffc

Please sign in to comment.