mirror of
https://github.com/servo/servo.git
synced 2025-08-04 21:20:23 +01:00
Add make_url with some custom logic
This commit is contained in:
parent
f48309739f
commit
1d7e37e299
2 changed files with 40 additions and 0 deletions
|
@ -101,6 +101,7 @@ mod util {
|
||||||
mod color;
|
mod color;
|
||||||
mod unsafe;
|
mod unsafe;
|
||||||
mod time;
|
mod time;
|
||||||
|
mod url;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[warn(no_non_implicitly_copyable_typarams)]
|
#[warn(no_non_implicitly_copyable_typarams)]
|
||||||
|
|
39
src/servo/util/url.rs
Normal file
39
src/servo/util/url.rs
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
export make_url;
|
||||||
|
|
||||||
|
import std::net::url;
|
||||||
|
import url::{get_scheme, url};
|
||||||
|
|
||||||
|
/**
|
||||||
|
Create a URL object from a string. Does various helpful browsery things like
|
||||||
|
|
||||||
|
* If there's no current url and the path looks like a file then it will
|
||||||
|
create a file url based of the current working directory
|
||||||
|
* If there's a current url and the new path is relative then the new url
|
||||||
|
is based off the current url
|
||||||
|
|
||||||
|
*/
|
||||||
|
fn make_url(str_url: ~str, current_url: option<url>) -> url {
|
||||||
|
let str_url = if get_scheme(str_url).is_some() {
|
||||||
|
str_url
|
||||||
|
} else {
|
||||||
|
if current_url.is_none() {
|
||||||
|
// If all we have is a filename, assume it's a local relative file
|
||||||
|
// and build an absolute path with the cwd
|
||||||
|
~"file://" + path::connect(os::getcwd(), str_url)
|
||||||
|
} else {
|
||||||
|
fail;//current_url.get().scheme + "://" + str_url
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// FIXME: Need to handle errors
|
||||||
|
url::from_str(str_url).get()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_create_absolute_file_url_if_current_url_is_none_and_str_url_looks_filey() {
|
||||||
|
let file = ~"local.html";
|
||||||
|
let url = make_url(file, none);
|
||||||
|
#debug("url: %?", url);
|
||||||
|
assert url.scheme == ~"file";
|
||||||
|
assert url.path.contains(os::getcwd());
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue