Skip to content

Truly independent web browser

LibWeb: Implement URLPattern in Rust

Aliaksandr Kalenikdc63328e7639
diff --git a/Cargo.lock b/Cargo.lock
index 715726e6bcf..b3bc7f460b5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -977,6 +977,7 @@ dependencies = [
"encoding_rs",
"ladybird_allocator",
"libjs_rust",
+ "liburl_rust",
"libweb_rust",
"rustc-demangle",
]
diff --git a/Libraries/LibWeb/Bindings/Rust/Cargo.toml b/Libraries/LibWeb/Bindings/Rust/Cargo.toml
index da3a1d6e5df..1220e7dc07d 100644
--- a/Libraries/LibWeb/Bindings/Rust/Cargo.toml
+++ b/Libraries/LibWeb/Bindings/Rust/Cargo.toml
@@ -22,6 +22,7 @@ ak = { path = "../../../../AK/Rust" }
encoding_rs = "0.8.40"
ladybird_allocator = { path = "../../../RustAllocator", optional = true }
libjs_rust = { path = "../../../LibJS/Rust", default-features = false }
+liburl_rust = { path = "../../../LibURL/Rust", default-features = false }
libweb_rust = { path = "../../Rust" }
rustc-demangle = "0.1"
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
index ee0e83a665c..6ed773421c9 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
@@ -68,6 +68,18 @@ impl UsvString {
self.0
}
+ pub(crate) fn from_utf8(string: &str) -> Self {
+ Self(Utf16String::from_utf8(string))
+ }
+
+ pub(crate) fn to_utf8(&self) -> String {
+ match self.units() {
+ Utf16StringUnits::Ascii(bytes) => String::from_utf8(bytes.to_vec()),
+ Utf16StringUnits::Utf16(code_units) => String::from_utf16(code_units).map_err(|_| unreachable!()),
+ }
+ .expect("a USVString is well-formed")
+ }
+
/// The string of the code units, with U+FFFD in place of every surrogate that is not part of a pair.
pub(crate) fn from_utf16_replacing_lone_surrogates(code_units: &[u16]) -> Self {
Self(Utf16String::well_formed_from_utf16(code_units))
diff --git a/Libraries/LibWeb/Bindings/Rust/src/lib.rs b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
index 009153a4d0e..7235142c4e3 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/lib.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
@@ -38,6 +38,11 @@ pub(crate) mod encoding;
allow(dead_code, reason = "only the generated bindings use the interfaces")
)]
pub(crate) mod speech;
+#[cfg_attr(
+ not(web_idl_bindings),
+ allow(dead_code, reason = "only the generated bindings use the interfaces")
+)]
+pub(crate) mod url_pattern;
// NB: Only C++ calls into libweb_rust, so nothing in this crate names it. Linking it keeps its extern "C" functions in
// the static library.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/url_pattern/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/url_pattern/mod.rs
new file mode 100644
index 00000000000..e328577ee38
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/url_pattern/mod.rs
@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! The URL Pattern Standard, https://urlpattern.spec.whatwg.org/
+
+#[cfg(not(test))]
+#[expect(
+ clippy::module_inception,
+ reason = "the module of an interface has the name of its IDL file, which here is also that of its directory"
+)]
+pub(crate) mod url_pattern;
diff --git a/Libraries/LibWeb/Bindings/Rust/src/url_pattern/url_pattern.rs b/Libraries/LibWeb/Bindings/Rust/src/url_pattern/url_pattern.rs
new file mode 100644
index 00000000000..7ed3b6c43e2
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/url_pattern/url_pattern.rs
@@ -0,0 +1,286 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! URLPattern, https://urlpattern.spec.whatwg.org/#urlpattern
+
+use ak::Utf16String;
+use libjs_rust::api::Trace;
+use liburl_rust::pattern::{
+ ComponentResult, ErrorInfo, GroupMatch, IgnoreCase, Init, Input, MatchInput, Pattern, Result as MatchResult,
+};
+
+use crate::bindings::conversions::UsvString;
+use crate::bindings::{CallContext, Exception, WebIdlResult};
+
+/// The union URLPatternInput of URLPattern/URLPattern.idl, whose members the bindings convert.
+pub(crate) enum URLPatternInput {
+ USVString(UsvString),
+ URLPatternInit(URLPatternInit),
+}
+
+/// The dictionary URLPatternInit of URLPattern/URLPattern.idl, whose members the bindings convert.
+pub(crate) struct URLPatternInit {
+ pub(crate) base_url: Option<UsvString>,
+ pub(crate) hash: Option<UsvString>,
+ pub(crate) hostname: Option<UsvString>,
+ pub(crate) password: Option<UsvString>,
+ pub(crate) pathname: Option<UsvString>,
+ pub(crate) port: Option<UsvString>,
+ pub(crate) protocol: Option<UsvString>,
+ pub(crate) search: Option<UsvString>,
+ pub(crate) username: Option<UsvString>,
+}
+
+/// The dictionary URLPatternOptions of URLPattern/URLPattern.idl, whose members the bindings convert.
+pub(crate) struct URLPatternOptions {
+ pub(crate) ignore_case: bool,
+}
+
+/// The dictionary URLPatternResult of URLPattern/URLPattern.idl, whose members the bindings convert.
+pub(crate) struct URLPatternResult {
+ pub(crate) hash: Option<URLPatternComponentResult>,
+ pub(crate) hostname: Option<URLPatternComponentResult>,
+ pub(crate) inputs: Option<Vec<URLPatternInput>>,
+ pub(crate) password: Option<URLPatternComponentResult>,
+ pub(crate) pathname: Option<URLPatternComponentResult>,
+ pub(crate) port: Option<URLPatternComponentResult>,
+ pub(crate) protocol: Option<URLPatternComponentResult>,
+ pub(crate) search: Option<URLPatternComponentResult>,
+ pub(crate) username: Option<URLPatternComponentResult>,
+}
+
+/// The dictionary URLPatternComponentResult of URLPattern/URLPattern.idl, whose members the bindings convert.
+pub(crate) struct URLPatternComponentResult {
+ pub(crate) groups: Option<Vec<(UsvString, Option<UsvString>)>>,
+ pub(crate) input: Option<UsvString>,
+}
+
+fn optional_utf8(string: Option<UsvString>) -> Option<String> {
+ string.map(|string| string.to_utf8())
+}
+
+fn optional_usv_string(string: Option<String>) -> Option<UsvString> {
+ string.as_deref().map(UsvString::from_utf8)
+}
+
+impl From<URLPatternInit> for Init {
+ fn from(init: URLPatternInit) -> Self {
+ Self {
+ protocol: optional_utf8(init.protocol),
+ username: optional_utf8(init.username),
+ password: optional_utf8(init.password),
+ hostname: optional_utf8(init.hostname),
+ port: optional_utf8(init.port),
+ pathname: optional_utf8(init.pathname),
+ search: optional_utf8(init.search),
+ hash: optional_utf8(init.hash),
+ base_url: optional_utf8(init.base_url),
+ }
+ }
+}
+
+impl From<Input> for URLPatternInput {
+ fn from(input: Input) -> Self {
+ match input {
+ Input::String(string) => Self::USVString(UsvString::from_utf8(&string)),
+ Input::Init(init) => Self::URLPatternInit(URLPatternInit {
+ base_url: optional_usv_string(init.base_url),
+ hash: optional_usv_string(init.hash),
+ hostname: optional_usv_string(init.hostname),
+ password: optional_usv_string(init.password),
+ pathname: optional_usv_string(init.pathname),
+ port: optional_usv_string(init.port),
+ protocol: optional_usv_string(init.protocol),
+ search: optional_usv_string(init.search),
+ username: optional_usv_string(init.username),
+ }),
+ }
+ }
+}
+
+impl From<ComponentResult> for URLPatternComponentResult {
+ fn from(result: ComponentResult) -> Self {
+ let groups = result.groups.into_iter().map(|(name, group)| {
+ let group = match group {
+ GroupMatch::String(string) => Some(UsvString::from_utf8(&string)),
+ GroupMatch::Empty => None,
+ };
+ (UsvString::from_utf8(&name), group)
+ });
+ Self {
+ groups: Some(groups.collect()),
+ input: Some(UsvString::from_utf8(&result.input)),
+ }
+ }
+}
+
+impl From<MatchResult> for URLPatternResult {
+ fn from(result: MatchResult) -> Self {
+ Self {
+ hash: Some(result.hash.into()),
+ hostname: Some(result.hostname.into()),
+ inputs: Some(result.inputs.into_iter().map(Into::into).collect()),
+ password: Some(result.password.into()),
+ pathname: Some(result.pathname.into()),
+ port: Some(result.port.into()),
+ protocol: Some(result.protocol.into()),
+ search: Some(result.search.into()),
+ username: Some(result.username.into()),
+ }
+ }
+}
+
+/// Every error of a URL pattern is a TypeError.
+fn type_error(error: ErrorInfo) -> Exception {
+ Exception::TypeError(Utf16String::from_utf8(&error.message))
+}
+
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct URLPattern {
+ /// The associated URL pattern, https://urlpattern.spec.whatwg.org/#urlpattern-associated-url-pattern
+ #[gc(untraced)]
+ pattern: Pattern,
+}
+
+#[expect(
+ clippy::unnecessary_wraps,
+ reason = "the bindings call every method of an implementation for its WebIDL result, whether or not it can fail"
+)]
+impl URLPattern {
+ // https://urlpattern.spec.whatwg.org/#urlpattern-initialize
+ fn initialize(
+ input: URLPatternInput,
+ base_url: Option<UsvString>,
+ options: &URLPatternOptions,
+ ) -> WebIdlResult<Self> {
+ // 1. Set this's associated URL pattern to the result of create given input, baseURL, and options.
+ let input = match input {
+ URLPatternInput::USVString(string) => Input::String(string.to_utf8()),
+ URLPatternInput::URLPatternInit(init) => Input::Init(init.into()),
+ };
+ let ignore_case = if options.ignore_case {
+ IgnoreCase::Yes
+ } else {
+ IgnoreCase::No
+ };
+ let pattern = Pattern::create(&input, &optional_utf8(base_url), ignore_case).map_err(type_error)?;
+ Ok(Self { pattern })
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-urlpattern
+ pub(crate) fn construct_with_base_url(
+ _: &mut CallContext,
+ input: URLPatternInput,
+ base_url: UsvString,
+ options: URLPatternOptions,
+ ) -> WebIdlResult<Self> {
+ // 1. Run initialize given this, input, baseURL, and options.
+ Self::initialize(input, Some(base_url), &options)
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-urlpattern-input-options
+ pub(crate) fn construct(
+ _: &mut CallContext,
+ input: URLPatternInput,
+ options: URLPatternOptions,
+ ) -> WebIdlResult<Self> {
+ // 1. Run initialize given this, input, null, and options.
+ Self::initialize(input, None, &options)
+ }
+
+ // https://urlpattern.spec.whatwg.org/#url-pattern-match
+ fn match_(&self, input: URLPatternInput, base_url: Option<UsvString>) -> WebIdlResult<Option<MatchResult>> {
+ let input = match input {
+ URLPatternInput::USVString(string) => MatchInput::String(string.to_utf8()),
+ URLPatternInput::URLPatternInit(init) => MatchInput::Init(init.into()),
+ };
+ self.pattern
+ .r#match(&input, &optional_utf8(base_url))
+ .map_err(type_error)
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-test
+ pub(crate) fn test(
+ &self,
+ _: &mut CallContext,
+ input: URLPatternInput,
+ base_url: Option<UsvString>,
+ ) -> WebIdlResult<bool> {
+ // 1. Let result be the result of match given this's associated URL pattern, input, and baseURL if given.
+ let result = self.match_(input, base_url)?;
+
+ // 2. If result is null, return false.
+ // 3. Return true.
+ Ok(result.is_some())
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec
+ pub(crate) fn exec(
+ &self,
+ _: &mut CallContext,
+ input: URLPatternInput,
+ base_url: Option<UsvString>,
+ ) -> WebIdlResult<Option<URLPatternResult>> {
+ // 1. Return the result of match given this's associated URL pattern, input, and baseURL if given.
+ Ok(self.match_(input, base_url)?.map(Into::into))
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-protocol
+ pub(crate) fn protocol(&self, _: &mut CallContext) -> WebIdlResult<UsvString> {
+ // 1. Return this's associated URL pattern's protocol component's pattern string.
+ Ok(UsvString::from_utf8(&self.pattern.protocol_component().pattern_string))
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-username
+ pub(crate) fn username(&self, _: &mut CallContext) -> WebIdlResult<UsvString> {
+ // 1. Return this's associated URL pattern's username component's pattern string.
+ Ok(UsvString::from_utf8(&self.pattern.username_component().pattern_string))
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-password
+ pub(crate) fn password(&self, _: &mut CallContext) -> WebIdlResult<UsvString> {
+ // 1. Return this's associated URL pattern's password component's pattern string.
+ Ok(UsvString::from_utf8(&self.pattern.password_component().pattern_string))
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-hostname
+ pub(crate) fn hostname(&self, _: &mut CallContext) -> WebIdlResult<UsvString> {
+ // 1. Return this's associated URL pattern's hostname component's pattern string.
+ Ok(UsvString::from_utf8(&self.pattern.hostname_component().pattern_string))
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-port
+ pub(crate) fn port(&self, _: &mut CallContext) -> WebIdlResult<UsvString> {
+ // 1. Return this's associated URL pattern's port component's pattern string.
+ Ok(UsvString::from_utf8(&self.pattern.port_component().pattern_string))
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-pathname
+ pub(crate) fn pathname(&self, _: &mut CallContext) -> WebIdlResult<UsvString> {
+ // 1. Return this's associated URL pattern's pathname component's pattern string.
+ Ok(UsvString::from_utf8(&self.pattern.pathname_component().pattern_string))
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-search
+ pub(crate) fn search(&self, _: &mut CallContext) -> WebIdlResult<UsvString> {
+ // 1. Return this's associated URL pattern's search component's pattern string.
+ Ok(UsvString::from_utf8(&self.pattern.search_component().pattern_string))
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-hash
+ pub(crate) fn hash(&self, _: &mut CallContext) -> WebIdlResult<UsvString> {
+ // 1. Return this's associated URL pattern's hash component's pattern string.
+ Ok(UsvString::from_utf8(&self.pattern.hash_component().pattern_string))
+ }
+
+ // https://urlpattern.spec.whatwg.org/#dom-urlpattern-hasregexpgroups
+ pub(crate) fn has_reg_exp_groups(&self, _: &mut CallContext) -> WebIdlResult<bool> {
+ // 1. If this's associated URL pattern's has regexp groups, then return true.
+ // 2. Return false.
+ Ok(self.pattern.has_regexp_groups())
+ }
+}
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 715c859b4c5..e82c2aad40d 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -970,7 +970,6 @@ set(SOURCES
UIEvents/TextEvent.cpp
UIEvents/UIEvent.cpp
UIEvents/WheelEvent.cpp
- URLPattern/URLPattern.cpp
UserTiming/PerformanceMark.cpp
UserTiming/PerformanceMeasure.cpp
ViewTransition/ViewTransition.cpp
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index 577d21afc01..23034bbdc24 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -1194,12 +1194,6 @@ class UIEvent;
}
-namespace Web::URLPattern {
-
-class URLPattern;
-
-}
-
namespace Web::DOMURL {
class DOMURL;
diff --git a/Libraries/LibWeb/URLPattern/URLPattern.cpp b/Libraries/LibWeb/URLPattern/URLPattern.cpp
deleted file mode 100644
index 231d85d2524..00000000000
--- a/Libraries/LibWeb/URLPattern/URLPattern.cpp
+++ /dev/null
@@ -1,223 +0,0 @@
-/*
- * Copyright (c) 2025, Shannon Booth <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGC/Heap.h>
-#include <LibWeb/URLPattern/URLPattern.h>
-#include <LibWeb/WebIDL/ExceptionOr.h>
-
-namespace Web::URLPattern {
-
-GC_DEFINE_ALLOCATOR(URLPattern);
-
-static URL::RustIntegration::URLPattern::Init to_internal_url_pattern_init(Bindings::URLPatternInit const& init)
-{
- return {
- .protocol = init.protocol.map([](Utf16String const& value) { return value.to_utf8_but_should_be_ported_to_utf16(); }),
- .username = init.username.map([](Utf16String const& value) { return value.to_utf8_but_should_be_ported_to_utf16(); }),
- .password = init.password.map([](Utf16String const& value) { return value.to_utf8_but_should_be_ported_to_utf16(); }),
- .hostname = init.hostname.map([](Utf16String const& value) { return value.to_utf8_but_should_be_ported_to_utf16(); }),
- .port = init.port.map([](Utf16String const& value) { return value.to_utf8_but_should_be_ported_to_utf16(); }),
- .pathname = init.pathname.map([](Utf16String const& value) { return value.to_utf8_but_should_be_ported_to_utf16(); }),
- .search = init.search.map([](Utf16String const& value) { return value.to_utf8_but_should_be_ported_to_utf16(); }),
- .hash = init.hash.map([](Utf16String const& value) { return value.to_utf8_but_should_be_ported_to_utf16(); }),
- .base_url = init.base_url.map([](Utf16String const& value) { return value.to_utf8_but_should_be_ported_to_utf16(); }),
- };
-}
-
-static Optional<String> to_internal_optional_string(Optional<Utf16String> const& value)
-{
- return value.map([](Utf16String const& string) { return string.to_utf8_but_should_be_ported_to_utf16(); });
-}
-
-static URL::RustIntegration::URLPattern::Input to_internal_url_pattern_input(URLPatternInput const& input)
-{
- return input.visit(
- [](Utf16String const& input_string) -> URL::RustIntegration::URLPattern::Input { return input_string.to_utf8_but_should_be_ported_to_utf16(); },
- [](Bindings::URLPatternInit const& input_init) -> URL::RustIntegration::URLPattern::Input { return to_internal_url_pattern_init(input_init); });
-}
-
-static URL::FFI::IgnoreCase to_internal_ignore_case(Bindings::URLPatternOptions const& options)
-{
- return options.ignore_case ? URL::FFI::IgnoreCase::Yes : URL::FFI::IgnoreCase::No;
-}
-
-static Bindings::URLPatternInit to_bindings_url_pattern_init(URL::RustIntegration::URLPattern::Init const& init)
-{
- return {
- .base_url = init.base_url.map([](String const& value) { return Utf16String::from_utf8(value); }),
- .hash = init.hash.map([](String const& value) { return Utf16String::from_utf8(value); }),
- .hostname = init.hostname.map([](String const& value) { return Utf16String::from_utf8(value); }),
- .password = init.password.map([](String const& value) { return Utf16String::from_utf8(value); }),
- .pathname = init.pathname.map([](String const& value) { return Utf16String::from_utf8(value); }),
- .port = init.port.map([](String const& value) { return Utf16String::from_utf8(value); }),
- .protocol = init.protocol.map([](String const& value) { return Utf16String::from_utf8(value); }),
- .search = init.search.map([](String const& value) { return Utf16String::from_utf8(value); }),
- .username = init.username.map([](String const& value) { return Utf16String::from_utf8(value); }),
- };
-}
-
-static Bindings::URLPatternComponentResult to_bindings_url_pattern_component_result(URL::RustIntegration::URLPattern::Component::Result const& result)
-{
- OrderedHashMap<Utf16String, Variant<Utf16String, Empty, Empty>> groups;
- for (auto const& [key, value] : result.groups) {
- groups.set(Utf16String::from_utf8(key), value.visit([](String const& string_value) -> Variant<Utf16String, Empty, Empty> { return Utf16String::from_utf8(string_value); }, [](Empty) -> Variant<Utf16String, Empty, Empty> { return Empty {}; }));
- }
-
- return {
- .groups = Optional<OrderedHashMap<Utf16String, Variant<Utf16String, Empty, Empty>>> { move(groups) },
- .input = Optional<Utf16String> { Utf16String::from_utf8(result.input) },
- };
-}
-
-static Bindings::URLPatternResult to_bindings_url_pattern_result(URL::RustIntegration::URLPattern::Result const& result)
-{
- Vector<URLPatternInput> inputs;
- inputs.ensure_capacity(result.inputs.size());
- for (auto const& input : result.inputs) {
- inputs.unchecked_append(input.visit(
- [](String const& string_value) -> URLPatternInput { return Utf16String::from_utf8(string_value); },
- [](URL::RustIntegration::URLPattern::Init const& init_value) -> URLPatternInput { return to_bindings_url_pattern_init(init_value); }));
- }
-
- return {
- .hash = Optional<Bindings::URLPatternComponentResult> { to_bindings_url_pattern_component_result(result.hash) },
- .hostname = Optional<Bindings::URLPatternComponentResult> { to_bindings_url_pattern_component_result(result.hostname) },
- .inputs = Optional<Vector<URLPatternInput>> { move(inputs) },
- .password = Optional<Bindings::URLPatternComponentResult> { to_bindings_url_pattern_component_result(result.password) },
- .pathname = Optional<Bindings::URLPatternComponentResult> { to_bindings_url_pattern_component_result(result.pathname) },
- .port = Optional<Bindings::URLPatternComponentResult> { to_bindings_url_pattern_component_result(result.port) },
- .protocol = Optional<Bindings::URLPatternComponentResult> { to_bindings_url_pattern_component_result(result.protocol) },
- .search = Optional<Bindings::URLPatternComponentResult> { to_bindings_url_pattern_component_result(result.search) },
- .username = Optional<Bindings::URLPatternComponentResult> { to_bindings_url_pattern_component_result(result.username) },
- };
-}
-
-static WebIDL::ExceptionOr<GC::Ref<URLPattern>> create_url_pattern(URLPatternInput const& input, Optional<Utf16String> const& base_url, Bindings::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::RustIntegration::URLPattern::create(to_internal_url_pattern_input(input), to_internal_optional_string(base_url), to_internal_ignore_case(options));
- if (pattern_or_error.is_error())
- return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, Utf16String::from_utf8(pattern_or_error.error().message) };
- return GC::Heap::the().allocate<URLPattern>(pattern_or_error.release_value());
-}
-
-URLPattern::URLPattern(URL::RustIntegration::URLPattern pattern)
- : m_url_pattern(move(pattern))
-{
-}
-
-URLPattern::~URLPattern() = default;
-
-// https://urlpattern.spec.whatwg.org/#urlpattern-initialize
-WebIDL::ExceptionOr<GC::Ref<URLPattern>> URLPattern::create(URLPatternInput const& input, Utf16String const& base_url, Bindings::URLPatternOptions const& options)
-{
- return create_url_pattern(input, Optional<Utf16String> { base_url }, options);
-}
-
-// https://urlpattern.spec.whatwg.org/#urlpattern-initialize
-WebIDL::ExceptionOr<GC::Ref<URLPattern>> URLPattern::create(URLPatternInput const& input, Bindings::URLPatternOptions const& options)
-{
- return create_url_pattern(input, Optional<Utf16String> {}, options);
-}
-
-// https://urlpattern.spec.whatwg.org/#dom-urlpattern-test
-WebIDL::ExceptionOr<bool> URLPattern::test(URLPatternInput const& input, Optional<Utf16String> const& base_url) const
-{
- // 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(to_internal_url_pattern_input(input), to_internal_optional_string(base_url));
- if (result_or_error.is_error())
- return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, Utf16String::from_utf8(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<Optional<Bindings::URLPatternResult>> URLPattern::exec(URLPatternInput const& input, Optional<Utf16String> 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(to_internal_url_pattern_input(input), to_internal_optional_string(base_url));
- if (result_or_error.is_error())
- return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, Utf16String::from_utf8(result_or_error.error().message) };
- auto result = result_or_error.release_value();
- if (!result.has_value())
- return Optional<Bindings::URLPatternResult> {};
- return to_bindings_url_pattern_result(*result);
-}
-
-// https://urlpattern.spec.whatwg.org/#dom-urlpattern-protocol
-Utf16String URLPattern::protocol() const
-{
- // 1. Return this's associated URL pattern's protocol component's pattern string.
- return Utf16String::from_utf8(m_url_pattern.protocol_component().pattern_string);
-}
-
-// https://urlpattern.spec.whatwg.org/#dom-urlpattern-username
-Utf16String URLPattern::username() const
-{
- // 1. Return this's associated URL pattern's username component's pattern string.
- return Utf16String::from_utf8(m_url_pattern.username_component().pattern_string);
-}
-
-// https://urlpattern.spec.whatwg.org/#dom-urlpattern-password
-Utf16String URLPattern::password() const
-{
- // 1. Return this's associated URL pattern's password component's pattern string.
- return Utf16String::from_utf8(m_url_pattern.password_component().pattern_string);
-}
-
-// https://urlpattern.spec.whatwg.org/#dom-urlpattern-hostname
-Utf16String URLPattern::hostname() const
-{
- // 1. Return this's associated URL pattern's hostname component's pattern string.
- return Utf16String::from_utf8(m_url_pattern.hostname_component().pattern_string);
-}
-
-// https://urlpattern.spec.whatwg.org/#dom-urlpattern-port
-Utf16String URLPattern::port() const
-{
- // 1. Return this's associated URL pattern's port component's pattern string.
- return Utf16String::from_utf8(m_url_pattern.port_component().pattern_string);
-}
-
-// https://urlpattern.spec.whatwg.org/#dom-urlpattern-pathname
-Utf16String URLPattern::pathname() const
-{
- // 1. Return this's associated URL pattern's pathname component's pattern string.
- return Utf16String::from_utf8(m_url_pattern.pathname_component().pattern_string);
-}
-
-// https://urlpattern.spec.whatwg.org/#dom-urlpattern-search
-Utf16String URLPattern::search() const
-{
- // 1. Return this's associated URL pattern's search component's pattern string.
- return Utf16String::from_utf8(m_url_pattern.search_component().pattern_string);
-}
-
-// https://urlpattern.spec.whatwg.org/#dom-urlpattern-hash
-Utf16String URLPattern::hash() const
-{
- // 1. Return this's associated URL pattern's hash component's pattern string.
- return Utf16String::from_utf8(m_url_pattern.hash_component().pattern_string);
-}
-
-// https://urlpattern.spec.whatwg.org/#dom-urlpattern-hasregexpgroups
-bool URLPattern::has_reg_exp_groups() const
-{
- // 1. If this's associated URL pattern's has regexp groups, then return true.
- if (m_url_pattern.has_regexp_groups())
- return true;
-
- // 2. Return false.
- return false;
-}
-
-}
diff --git a/Libraries/LibWeb/URLPattern/URLPattern.h b/Libraries/LibWeb/URLPattern/URLPattern.h
deleted file mode 100644
index 60c3c211d7e..00000000000
--- a/Libraries/LibWeb/URLPattern/URLPattern.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright (c) 2025, Shannon Booth <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/String.h>
-#include <AK/Variant.h>
-#include <LibGC/Ptr.h>
-#include <LibURL/RustIntegration.h>
-#include <LibWeb/Bindings/URLPattern.h>
-#include <LibWeb/Bindings/Wrappable.h>
-#include <LibWeb/WebIDL/ExceptionOr.h>
-
-namespace Web::URLPattern {
-
-using URLPatternInput = Variant<Utf16String, Bindings::URLPatternInit>;
-
-// https://urlpattern.spec.whatwg.org/#urlpattern
-class URLPattern : public Bindings::GCAllocatedWrappable {
- WEB_WRAPPABLE(URLPattern, Bindings::GCAllocatedWrappable);
- GC_DECLARE_ALLOCATOR(URLPattern);
-
-public:
- static WebIDL::ExceptionOr<GC::Ref<URLPattern>> create(URLPatternInput const&, Utf16String const& base_url, Bindings::URLPatternOptions const&);
- static WebIDL::ExceptionOr<GC::Ref<URLPattern>> create(URLPatternInput const&, Bindings::URLPatternOptions const&);
-
- WebIDL::ExceptionOr<bool> test(URLPatternInput const&, Optional<Utf16String> const& base_url) const;
- WebIDL::ExceptionOr<Optional<Bindings::URLPatternResult>> exec(URLPatternInput const&, Optional<Utf16String> const& base_url) const;
-
- Utf16String protocol() const;
- Utf16String username() const;
- Utf16String password() const;
- Utf16String hostname() const;
- Utf16String port() const;
- Utf16String pathname() const;
- Utf16String search() const;
- Utf16String hash() const;
-
- bool has_reg_exp_groups() const;
-
- virtual ~URLPattern() override;
-
-protected:
- explicit URLPattern(URL::RustIntegration::URLPattern);
-
-private:
- // https://urlpattern.spec.whatwg.org/#ref-for-url-pattern%E2%91%A0
- // Each URLPattern has an associated URL pattern, a URL pattern.
- URL::RustIntegration::URLPattern m_url_pattern;
-};
-
-}
diff --git a/Libraries/LibWeb/URLPattern/URLPattern.idl b/Libraries/LibWeb/URLPattern/URLPattern.idl
index 4b13a8e4978..dec1000b994 100644
--- a/Libraries/LibWeb/URLPattern/URLPattern.idl
+++ b/Libraries/LibWeb/URLPattern/URLPattern.idl
@@ -1,10 +1,10 @@
typedef (USVString or URLPatternInit) URLPatternInput;
// https://urlpattern.spec.whatwg.org/#urlpattern
-[Exposed=(Window,Worker)]
+[Exposed=(Window,Worker), ImplementedIn=Rust]
interface URLPattern {
- constructor(URLPatternInput input, USVString baseURL, optional URLPatternOptions options = {});
- constructor(optional URLPatternInput input = {}, optional URLPatternOptions options = {});
+ [ImplementedAs=construct_with_base_url] constructor(URLPatternInput input, USVString baseURL, optional URLPatternOptions options = {});
+ [ImplementedAs=construct] constructor(optional URLPatternInput input = {}, optional URLPatternOptions options = {});
boolean test(optional URLPatternInput input = {}, optional USVString baseURL);