From 8be8cf5c7c121baa82c3651ac357174a1a15c471 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Fri, 26 Jul 2024 03:25:58 -0700 Subject: [PATCH] feat: hasRegExpGroups (#43) --- src/component.rs | 4 ++++ src/lib.rs | 29 +++++++++++++++++++++++++++++ src/quirks.rs | 3 +++ 3 files changed, 36 insertions(+) diff --git a/src/component.rs b/src/component.rs index 5932b08..4a0c581 100644 --- a/src/component.rs +++ b/src/component.rs @@ -18,6 +18,7 @@ pub(crate) struct Component { pub regexp: Result, pub group_name_list: Vec, pub matcher: Matcher, + pub has_regexp_group: bool, } impl Component { @@ -46,6 +47,9 @@ impl Component { regexp, group_name_list: name_list, matcher, + has_regexp_group: part_list + .iter() + .any(|part| part.kind == PartType::Regexp), }) } diff --git a/src/lib.rs b/src/lib.rs index b28e3dd..a584aba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -378,6 +378,18 @@ impl UrlPattern { &self.hash.pattern_string } + /// Returns whether the URLPattern contains one or more groups which uses regular expression matching. + pub fn has_regexp_groups(&self) -> bool { + self.protocol.has_regexp_group + || self.username.has_regexp_group + || self.password.has_regexp_group + || self.hostname.has_regexp_group + || self.port.has_regexp_group + || self.pathname.has_regexp_group + || self.search.has_regexp_group + || self.hash.has_regexp_group + } + // Ref: https://wicg.github.io/urlpattern/#dom-urlpattern-test /// Test if a given [UrlPatternInput] (with optional base url), matches the /// pattern. @@ -833,4 +845,21 @@ mod tests { }) .unwrap(); } + + #[test] + fn has_regexp_group() { + let pattern = ::parse(UrlPatternInit { + pathname: Some("/:foo.".to_owned()), + ..Default::default() + }) + .unwrap(); + assert!(!pattern.has_regexp_groups()); + + let pattern = ::parse(UrlPatternInit { + pathname: Some("/(.*?)".to_owned()), + ..Default::default() + }) + .unwrap(); + assert!(pattern.has_regexp_groups()); + } } diff --git a/src/quirks.rs b/src/quirks.rs index fd92e1b..361f648 100644 --- a/src/quirks.rs +++ b/src/quirks.rs @@ -79,6 +79,7 @@ pub fn process_construct_pattern_input( Ok(init) } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] pub struct UrlPattern { pub protocol: UrlPatternComponent, pub username: UrlPatternComponent, @@ -88,6 +89,7 @@ pub struct UrlPattern { pub pathname: UrlPatternComponent, pub search: UrlPatternComponent, pub hash: UrlPatternComponent, + pub has_regexp_groups: bool, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -186,6 +188,7 @@ impl RegExp for EcmaRegexp { pub fn parse_pattern(init: crate::UrlPatternInit) -> Result { let pattern = crate::UrlPattern::::parse_internal(init, false)?; let urlpattern = UrlPattern { + has_regexp_groups: pattern.has_regexp_groups(), protocol: pattern.protocol.into(), username: pattern.username.into(), password: pattern.password.into(),