فهرست منبع

Introduce Url::join

Anthony Ramine 10 سال پیش
والد
کامیت
0ad4d961b1
3فایلهای تغییر یافته به همراه19 افزوده شده و 3 حذف شده
  1. 1 1
      Cargo.toml
  2. 16 0
      src/lib.rs
  3. 2 2
      tests/tests.rs

+ 1 - 1
Cargo.toml

@@ -1,7 +1,7 @@
 [package]
 
 name = "url"
-version = "0.5.1"
+version = "0.5.2"
 authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
 
 description = "URL library for Rust, based on the WHATWG URL Standard"

+ 16 - 0
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<String> {
         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<Url> {
+        UrlParser::new().base_url(self).parse(input)
+    }
 }
 
 

+ 2 - 2
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);