wasm_toolkit/window_ops/location.rs
1use crate::{WasmDocument, WasmToolkitCommon, WasmToolkitError, WasmToolkitResult};
2
3use super::WasmWindow;
4
5impl WasmWindow {
6 pub fn document(&self) -> WasmToolkitResult<WasmDocument> {
7 self.inner()
8 .document()
9 .map(WasmDocument::new)
10 .ok_or(WasmToolkitError::DocumentNotFound)
11 }
12
13 pub fn location(&self) -> web_sys::Location {
14 self.inner().location()
15 }
16
17 /// The protocol property of the URL interface is a string containing the protocol
18 /// or scheme of the URL, including the final ":".
19 ///
20 /// This property can be set to change the protocol of the URL. A ":"
21 /// is appended to the provided string if not provided.
22 /// The provided scheme has to be compatible with the rest of the URL to be considered valid.
23 pub fn protocol(&self) -> WasmToolkitResult<String> {
24 self.location().protocol().map_err(|error| {
25 let outcome = WasmToolkitCommon::exception_or_stringify(&error);
26
27 WasmToolkitError::ProtocolNotFound(outcome)
28 })
29 }
30
31 /// The port property of the URL interface is a string containing the port number of the URL.
32 /// If the port is the default for the protocol
33 /// (80 for ws: and http:, 443 for wss: and https:, and 21 for ftp:),
34 /// this property contains an empty string, "".
35 ///
36 /// This property can be set to change the port of the URL.
37 /// If the URL has no host or its scheme is file:, then setting this property has no effect.
38 /// It also silently ignores invalid port numbers.
39 pub fn port(&self) -> WasmToolkitResult<String> {
40 self.location().port().map_err(|error| {
41 let outcome = WasmToolkitCommon::exception_or_stringify(&error);
42
43 WasmToolkitError::PortNotFound(outcome)
44 })
45 }
46
47 /// The hostname property of the URL interface is a string containing either
48 /// the domain name or IP address of the URL. If the URL does not have a hostname,
49 /// this property contains an empty string, "". IPv4 and IPv6 addresses are normalized,
50 /// such as stripping leading zeros, and domain names are converted to IDN.
51 ///
52 /// This property can be set to change the hostname of the URL.
53 /// If the URL's scheme is not hierarchical (which the URL standard calls "special schemes"),
54 /// then it has no concept of a host and setting this property has no effect.
55 ///
56 /// ### Examples
57 /// ```
58 /// https://developer.mozilla.org/en-US/docs/Web/API/URL/hostname
59 /// Yields: 'developer.mozilla.org
60 ///
61 /// 你好.com
62 /// Yields: 'xn--6qq79v.com
63 /// ```
64 pub fn hostname(&self) -> WasmToolkitResult<String> {
65 self.location().hostname().map_err(|error| {
66 let outcome = WasmToolkitCommon::exception_or_stringify(&error);
67
68 WasmToolkitError::HostnameNotFound(outcome)
69 })
70 }
71
72 /// The host property of the URL interface is a string containing the host,
73 /// which is the hostname, and then, if the port of the URL is nonempty, a ":",
74 /// followed by the port of the URL. If the URL does not have a hostname,
75 /// this property contains an empty string, "".
76 ///
77 /// This property can be set to change both the hostname and the port of the URL.
78 /// If the URL's scheme is not hierarchical (which the URL standard calls "special schemes"),
79 /// then it has no concept of a host and setting this property has no effect.
80 ///
81 /// **Note**: If the given value for the host setter lacks a port,
82 /// the URL's port will not change. This can be unexpected as the host getter does
83 /// return a URL-port string, so one might have assumed the setter to always "reset" both.
84 ///
85 /// ### Examples
86 /// ```
87 /// 1. https://developer.mozilla.org/en-US/docs/Web/API/URL/host
88 /// "developer.mozilla.org"
89 ///
90 /// 2. https://developer.mozilla.org:443/en-US/docs/Web/API/URL/host
91 /// developer.mozilla.org #The port number is not included because 443 is the scheme's default port
92 ///
93 /// 3. https://developer.mozilla.org:4097/en-US/docs/Web/API/URL/host
94 /// developer.mozilla.org:4097
95 /// ```
96 pub fn host(&self) -> WasmToolkitResult<String> {
97 self.location().host().map_err(|error| {
98 let outcome = WasmToolkitCommon::exception_or_stringify(&error);
99
100 WasmToolkitError::HostNotFound(outcome)
101 })
102 }
103
104 /// The origin read-only property of the Location interface returns a string
105 /// containing the Unicode serialization of the origin of the location's URL.
106 /// It contains the scheme as the first part.
107 ///
108 /// The port is only included if it's not the default for the protocol.
109 ///
110 /// ### Example: 'https://developer.mozilla.org'
111 pub fn origin(&self) -> WasmToolkitResult<String> {
112 self.location().origin().map_err(|error| {
113 let outcome = WasmToolkitCommon::exception_or_stringify(&error);
114
115 WasmToolkitError::HostNotFound(outcome)
116 })
117 }
118
119 /// The href property of the Location interface is a stringifier that returns
120 /// a string containing the whole URL, and allows the href to be updated.
121 /// Setting the value of href navigates to the provided URL.
122 ///
123 /// If you want redirection, use location.replace().
124 /// The difference from setting the href property value is that when
125 /// using the location.replace() method, after navigating to the given URL,
126 /// the current page will not be saved in session history —
127 /// meaning the user won't be able to use the back button to navigate to it.
128 ///
129 /// ### Example
130 /// https://developer.mozilla.org/en-US/docs/Web/API/URL/href
131 /// Yields: https://developer.mozilla.org/en-US/docs/Web/API/URL/href
132 /// ```
133 pub fn href(&self) -> WasmToolkitResult<String> {
134 self.location().href().map_err(|error| {
135 let outcome = WasmToolkitCommon::exception_or_stringify(&error);
136
137 WasmToolkitError::HrefNotFound(outcome)
138 })
139 }
140
141 /// The hash property of the URL interface is a string containing a "#" followed
142 /// by the fragment identifier of the URL. If the URL does not have a fragment identifier,
143 /// this property contains an empty string, "".
144 ///
145 /// This property can be set to change
146 /// the fragment identifier of the URL. When setting, a single "#" prefix is
147 /// added to the provided value, if not already present.
148 /// Setting it to "" removes the fragment identifier.
149 ///
150 /// The fragment is percent-encoded when setting but not percent-decoded when reading.
151 pub fn hash(&self) -> WasmToolkitResult<String> {
152 self.location().hash().map_err(|error| {
153 let outcome: String = WasmToolkitCommon::exception_or_stringify(&error);
154
155 WasmToolkitError::HashNotFound(outcome)
156 })
157 }
158
159 pub fn navigator(&self) -> web_sys::Navigator {
160 self.inner().navigator()
161 }
162
163 pub fn language(&self) -> WasmToolkitResult<String> {
164 self.navigator()
165 .language()
166 .ok_or(WasmToolkitError::BrowserLanguageNotFound)
167 }
168}