LibWeb/URLPattern: Implement the URLPattern constructors

The underlying URL::Pattern implementation still has to be fully
implemented, but this does complete the IDL wrapper layer of the
implementation.
This commit is contained in:
Shannon Booth 2025-03-01 14:47:36 +13:00 committed by Tim Flynn
commit ba93e2a8a3
Notes: github-actions[bot] 2025-03-04 21:52:53 +00:00
2 changed files with 22 additions and 6 deletions

View file

@ -13,8 +13,9 @@ namespace Web::URLPattern {
GC_DEFINE_ALLOCATOR(URLPattern); GC_DEFINE_ALLOCATOR(URLPattern);
URLPattern::URLPattern(JS::Realm& realm) URLPattern::URLPattern(JS::Realm& realm, URL::Pattern::Pattern pattern)
: PlatformObject(realm) : PlatformObject(realm)
, m_url_pattern(move(pattern))
{ {
} }
@ -26,14 +27,28 @@ void URLPattern::initialize(JS::Realm& realm)
WEB_SET_PROTOTYPE_FOR_INTERFACE(URLPattern); WEB_SET_PROTOTYPE_FOR_INTERFACE(URLPattern);
} }
WebIDL::ExceptionOr<GC::Ref<URLPattern>> URLPattern::construct_impl(JS::Realm& realm, URLPatternInput const&, String const&, URLPatternOptions const&) // https://urlpattern.spec.whatwg.org/#dom-urlpattern-urlpattern
WebIDL::ExceptionOr<GC::Ref<URLPattern>> URLPattern::construct_impl(JS::Realm& realm, URLPatternInput const& input, String const& base_url, URLPatternOptions const& options)
{ {
return realm.create<URLPattern>(realm); // 1. Run initialize given this, input, baseURL, and options.
return create(realm, input, base_url, options);
} }
WebIDL::ExceptionOr<GC::Ref<URLPattern>> URLPattern::construct_impl(JS::Realm& realm, URLPatternInput const&, URLPatternOptions const&) // https://urlpattern.spec.whatwg.org/#dom-urlpattern-urlpattern-input-options
WebIDL::ExceptionOr<GC::Ref<URLPattern>> URLPattern::construct_impl(JS::Realm& realm, URLPatternInput const& input, URLPatternOptions const& options)
{ {
return realm.create<URLPattern>(realm); // 1. Run initialize given this, input, null, and options.
return create(realm, input, {}, options);
}
// https://urlpattern.spec.whatwg.org/#urlpattern-initialize
WebIDL::ExceptionOr<GC::Ref<URLPattern>> URLPattern::create(JS::Realm& realm, URLPatternInput const& input, Optional<String> const& base_url, URLPatternOptions const& options)
{
// 1. Set thiss associated URL pattern to the result of create given input, baseURL, and options.
auto pattern_or_error = URL::Pattern::Pattern::create(input, base_url, options);
if (pattern_or_error.is_error())
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, pattern_or_error.error().message };
return realm.create<URLPattern>(realm, pattern_or_error.release_value());
} }
// https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec // https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec

View file

@ -24,6 +24,7 @@ class URLPattern : public Bindings::PlatformObject {
GC_DECLARE_ALLOCATOR(URLPattern); GC_DECLARE_ALLOCATOR(URLPattern);
public: public:
static WebIDL::ExceptionOr<GC::Ref<URLPattern>> create(JS::Realm&, URLPatternInput const&, Optional<String> const& base_url, URLPatternOptions const& = {});
static WebIDL::ExceptionOr<GC::Ref<URLPattern>> construct_impl(JS::Realm&, URLPatternInput const&, String const& base_url, URLPatternOptions const& = {}); static WebIDL::ExceptionOr<GC::Ref<URLPattern>> construct_impl(JS::Realm&, URLPatternInput const&, String const& base_url, URLPatternOptions const& = {});
static WebIDL::ExceptionOr<GC::Ref<URLPattern>> construct_impl(JS::Realm&, URLPatternInput const&, URLPatternOptions const& = {}); static WebIDL::ExceptionOr<GC::Ref<URLPattern>> construct_impl(JS::Realm&, URLPatternInput const&, URLPatternOptions const& = {});
@ -45,7 +46,7 @@ public:
protected: protected:
virtual void initialize(JS::Realm&) override; virtual void initialize(JS::Realm&) override;
explicit URLPattern(JS::Realm&); explicit URLPattern(JS::Realm&, URL::Pattern::Pattern);
private: private:
// https://urlpattern.spec.whatwg.org/#ref-for-url-pattern%E2%91%A0 // https://urlpattern.spec.whatwg.org/#ref-for-url-pattern%E2%91%A0