From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <yew-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id C8F671FF15E
	for <inbox@lore.proxmox.com>; Tue,  6 May 2025 09:22:13 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 5EDA6258B;
	Tue,  6 May 2025 09:22:30 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: yew-devel@lists.proxmox.com
Date: Tue,  6 May 2025 09:21:55 +0200
Message-Id: <20250506072155.357452-1-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.39.5
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.021 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [webauthn.rs, xtermjs.rs, utils.rs]
Subject: [yew-devel] [PATCH yew-comp] tree wide: use gloo_utils for
 body/window/document
X-BeenThere: yew-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Yew framework devel list at Proxmox <yew-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/yew-devel>, 
 <mailto:yew-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/yew-devel/>
List-Post: <mailto:yew-devel@lists.proxmox.com>
List-Help: <mailto:yew-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/yew-devel>, 
 <mailto:yew-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Yew framework devel list at Proxmox <yew-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: yew-devel-bounces@lists.proxmox.com
Sender: "yew-devel" <yew-devel-bounces@lists.proxmox.com>

to avoid using unwrap ourselves. Also inline the variables if there was
just one use of them.

Signed-off-by: Dominik Csapak <d.csapak@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



_______________________________________________
yew-devel mailing list
yew-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/yew-devel