aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/location.rs
blob: c366139649b9d62aeb858eceb4b7be5bc356e115 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::LocationBinding;
use dom::bindings::codegen::Bindings::LocationBinding::LocationMethods;
use dom::bindings::error::ErrorResult;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, Root};
use dom::bindings::str::USVString;
use dom::bindings::utils::{Reflector, reflect_dom_object};
use dom::urlhelper::UrlHelper;
use dom::window::Window;
use url::{Url, UrlParser};
use util::str::DOMString;

#[dom_struct]
pub struct Location {
    reflector_: Reflector,
    window: JS<Window>,
}

impl Location {
    fn new_inherited(window: &Window) -> Location {
        Location {
            reflector_: Reflector::new(),
            window: JS::from_ref(window)
        }
    }

    pub fn new(window: &Window) -> Root<Location> {
        reflect_dom_object(box Location::new_inherited(window),
                           GlobalRef::Window(window),
                           LocationBinding::Wrap)
    }

    fn get_url(&self) -> Url {
        self.window.root().get_url()
    }

    fn set_url_component(&self, value: USVString,
                         setter: fn(&mut Url, USVString)) {
        let window = self.window.root();
        let mut url = window.get_url();
        setter(&mut url, value);
        window.load_url(url);
    }
}

impl LocationMethods for Location {
    // https://html.spec.whatwg.org/multipage/#dom-location-assign
    fn Assign(&self, url: DOMString) {
        let window = self.window.root();
        // TODO: per spec, we should use the _API base URL_ specified by the
        //       _entry settings object_.
        let base_url = window.get_url();
        if let Ok(url) = UrlParser::new().base_url(&base_url).parse(&url) {
            window.load_url(url);
        }
    }

    // https://html.spec.whatwg.org/multipage/#dom-location-reload
    fn Reload(&self) {
        self.window.root().load_url(self.get_url());
    }

    // https://url.spec.whatwg.org/#dom-urlutils-hash
    fn Hash(&self) -> USVString {
        UrlHelper::Hash(&self.get_url())
    }

    // https://url.spec.whatwg.org/#dom-urlutils-hash
    fn SetHash(&self, value: USVString) {
        self.set_url_component(value, UrlHelper::SetHash);
    }

    // https://url.spec.whatwg.org/#dom-urlutils-host
    fn Host(&self) -> USVString {
        UrlHelper::Host(&self.get_url())
    }

    // https://url.spec.whatwg.org/#dom-urlutils-host
    fn SetHost(&self, value: USVString) {
        self.set_url_component(value, UrlHelper::SetHost);
    }

    // https://url.spec.whatwg.org/#dom-urlutils-hostname
    fn Hostname(&self) -> USVString {
        UrlHelper::Hostname(&self.get_url())
    }

    // https://url.spec.whatwg.org/#dom-urlutils-hostname
    fn SetHostname(&self, value: USVString) {
        self.set_url_component(value, UrlHelper::SetHostname);
    }

    // https://url.spec.whatwg.org/#dom-urlutils-href
    fn Href(&self) -> USVString {
        UrlHelper::Href(&self.get_url())
    }

    // https://url.spec.whatwg.org/#dom-urlutils-href
    fn SetHref(&self, value: USVString) -> ErrorResult {
        let window = self.window.root();
        if let Ok(url) = UrlParser::new().base_url(&window.get_url()).parse(&value.0) {
            window.load_url(url);
        };
        Ok(())
    }

    // https://url.spec.whatwg.org/#dom-urlutils-password
    fn Password(&self) -> USVString {
        UrlHelper::Password(&self.get_url())
    }

    // https://url.spec.whatwg.org/#dom-urlutils-password
    fn SetPassword(&self, value: USVString) {
        self.set_url_component(value, UrlHelper::SetPassword);
    }

    // https://url.spec.whatwg.org/#dom-urlutils-pathname
    fn Pathname(&self) -> USVString {
        UrlHelper::Pathname(&self.get_url())
    }

    // https://url.spec.whatwg.org/#dom-urlutils-pathname
    fn SetPathname(&self, value: USVString) {
        self.set_url_component(value, UrlHelper::SetPathname);
    }

    // https://url.spec.whatwg.org/#dom-urlutils-port
    fn Port(&self) -> USVString {
        UrlHelper::Port(&self.get_url())
    }

    // https://url.spec.whatwg.org/#dom-urlutils-port
    fn SetPort(&self, value: USVString) {
        self.set_url_component(value, UrlHelper::SetPort);
    }

    // https://url.spec.whatwg.org/#dom-urlutils-protocol
    fn Protocol(&self) -> USVString {
        UrlHelper::Protocol(&self.get_url())
    }

    // https://url.spec.whatwg.org/#dom-urlutils-protocol
    fn SetProtocol(&self, value: USVString) {
        self.set_url_component(value, UrlHelper::SetProtocol);
    }

    // https://url.spec.whatwg.org/#URLUtils-stringification-behavior
    fn Stringifier(&self) -> DOMString {
        self.Href().0
    }

    // https://url.spec.whatwg.org/#dom-urlutils-search
    fn Search(&self) -> USVString {
        UrlHelper::Search(&self.get_url())
    }

    // https://url.spec.whatwg.org/#dom-urlutils-search
    fn SetSearch(&self, value: USVString) {
        self.set_url_component(value, UrlHelper::SetSearch);
    }

    // https://url.spec.whatwg.org/#dom-urlutils-username
    fn Username(&self) -> USVString {
        UrlHelper::Username(&self.get_url())
    }

    // https://url.spec.whatwg.org/#dom-urlutils-username
    fn SetUsername(&self, value: USVString) {
        self.set_url_component(value, UrlHelper::SetUsername);
    }
}