diff --git a/src/dom.rs b/src/dom.rs index 3c323cb..dac116e 100644 --- a/src/dom.rs +++ b/src/dom.rs @@ -1170,7 +1170,7 @@ pub mod __internal { #[cfg(test)] mod tests { use super::{DomBuilder, text_signal, RefFn}; - use crate::{html, shadow_root, ShadowRootMode}; + use crate::{html, shadow_root, ShadowRootMode, with_cfg}; use futures_signals::signal::{always, SignalExt}; use lazy_static::lazy_static; use web_sys::HtmlElement; @@ -1316,4 +1316,17 @@ mod tests { }) }); } + + #[test] + fn with_cfg() { + let _a = html!("div", { + .with_cfg!(target_arch = "wasm32", { + .attribute("foo", "bar") + }) + + .with_cfg!(all(not(foo), bar = "test", feature = "hi"), { + .attribute("foo", "bar") + }) + }); + } } diff --git a/src/macros.rs b/src/macros.rs index 30c056d..8bc3a2c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -44,6 +44,20 @@ macro_rules! with_node { } +#[macro_export] +macro_rules! with_cfg { + ($this:ident, $cfg:meta, { $($methods:tt)* }) => {{ + #[cfg($cfg)] + let this = $crate::apply_methods!($this, { $($methods)* }); + + #[cfg(not($cfg))] + let this = $this; + + this + }}; +} + + #[macro_export] macro_rules! shadow_root { ($this:ident, $mode:expr => { $($methods:tt)* }) => {{