Skip to content

Commit 8bbce5a

Browse files
authored
Merge branch 'denoland:main' into main
2 parents 1e6b648 + 734ad04 commit 8bbce5a

File tree

5 files changed

+59
-13
lines changed

5 files changed

+59
-13
lines changed

src/canonicalize_and_process.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ pub fn canonicalize_hostname(value: &str) -> Result<String, Error> {
4646
return Ok(String::new());
4747
}
4848
let mut url = url::Url::parse("http://dummy.test").unwrap();
49-
url.set_host(Some(value)).map_err(Error::Url)?;
49+
url::quirks::set_hostname(&mut url, value)
50+
.map_err(|_| Error::Url(url::ParseError::InvalidDomainCharacter))?;
5051
Ok(url::quirks::hostname(&url).to_string())
5152
}
5253

src/component.rs

Lines changed: 12 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,15 @@ 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 regexp.is_ok() && R::syntax() == RegexSyntax::EcmaScript {
49+
for part in part_list.iter() {
50+
if part.kind == PartType::Regexp {
51+
regexp = R::parse(&regexp_string, flags, true).map_err(Error::RegExp);
52+
}
53+
}
54+
}
4655
let pattern_string = generate_pattern_string(&part_list, &options);
4756
let matcher = generate_matcher::<R>(&part_list, &options, flags);
4857
Ok(Component {
@@ -350,7 +359,8 @@ fn generate_matcher<R: RegExp>(
350359
part_list => {
351360
let (regexp_string, _) =
352361
generate_regular_expression_and_name_list(part_list, options);
353-
let regexp = R::parse(&regexp_string, flags).map_err(Error::RegExp);
362+
let regexp =
363+
R::parse(&regexp_string, flags, false).map_err(Error::RegExp);
354364
InnerMatcher::RegExp { regexp }
355365
}
356366
};

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

src/testdata/urlpatterntestdata.json

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,17 @@
18831883
{
18841884
"pattern": [ "https://{sub.}?example{.com/}foo" ],
18851885
"inputs": [ "https://example.com/foo" ],
1886-
"expected_obj": "error"
1886+
"exactly_empty_components": [ "port" ],
1887+
"expected_obj": {
1888+
"protocol": "https",
1889+
"hostname": "{sub.}?example.com",
1890+
"pathname": "*"
1891+
},
1892+
"expected_match": {
1893+
"protocol": { "input": "https", "groups": {} },
1894+
"hostname": { "input": "example.com", "groups": {} },
1895+
"pathname": { "input": "/foo", "groups": { "0": "/foo" } }
1896+
}
18871897
},
18881898
{
18891899
"pattern": [ "{https://}example.com/foo" ],
@@ -2441,15 +2451,27 @@
24412451
},
24422452
{
24432453
"pattern": [{ "hostname": "bad#hostname" }],
2444-
"expected_obj": "error"
2454+
"inputs": [{ "hostname": "bad" }],
2455+
"expected_obj": {
2456+
"hostname": "bad"
2457+
},
2458+
"expected_match": {
2459+
"hostname": { "input": "bad", "groups": {} }
2460+
}
24452461
},
24462462
{
24472463
"pattern": [{ "hostname": "bad%hostname" }],
24482464
"expected_obj": "error"
24492465
},
24502466
{
24512467
"pattern": [{ "hostname": "bad/hostname" }],
2452-
"expected_obj": "error"
2468+
"inputs": [{ "hostname": "bad" }],
2469+
"expected_obj": {
2470+
"hostname": "bad"
2471+
},
2472+
"expected_match": {
2473+
"hostname": { "input": "bad", "groups": {} }
2474+
}
24532475
},
24542476
{
24552477
"skip": "likely a bug in rust-url",
@@ -2482,7 +2504,11 @@
24822504
},
24832505
{
24842506
"pattern": [{ "hostname": "bad\\\\hostname" }],
2485-
"expected_obj": "error"
2507+
"inputs": [{ "hostname": "badhostname" }],
2508+
"expected_obj": {
2509+
"hostname": "bad"
2510+
},
2511+
"expected_match": null
24862512
},
24872513
{
24882514
"pattern": [{ "hostname": "bad^hostname" }],

0 commit comments

Comments
 (0)