Skip to content

Method for stripping out the websys node in vdom. #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 31, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 68 additions & 12 deletions src/dom_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,10 @@ impl Text {
node_ws: None,
}
}

pub fn strip_ws_node(&mut self) {
self.node_ws.take();
}
}

/// A component in our virtual DOM.
Expand All @@ -743,19 +747,8 @@ impl<Ms: 'static> Clone for Node<Ms> {
}
}

// Element methods
impl<Ms> Node<Ms> {
fn is_text(&self) -> bool {
if let Node::Text(_) = self {
true
} else {
false
}
}

pub fn new_text(text: impl Into<Cow<'static, str>>) -> Self {
Node::Text(Text::new(text))
}

/// See `El::from_markdown`
pub fn from_markdown(markdown: &str) -> Vec<Node<Ms>> {
El::from_markdown(markdown)
Expand Down Expand Up @@ -836,6 +829,61 @@ impl<Ms> Node<Ms> {
}
}

// Convenience methods
impl<Ms> Node<Ms> {
pub fn new_text(text: impl Into<Cow<'static, str>>) -> Self {
Node::Text(Text::new(text))
}

pub fn is_text(&self) -> bool {
if let Node::Text(_) = self {
true
} else {
false
}
}
pub fn is_el(&self) -> bool {
if let Node::Element(_) = self {
true
} else {
false
}
}
pub fn is_empty(&self) -> bool {
if let Node::Empty = self {
true
} else {
false
}
}

pub fn text(&self) -> Option<&Text> {
if let Node::Text(t) = self {
Some(t)
} else {
None
}
}
pub fn el(&self) -> Option<&El<Ms>> {
if let Node::Element(e) = self {
Some(e)
} else {
None
}
}
}

// Backing node manipulation
impl<Ms> Node<Ms> {
pub fn strip_ws_nodes_from_self_and_children(&mut self) {
match self {
Node::Text(t) => t.strip_ws_node(),
Node::Element(e) => e.strip_ws_nodes_from_self_and_children(),
Node::Empty => (),
}
}
}

impl<Ms: 'static, OtherMs: 'static> MessageMapper<Ms, OtherMs> for Node<Ms> {
type SelfWithOtherMs = Node<OtherMs>;
/// See note on impl for El
Expand Down Expand Up @@ -1096,6 +1144,14 @@ impl<Ms> El<Ms> {
})
.collect()
}

/// Remove websys nodes.
pub fn strip_ws_nodes_from_self_and_children(&mut self) {
self.node_ws.take();
for child in &mut self.children {
child.strip_ws_nodes_from_self_and_children();
}
}
}

/// Allow the user to clone their Els. Note that there's no easy way to clone the
Expand Down