From 776c5e8839732bad309c94a6ddf04a7cb63f613e Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Thu, 22 Dec 2022 13:52:41 -0600 Subject: [PATCH] fix hot reloading with global and svg attibutes --- packages/html/src/elements.rs | 3 +- packages/html/src/global_attributes.rs | 3776 +++++++++++++++++++----- 2 files changed, 3016 insertions(+), 763 deletions(-) diff --git a/packages/html/src/elements.rs b/packages/html/src/elements.rs index 5f5ff59c5..2e0000693 100644 --- a/packages/html/src/elements.rs +++ b/packages/html/src/elements.rs @@ -1,4 +1,5 @@ #![allow(non_upper_case_globals)] +use crate::{map_global_attributes, map_svg_attributes}; use crate::{GlobalAttributes, SvgAttributes}; use dioxus_rsx::HotReloadingContext; @@ -218,7 +219,7 @@ macro_rules! builder_constructors { } ); )* - None + map_global_attributes(attribute).or_else(|| map_svg_attributes(attribute)) } fn map_element(element: &str) -> Option<(&'static str, Option<&'static str>)> { diff --git a/packages/html/src/global_attributes.rs b/packages/html/src/global_attributes.rs index 5c4ac42e8..cd4e8f44a 100644 --- a/packages/html/src/global_attributes.rs +++ b/packages/html/src/global_attributes.rs @@ -2,70 +2,135 @@ use crate::AttributeDiscription; -macro_rules! no_namespace_trait_methods { +macro_rules! trait_method { ( - $( + ( $(#[$attr:meta])* $name:ident; - )* + ) ) => { - $( - $(#[$attr])* - const $name: AttributeDiscription = ( - stringify!($name), - None, - false - ); - )* + $(#[$attr])* + const $name: AttributeDiscription = ( + stringify!($name), + None, + false + ); }; -} -macro_rules! style_trait_methods { ( - $( + ( $(#[$attr:meta])* - $name:ident: $lit:literal, - )* + $name:ident: $lit:literal; + ) ) => { - $( - $(#[$attr])* - const $name: AttributeDiscription = ( - $lit, - Some("style"), - false - ); - )* + $(#[$attr])* + const $name: AttributeDiscription = ( + $lit, + None, + false + ); }; -} -macro_rules! aria_trait_methods { ( - $( + ( $(#[$attr:meta])* - $name:ident: $lit:literal, - )* + $name:ident: $lit:literal, $ns:literal; + ) ) => { - $( - $(#[$attr])* - const $name: AttributeDiscription = ( - $lit, - None, - false - ); - )* + $(#[$attr])* + const $name: AttributeDiscription = ( + $lit, + Some($ns), + false + ); }; } -pub trait GlobalAttributes { - /// Prevent the default action for this element. - /// - /// For more information, see the MDN docs: - /// +macro_rules! trait_method_mapping { + ( + $matching:ident; + ( + $(#[$attr:meta])* + $name:ident; + ) + ) => { + if $matching == stringify!($name) { + return Some((stringify!($name), None)); + } + }; + ( + $matching:ident; + ( + $(#[$attr:meta])* + $name:ident: $lit:literal; + ) + ) => { + if $matching == stringify!($name) { + return Some(($lit, None)); + } + }; + ( + $matching:ident; + ( + $(#[$attr:meta])* + $name:ident: $lit:literal, $ns:literal; + ) + ) => { + if $matching == stringify!($name) { + return Some(($lit, Some($ns))); + } + }; +} - const prevent_default: AttributeDiscription = ("dioxus-prevent-default", None, false); +macro_rules! trait_methods { + ( + $(#[$attr:meta])* + $trait:ident; + $fn:ident; + $( + $tokens:tt, + )* + ) => { + pub trait $trait { + $( + trait_method! { + $tokens + } + )* + } - no_namespace_trait_methods! { + pub(crate) fn $fn(attr: &str) -> Option<(&'static str, Option<&'static str>)> { + $( + trait_method_mapping! { + attr; + $tokens + } + )* + None + } + }; +} +trait_methods! { + GlobalAttributes; + map_global_attributes; + ( + /// Prevent the default action for this element. + /// + /// For more information, see the MDN docs: + /// + prevent_default: "dioxus-prevent-default"; + ), + ( + /// accesskey; + ), + ( + /// autocapitalize; + ), + ( + /// autofocus; + ), + ( /// The HTML class attribute is used to specify a class for an HTML element. /// /// ## Details @@ -94,39 +159,117 @@ pub trait GlobalAttributes { /// padding: 10px; /// } /// ``` + /// class; + ), + ( + /// contenteditable; + ), + ( + /// data; + ), + ( + /// dir; + ), + ( + /// draggable; + ), + ( + /// enterkeyhint; + ), + ( + /// exportparts; + ), + ( + ///