[yew-devel] [PATCH yew-comp] tree wide: use gloo_utils for body/window/document
Dominik Csapak
d.csapak at proxmox.com
Tue May 6 09:21:55 CEST 2025
to avoid using unwrap ourselves. Also inline the variables if there was
just one use of them.
Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
Cargo.toml | 1 +
src/help_button.rs | 3 +--
src/log_view.rs | 3 +--
src/sanitize_html.rs | 3 +--
src/tfa/tfa_add_recovery.rs | 8 ++------
src/tfa/tfa_add_totp.rs | 3 +--
src/tfa/tfa_add_webauthn.rs | 2 +-
src/tfa/webauthn.rs | 2 +-
src/utils.rs | 6 +++---
src/xtermjs.rs | 4 +---
10 files changed, 13 insertions(+), 22 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
index c14311e..c0e6159 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -43,6 +43,7 @@ indexmap = { version = ">= 1.9", features = ["std"] }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
wasm-logger = "0.2"
gloo-events = "0.2"
+gloo-utils = "0.2"
gloo-timers = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_plain = "1.0"
diff --git a/src/help_button.rs b/src/help_button.rs
index 71ee670..c62121e 100644
--- a/src/help_button.rs
+++ b/src/help_button.rs
@@ -57,8 +57,7 @@ pub fn pbs_help_button(props: &HelpButton) -> Html {
.onclick({
let url = get_help_link(props.section.as_deref());
move |_| {
- let window = web_sys::window().unwrap();
- let _ = window.open_with_url_and_target(&url, "top");
+ let _ = gloo_utils::window().open_with_url_and_target(&url, "top");
}
})
.into()
diff --git a/src/log_view.rs b/src/log_view.rs
index 269f1ba..362c528 100644
--- a/src/log_view.rs
+++ b/src/log_view.rs
@@ -552,8 +552,7 @@ impl Component for PwtLogView {
if self.line_height.is_none() {
if let Some(el) = self.page_ref.cast::<web_sys::Element>() {
// get font size in pixels
- let window = web_sys::window().unwrap();
- if let Ok(Some(style)) = window.get_computed_style(&el) {
+ if let Ok(Some(style)) = gloo_utils::window().get_computed_style(&el) {
if let Ok(line_height) = style.get_property_value("line-height") {
let line_height = line_height.trim_end_matches("px");
if let Ok(line_height) = line_height.parse::<f64>() {
diff --git a/src/sanitize_html.rs b/src/sanitize_html.rs
index 101c525..35277c9 100644
--- a/src/sanitize_html.rs
+++ b/src/sanitize_html.rs
@@ -96,8 +96,7 @@ fn sanitize_html_element(node: &web_sys::Node, base_url: &str) -> Result<(), Err
/// "bad" node.type and drops "bad" attributes from the remaining nodes.
/// "bad" means anything which can do XSS or break the layout of the outer page.
pub fn sanitize_html(text: &str) -> Result<String, Error> {
- let window = web_sys::window().unwrap();
- let location = window.location();
+ let location = gloo_utils::window().location();
let origin = location.origin().unwrap_or_default();
let dom_parser = web_sys::DomParser::new().map_err(convert_js_error)?;
diff --git a/src/tfa/tfa_add_recovery.rs b/src/tfa/tfa_add_recovery.rs
index 6b4bb76..5c50c00 100644
--- a/src/tfa/tfa_add_recovery.rs
+++ b/src/tfa/tfa_add_recovery.rs
@@ -195,12 +195,9 @@ impl Component for ProxmoxTfaAddRecovery {
}
Msg::PrintKeys => {
if let Some(data) = &self.recovery_keys {
- let window = web_sys::window().unwrap();
- let document = window.document().unwrap();
- let body = document.body().unwrap();
let print_page = create_paperkey_page(data, self.print_counter);
self.print_counter += 1;
- self.print_portal = Some(create_portal(print_page, body.into()));
+ self.print_portal = Some(create_portal(print_page, gloo_utils::body().into()));
}
true
}
@@ -244,8 +241,7 @@ impl From<TfaAddRecovery> for VNode {
}
fn create_paperkey_page(data: &RecoveryKeyInfo, print_counter: usize) -> Html {
- let window = web_sys::window().unwrap();
- let document = window.document().unwrap();
+ let document = gloo_utils::document();
let userid = data.userid.clone();
let title = document.title();
diff --git a/src/tfa/tfa_add_totp.rs b/src/tfa/tfa_add_totp.rs
index 2c8b839..8fe3afa 100644
--- a/src/tfa/tfa_add_totp.rs
+++ b/src/tfa/tfa_add_totp.rs
@@ -201,10 +201,9 @@ fn render_qrcode(text: &str) -> Html {
}
fn randomize_secret() -> String {
- let window = web_sys::window().unwrap();
let mut rnd: [u8; 32] = [0u8; 32];
- let crypto = window.crypto().unwrap();
+ let crypto = gloo_utils::window().crypto().unwrap();
let _ = crypto.get_random_values_with_u8_array(&mut rnd);
let mut data = String::new();
diff --git a/src/tfa/tfa_add_webauthn.rs b/src/tfa/tfa_add_webauthn.rs
index fb6ee23..5bc07e7 100644
--- a/src/tfa/tfa_add_webauthn.rs
+++ b/src/tfa/tfa_add_webauthn.rs
@@ -148,7 +148,7 @@ async fn create_item(
let challenge_string = fixup_challenge(&challenge, abort_signal)?;
- let promise = super::webauthn::WasmWindow::from(web_sys::window().unwrap())
+ let promise = super::webauthn::WasmWindow::from(gloo_utils::window())
.navigator()
.credentials()
.create(&challenge)
diff --git a/src/tfa/webauthn.rs b/src/tfa/webauthn.rs
index fb9905e..ef6d42b 100644
--- a/src/tfa/webauthn.rs
+++ b/src/tfa/webauthn.rs
@@ -174,7 +174,7 @@ impl ProxmoxWebAuthn {
.ok()
.context("failed to set 'signal' property in challenge")?;
- let promise = WasmWindow::from(web_sys::window().unwrap())
+ let promise = WasmWindow::from(gloo_utils::window())
.navigator()
.credentials()
.get_with_options(challenge)
diff --git a/src/utils.rs b/src/utils.rs
index d500b6d..1025f53 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -329,8 +329,8 @@ pub fn json_array_to_flat_string(list: &[Value]) -> String {
pub fn copy_to_clipboard(node_ref: &NodeRef) {
if let Some(el) = node_ref.cast::<web_sys::HtmlInputElement>() {
- let window = web_sys::window().unwrap();
- let document = window.document().unwrap();
+ let window = gloo_utils::window();
+ let document = gloo_utils::document();
let selection = window.get_selection().unwrap().unwrap();
let _ = selection.remove_all_ranges();
@@ -347,7 +347,7 @@ pub fn copy_to_clipboard(node_ref: &NodeRef) {
/// Set the browser window.location.href
pub fn set_location_href(href: &str) {
- let window = web_sys::window().unwrap();
+ let window = gloo_utils::window();
let location = window.location();
let _ = location.set_href(href);
}
diff --git a/src/xtermjs.rs b/src/xtermjs.rs
index b0f8eee..c799201 100644
--- a/src/xtermjs.rs
+++ b/src/xtermjs.rs
@@ -46,9 +46,7 @@ impl XTermJs {
let features =
"toolbar=no,location=no,status=no,menubar=no,resizable=yes,width=800,height=420";
- let window = web_sys::window().unwrap();
-
- match window.open_with_url_and_target_and_features(&url, target, features) {
+ match gloo_utils::window().open_with_url_and_target_and_features(&url, target, features) {
Ok(Some(new_window)) => {
let _ = new_window.focus();
}
--
2.39.5
More information about the yew-devel
mailing list