diff options
author | Brian Anderson <banderson@mozilla.com> | 2012-07-27 18:04:09 -0700 |
---|---|---|
committer | Brian Anderson <banderson@mozilla.com> | 2012-07-27 18:04:09 -0700 |
commit | c37528df7de1c8a6a78cd47e36ebfc9548dd73f5 (patch) | |
tree | 5ebff79e924a978d96d5fece572248897905ef9f /src/servo | |
parent | 633c0135672ca161ce9feb6ad6f85145930419ee (diff) | |
download | servo-c37528df7de1c8a6a78cd47e36ebfc9548dd73f5.tar.gz servo-c37528df7de1c8a6a78cd47e36ebfc9548dd73f5.zip |
Make relative hrefs work
Diffstat (limited to 'src/servo')
-rw-r--r-- | src/servo/content.rs | 2 | ||||
-rw-r--r-- | src/servo/parser/html_builder.rs | 6 | ||||
-rw-r--r-- | src/servo/util/url.rs | 56 |
3 files changed, 59 insertions, 5 deletions
diff --git a/src/servo/content.rs b/src/servo/content.rs index f6c8f035094..04d019a4d84 100644 --- a/src/servo/content.rs +++ b/src/servo/content.rs @@ -131,7 +131,7 @@ class Content<S:Sink send copy> { // Note: we can parse the next document in parallel // with any previous documents. 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 js_scripts = js_port.recv(); diff --git a/src/servo/parser/html_builder.rs b/src/servo/parser/html_builder.rs index 3bdc8dbf526..ec80e5debc9 100644 --- a/src/servo/parser/html_builder.rs +++ b/src/servo/parser/html_builder.rs @@ -153,7 +153,7 @@ fn js_script_listener(to_parent : comm::chan<~[~[u8]]>, from_parent : comm::port } #[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. let mut cur_node = scope.new_node(Element(ElementData(~"html", ~HTMLDivElement))); // 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) { #debug["Linking to a css sheet named: %s", filename]; // FIXME: Need to base the new url on the current url - let url = make_url(filename, none); - style_chan.send(File(url)); + let new_url = make_url(filename, some(url)); + style_chan.send(File(new_url)); } none { /* fall through*/ } } diff --git a/src/servo/util/url.rs b/src/servo/util/url.rs index cbdbae33d98..f9148f9d296 100644 --- a/src/servo/util/url.rs +++ b/src/servo/util/url.rs @@ -21,7 +21,17 @@ fn make_url(str_url: ~str, current_url: option<url>) -> url { // and build an absolute path with the cwd ~"file://" + path::connect(os::getcwd(), str_url) } 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.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"; +} |