Procházet zdrojové kódy

Move webidl.rs to Servo.

Simon Sapin před 10 roky
rodič
revize
ef0a1b2a92
3 změnil soubory, kde provedl 93 přidání a 258 odebrání
  1. 67 8
      src/lib.rs
  2. 0 232
      src/webidl.rs
  3. 26 18
      tests/wpt.rs

+ 67 - 8
src/lib.rs

@@ -144,14 +144,12 @@ pub use origin::{Origin, OpaqueOrigin};
 pub use host::{Host, HostAndPort, SocketAddrs};
 pub use parser::ParseError;
 pub use slicing::Position;
-pub use webidl::WebIdl;
 
 mod encoding;
 mod host;
 mod origin;
 mod parser;
 mod slicing;
-mod webidl;
 
 pub mod percent_encoding;
 pub mod form_urlencoded;
@@ -822,14 +820,9 @@ impl Url {
     /// * This URL is cannot-be-a-base and the new scheme is one of
     ///   `http`, `https`, `ws`, `wss`, `ftp`, or `gopher`
     pub fn set_scheme(&mut self, scheme: &str) -> Result<(), ()> {
-        self.set_scheme_internal(scheme, false)
-    }
-
-    fn set_scheme_internal(&mut self, scheme: &str, allow_extra_input_after_colon: bool)
-                          -> Result<(), ()> {
         let mut parser = Parser::for_setter(String::new());
         let remaining = try!(parser.parse_scheme(scheme));
-        if (!remaining.is_empty() && !allow_extra_input_after_colon) ||
+        if !remaining.is_empty() ||
                 (!self.has_host() && SchemeType::from(&parser.serialization).is_special()) {
             return Err(())
         }
@@ -853,6 +846,72 @@ impl Url {
         Ok(())
     }
 
+    /// Setter for https://url.spec.whatwg.org/#dom-url-host
+    ///
+    /// Unless you need to be interoperable with web browsers,
+    /// use `set_host` and `set_port` instead.
+    pub fn quirky_set_host_and_port(&mut self, new_host: &str) -> Result<(), ()> {
+        if self.cannot_be_a_base() {
+            return Err(())
+        }
+        let host;
+        let opt_port;
+        {
+            let scheme = self.scheme();
+            let result = Parser::parse_host(new_host, SchemeType::from(scheme), |_| ());
+            match result {
+                Ok((h, remaining)) => {
+                    host = h;
+                    opt_port = if remaining.starts_with(':') {
+                        Parser::parse_port(remaining, |_| (), || parser::default_port(scheme))
+                        .ok().map(|(port, _remaining)| port)
+                    } else {
+                        None
+                    };
+                }
+                Err(_) => return Err(())
+            }
+        }
+        self.set_host_internal(host, opt_port);
+        Ok(())
+    }
+
+    /// Setter for https://url.spec.whatwg.org/#dom-url-hostname
+    ///
+    /// Unless you need to be interoperable with web browsers, use `set_host` instead.
+    pub fn quirky_set_host(&mut self, new_hostname: &str) -> Result<(), ()> {
+        if self.cannot_be_a_base() {
+            return Err(())
+        }
+        let result = Parser::parse_host(new_hostname, SchemeType::from(self.scheme()), |_| ());
+        if let Ok((host, _remaining)) = result {
+            self.set_host_internal(host, None);
+            Ok(())
+        } else {
+            Err(())
+        }
+    }
+
+    /// Setter for https://url.spec.whatwg.org/#dom-url-port
+    ///
+    /// Unless you need to be interoperable with web browsers, use `set_port` instead.
+    pub fn quirky_set_port(&mut self, new_port: &str) -> Result<(), ()> {
+        let result;
+        {
+            // has_host implies !cannot_be_a_base
+            let scheme = self.scheme();
+            if !self.has_host() || scheme == "file" {
+                return Err(())
+            }
+            result = Parser::parse_port(new_port, |_| (), || parser::default_port(scheme))
+        }
+        if let Ok((new_port, _remaining)) = result {
+            self.set_port_internal(new_port);
+            Ok(())
+        } else {
+            Err(())
+        }
+    }
     /// Convert a file name as `std::path::Path` into an URL in the `file` scheme.
     ///
     /// This returns `Err` if the given path is not absolute or,

+ 0 - 232
src/webidl.rs

@@ -1,232 +0,0 @@
-// Copyright 2016 Simon Sapin.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-use {Url, ParseError};
-use host::Host;
-use idna::domain_to_unicode;
-use parser::{Parser, SchemeType, default_port};
-
-/// https://url.spec.whatwg.org/#api
-pub struct WebIdl;
-
-impl WebIdl {
-    /// https://url.spec.whatwg.org/#dom-url-domaintoascii
-    pub fn domain_to_ascii(domain: &str) -> String {
-        match Host::parse(domain) {
-            Ok(Host::Domain(domain)) => domain,
-            _ => String::new(),
-        }
-    }
-
-    /// https://url.spec.whatwg.org/#dom-url-domaintounicode
-    pub fn domain_to_unicode(domain: &str) -> String {
-        match Host::parse(domain) {
-            Ok(Host::Domain(ref domain)) => {
-                let (unicode, _errors) = domain_to_unicode(domain);
-                unicode
-            }
-            _ => String::new(),
-        }
-    }
-
-    /// Getter for https://url.spec.whatwg.org/#dom-url-href
-    pub fn href(url: &Url) -> &str {
-        &url.serialization
-    }
-
-    /// Setter for https://url.spec.whatwg.org/#dom-url-href
-    pub fn set_href(url: &mut Url, value: &str) -> Result<(), ParseError> {
-        *url = try!(Url::parse(value));
-        Ok(())
-    }
-
-    /// Getter for https://url.spec.whatwg.org/#dom-url-origin
-    pub fn origin(url: &Url) -> String {
-        url.origin().unicode_serialization()
-    }
-
-    /// Getter for https://url.spec.whatwg.org/#dom-url-protocol
-    #[inline]
-    pub fn protocol(url: &Url) -> &str {
-        debug_assert!(url.byte_at(url.scheme_end) == b':');
-        url.slice(..url.scheme_end + 1)
-    }
-
-    /// Setter for https://url.spec.whatwg.org/#dom-url-protocol
-    pub fn set_protocol(url: &mut Url, new_protocol: &str) {
-        let _ = url.set_scheme_internal(new_protocol, true);
-    }
-
-    /// Getter for https://url.spec.whatwg.org/#dom-url-username
-    #[inline]
-    pub fn username(url: &Url) -> &str {
-        url.username()
-    }
-
-    /// Setter for https://url.spec.whatwg.org/#dom-url-username
-    pub fn set_username(url: &mut Url, new_username: &str) {
-        let _ = url.set_username(new_username);
-    }
-
-    /// Getter for https://url.spec.whatwg.org/#dom-url-password
-    #[inline]
-    pub fn password(url: &Url) -> &str {
-        url.password().unwrap_or("")
-    }
-
-    /// Setter for https://url.spec.whatwg.org/#dom-url-password
-    pub fn set_password(url: &mut Url, new_password: &str) {
-        let _ = url.set_password(if new_password.is_empty() { None } else { Some(new_password) });
-    }
-
-    /// Getter for https://url.spec.whatwg.org/#dom-url-host
-    #[inline]
-    pub fn host(url: &Url) -> &str {
-        let host = url.slice(url.host_start..url.path_start);
-        host
-    }
-
-    /// Setter for https://url.spec.whatwg.org/#dom-url-host
-    pub fn set_host(url: &mut Url, new_host: &str) {
-        if url.cannot_be_a_base() {
-            return
-        }
-        let host;
-        let opt_port;
-        {
-            let scheme = url.scheme();
-            let result = Parser::parse_host(new_host, SchemeType::from(scheme), |_| ());
-            match result {
-                Ok((h, remaining)) => {
-                    host = h;
-                    opt_port = if remaining.starts_with(':') {
-                        Parser::parse_port(remaining, |_| (), || default_port(scheme))
-                        .ok().map(|(port, _remaining)| port)
-                    } else {
-                        None
-                    };
-                }
-                Err(_) => return
-            }
-        }
-        url.set_host_internal(host, opt_port)
-    }
-
-    /// Getter for https://url.spec.whatwg.org/#dom-url-hostname
-    #[inline]
-    pub fn hostname(url: &Url) -> &str {
-        url.host_str().unwrap_or("")
-    }
-
-    /// Setter for https://url.spec.whatwg.org/#dom-url-hostname
-    pub fn set_hostname(url: &mut Url, new_hostname: &str) {
-        if url.cannot_be_a_base() {
-            return
-        }
-        let result = Parser::parse_host(new_hostname, SchemeType::from(url.scheme()), |_| ());
-        if let Ok((host, _remaining)) = result {
-            url.set_host_internal(host, None)
-        }
-    }
-
-    /// Getter for https://url.spec.whatwg.org/#dom-url-port
-    #[inline]
-    pub fn port(url: &Url) -> &str {
-        if url.port.is_some() {
-            debug_assert!(url.byte_at(url.host_end) == b':');
-            url.slice(url.host_end + 1..url.path_start)
-        } else {
-            ""
-        }
-    }
-
-    /// Setter for https://url.spec.whatwg.org/#dom-url-port
-    pub fn set_port(url: &mut Url, new_port: &str) {
-        let result;
-        {
-            // has_host implies !cannot_be_a_base
-            let scheme = url.scheme();
-            if !url.has_host() || scheme == "file" {
-                return
-            }
-            result = Parser::parse_port(new_port, |_| (), || default_port(scheme))
-        }
-        if let Ok((new_port, _remaining)) = result {
-            url.set_port_internal(new_port)
-        }
-    }
-
-    /// Getter for https://url.spec.whatwg.org/#dom-url-pathname
-    #[inline]
-    pub fn pathname(url: &Url) -> &str {
-         url.path()
-    }
-
-    /// Setter for https://url.spec.whatwg.org/#dom-url-pathname
-    pub fn set_pathname(url: &mut Url, new_pathname: &str) {
-        if !url.cannot_be_a_base() {
-            url.set_path(new_pathname)
-        }
-    }
-
-    /// Getter for https://url.spec.whatwg.org/#dom-url-search
-    pub fn search(url: &Url) -> &str {
-        match (url.query_start, url.fragment_start) {
-            (Some(query_start), None) if {
-                debug_assert!(url.byte_at(query_start) == b'?');
-                // If the query (after ?) is not empty
-                (query_start as usize) < url.serialization.len() - 1
-            } => url.slice(query_start..),
-
-            (Some(query_start), Some(fragment_start)) if {
-                debug_assert!(url.byte_at(query_start) == b'?');
-                // If the fragment (after ?) is not empty
-                query_start < fragment_start
-            } => url.slice(query_start..fragment_start),
-
-            _ => "",
-        }
-    }
-
-    /// Setter for https://url.spec.whatwg.org/#dom-url-search
-    pub fn set_search(url: &mut Url, new_search: &str) {
-        url.set_query(match new_search {
-            "" => None,
-            _ if new_search.starts_with('?') => Some(&new_search[1..]),
-            _ => Some(new_search),
-        })
-    }
-
-    /// Getter for https://url.spec.whatwg.org/#dom-url-searchparams
-    pub fn search_params(url: &Url) -> Vec<(String, String)> {
-        url.query_pairs().unwrap_or_else(Vec::new)
-    }
-
-    /// Getter for https://url.spec.whatwg.org/#dom-url-hash
-    pub fn hash(url: &Url) -> &str {
-        match url.fragment_start {
-            Some(start) if {
-                debug_assert!(url.byte_at(start) == b'#');
-                // If the fragment (after #) is not empty
-                (start as usize) < url.serialization.len() - 1
-            } => url.slice(start..),
-            _ => "",
-        }
-    }
-
-    /// Setter for https://url.spec.whatwg.org/#dom-url-hash
-    pub fn set_hash(url: &mut Url, new_hash: &str) {
-        if url.scheme() != "javascript" {
-            url.set_fragment(match new_hash {
-                "" => None,
-                _ if new_hash.starts_with('#') => Some(&new_hash[1..]),
-                _ => Some(new_hash),
-            })
-        }
-    }
-}

+ 26 - 18
tests/wpt.rs

@@ -13,7 +13,7 @@ extern crate test;
 extern crate url;
 
 use rustc_serialize::json::Json;
-use url::{Url, WebIdl};
+use url::{Url, Position};
 
 
 fn run_one(input: String, base: String, expected: Result<TestCase, ()>) {
@@ -28,30 +28,38 @@ fn run_one(input: String, base: String, expected: Result<TestCase, ()>) {
         (Ok(_), Err(())) => panic!("Expected a parse error for URL {:?}", input),
     };
 
-    macro_rules! assert_getter {
-        ($attribute: ident) => { assert_getter!($attribute, expected.$attribute) };
-        ($attribute: ident, $expected: expr) => {
+    macro_rules! assert_eq {
+        ($expected: expr, $got: expr) => {
             {
-                let a = WebIdl::$attribute(&url);
-                let b = $expected;
-                assert!(a == b, "{:?} != {:?} for URL {:?}", a, b, url);
+                let expected = $expected;
+                let got = $got;
+                assert!(expected == got, "{:?} != {} {:?} for URL {:?}",
+                        got, stringify!($expected), expected, url);
             }
         }
     }
 
-    assert_getter!(href);
+    assert_eq!(expected.href, url.as_str());
     if let Some(expected_origin) = expected.origin {
-        assert_getter!(origin, expected_origin);
+        assert_eq!(expected_origin, url.origin().unicode_serialization());
+    }
+    assert_eq!(expected.protocol, &url.as_str()[..url.scheme().len() + ":".len()]);
+    assert_eq!(expected.username, url.username());
+    assert_eq!(expected.password, url.password().unwrap_or(""));
+    assert_eq!(expected.host, &url[Position::BeforeHost..Position::AfterPort]);
+    assert_eq!(expected.hostname, url.host_str().unwrap_or(""));
+    assert_eq!(expected.port, &url[Position::BeforePort..Position::AfterPort]);
+    assert_eq!(expected.pathname, url.path());
+    assert_eq!(expected.search, trim(&url[Position::AfterPath..Position::AfterQuery]));
+    assert_eq!(expected.hash, trim(&url[Position::AfterQuery..]));
+}
+
+fn trim(s: &str) -> &str {
+    if s.len() == 1 {
+        ""
+    } else {
+        s
     }
-    assert_getter!(protocol);
-    assert_getter!(username);
-    assert_getter!(password);
-    assert_getter!(host);
-    assert_getter!(hostname);
-    assert_getter!(port);
-    assert_getter!(pathname);
-    assert_getter!(search);
-    assert_getter!(hash);
 }
 
 struct TestCase {