Skip to content

Commit

Permalink
feat: hasRegExpGroups (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Jul 26, 2024
1 parent 783c76b commit 8be8cf5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(crate) struct Component<R: RegExp> {
pub regexp: Result<R, Error>,
pub group_name_list: Vec<String>,
pub matcher: Matcher<R>,
pub has_regexp_group: bool,
}

impl<R: RegExp> Component<R> {
Expand Down Expand Up @@ -46,6 +47,9 @@ impl<R: RegExp> Component<R> {
regexp,
group_name_list: name_list,
matcher,
has_regexp_group: part_list
.iter()
.any(|part| part.kind == PartType::Regexp),
})
}

Expand Down
29 changes: 29 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,18 @@ impl<R: RegExp> UrlPattern<R> {
&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.
Expand Down Expand Up @@ -833,4 +845,21 @@ mod tests {
})
.unwrap();
}

#[test]
fn has_regexp_group() {
let pattern = <UrlPattern>::parse(UrlPatternInit {
pathname: Some("/:foo.".to_owned()),
..Default::default()
})
.unwrap();
assert!(!pattern.has_regexp_groups());

let pattern = <UrlPattern>::parse(UrlPatternInit {
pathname: Some("/(.*?)".to_owned()),
..Default::default()
})
.unwrap();
assert!(pattern.has_regexp_groups());
}
}
3 changes: 3 additions & 0 deletions src/quirks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)]
Expand Down Expand Up @@ -186,6 +188,7 @@ impl RegExp for EcmaRegexp {
pub fn parse_pattern(init: crate::UrlPatternInit) -> Result<UrlPattern, Error> {
let pattern = crate::UrlPattern::<EcmaRegexp>::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(),
Expand Down

0 comments on commit 8be8cf5

Please sign in to comment.