From 5529b005d724b52fbbc3d2e37b6a9d1840c5cf95 Mon Sep 17 00:00:00 2001 From: Ke7in Date: Fri, 8 Mar 2024 18:45:58 -0500 Subject: [PATCH 1/3] Add methods to UseDocument Added methods in the order they appear on the website discussed (for convenience) up to `preferred_style_sheet_set`. --- src/use_document.rs | 194 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 188 insertions(+), 6 deletions(-) diff --git a/src/use_document.rs b/src/use_document.rs index dfbf8b7..16cd226 100644 --- a/src/use_document.rs +++ b/src/use_document.rs @@ -1,11 +1,12 @@ use cfg_if::cfg_if; +use js_sys::Function; use std::ops::Deref; use crate::core::impl_ssr_safe_method; #[cfg(not(feature = "ssr"))] use leptos::*; use wasm_bindgen::JsValue; -use web_sys::NodeList; +use web_sys::{Document, Element, HtmlCollection, HtmlElement, HtmlHeadElement, Location, NodeList, VisibilityState, Window}; /// SSR safe `document()`. /// This returns just a new-type wrapper around `Option`. @@ -39,10 +40,10 @@ pub fn use_document() -> UseDocument { /// Return type of [`use_document`]. #[derive(Debug, Clone, PartialEq, Eq)] -pub struct UseDocument(Option); +pub struct UseDocument(Option); impl Deref for UseDocument { - type Target = Option; + type Target = Option; fn deref(&self) -> &Self::Target { &self.0 } @@ -51,22 +52,203 @@ impl Deref for UseDocument { impl UseDocument { impl_ssr_safe_method!( /// Returns `Some(Document)` in the Browser. `None` otherwise. - body(&self) -> Option; + body(&self) -> Option; .unwrap_or_default() ); impl_ssr_safe_method!( /// Returns the active (focused) `Some(web_sys::Element)` in the Browser. `None` otherwise. - active_element(&self) -> Option; + active_element(&self) -> Option; .unwrap_or_default() ); impl_ssr_safe_method!( - query_selector(&self, selector: &str) -> Result, JsValue>; + query_selector(&self, selector: &str) -> Result, JsValue>; .unwrap_or(Ok(None)) ); impl_ssr_safe_method!( query_selector_all(&self, selectors: &str) -> Option> ); + + impl_ssr_safe_method!( + url(&self) -> Option> + ); + + impl_ssr_safe_method!( + document_uri(&self) -> Option> + ); + + impl_ssr_safe_method!( + compat_mode(&self) -> Option + ); + + impl_ssr_safe_method!( + character_set(&self) -> Option + ); + + impl_ssr_safe_method!( + charset(&self) -> Option + ); + + impl_ssr_safe_method!( + input_encoding(&self) -> Option + ); + + impl_ssr_safe_method!( + content_type(&self) -> Option + ); + + impl_ssr_safe_method!( + document_element(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + location(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + referrer(&self) -> Option + ); + + impl_ssr_safe_method!( + last_modified(&self) -> Option + ); + + impl_ssr_safe_method!( + ready_state(&self) -> Option + ); + + impl_ssr_safe_method!( + title(&self) -> Option + ); + + impl_ssr_safe_method!( + dir(&self) -> Option + ); + + impl_ssr_safe_method!( + head(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + images(&self) -> Option + ); + + impl_ssr_safe_method!( + embeds(&self) -> Option + ); + + impl_ssr_safe_method!( + plugins(&self) -> Option + ); + + impl_ssr_safe_method!( + links(&self) -> Option + ); + + impl_ssr_safe_method!( + forms(&self) -> Option + ); + + impl_ssr_safe_method!( + scripts(&self) -> Option + ); + + impl_ssr_safe_method!( + default_view(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + onreadystatechange(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + onbeforescriptexecute(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + onafterscriptexecute(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + onselectionchange(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + current_script(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + anchors(&self) -> Option + ); + + impl_ssr_safe_method!( + applets(&self) -> Option + ); + + impl_ssr_safe_method!( + fullscreen(&self) -> Option + ); + + impl_ssr_safe_method!( + fullscreen_enabled(&self) -> Option + ); + + impl_ssr_safe_method!( + onfullscreenchange(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + onfullscreenerror(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + onpointerlockchange(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + onpointerlockerror(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + hidden(&self) -> Option + ); + + impl_ssr_safe_method!( + visibility_state(&self) -> Option + ); + + impl_ssr_safe_method!( + onvisibilitychange(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + selected_style_sheet_set(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + last_style_sheet_set(&self) -> Option; + .unwrap_or_default() + ); + + impl_ssr_safe_method!( + preferred_style_sheet_set(&self) -> Option; + .unwrap_or_default() + ); } From 09b08f64f5d5b50e1560491a10da827271f123bc Mon Sep 17 00:00:00 2001 From: Ke7in <60681675+luckynumberke7in@users.noreply.github.com> Date: Tue, 20 Aug 2024 21:40:23 -0400 Subject: [PATCH 2/3] Update "hidden" in use_document Changed to hide elements by default on server. --- src/use_document.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/use_document.rs b/src/use_document.rs index 16cd226..5a3f1bc 100644 --- a/src/use_document.rs +++ b/src/use_document.rs @@ -225,7 +225,9 @@ impl UseDocument { ); impl_ssr_safe_method!( - hidden(&self) -> Option + /// Hides on server by default + hidden(&self) -> bool; + .unwrap_or(true) ); impl_ssr_safe_method!( From 44cab1027dfa64ba0602ffc4e4dd837b3dd37883 Mon Sep 17 00:00:00 2001 From: Ke7in <60681675+luckynumberke7in@users.noreply.github.com> Date: Wed, 21 Aug 2024 16:41:04 -0400 Subject: [PATCH 3/3] fix cargo fmt issues Forgot to set up "cargo fmt" on save --new system. --- src/use_document.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/use_document.rs b/src/use_document.rs index 5a3f1bc..126fcd0 100644 --- a/src/use_document.rs +++ b/src/use_document.rs @@ -6,7 +6,10 @@ use crate::core::impl_ssr_safe_method; #[cfg(not(feature = "ssr"))] use leptos::*; use wasm_bindgen::JsValue; -use web_sys::{Document, Element, HtmlCollection, HtmlElement, HtmlHeadElement, Location, NodeList, VisibilityState, Window}; +use web_sys::{ + Document, Element, HtmlCollection, HtmlElement, HtmlHeadElement, Location, NodeList, + VisibilityState, Window, +}; /// SSR safe `document()`. /// This returns just a new-type wrapper around `Option`.