diff --git a/Libraries/LibURL/CMakeLists.txt b/Libraries/LibURL/CMakeLists.txt index f8d4be2063c..2a6168e2bbd 100644 --- a/Libraries/LibURL/CMakeLists.txt +++ b/Libraries/LibURL/CMakeLists.txt @@ -7,6 +7,7 @@ set(SOURCES Site.cpp URL.cpp ${PUBLIC_SUFFIX_SOURCES} + Pattern/Pattern.cpp ) serenity_lib(LibURL url) diff --git a/Libraries/LibURL/Pattern/Pattern.cpp b/Libraries/LibURL/Pattern/Pattern.cpp new file mode 100644 index 00000000000..9a940619abc --- /dev/null +++ b/Libraries/LibURL/Pattern/Pattern.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025, Shannon Booth + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace URL::Pattern { + +// https://urlpattern.spec.whatwg.org/#url-pattern-has-regexp-groups +bool Pattern::has_regexp_groups() const +{ + // 1. If urlPattern’s protocol component has regexp groups is true, then return true. + if (m_protocol_component.has_regexp_groups) + return true; + + // 2. If urlPattern’s username component has regexp groups is true, then return true. + if (m_username_component.has_regexp_groups) + return true; + + // 3. If urlPattern’s password component has regexp groups is true, then return true. + if (m_password_component.has_regexp_groups) + return true; + + // 4. If urlPattern’s hostname component has regexp groups is true, then return true. + if (m_hostname_component.has_regexp_groups) + return true; + + // 5. If urlPattern’s port component has regexp groups is true, then return true. + if (m_port_component.has_regexp_groups) + return true; + + // 6. If urlPattern’s pathname component has regexp groups is true, then return true. + if (m_pathname_component.has_regexp_groups) + return true; + + // 7. If urlPattern’s search component has regexp groups is true, then return true. + if (m_search_component.has_regexp_groups) + return true; + + // 8. If urlPattern’s hash component has regexp groups is true, then return true. + if (m_hash_component.has_regexp_groups) + return true; + + // 9. Return false. + return false; +} + +} diff --git a/Libraries/LibURL/Pattern/Pattern.h b/Libraries/LibURL/Pattern/Pattern.h index 8b367849856..ab13d0f9414 100644 --- a/Libraries/LibURL/Pattern/Pattern.h +++ b/Libraries/LibURL/Pattern/Pattern.h @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace URL::Pattern { @@ -42,4 +43,52 @@ struct Result { ComponentResult hash; }; +// https://urlpattern.spec.whatwg.org/#url-pattern +class Pattern { +public: + bool has_regexp_groups() const; + + Component const& protocol_component() const { return m_protocol_component; } + Component const& username_component() const { return m_username_component; } + Component const& password_component() const { return m_password_component; } + Component const& hostname_component() const { return m_hostname_component; } + Component const& port_component() const { return m_port_component; } + Component const& pathname_component() const { return m_pathname_component; } + Component const& search_component() const { return m_search_component; } + Component const& hash_component() const { return m_hash_component; } + +private: + // https://urlpattern.spec.whatwg.org/#url-pattern-protocol-component + // protocol component, a component + Component m_protocol_component; + + // https://urlpattern.spec.whatwg.org/#url-pattern-username-component + // username component, a component + Component m_username_component; + + // https://urlpattern.spec.whatwg.org/#url-pattern-password-component + // password component, a component + Component m_password_component; + + // https://urlpattern.spec.whatwg.org/#url-pattern-hostname-component + // hostname component, a component + Component m_hostname_component; + + // https://urlpattern.spec.whatwg.org/#url-pattern-port-component + // port component, a component + Component m_port_component; + + // https://urlpattern.spec.whatwg.org/#url-pattern-pathname-component + // pathname component, a component + Component m_pathname_component; + + // https://urlpattern.spec.whatwg.org/#url-pattern-search-component + // search component, a component + Component m_search_component; + + // https://urlpattern.spec.whatwg.org/#url-pattern-hash-component + // hash component, a component + Component m_hash_component; +}; + }