Make relative hrefs work

This commit is contained in:
Brian Anderson 2012-07-27 18:04:09 -07:00
parent 633c013567
commit c37528df7d
3 changed files with 59 additions and 5 deletions

View file

@ -131,7 +131,7 @@ class Content<S:Sink send copy> {
// Note: we can parse the next document in parallel // Note: we can parse the next document in parallel
// with any previous documents. // with any previous documents.
let stream = spawn_html_lexer_task(copy url, self.resource_task); let stream = spawn_html_lexer_task(copy url, self.resource_task);
let (root, style_port, js_port) = build_dom(self.scope, stream); let (root, style_port, js_port) = build_dom(self.scope, stream, url);
let css_rules = style_port.recv(); let css_rules = style_port.recv();
let js_scripts = js_port.recv(); let js_scripts = js_port.recv();

View file

@ -153,7 +153,7 @@ fn js_script_listener(to_parent : comm::chan<~[~[u8]]>, from_parent : comm::port
} }
#[warn(no_non_implicitly_copyable_typarams)] #[warn(no_non_implicitly_copyable_typarams)]
fn build_dom(scope: NodeScope, stream: comm::port<Token>) -> (Node, comm::port<Stylesheet>, comm::port<~[~[u8]]>) { fn build_dom(scope: NodeScope, stream: comm::port<Token>, url: url) -> (Node, comm::port<Stylesheet>, comm::port<~[~[u8]]>) {
// The current reference node. // The current reference node.
let mut cur_node = scope.new_node(Element(ElementData(~"html", ~HTMLDivElement))); let mut cur_node = scope.new_node(Element(ElementData(~"html", ~HTMLDivElement)));
// We will spawn a separate task to parse any css that is // We will spawn a separate task to parse any css that is
@ -204,8 +204,8 @@ fn build_dom(scope: NodeScope, stream: comm::port<Token>) -> (Node, comm::port<S
some(filename) { some(filename) {
#debug["Linking to a css sheet named: %s", filename]; #debug["Linking to a css sheet named: %s", filename];
// FIXME: Need to base the new url on the current url // FIXME: Need to base the new url on the current url
let url = make_url(filename, none); let new_url = make_url(filename, some(url));
style_chan.send(File(url)); style_chan.send(File(new_url));
} }
none { /* fall through*/ } none { /* fall through*/ }
} }

View file

@ -21,7 +21,17 @@ fn make_url(str_url: ~str, current_url: option<url>) -> url {
// and build an absolute path with the cwd // and build an absolute path with the cwd
~"file://" + path::connect(os::getcwd(), str_url) ~"file://" + path::connect(os::getcwd(), str_url)
} else { } else {
fail;//current_url.get().scheme + "://" + str_url let current_url = current_url.get();
#debug("make_url: current_url: %?", current_url);
if current_url.path.is_empty() || current_url.path.ends_with("/") {
current_url.scheme + "://" + path::connect(current_url.host, str_url)
} else {
let path = path::split(current_url.path);
let path = path.init();
let path = path::connect_many(path + ~[str_url]);
current_url.scheme + "://" + path::connect(current_url.host, path)
}
} }
}; };
@ -37,3 +47,47 @@ fn should_create_absolute_file_url_if_current_url_is_none_and_str_url_looks_file
assert url.scheme == ~"file"; assert url.scheme == ~"file";
assert url.path.contains(os::getcwd()); assert url.path.contains(os::getcwd());
} }
#[test]
fn should_create_url_based_on_old_url_1() {
let old_str = ~"http://example.com";
let old_url = make_url(old_str, none);
let new_str = ~"index.html";
let new_url = make_url(new_str, some(old_url));
assert new_url.scheme == ~"http";
assert new_url.host == ~"example.com";
assert new_url.path == ~"/index.html";
}
#[test]
fn should_create_url_based_on_old_url_2() {
let old_str = ~"http://example.com/";
let old_url = make_url(old_str, none);
let new_str = ~"index.html";
let new_url = make_url(new_str, some(old_url));
assert new_url.scheme == ~"http";
assert new_url.host == ~"example.com";
assert new_url.path == ~"/index.html";
}
#[test]
fn should_create_url_based_on_old_url_3() {
let old_str = ~"http://example.com/index.html";
let old_url = make_url(old_str, none);
let new_str = ~"crumpet.html";
let new_url = make_url(new_str, some(old_url));
assert new_url.scheme == ~"http";
assert new_url.host == ~"example.com";
assert new_url.path == ~"/crumpet.html";
}
#[test]
fn should_create_url_based_on_old_url_4() {
let old_str = ~"http://example.com/snarf/index.html";
let old_url = make_url(old_str, none);
let new_str = ~"crumpet.html";
let new_url = make_url(new_str, some(old_url));
assert new_url.scheme == ~"http";
assert new_url.host == ~"example.com";
assert new_url.path == ~"/snarf/crumpet.html";
}