Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Sep 2, 2021
1 parent 6d7fe84 commit 4be9fb0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
8 changes: 3 additions & 5 deletions src/canonicalize_and_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ pub fn canonicalize_port(
}
}

// Ref: https://wicg.github.io/urlpattern/#canonicalize-a-standard-pathname
pub fn canonicalize_standard_pathname(
value: &str,
) -> Result<String, ParseError> {
// Ref: https://wicg.github.io/urlpattern/#canonicalize-a-pathname
pub fn canonicalize_pathname(value: &str) -> Result<String, ParseError> {
let mut url = url::Url::parse("http://dummy.test").unwrap();
url.set_path(value);
Ok(url::quirks::pathname(&url).to_string())
Expand Down Expand Up @@ -168,7 +166,7 @@ pub fn process_pathname_init(
} else {
match protocol_value {
Some(protocol) if is_special_scheme(protocol) => {
canonicalize_standard_pathname(pathname_value)
canonicalize_pathname(pathname_value)
}
_ => canonicalize_cannot_be_a_base_url_pathname(pathname_value),
}
Expand Down
18 changes: 10 additions & 8 deletions src/constructor_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct ConstructorStringParser<'a> {
token_index: usize,
token_increment: usize,
group_depth: usize,
should_treat_as_standard_url: bool,
protocol_matches_special_scheme: bool,
state: ConstructorStringParserState,
}

Expand Down Expand Up @@ -194,16 +194,18 @@ impl<'a> ConstructorStringParser<'a> {
self.token_list[self.token_index].kind == TokenType::Close
}

// Ref: https://wicg.github.io/urlpattern/#compute-should-treat-as-a-standard-url
fn compute_should_treat_as_standard_url(&mut self) -> Result<(), ParseError> {
// Ref: https://wicg.github.io/urlpattern/#compute-protocol-matches-a-special-scheme-flag
fn compute_protocol_matches_special_scheme(
&mut self,
) -> Result<(), ParseError> {
let protocol_string = self.make_component_string();
let protocol_component = crate::component::Component::compile(
&protocol_string,
crate::canonicalize_and_process::canonicalize_protocol,
Default::default(),
)?;
if protocol_component.protocol_component_matches_special_scheme() {
self.should_treat_as_standard_url = true;
self.protocol_matches_special_scheme = true;
}
Ok(())
}
Expand Down Expand Up @@ -244,7 +246,7 @@ pub fn parse_constructor_string(
token_index: 0,
token_increment: 1,
group_depth: 0,
should_treat_as_standard_url: false,
protocol_matches_special_scheme: false,
state: ConstructorStringParserState::Init,
};

Expand Down Expand Up @@ -302,16 +304,16 @@ pub fn parse_constructor_string(
}
ConstructorStringParserState::Protocol => {
if parser.is_protocol_suffix() {
parser.compute_should_treat_as_standard_url()?;
if parser.should_treat_as_standard_url {
parser.compute_protocol_matches_special_scheme()?;
if parser.protocol_matches_special_scheme {
parser.result.pathname = Some(String::from("/"));
}
let mut next_state = ConstructorStringParserState::Pathname;
let mut skip = 1;
if parser.next_is_authority_slashes() {
next_state = ConstructorStringParserState::Authority;
skip = 3;
} else if parser.should_treat_as_standard_url {
} else if parser.protocol_matches_special_scheme {
next_state = ConstructorStringParserState::Authority;
}
parser.change_state(next_state, skip);
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ impl UrlPattern {
let pathname = if protocol.protocol_component_matches_special_scheme() {
Component::compile(
&processed_init.pathname.unwrap(),
canonicalize_and_process::canonicalize_standard_pathname,
parser::Options::standard_pathname(),
canonicalize_and_process::canonicalize_pathname,
parser::Options::pathname(),
)?
} else {
Component::compile(
Expand Down
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ impl Options {
}
}

// Ref: https://wicg.github.io/urlpattern/#standard-pathname-options
// Ref: https://wicg.github.io/urlpattern/#pathname-options
#[inline]
pub fn standard_pathname() -> Self {
pub fn pathname() -> Self {
Options {
delimiter_code_point: String::from("/"),
prefix_code_point: String::from("/"),
Expand Down

0 comments on commit 4be9fb0

Please sign in to comment.