Skip to content

Commit 984baf7

Browse files
committed
Merge branch 'master' of https://github.com/David-OConnor/seed
2 parents ab02a1c + b2f0367 commit 984baf7

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

src/dom_types.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ use crate::vdom::Mailbox; // todo temp
1212

1313
//use regex::Regex;
1414

15+
/// Common Namespaces
16+
#[derive(Debug,Clone,PartialEq)]
17+
pub enum Namespace {
18+
/// SVG Namespace
19+
Svg,
20+
Custom(String)
21+
}
22+
23+
impl Namespace {
24+
pub fn as_str(&self) -> &str {
25+
use self::Namespace::*;
26+
match self{
27+
Svg => "http://www.w3.org/2000/svg",
28+
Custom(s) => s
29+
}
30+
}
31+
}
1532

1633
// todo cleanup enums vs &strs for restricting events/styles/attrs to
1734
// todo valid ones.
@@ -520,12 +537,14 @@ make_tags! {
520537

521538
Details => "details", Dialog => "dialog", Menu => "menu", MenuItem => "menuitem", Summary => "summary",
522539

523-
Content => "content", Element => "element", Shadow => "shadow", Slot => "slot", Template => "template"
540+
Content => "content", Element => "element", Shadow => "shadow", Slot => "slot", Template => "template",
541+
542+
Svg => "svg", Line => "line", Rect => "rect", Circle => "circle"
524543
}
525544

526545
/// An component in our virtual DOM.
527546
pub struct El<Ms: Clone + 'static> {
528-
// M sis a message type, as in part of TEA.
547+
// Ms is a message type, as in part of TEA.
529548
// We call this 'El' instead of 'Element' for brevity, and to prevent
530549
// confusion with web_sys::Element.
531550

@@ -536,6 +555,7 @@ pub struct El<Ms: Clone + 'static> {
536555
pub listeners: Vec<Listener<Ms>>,
537556
pub text: Option<String>,
538557
pub children: Vec<El<Ms>>,
558+
pub namespace: Option<Namespace>,
539559

540560
// Things that get filled in later, to assist with rendering.
541561
pub id: Option<u32>,
@@ -550,16 +570,17 @@ pub struct El<Ms: Clone + 'static> {
550570

551571
impl<Ms: Clone + 'static> El<Ms> {
552572
pub fn new(tag: Tag, attrs: Attrs, style: Style,
553-
listeners: Vec<Listener<Ms>>, text: &str, children: Vec<El<Ms>>) -> Self {
573+
listeners: Vec<Listener<Ms>>, text: &str, children: Vec<El<Ms>>, namespace: Option<Namespace>) -> Self {
554574
Self {tag, attrs, style, text: Some(text.into()), children,
555-
el_ws: None, listeners, id: None, nest_level: None, raw_html: false}
575+
el_ws: None, listeners, id: None, nest_level: None, raw_html: false, namespace
576+
}
556577
}
557578

558579
/// Create an empty element, specifying only the tag
559580
pub fn empty(tag: Tag) -> Self {
560581
Self {tag, attrs: Attrs::empty(), style: Style::empty(),
561582
text: None, children: Vec::new(), el_ws: None,
562-
listeners: Vec::new(), id: None, nest_level: None, raw_html: false}
583+
listeners: Vec::new(), id: None, nest_level: None, raw_html: false, namespace: None }
563584
}
564585

565586
/// Create an element that will display markdown from the text you pass to it, as HTML
@@ -651,6 +672,7 @@ impl<Ms: Clone + 'static> El<Ms> {
651672
nest_level: None,
652673
el_ws: self.el_ws.clone(),
653674
raw_html: self.raw_html,
675+
namespace: self.namespace.clone(),
654676
}
655677
}
656678

@@ -682,6 +704,7 @@ impl<Ms: Clone + 'static> Clone for El<Ms> {
682704
el_ws: self.el_ws.clone(),
683705
listeners: Vec::new(),
684706
raw_html: self.raw_html,
707+
namespace: self.namespace.clone(),
685708
}
686709
}
687710
}

src/shortcuts.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,54 @@ macro_rules! iframe {
853853
};
854854
}
855855

856-
//// End element-creaion macros.
856+
#[macro_export]
857+
macro_rules! svg {
858+
( $($part:expr),* $(,)* ) => {
859+
{
860+
let mut el = El::empty(seed::dom_types::Tag::Svg);
861+
el.namespace = Some(seed::dom_types::Namespace::Svg);
862+
$ ( $part.update(&mut el); )*
863+
el
864+
}
865+
};
866+
}
867+
868+
#[macro_export]
869+
macro_rules! line {
870+
( $($part:expr),* $(,)* ) => {
871+
{
872+
let mut el = El::empty(seed::dom_types::Tag::Line);
873+
el.namespace = Some(seed::dom_types::Namespace::Svg);
874+
$ ( $part.update(&mut el); )*
875+
el
876+
}
877+
};
878+
}
879+
880+
#[macro_export]
881+
macro_rules! rect {
882+
( $($part:expr),* $(,)* ) => {
883+
{
884+
let mut el = El::empty(seed::dom_types::Tag::Rect);
885+
el.namespace = Some(seed::dom_types::Namespace::Svg);
886+
$ ( $part.update(&mut el); )*
887+
el
888+
}
889+
};
890+
}
891+
892+
#[macro_export]
893+
macro_rules! circle {
894+
( $($part:expr),* $(,)* ) => {
895+
{
896+
let mut el = El::empty(seed::dom_types::Tag::Circle);
897+
el.namespace = Some(seed::dom_types::Namespace::Svg);
898+
$ ( $part.update(&mut el); )*
899+
el
900+
}
901+
};
902+
}
903+
//// End element-creation macros.
857904

858905

859906
/// Provide a shortcut for creating attributes.

src/websys_bridge.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ fn set_attr_shim(el_ws: &web_sys::Element, name: &str, val: &str) {
3535
/// See also: https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Node.html
3636
pub fn make_websys_el<Ms: Clone>(el_vdom: &mut dom_types::El<Ms>, document: &web_sys::Document) -> web_sys::Element {
3737
// Create the DOM-analog element; it won't render until attached to something.
38-
let el_ws = document.create_element(&el_vdom.tag.as_str()).expect("Problem creating web-sys El");
38+
let tag = el_vdom.tag.as_str();
39+
let el_ws = match el_vdom.namespace {
40+
Some(ref ns) => document.create_element_ns(Some(ns.as_str()), tag).expect("Problem creating web-sys El"),
41+
None => document.create_element(tag).expect("Problem creating web-sys El")
42+
};
3943

4044
for (name, val) in &el_vdom.attrs.vals {
4145
set_attr_shim(&el_ws, name, val);

0 commit comments

Comments
 (0)