Skip to content
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

fix: parse_path failed under windows #1571

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Changes from 2 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
51 changes: 37 additions & 14 deletions crates/mako/src/ast/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,20 +331,33 @@ type Params = Vec<(String, String)>;
type Fragment = Option<String>;

pub fn parse_path(path: &str) -> Result<(PathName, Search, Params, Fragment)> {
let base = "http://a.com/";
let base_url = Url::parse(base)?;
let full_url = base_url.join(path)?;
let path = full_url.path().to_string();
let fragment = full_url.fragment().map(|s| s.to_string());
let search = full_url.query().unwrap_or("").to_string();
let query_vec = full_url
.query_pairs()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
// dir or filename may contains space or other special characters
// so we need to decode it, e.g. "a%20b" -> "a b"
let path = percent_decode_str(&path).decode_utf8()?;
Ok((path.to_string(), search, query_vec, fragment))
#[cfg(target_os = "windows")]
let path = {
let prefix = "\\\\?\\";
let path = path.trim_start_matches(prefix);
path.replace('\\', "/")
};
#[cfg(not(target_os = "windows"))]
let path = path.to_string();
if path.contains('?') {
let (path, search) = path.split_once('?').unwrap();
let base = "http://a.com/";
let base_url = Url::parse(base)?;
let full_url = base_url.join(format!("?{}", search).as_str())?;
let fragment = full_url.fragment().map(|s| s.to_string());
let search = full_url.query().unwrap_or("").to_string();
let query_vec = full_url
.query_pairs()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
// dir or filename may contains space or other special characters
// so we need to decode it, e.g. "a%20b" -> "a b"
let path = percent_decode_str(path).decode_utf8()?;
Ok((path.to_string(), search.to_string(), query_vec, fragment))
} else {
let path = percent_decode_str(&path).decode_utf8()?;
Ok((path.to_string(), "".to_string(), vec![], None))
}
}

#[cfg(test)]
Expand All @@ -371,4 +384,14 @@ mod tests {
);
assert_eq!(f.path(), Some("/root/d.js".to_string()));
}

#[test]
fn test_parse_path_support_windows() {
let path = "C:\\a\\b\\c?foo";
let (path, search, params, fragment) = parse_path(path).unwrap();
assert_eq!(path, "C:\\a\\b\\c");
assert_eq!(search, "foo");
assert_eq!(params, vec![("foo".to_string(), "".to_string())]);
assert_eq!(fragment, None);
}
}
Loading