Skip to content

Commit a1822f8

Browse files
committed
Fix scheme setter
> test result: FAILED. 650 passed; 63 failed; 0 ignored; 0 measured
1 parent 1a3e8f1 commit a1822f8

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,9 +1978,15 @@ impl Url {
19781978
pub fn set_scheme(&mut self, scheme: &str) -> Result<(), ()> {
19791979
let mut parser = Parser::for_setter(String::new());
19801980
let remaining = parser.parse_scheme(parser::Input::new(scheme))?;
1981-
if !remaining.is_empty()
1982-
|| (!self.has_host() && SchemeType::from(&parser.serialization).is_special())
1983-
{
1981+
let new_scheme_type = SchemeType::from(&parser.serialization);
1982+
let old_scheme_type = SchemeType::from(self.scheme());
1983+
// Switching from special scheme to non special scheme
1984+
// and switching from file to non file is not allowed
1985+
if old_scheme_type != new_scheme_type {
1986+
return Err(());
1987+
}
1988+
1989+
if !remaining.is_empty() || (!self.has_host() && new_scheme_type.is_special()) {
19841990
return Err(());
19851991
}
19861992
let old_scheme_end = self.scheme_end;
@@ -2004,6 +2010,13 @@ impl Url {
20042010

20052011
parser.serialization.push_str(self.slice(old_scheme_end..));
20062012
self.serialization = parser.serialization;
2013+
2014+
// Update the port so it can be removed
2015+
// If it is the scheme's default
2016+
// We don't mind it silently failing
2017+
// If there was no port in the first place
2018+
let _ = self.set_port(self.port());
2019+
20072020
Ok(())
20082021
}
20092022

src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl fmt::Display for SyntaxViolation {
156156
}
157157
}
158158

159-
#[derive(Copy, Clone)]
159+
#[derive(Copy, Clone, PartialEq)]
160160
pub enum SchemeType {
161161
File,
162162
SpecialNotFile,

0 commit comments

Comments
 (0)