aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/location.rs
blob: 6c4ba3d8c7a8933185f162912ae75b17371507fa (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
/* 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::global::GlobalRef;
use dom::bindings::js::{JS, JSRef, Temporary};
use dom::bindings::str::USVString;
use dom::bindings::utils::{Reflector, reflect_dom_object};
use dom::urlhelper::UrlHelper;
use dom::window::Window;
use dom::window::WindowHelpers;

use util::str::DOMString;
use url::Url;

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

impl Location {
    fn new_inherited(window: JSRef<Window>) -> Location {
        Location {
            reflector_: Reflector::new(),
            window: JS::from_rooted(window)
        }
    }

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

impl<'a> LocationMethods for JSRef<'a, Location> {
    // https://html.spec.whatwg.org/multipage/browsers.html#dom-location-assign
    fn Assign(self, url: DOMString) {
        self.window.root().r().load_url(url);
    }

    fn Href(self) -> USVString {
        UrlHelper::Href(&self.get_url())
    }

    fn Pathname(self) -> USVString {
        UrlHelper::Pathname(&self.get_url())
    }

    fn Stringify(self) -> DOMString {
        self.Href().0
    }

    fn Search(self) -> USVString {
        UrlHelper::Search(&self.get_url())
    }

    fn Hash(self) -> USVString {
        UrlHelper::Hash(&self.get_url())
    }
}

trait PrivateLocationHelpers {
    fn get_url(self) -> Url;
}

impl<'a> PrivateLocationHelpers for JSRef<'a, Location> {
    fn get_url(self) -> Url {
        let window = self.window.root();
        window.r().get_url()
    }
}