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
use crate::prelude::*;
use crate::utils::document;
use crate::utils::ResultExt;
use std::{fmt, io};
/// Representation of the location (URL) of the object it is linked to.
#[derive(Debug)]
pub struct Location {
inner: web_sys::Location,
}
impl Location {
/// Create a new instance of `Location` by reading `document.location`.
pub fn new() -> Self {
Self {
inner: document().location().unwrap_throw(),
}
}
/// Access the URL at the current location.
pub fn href(&self) -> String {
self.inner.href().unwrap_throw()
}
/// Loads the resource at the URL provided in parameter.
///
/// # Errors
///
/// An error may be returned if the url is malformed.
pub fn assign(&self, url: &str) -> io::Result<()> {
self.inner
.assign(url)
.err_kind(io::ErrorKind::InvalidInput)?;
Ok(())
}
/// Replaces the current resource with the one at the provided URL.
///
/// The difference from the `assign` method is that after using `replace` the
/// current page will not be saved in session `History`, meaning the user
/// won't be able to use the back button to navigate to it
///
/// # Errors
///
/// An error may be returned if the url is malformed.
pub fn replace(&self, url: &str) -> io::Result<()> {
self.inner
.replace(url)
.err_kind(io::ErrorKind::InvalidInput)?;
Ok(())
}
/// Reloads the current URL, like the Refresh button.
///
/// # Errors
///
/// An error may be returned if the origin of the script calling `reload`
/// differs from the origin of the page that owns the `Location` object.
pub fn reload(&self) -> io::Result<()> {
self.inner
.reload()
.err_kind(io::ErrorKind::PermissionDenied)?;
Ok(())
}
}
impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.href())
}
}
impl Default for Location {
fn default() -> Self {
Self::new()
}
}