Skip to content

fixed quirks::parse_pattern of WPT [{ "pathname": "/(\\m)" }] #64

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 12 additions & 2 deletions src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::parser::Options;
use crate::parser::Part;
use crate::parser::PartModifier;
use crate::parser::PartType;
use crate::parser::RegexSyntax;
use crate::parser::FULL_WILDCARD_REGEXP_VALUE;
use crate::regexp::RegExp;
use crate::tokenizer::is_valid_name_codepoint;
Expand Down Expand Up @@ -42,7 +43,15 @@ impl<R: RegExp> Component<R> {
let (regexp_string, name_list) =
generate_regular_expression_and_name_list(&part_list, &options);
let flags = if options.ignore_case { "ui" } else { "u" };
let regexp = R::parse(&regexp_string, flags).map_err(Error::RegExp);
let mut regexp =
R::parse(&regexp_string, flags, false).map_err(Error::RegExp);
if regexp.is_ok() && R::syntax() == RegexSyntax::EcmaScript {
for part in part_list.iter() {
if part.kind == PartType::Regexp {
regexp = R::parse(&regexp_string, flags, true).map_err(Error::RegExp);
}
}
}
let pattern_string = generate_pattern_string(&part_list, &options);
let matcher = generate_matcher::<R>(&part_list, &options, flags);
Ok(Component {
Expand Down Expand Up @@ -350,7 +359,8 @@ fn generate_matcher<R: RegExp>(
part_list => {
let (regexp_string, _) =
generate_regular_expression_and_name_list(part_list, options);
let regexp = R::parse(&regexp_string, flags).map_err(Error::RegExp);
let regexp =
R::parse(&regexp_string, flags, false).map_err(Error::RegExp);
InnerMatcher::RegExp { regexp }
}
};
Expand Down
17 changes: 13 additions & 4 deletions src/quirks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,20 @@ impl RegExp for EcmaRegexp {
RegexSyntax::EcmaScript
}

fn parse(pattern: &str, flags: &str) -> Result<Self, ()> {
Ok(EcmaRegexp(pattern.to_string(), flags.to_string()))
fn parse(pattern: &str, flags: &str, force_eval: bool) -> Result<Self, ()> {
if force_eval {
let regexp = regex::Regex::parse(pattern, flags, false);
match regexp {
Ok(r) => Ok(EcmaRegexp(r.to_string(), flags.to_string())),
_ => Err(()),
}
} else {
Ok(EcmaRegexp(pattern.to_string(), flags.to_string()))
}
}

fn matches<'a>(&self, text: &'a str) -> Option<Vec<Option<&'a str>>> {
let regexp = regex::Regex::parse(&self.0, &self.1).ok()?;
let regexp = regex::Regex::parse(&self.0, &self.1, false).ok()?;
regexp.matches(text)
}
}
Expand All @@ -191,7 +199,8 @@ pub fn parse_pattern(
options: UrlPatternOptions,
) -> Result<UrlPattern, Error> {
let pattern =
crate::UrlPattern::<EcmaRegexp>::parse_internal(init, false, options)?;
crate::UrlPattern::<EcmaRegexp>::parse_internal(init, true, options)?;

let urlpattern = UrlPattern {
has_regexp_groups: pattern.has_regexp_groups(),
protocol: pattern.protocol.into(),
Expand Down
4 changes: 2 additions & 2 deletions src/regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub trait RegExp: Sized {

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

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

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

Expand Down