use crate::virtual_dom::{At, Attrs};
use wasm_bindgen::JsValue;
#[macro_export]
macro_rules! with_dollar_sign {
($($body:tt)*) => {
macro_rules! __with_dollar_sign { $($body)* }
__with_dollar_sign!($);
}
}
#[macro_export]
macro_rules! struct_urls {
() => {
pub struct Urls<'a> {
base_url: std::borrow::Cow<'a, $crate::browser::Url>,
}
impl<'a> Urls<'a> {
pub fn new(base_url: impl Into<std::borrow::Cow<'a, $crate::browser::Url>>) -> Self {
Self {
base_url: base_url.into(),
}
}
pub fn base_url(self) -> $crate::browser::Url {
self.base_url.into_owned()
}
}
};
}
macro_rules! element {
($($Tag:ident => $Tag_camel:ident);+) => {
with_dollar_sign! {
($d:tt) => {
$(
#[macro_export]
macro_rules! $Tag {
( $d($d part:expr),* $d(,)? ) => {
{
#[allow(unused_mut)]
let mut el = El::empty($crate::virtual_dom::Tag::$Tag_camel);
$d (
$d part.update_el(&mut el);
)*
$crate::virtual_dom::Node::Element(el)
}
};
}
)+
}
}
}
}
macro_rules! element_svg {
($($Tag:ident => $Tag_camel:ident);+) => {
with_dollar_sign! {
($d:tt) => {
$(
#[macro_export]
macro_rules! $Tag {
( $d($d part:expr),* $d(,)? ) => {
{
#[allow(unused_mut)]
let mut el = El::empty_svg($crate::virtual_dom::Tag::$Tag_camel);
$d ( $d part.update_el(&mut el); )*
$crate::virtual_dom::Node::Element(el)
}
};
}
)+
}
}
}
}
element! {
address => Address; article => Article; aside => Aside; footer => Footer;
header => Header; h1 => H1;
h2 => H2; h3 => H3; h4 => H4; h5 => H5; h6 => H6;
hgroup => Hgroup; main => Main; nav => Nav; section => Section;
blockquote => BlockQuote;
dd => Dd; dir => Dir; div => Div; dl => Dl; dt => Dt; figcaption => FigCaption; figure => Figure;
hr => Hr; li => Li; ol => Ol; p => P; pre => Pre; ul => Ul;
a => A; abbr => Abbr;
b => B; bdi => Bdi; bdo => Bdo; br => Br; cite => Cite; code => Code; data => Data;
dfn => Dfn; em => Em; i => I; kbd => Kbd; mark => Mark; q => Q; rb => Rb;
rp => Rp; rt => Rt; rtc => Rtc; ruby => Ruby; s => S; samp => Samp; small => Small;
span => Span; strong => Strong; sub => Sub; sup => Sup; time => Time; tt => Tt;
u => U; var => Var; wbr => Wbr;
area => Area; audio => Audio; img => Img; map => Map; track => Track; video => Video;
applet => Applet; embed => Embed; iframe => Iframe;
noembed => NoEmbed; object => Object; param => Param; picture => Picture; source => Source;
canvas => Canvas; noscript => NoScript; Script => Script;
del => Del; ins => Ins;
caption => Caption; col => Col; colgroup => ColGroup; table => Table; tbody => Tbody;
td => Td; tfoot => Tfoot; th => Th; thead => Thead; tr => Tr;
button => Button; datalist => DataList; fieldset => FieldSet; form => Form; input => Input;
label => Label; legend => Legend; meter => Meter; optgroup => OptGroup; option => Option;
output => Output; progress => Progress; select => Select; textarea => TextArea;
details => Details; dialog => Dialog; menu => Menu; menuitem => MenuItem; summary => Summary;
content => Content; element => Element; shadow => Shadow; slot => Slot; template => Template
}
element_svg! {
line_ => Line; rect => Rect; circle => Circle; ellipse => Elipse; polygon => Polygon; polyline => Polyline;
mesh => Mesh; path => Path;
defs => Defs; g => G; marker => Marker; mask => Mask;
pattern => Pattern; svg => Svg; switch => Switch; symbol => Symbol; unknown => Unknown;
linearGradient => LinearGradient; radialGradient => RadialGradient; meshGradient => MeshGradient;
stop => Stop;
image => Image;
r#use => Use;
altGlyph => AltGlyph; altGlyphDef => AltGlyphDef; altGlyphItem => AltGlyphItem; glyph => Glyph;
glyphRef => GlyphRef; textPath => TextPath; text => Text; tref => TRef; tspan => TSpan;
clipPath => ClipPath; cursor => Cursor; filter => Filter; foreignObject => ForeignObject;
hatchpath => HatchPath; meshPatch => MeshPatch; meshrow => MeshRow; view => View;
animate => Animate; animateColor => AnimateColor; animateMotion => AnimateMotion;
animateTransform => AnimateTransform; discard => Discard; mpath => Mpath; set => Set;
desc => Desc; metadata => Metadata; title => Title;
feBlend => FeBlend; feColorMatrix => FeColorMatrix; feComponentTransfer => FeComponentTransfer;
feComposite => FeComposite; feConvolveMatrix => FeConvolveMatrix;
feDiffuseLighting => FeDiffuseLighting; feDisplacementMap => FeDisplacementMap;
feDropShadow => FeDropShadow; feFlood => FeFlood; feFuncA => FeFuncA; feFuncB => FeFuncB;
feFuncG => FeFuncG; feFuncR => FeFuncR; feGaussianBlur => FeGaussianBlur; feImage => FeImage;
feMerge => FeMerge; feMergeNode => FeMergeNode; feMorphology => FeMorphology;
feOffset => FeOffset; feSpecularLighting => FeSpecularLighting; feTile => FeTile;
feTurbulence => FeTurbulence;
font => Font; hkern => HKern; vkern => VKern;
hatch => Hatch; solidcolor => SolidColor
}
#[macro_export]
macro_rules! empty {
() => {
$crate::virtual_dom::Node::Empty
};
}
#[macro_export]
macro_rules! raw {
($raw_html:expr) => {
Node::from_html($raw_html)
};
}
#[macro_export]
macro_rules! md {
($md:expr) => {
Node::from_markdown($md)
};
}
#[macro_export]
macro_rules! plain {
($text:expr) => {
$crate::virtual_dom::Node::new_text($text)
};
}
#[macro_export]
macro_rules! custom {
( $($part:expr),* $(,)? ) => {
{
let default_tag_name = "missing-tag-name";
let mut el = El::empty($crate::virtual_dom::Tag::from(default_tag_name));
$ ( $part.update_el(&mut el); )*
if let $crate::virtual_dom::Tag::Custom(tag_name) = &el.tag {
let tag_changed = tag_name != default_tag_name;
assert!(tag_changed, "Tag has not been set in `custom!` element. Add e.g. `Tag::from(\"code-block\")`.");
}
$crate::virtual_dom::Node::Element(el)
}
};
}
#[macro_export]
macro_rules! attrs {
{ $($key:expr => $value:expr $(;)?$(,)?)* } => {
{
let mut vals = IndexMap::new();
$(
vals.insert($key.into(), (&$value).into());
)*
$crate::virtual_dom::Attrs::new(vals)
}
};
}
#[deprecated(since = "0.8.0", note = "use [`C!`](macro.C!.html) instead")]
#[macro_export]
macro_rules! class {
{ $($class:expr $(=> $predicate:expr)? $(,)?)* } => {
{
let mut result = $crate::virtual_dom::Attrs::empty();
let mut classes = Vec::new();
$(
(||{
$(
if !$predicate { return }
)?
classes.push($class);
})();
)*
result.add_multiple(At::Class, &classes);
result
}
};
}
#[macro_export]
macro_rules! C {
( $($class:expr $(,)?)* ) => {
{
let mut all_classes = Vec::new();
$(
$crate::shortcuts::_fill_all_classes(&mut all_classes, $class.to_classes());
)*
$crate::shortcuts::_all_classes_to_attrs(&all_classes)
}
};
}
pub fn _fill_all_classes(all_classes: &mut Vec<String>, classes: Option<Vec<String>>) {
if let Some(classes) = classes {
for class in classes {
if !class.is_empty() {
all_classes.push(class);
}
}
}
}
pub fn _all_classes_to_attrs(all_classes: &[String]) -> Attrs {
let mut attrs = Attrs::empty();
if !all_classes.is_empty() {
attrs.add_multiple(
At::Class,
&all_classes.iter().map(String::as_str).collect::<Vec<_>>(),
);
}
attrs
}
#[macro_export]
macro_rules! IF {
( $predicate:expr => $value:expr ) => {{
if $predicate {
Some($value)
} else {
None
}
}};
}
#[macro_export]
macro_rules! id {
{ $id:expr } => {
{
$crate::virtual_dom::Attrs::from_id($id)
}
};
}
#[macro_export]
macro_rules! style {
{ $($key:expr => $value:expr $(;)?$(,)?)* } => {
{
#[allow(unused_imports)]
use $crate::virtual_dom::values::{
ToCSSValueForCSSValue, ToCSSValueForOptionToString, ToCSSValueForToString
};
let mut vals = IndexMap::new();
$(
vals.insert($key.into(), ($value).to_css_value());
)*
$crate::virtual_dom::Style::new(vals)
}
};
}
#[macro_export]
macro_rules! nodes {
( $($element:expr $(,)?)* ) => {
{
use $crate::virtual_dom::IntoNodes;
let mut nodes = Vec::new();
$(
nodes.append(&mut ($element).into_nodes());
)*
nodes
}
};
}
#[cfg(use_nightly)]
pub const fn wrap_debug<T>(object: T) -> dbg::WrapDebug<T> {
dbg::WrapDebug(object)
}
#[cfg(not(use_nightly))]
pub fn wrap_debug<T: std::fmt::Debug>(object: T) -> T {
object
}
#[macro_export]
macro_rules! log {
{ $($expr:expr),* $(,)? } => {
{
let mut formatted_exprs = Vec::new();
$(
formatted_exprs.push(format!("{:#?}", $crate::shortcuts::wrap_debug(&$expr)));
)*
$crate::shortcuts::log_1(
&formatted_exprs
.as_slice()
.join(" ")
.into()
);
}
};
}
pub fn log_1(data_1: &JsValue) {
web_sys::console::log_1(data_1);
}
#[macro_export]
macro_rules! error {
{ $($expr:expr),* $(,)? } => {
{
let mut formatted_exprs = Vec::new();
$(
formatted_exprs.push(format!("{:#?}", $crate::shortcuts::wrap_debug(&$expr)));
)*
$crate::shortcuts::error_1(
&formatted_exprs
.as_slice()
.join(" ")
.into()
);
}
};
}
pub fn error_1(data_1: &JsValue) {
web_sys::console::error_1(data_1);
}
#[macro_export]
macro_rules! key_value_pairs {
{ $($key:expr => $value:expr),* $(,)? } => {
{
let mut result = IndexMap::new();
$(
result.insert($key.to_string(), $value.to_string());
)*
result
}
};
}