diff --git a/Libraries/LibWeb/URLPattern/URLPattern.cpp b/Libraries/LibWeb/URLPattern/URLPattern.cpp index bafd9c8efb1..42a70ffbecb 100644 --- a/Libraries/LibWeb/URLPattern/URLPattern.cpp +++ b/Libraries/LibWeb/URLPattern/URLPattern.cpp @@ -51,11 +51,31 @@ WebIDL::ExceptionOr> URLPattern::create(JS::Realm& realm, UR return realm.create(realm, pattern_or_error.release_value()); } -// https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec -Optional URLPattern::exec(URLPatternInput const&, Optional const&) const +// https://urlpattern.spec.whatwg.org/#dom-urlpattern-test +WebIDL::ExceptionOr URLPattern::test(URLPatternInput const& input, Optional const& base_url) const { - dbgln("FIXME: Implement URLPattern::match"); - return {}; + // 1. Let result be the result of match given this's associated URL pattern, input, and baseURL if given. + auto result_or_error = m_url_pattern.match(input, base_url); + if (result_or_error.is_error()) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, result_or_error.error().message }; + auto result = result_or_error.release_value(); + + // 2. If result is null, return false. + if (!result.has_value()) + return false; + + // 3. Return true. + return true; +} + +// https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec +WebIDL::ExceptionOr> URLPattern::exec(URLPatternInput const& input, Optional const& base_url) const +{ + // 1. Return the result of match given this's associated URL pattern, input, and baseURL if given. + auto result_or_error = m_url_pattern.match(input, base_url); + if (result_or_error.is_error()) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, result_or_error.error().message }; + return result_or_error.release_value(); } // https://urlpattern.spec.whatwg.org/#dom-urlpattern-protocol diff --git a/Libraries/LibWeb/URLPattern/URLPattern.h b/Libraries/LibWeb/URLPattern/URLPattern.h index 27e6be7d9ab..87821c18e4b 100644 --- a/Libraries/LibWeb/URLPattern/URLPattern.h +++ b/Libraries/LibWeb/URLPattern/URLPattern.h @@ -28,7 +28,8 @@ public: 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& = {}); - Optional exec(URLPatternInput const&, Optional const&) const; + WebIDL::ExceptionOr test(URLPatternInput const&, Optional const& base_url) const; + WebIDL::ExceptionOr> exec(URLPatternInput const&, Optional const& base_url) const; String const& protocol() const; String const& username() const; diff --git a/Libraries/LibWeb/URLPattern/URLPattern.idl b/Libraries/LibWeb/URLPattern/URLPattern.idl index c06f29d0d24..bcba877dcd5 100644 --- a/Libraries/LibWeb/URLPattern/URLPattern.idl +++ b/Libraries/LibWeb/URLPattern/URLPattern.idl @@ -6,7 +6,7 @@ interface URLPattern { constructor(URLPatternInput input, USVString baseURL, optional URLPatternOptions options = {}); constructor(optional URLPatternInput input = {}, optional URLPatternOptions options = {}); - [FIXME] boolean test(optional URLPatternInput input = {}, optional USVString baseURL); + boolean test(optional URLPatternInput input = {}, optional USVString baseURL); URLPatternResult? exec(optional URLPatternInput input = {}, optional USVString baseURL);