diff --git a/Libraries/LibWeb/URLPattern/URLPattern.cpp b/Libraries/LibWeb/URLPattern/URLPattern.cpp index 9e8cd4b7a22..bafd9c8efb1 100644 --- a/Libraries/LibWeb/URLPattern/URLPattern.cpp +++ b/Libraries/LibWeb/URLPattern/URLPattern.cpp @@ -13,8 +13,9 @@ namespace Web::URLPattern { GC_DEFINE_ALLOCATOR(URLPattern); -URLPattern::URLPattern(JS::Realm& realm) +URLPattern::URLPattern(JS::Realm& realm, URL::Pattern::Pattern pattern) : PlatformObject(realm) + , m_url_pattern(move(pattern)) { } @@ -26,14 +27,28 @@ void URLPattern::initialize(JS::Realm& realm) WEB_SET_PROTOTYPE_FOR_INTERFACE(URLPattern); } -WebIDL::ExceptionOr> URLPattern::construct_impl(JS::Realm& realm, URLPatternInput const&, String const&, URLPatternOptions const&) +// https://urlpattern.spec.whatwg.org/#dom-urlpattern-urlpattern +WebIDL::ExceptionOr> URLPattern::construct_impl(JS::Realm& realm, URLPatternInput const& input, String const& base_url, URLPatternOptions const& options) { - return realm.create(realm); + // 1. Run initialize given this, input, baseURL, and options. + return create(realm, input, base_url, options); } -WebIDL::ExceptionOr> URLPattern::construct_impl(JS::Realm& realm, URLPatternInput const&, URLPatternOptions const&) +// https://urlpattern.spec.whatwg.org/#dom-urlpattern-urlpattern-input-options +WebIDL::ExceptionOr> URLPattern::construct_impl(JS::Realm& realm, URLPatternInput const& input, URLPatternOptions const& options) { - return realm.create(realm); + // 1. Run initialize given this, input, null, and options. + return create(realm, input, {}, options); +} + +// https://urlpattern.spec.whatwg.org/#urlpattern-initialize +WebIDL::ExceptionOr> URLPattern::create(JS::Realm& realm, URLPatternInput const& input, Optional const& base_url, URLPatternOptions const& options) +{ + // 1. Set this’s 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(realm, pattern_or_error.release_value()); } // https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec diff --git a/Libraries/LibWeb/URLPattern/URLPattern.h b/Libraries/LibWeb/URLPattern/URLPattern.h index d039d32da19..27e6be7d9ab 100644 --- a/Libraries/LibWeb/URLPattern/URLPattern.h +++ b/Libraries/LibWeb/URLPattern/URLPattern.h @@ -24,6 +24,7 @@ class URLPattern : public Bindings::PlatformObject { GC_DECLARE_ALLOCATOR(URLPattern); public: + static WebIDL::ExceptionOr> create(JS::Realm&, URLPatternInput const&, Optional const& base_url, URLPatternOptions const& = {}); static WebIDL::ExceptionOr> construct_impl(JS::Realm&, URLPatternInput const&, String const& base_url, URLPatternOptions const& = {}); static WebIDL::ExceptionOr> construct_impl(JS::Realm&, URLPatternInput const&, URLPatternOptions const& = {}); @@ -45,7 +46,7 @@ public: protected: virtual void initialize(JS::Realm&) override; - explicit URLPattern(JS::Realm&); + explicit URLPattern(JS::Realm&, URL::Pattern::Pattern); private: // https://urlpattern.spec.whatwg.org/#ref-for-url-pattern%E2%91%A0