Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hasRegExpGroups #43

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 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
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
Loading