From 0ad4d961b12274cccd45d21883cc2385b33f3aab Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Thu, 10 Dec 2015 14:07:15 -0500 Subject: [PATCH] Introduce Url::join --- Cargo.toml | 2 +- src/lib.rs | 16 ++++++++++++++++ tests/tests.rs | 4 ++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a7d64ac5..d30d592b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "url" -version = "0.5.1" +version = "0.5.2" authors = [ "Simon Sapin " ] description = "URL library for Rust, based on the WHATWG URL Standard" diff --git a/src/lib.rs b/src/lib.rs index 9298e81c..122414ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,6 +116,14 @@ let css_url = UrlParser::new().base_url(&this_document).parse("../main.css").unw assert!(css_url.serialize() == "http://servo.github.io/rust-url/main.css".to_string()); ``` +For convenience, the `join` method on `Url` is also provided to achieve the same result: + +``` +use url::Url; + +let this_document = Url::parse("http://servo.github.io/rust-url/url/index.html").unwrap(); +let css_url = this_document.join("../main.css").unwrap(); +assert!(&*css_url.serialize() == "http://servo.github.io/rust-url/main.css") */ #![cfg_attr(feature="heap_size", feature(plugin, custom_derive))] @@ -855,6 +863,14 @@ impl Url { pub fn lossy_percent_decode_fragment(&self) -> Option { self.fragment.as_ref().map(|value| lossy_utf8_percent_decode(value.as_bytes())) } + + /// Join a path with a base URL. + /// + /// Corresponds to the basic URL parser where `self` is the given base URL. + #[inline] + pub fn join(&self, input: &str) -> ParseResult { + UrlParser::new().base_url(self).parse(input) + } } diff --git a/tests/tests.rs b/tests/tests.rs index bf4248ec..7a20824d 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -10,7 +10,7 @@ extern crate url; use std::char; use std::net::{Ipv4Addr, Ipv6Addr}; -use url::{UrlParser, Url, SchemeData, RelativeSchemeData, Host}; +use url::{Host, RelativeSchemeData, SchemeData, Url}; #[test] @@ -33,7 +33,7 @@ fn url_parsing() { Ok(base) => base, Err(message) => panic!("Error parsing base {}: {}", base, message) }; - let url = UrlParser::new().base_url(&base).parse(&input); + let url = base.join(&input); if expected_scheme.is_none() { if url.is_ok() && !expected_failure { panic!("Expected a parse error for URL {}", input);