1 use webcore::value::Reference;
2 use webapi::dom_exception::SecurityError;
3 
4 /// The `Location` interface represents the location (URL) of the object it
5 /// is linked to. Changes done on it are reflected on the object it relates
6 /// to. Both the [Document](struct.Document.html) and [Window](struct.Window.html)
7 /// interface have such a linked `Location`, accessible via [Document::location](struct.Document.html#method.location)
8 /// and [Window::location](struct.Window.html#method.location) respectively.
9 ///
10 /// Note that all `Location` methods can return a `SecurityError` if the `Location` object's
11 /// relevant `Document`'s origin is not same origin-domain with the entry settings object's origin.
12 /// See: https://html.spec.whatwg.org/#dom-location-href
13 ///
14 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Location)
15 // https://html.spec.whatwg.org/#location
16 #[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
17 #[reference(instance_of = "Location")]
18 pub struct Location( Reference );
19 
20 impl Location {
21     /// The entire URL.
22     ///
23     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Location/href)
24     // https://html.spec.whatwg.org/#the-location-interface:dom-location-href
href( &self ) -> Result< String, SecurityError >25     pub fn href( &self ) -> Result< String, SecurityError > {
26         js_try!( return @{self}.href; ).unwrap()
27     }
28 
29     /// Returns a `String` containing the Unicode serialization of the origin of the represented
30     /// URL, that is:
31     ///
32     /// - For URL using the http or https, the scheme followed by `'://'`, followed by the domain,
33     ///   followed by `':'`, followed by the port (the default port, 80 and 443 respectively, if
34     ///   explicitely specified);
35     /// - For URL using `file: scheme`, the value is browser dependant.
36     /// - For URL using the blob: scheme, the origin of the URL following blob:. E.g
37     ///   "blob:https://mozilla.org" will have "https://mozilla.org".
38     ///
39     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Location/origin)
40     // https://html.spec.whatwg.org/#the-location-interface:dom-location-origin
origin( &self ) -> Result< String, SecurityError >41     pub fn origin( &self ) -> Result< String, SecurityError > {
42         js_try!( return @{self}.origin; ).unwrap()
43     }
44 
45     /// Returns a `String` representing the protocol scheme of the URL, including the final ':'.
46     ///
47     /// Example: `http:`
48     ///
49     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Location/protocol)
50     // https://html.spec.whatwg.org/#the-location-interface:dom-location-protocol
protocol( &self ) -> Result< String, SecurityError >51     pub fn protocol( &self ) -> Result< String, SecurityError > {
52         js_try!( return @{self}.protocol; ).unwrap()
53     }
54 
55     /// Returns a `String` containing the host (i.e. hostname) and then, if the port of the
56     /// URL is nonempty, a ':', and the port of the URL.
57     ///
58     /// Example: `hitchhikers.com:4242`
59     ///
60     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Location/host)
61     // https://html.spec.whatwg.org/#the-location-interface:dom-location-host
host( &self ) -> Result< String, SecurityError >62     pub fn host( &self ) -> Result< String, SecurityError > {
63         js_try!( return @{self}.host; ).unwrap()
64     }
65 
66     /// Returns a `String` which is the domain of the URL
67     ///
68     /// Example: `mozilla.com`
69     ///
70     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Location/hostname)
71     // https://html.spec.whatwg.org/#the-location-interface:dom-location-hostname
hostname( &self ) -> Result< String, SecurityError >72     pub fn hostname( &self ) -> Result< String, SecurityError > {
73         js_try!( return @{self}.hostname; ).unwrap()
74     }
75 
76     /// Returns a `String` containing the port number or `""` if there is no port.
77     ///
78     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Location/port)
79     // https://html.spec.whatwg.org/#the-location-interface:dom-location-port
port( &self ) -> Result< String, SecurityError >80     pub fn port( &self ) -> Result< String, SecurityError > {
81         js_try!( return @{self}.port; ).unwrap()
82     }
83 
84     /// Returns a `String` containing an initial '/' followed by the path of the URL.
85     ///
86     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Location/pathname)
87     // https://html.spec.whatwg.org/#the-location-interface:dom-location-pathname
pathname( &self ) -> Result< String, SecurityError >88     pub fn pathname( &self ) -> Result< String, SecurityError > {
89         js_try!( return @{self}.pathname; ).unwrap()
90     }
91 
92     /// Returns a `String` which is a search string, also called a query string, that is a `String`
93     /// containing a '?' followed by the parameters of the URL.
94     ///
95     /// These can then be further parsed via another library.
96     ///
97     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Location/search)
98     // https://html.spec.whatwg.org/#the-location-interface:dom-location-search
search( &self ) -> Result< String, SecurityError >99     pub fn search( &self ) -> Result< String, SecurityError > {
100         js_try!( return @{self}.search; ).unwrap()
101     }
102 
103     /// Returns a `String` containing a '#' followed by the fragment
104     /// identifier of the URL. The fragment is not percent-decoded.
105     ///
106     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Location/hash)
107     // https://html.spec.whatwg.org/#the-location-interface:dom-location-hash
hash( &self ) -> Result< String, SecurityError >108     pub fn hash( &self ) -> Result< String, SecurityError > {
109         js_try!( return @{self}.hash; ).unwrap()
110     }
111 }
112