From bf5e25c9eca1f12a68f9a9eab0b1b9eedd232a79 Mon Sep 17 00:00:00 2001 From: Luke Chu <37006668+lukechu10@users.noreply.github.com> Date: Fri, 2 Sep 2022 20:58:19 +0200 Subject: [PATCH 1/2] Re-enable the missing svg elements --- packages/sycamore/src/web/html.rs | 74 ++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/packages/sycamore/src/web/html.rs b/packages/sycamore/src/web/html.rs index 84693e0b3..2f418fa96 100644 --- a/packages/sycamore/src/web/html.rs +++ b/packages/sycamore/src/web/html.rs @@ -14,7 +14,7 @@ macro_rules! define_elements { $ns:expr, $( $(#[$attr:meta])* - $el:ident { + $el:ident $(($tag:expr))? { $( $(#[$attr_method:meta])* $at:ident: $ty:path, @@ -23,24 +23,58 @@ macro_rules! define_elements { )* ) => { $( - #[allow(non_camel_case_types)] - #[doc = concat!("Build a [`<", stringify!($el), ">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/", stringify!($el), ") element.")] - $(#[$attr])* - #[derive(Debug)] - pub struct $el {} - - impl SycamoreElement for $el { - const TAG_NAME: &'static str = stringify!($el); - const NAME_SPACE: Option<&'static str> = $ns; + define_element_impl! { + $ns, + #[doc = concat!("Build a [`<", stringify!($el), ">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/", stringify!($el), ") element.")] + $(#[$attr])* + $el $(($tag))* { + $( + $(#[attr])* + $at: $ty + )* + } } + )* + }; +} + +macro_rules! define_element_impl { + ( + $ns:expr, + $(#[$attr:meta])* + $el:ident($tag:expr) { + $( + $(#[$attr_method:meta])* + $at:ident: $ty:path, + )* + } + ) => { + #[allow(non_camel_case_types)] + $(#[$attr])* + #[derive(Debug)] + pub struct $el {} + + impl SycamoreElement for $el { + const TAG_NAME: &'static str = $tag; + const NAME_SPACE: Option<&'static str> = $ns; + } - #[allow(non_snake_case)] - #[doc = concat!("Create a [`<", stringify!($el), ">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/", stringify!($el), ") element builder.")] + #[allow(non_snake_case)] + $(#[$attr])* + pub fn $el<'a, G: GenericNode>() -> ElementBuilder<'a, G, impl FnOnce(Scope<'a>) -> G> { + ElementBuilder::new(move |_| G::element::<$el>()) + } + }; + ( + $ns:expr, + $(#[$attr:meta])* + $el:ident $rest:tt + ) => { + define_element_impl! { + $ns, $(#[$attr])* - pub fn $el<'a, G: GenericNode>() -> ElementBuilder<'a, G, impl FnOnce(Scope<'a>) -> G> { - ElementBuilder::new(move |_| G::element::<$el>()) - } - )* + $el(stringify!($el)) $rest + } }; } @@ -210,7 +244,7 @@ define_elements! { define_elements! { Some("http://www.w3.org/2000/svg"), svg {}, - // a, + svg_a("a") {}, animate {}, animateMotion {}, animateTransform {}, @@ -263,15 +297,15 @@ define_elements! { polyline {}, radialGradient {}, rect {}, - // script {}, + svg_script("script") {}, set {}, stop {}, - // style {}, + svg_style("style") {}, switch {}, symbol {}, text {}, textPath {}, - // title {}, + svg_title("title") {}, tspan {}, r#use {}, view {}, From e407b87118952511f6d5468bd8f6056d226450d9 Mon Sep 17 00:00:00 2001 From: Luke Chu <37006668+lukechu10@users.noreply.github.com> Date: Sat, 3 Sep 2022 07:08:11 +0200 Subject: [PATCH 2/2] Add some tests for svg --- packages/sycamore/tests/ssr/main.rs | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/packages/sycamore/tests/ssr/main.rs b/packages/sycamore/tests/ssr/main.rs index e86e37023..cde28580e 100644 --- a/packages/sycamore/tests/ssr/main.rs +++ b/packages/sycamore/tests/ssr/main.rs @@ -170,3 +170,37 @@ fn no_ssr_sub_tree_should_not_be_emitted_in_ssr() { r#"

Rendered

"# ); } + +mod svg { + use super::*; + + #[test] + fn ssr_svg_elements() { + let out = sycamore::render_to_string(|cx| { + view! { cx, + svg(xmlns="http://www.w3.org/2000/svg") { + rect(width=100, height=100, fill="red") + } + } + }); + assert_eq!( + out, + r#""# + ); + } + + #[test] + fn ssr_svg_elements_with_same_name_as_html_elements() { + let out = sycamore::render_to_string(|cx| { + view! { cx, + svg(xmlns="http://www.w3.org/2000/svg") { + svg_a {} // Should render as "" + } + } + }); + assert_eq!( + out, + r#""# + ); + } +}