From 569ebeb6a475f1dd1d765a3b2ccc6330d712c356 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 1 Mar 2025 18:44:31 +1300 Subject: [PATCH] LibWeb/URLPattern: Implement IDL interface for URLPattern test and exec There is further work needed to complete the implementation of URL::Pattern::Pattern, but this implements the remaining URLPattern exec and test IDL interfaces, leaving all remaining work to LibURL. --- Libraries/LibWeb/URLPattern/URLPattern.cpp | 28 ++++++++++++++++++---- Libraries/LibWeb/URLPattern/URLPattern.h | 3 ++- Libraries/LibWeb/URLPattern/URLPattern.idl | 2 +- 3 files changed, 27 insertions(+), 6 deletions(-) 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);