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:
56
src/bun.js/bindings/webcore/JSURLPatternOptions.cpp
Normal file
56
src/bun.js/bindings/webcore/JSURLPatternOptions.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
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 "JSURLPatternOptions.h"
|
||||
|
||||
#include "JSDOMConvertBoolean.h"
|
||||
#include <JavaScriptCore/JSCInlines.h>
|
||||
|
||||
namespace WebCore {
|
||||
using namespace JSC;
|
||||
|
||||
template<> URLPatternOptions convertDictionary<URLPatternOptions>(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 {};
|
||||
}
|
||||
URLPatternOptions result;
|
||||
JSValue ignoreCaseValue;
|
||||
if (isNullOrUndefined)
|
||||
ignoreCaseValue = jsUndefined();
|
||||
else {
|
||||
ignoreCaseValue = object->get(&lexicalGlobalObject, Identifier::fromString(vm, "ignoreCase"_s));
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
}
|
||||
if (!ignoreCaseValue.isUndefined()) {
|
||||
result.ignoreCase = convert<IDLBoolean>(lexicalGlobalObject, ignoreCaseValue);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
} else
|
||||
result.ignoreCase = false;
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace WebCore
|
||||
Reference in New Issue
Block a user