mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
feat(url): implement URLPattern API (#25168)
## Summary Implements the [URLPattern Web API](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern) based on WebKit's implementation. URLPattern provides declarative pattern matching for URLs, similar to how regular expressions work for strings. ### Features - **Constructor**: Create patterns from strings or `URLPatternInit` dictionaries - **`test()`**: Check if a URL matches the pattern (returns boolean) - **`exec()`**: Extract matched groups from a URL (returns `URLPatternResult` or null) - **Pattern properties**: `protocol`, `username`, `password`, `hostname`, `port`, `pathname`, `search`, `hash` - **`hasRegExpGroups`**: Detect if the pattern uses custom regular expressions ### Example Usage ```js // Match URLs with a user ID parameter const pattern = new URLPattern({ pathname: '/users/:id' }); pattern.test('https://example.com/users/123'); // true pattern.test('https://example.com/posts/456'); // false const result = pattern.exec('https://example.com/users/123'); console.log(result.pathname.groups.id); // "123" // Wildcard matching const filesPattern = new URLPattern({ pathname: '/files/*' }); const match = filesPattern.exec('https://example.com/files/image.png'); console.log(match.pathname.groups[0]); // "image.png" ``` ## Implementation Notes - Adapted from WebKit's URLPattern implementation - Modified JS bindings to work with Bun's infrastructure (simpler `convertDictionary` patterns, WTF::Variant handling) - Added IsoSubspaces for proper GC integration ## Test Plan - [x] 408 tests from Web Platform Tests pass - [x] Tests fail with system Bun (URLPattern not defined), pass with debug build - [x] Manual testing of basic functionality Fixes https://github.com/oven-sh/bun/issues/2286 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
200
src/bun.js/bindings/webcore/JSURLPatternInit.cpp
Normal file
200
src/bun.js/bindings/webcore/JSURLPatternInit.cpp
Normal file
@@ -0,0 +1,200 @@
|
||||
/*
|
||||
This file is part of the WebKit open source project.
|
||||
This file has been generated by generate-bindings.pl. DO NOT MODIFY!
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public License
|
||||
along with this library; see the file COPYING.LIB. If not, write to
|
||||
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "JSURLPatternInit.h"
|
||||
|
||||
#include "JSDOMConvertStrings.h"
|
||||
#include "JSDOMGlobalObject.h"
|
||||
#include <JavaScriptCore/JSCInlines.h>
|
||||
#include <JavaScriptCore/ObjectConstructor.h>
|
||||
|
||||
namespace WebCore {
|
||||
using namespace JSC;
|
||||
|
||||
template<> URLPatternInit convertDictionary<URLPatternInit>(JSGlobalObject& lexicalGlobalObject, JSValue value)
|
||||
{
|
||||
auto& vm = JSC::getVM(&lexicalGlobalObject);
|
||||
auto throwScope = DECLARE_THROW_SCOPE(vm);
|
||||
bool isNullOrUndefined = value.isUndefinedOrNull();
|
||||
auto* object = isNullOrUndefined ? nullptr : value.getObject();
|
||||
if (!isNullOrUndefined && !object) [[unlikely]] {
|
||||
throwTypeError(&lexicalGlobalObject, throwScope);
|
||||
return {};
|
||||
}
|
||||
URLPatternInit result;
|
||||
JSValue baseURLValue;
|
||||
if (isNullOrUndefined)
|
||||
baseURLValue = jsUndefined();
|
||||
else {
|
||||
baseURLValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "baseURL"_s));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
if (!baseURLValue.isUndefined()) {
|
||||
result.baseURL = convert<IDLUSVString>(lexicalGlobalObject, baseURLValue);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
JSValue hashValue;
|
||||
if (isNullOrUndefined)
|
||||
hashValue = jsUndefined();
|
||||
else {
|
||||
hashValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "hash"_s));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
if (!hashValue.isUndefined()) {
|
||||
result.hash = convert<IDLUSVString>(lexicalGlobalObject, hashValue);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
JSValue hostnameValue;
|
||||
if (isNullOrUndefined)
|
||||
hostnameValue = jsUndefined();
|
||||
else {
|
||||
hostnameValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "hostname"_s));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
if (!hostnameValue.isUndefined()) {
|
||||
result.hostname = convert<IDLUSVString>(lexicalGlobalObject, hostnameValue);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
JSValue passwordValue;
|
||||
if (isNullOrUndefined)
|
||||
passwordValue = jsUndefined();
|
||||
else {
|
||||
passwordValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "password"_s));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
if (!passwordValue.isUndefined()) {
|
||||
result.password = convert<IDLUSVString>(lexicalGlobalObject, passwordValue);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
JSValue pathnameValue;
|
||||
if (isNullOrUndefined)
|
||||
pathnameValue = jsUndefined();
|
||||
else {
|
||||
pathnameValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "pathname"_s));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
if (!pathnameValue.isUndefined()) {
|
||||
result.pathname = convert<IDLUSVString>(lexicalGlobalObject, pathnameValue);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
JSValue portValue;
|
||||
if (isNullOrUndefined)
|
||||
portValue = jsUndefined();
|
||||
else {
|
||||
portValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "port"_s));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
if (!portValue.isUndefined()) {
|
||||
result.port = convert<IDLUSVString>(lexicalGlobalObject, portValue);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
JSValue protocolValue;
|
||||
if (isNullOrUndefined)
|
||||
protocolValue = jsUndefined();
|
||||
else {
|
||||
protocolValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "protocol"_s));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
if (!protocolValue.isUndefined()) {
|
||||
result.protocol = convert<IDLUSVString>(lexicalGlobalObject, protocolValue);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
JSValue searchValue;
|
||||
if (isNullOrUndefined)
|
||||
searchValue = jsUndefined();
|
||||
else {
|
||||
searchValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "search"_s));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
if (!searchValue.isUndefined()) {
|
||||
result.search = convert<IDLUSVString>(lexicalGlobalObject, searchValue);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
JSValue usernameValue;
|
||||
if (isNullOrUndefined)
|
||||
usernameValue = jsUndefined();
|
||||
else {
|
||||
usernameValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "username"_s));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
if (!usernameValue.isUndefined()) {
|
||||
result.username = convert<IDLUSVString>(lexicalGlobalObject, usernameValue);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
JSC::JSObject* convertDictionaryToJS(JSC::JSGlobalObject& lexicalGlobalObject, JSDOMGlobalObject& globalObject, const URLPatternInit& dictionary)
|
||||
{
|
||||
auto& vm = JSC::getVM(&lexicalGlobalObject);
|
||||
auto throwScope = DECLARE_THROW_SCOPE(vm);
|
||||
|
||||
auto result = constructEmptyObject(&lexicalGlobalObject, globalObject.objectPrototype());
|
||||
|
||||
if (!IDLUSVString::isNullValue(dictionary.baseURL)) {
|
||||
auto baseURLValue = toJS<IDLUSVString>(lexicalGlobalObject, throwScope, IDLUSVString::extractValueFromNullable(dictionary.baseURL));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
result->putDirect(vm, JSC::Identifier::fromString(vm, "baseURL"_s), baseURLValue);
|
||||
}
|
||||
if (!IDLUSVString::isNullValue(dictionary.hash)) {
|
||||
auto hashValue = toJS<IDLUSVString>(lexicalGlobalObject, throwScope, IDLUSVString::extractValueFromNullable(dictionary.hash));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
result->putDirect(vm, JSC::Identifier::fromString(vm, "hash"_s), hashValue);
|
||||
}
|
||||
if (!IDLUSVString::isNullValue(dictionary.hostname)) {
|
||||
auto hostnameValue = toJS<IDLUSVString>(lexicalGlobalObject, throwScope, IDLUSVString::extractValueFromNullable(dictionary.hostname));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
result->putDirect(vm, JSC::Identifier::fromString(vm, "hostname"_s), hostnameValue);
|
||||
}
|
||||
if (!IDLUSVString::isNullValue(dictionary.password)) {
|
||||
auto passwordValue = toJS<IDLUSVString>(lexicalGlobalObject, throwScope, IDLUSVString::extractValueFromNullable(dictionary.password));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
result->putDirect(vm, JSC::Identifier::fromString(vm, "password"_s), passwordValue);
|
||||
}
|
||||
if (!IDLUSVString::isNullValue(dictionary.pathname)) {
|
||||
auto pathnameValue = toJS<IDLUSVString>(lexicalGlobalObject, throwScope, IDLUSVString::extractValueFromNullable(dictionary.pathname));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
result->putDirect(vm, JSC::Identifier::fromString(vm, "pathname"_s), pathnameValue);
|
||||
}
|
||||
if (!IDLUSVString::isNullValue(dictionary.port)) {
|
||||
auto portValue = toJS<IDLUSVString>(lexicalGlobalObject, throwScope, IDLUSVString::extractValueFromNullable(dictionary.port));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
result->putDirect(vm, JSC::Identifier::fromString(vm, "port"_s), portValue);
|
||||
}
|
||||
if (!IDLUSVString::isNullValue(dictionary.protocol)) {
|
||||
auto protocolValue = toJS<IDLUSVString>(lexicalGlobalObject, throwScope, IDLUSVString::extractValueFromNullable(dictionary.protocol));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
result->putDirect(vm, JSC::Identifier::fromString(vm, "protocol"_s), protocolValue);
|
||||
}
|
||||
if (!IDLUSVString::isNullValue(dictionary.search)) {
|
||||
auto searchValue = toJS<IDLUSVString>(lexicalGlobalObject, throwScope, IDLUSVString::extractValueFromNullable(dictionary.search));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
result->putDirect(vm, JSC::Identifier::fromString(vm, "search"_s), searchValue);
|
||||
}
|
||||
if (!IDLUSVString::isNullValue(dictionary.username)) {
|
||||
auto usernameValue = toJS<IDLUSVString>(lexicalGlobalObject, throwScope, IDLUSVString::extractValueFromNullable(dictionary.username));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
result->putDirect(vm, JSC::Identifier::fromString(vm, "username"_s), usernameValue);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace WebCore
|
||||
Reference in New Issue
Block a user