Skip to content

Commit 00dccf7

Browse files
committed
fixed quirks::parse_pattern of [{ "pathname": "/(\m)" }]
1 parent e2c6c2c commit 00dccf7

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

src/component.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::parser::Options;
77
use crate::parser::Part;
88
use crate::parser::PartModifier;
99
use crate::parser::PartType;
10+
use crate::parser::RegexSyntax;
1011
use crate::parser::FULL_WILDCARD_REGEXP_VALUE;
1112
use crate::regexp::RegExp;
1213
use crate::tokenizer::is_valid_name_codepoint;
@@ -42,7 +43,18 @@ impl<R: RegExp> Component<R> {
4243
let (regexp_string, name_list) =
4344
generate_regular_expression_and_name_list(&part_list, &options);
4445
let flags = if options.ignore_case { "ui" } else { "u" };
45-
let regexp = R::parse(&regexp_string, flags).map_err(Error::RegExp);
46+
let mut regexp =
47+
R::parse(&regexp_string, flags, false).map_err(Error::RegExp);
48+
if let Ok(_) = &regexp {
49+
if R::syntax() == RegexSyntax::EcmaScript {
50+
for part in part_list.iter() {
51+
if part.kind == PartType::Regexp {
52+
regexp =
53+
R::parse(&regexp_string, flags, true).map_err(Error::RegExp);
54+
}
55+
}
56+
}
57+
}
4658
let pattern_string = generate_pattern_string(&part_list, &options);
4759
let matcher = generate_matcher::<R>(&part_list, &options, flags);
4860
Ok(Component {
@@ -350,7 +362,8 @@ fn generate_matcher<R: RegExp>(
350362
part_list => {
351363
let (regexp_string, _) =
352364
generate_regular_expression_and_name_list(part_list, options);
353-
let regexp = R::parse(&regexp_string, flags).map_err(Error::RegExp);
365+
let regexp =
366+
R::parse(&regexp_string, flags, false).map_err(Error::RegExp);
354367
InnerMatcher::RegExp { regexp }
355368
}
356369
};

src/quirks.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,20 @@ impl RegExp for EcmaRegexp {
175175
RegexSyntax::EcmaScript
176176
}
177177

178-
fn parse(pattern: &str, flags: &str) -> Result<Self, ()> {
179-
Ok(EcmaRegexp(pattern.to_string(), flags.to_string()))
178+
fn parse(pattern: &str, flags: &str, force_eval: bool) -> Result<Self, ()> {
179+
if force_eval {
180+
let regexp = regex::Regex::parse(pattern, flags, false);
181+
match regexp {
182+
Ok(r) => Ok(EcmaRegexp(r.to_string(), flags.to_string())),
183+
_ => Err(()),
184+
}
185+
} else {
186+
Ok(EcmaRegexp(pattern.to_string(), flags.to_string()))
187+
}
180188
}
181189

182190
fn matches<'a>(&self, text: &'a str) -> Option<Vec<Option<&'a str>>> {
183-
let regexp = regex::Regex::parse(&self.0, &self.1).ok()?;
191+
let regexp = regex::Regex::parse(&self.0, &self.1, false).ok()?;
184192
regexp.matches(text)
185193
}
186194
}
@@ -191,7 +199,8 @@ pub fn parse_pattern(
191199
options: UrlPatternOptions,
192200
) -> Result<UrlPattern, Error> {
193201
let pattern =
194-
crate::UrlPattern::<EcmaRegexp>::parse_internal(init, false, options)?;
202+
crate::UrlPattern::<EcmaRegexp>::parse_internal(init, true, options)?;
203+
195204
let urlpattern = UrlPattern {
196205
has_regexp_groups: pattern.has_regexp_groups(),
197206
protocol: pattern.protocol.into(),

src/regexp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub trait RegExp: Sized {
55

66
/// Generates a regexp pattern for the given string. If the pattern is
77
/// invalid, the parse function should return an error.
8-
fn parse(pattern: &str, flags: &str) -> Result<Self, ()>;
8+
fn parse(pattern: &str, flags: &str, force_eval: bool) -> Result<Self, ()>;
99

1010
/// Matches the given text against the regular expression and returns the list
1111
/// of captures. The matches are returned in the order they appear in the
@@ -22,7 +22,7 @@ impl RegExp for regex::Regex {
2222
RegexSyntax::Rust
2323
}
2424

25-
fn parse(pattern: &str, flags: &str) -> Result<Self, ()> {
25+
fn parse(pattern: &str, flags: &str, _force_eval: bool) -> Result<Self, ()> {
2626
regex::Regex::new(&format!("(?{flags}){pattern}")).map_err(|_| ())
2727
}
2828

0 commit comments

Comments
 (0)