From 9751c7450158ba6a950bed595d7bf68a624e2830 Mon Sep 17 00:00:00 2001 From: Filipe Rodrigues Date: Mon, 19 Feb 2024 08:39:11 +0000 Subject: [PATCH] Added the ability for custom element types in `dynatos_html::html`. --- dynatos-html/Cargo.toml | 2 +- dynatos-html/src/html.rs | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/dynatos-html/Cargo.toml b/dynatos-html/Cargo.toml index cff9704..f75e963 100644 --- a/dynatos-html/Cargo.toml +++ b/dynatos-html/Cargo.toml @@ -11,4 +11,4 @@ itertools = { workspace = true } js-sys = { workspace = true } tracing = { workspace = true } wasm-bindgen = { workspace = true } -web-sys = { workspace = true } +web-sys = { workspace = true, features = ["HtmlInputElement"] } diff --git a/dynatos-html/src/html.rs b/dynatos-html/src/html.rs index b44576a..664efb2 100644 --- a/dynatos-html/src/html.rs +++ b/dynatos-html/src/html.rs @@ -1,7 +1,10 @@ //! HTML elements // Imports -use {wasm_bindgen::JsValue, web_sys::Element}; +use { + wasm_bindgen::{JsCast, JsValue}, + web_sys::{Element, HtmlInputElement}, +}; /// Html namespace const HTML_NAMESPACE: &str = "http://www.w3.org/1999/xhtml"; @@ -16,12 +19,22 @@ macro el_name { }, } +/// Expands to `$ElTy`, if present, otherwise to `Element` +macro el_ty { + ($ElTy:ty) => { + $ElTy + }, + () => { + Element + }, +} + /// Declares all elements macro decl_elements( - $( $fn_name:ident $( = $el_name:literal )? ),* $(,)? + $( $fn_name:ident $( : $ElTy:ty )? $( = $el_name:literal )? ),* $(,)? ) { $( - pub fn $fn_name() -> Element { + pub fn $fn_name() -> el_ty![$( $ElTy )?] { // TODO: Cache the document in a thread local? let window = web_sys::window().expect("Unable to get window"); let document = window.document().expect("Unable to get document"); @@ -29,6 +42,8 @@ macro decl_elements( let el_name = el_name!($fn_name $(, $el_name)?); document.create_element_ns(Some(HTML_NAMESPACE), el_name) .unwrap_or_else(|err| self::on_create_fail(err, el_name)) + .dyn_into() + .expect("Element was of the incorrect type") } )* } @@ -100,7 +115,7 @@ decl_elements! { iframe, image, img, - input, + input: HtmlInputElement, ins, kbd, label,