Skip to main content

VirtualNode

Enum VirtualNode 

Source
pub enum VirtualNode<Dom>
where Dom: RealDom,
{ Element(VirtualElement<Dom>), Text(VirtualText), }
Expand description

When building your views you’ll typically use the html! macro to generate VirtualNode’s.

html! { <div> <span></span> </div> } really generates a VirtualNode with one child (span).

Later, on the client side, you’ll use the diff and patch modules to update the real DOM with your latest tree of virtual nodes (virtual dom).

Or on the server side you’ll just call .to_string() on your root virtual node in order to recursively render the node and all of its children.

§Examples

use virtual_node::VirtualNode;
let div = VirtualNode::<()>::new_element("div");
assert_eq!(div.to_string(), "<div></div>");

Variants§

§

Element(VirtualElement<Dom>)

An element node (node type ELEMENT_NODE).

§

Text(VirtualText)

A text node (node type TEXT_NODE).

Note: This wraps a VText instead of a plain String in order to enable custom methods like create_text_node() on the wrapped type.

Implementations§

Source§

impl<Handle> VirtualNode<Handle>
where Handle: RealDom,

Source

pub fn children_recursive<'a>(&'a self) -> Vec<&'a VirtualNode<Handle>>

Get a vector of all of the VirtualNode children / grandchildren / etc of your virtual_node.

Children are visited recursively depth first.

§Examples
let component = html! {
 <div>
   <span> {"Hi!"} </span>
   <em> {"There!!"} </em>
   <div> {"My Friend"} </div>
 </div>
};

let children = component.children_recursive();

assert_eq!(children[2].tag(), "em");
Source§

impl<Dom> VirtualNode<Dom>
where Dom: RealDom,

Source

pub fn new_element<S>(tag: S) -> VirtualNode<Dom>
where S: Into<String>,

Create a new virtual element node with a given tag.

These get patched into the DOM using document.createElement

let _div = VirtualNode::<()>::new_element("div");
Source

pub fn new_text<S>(text: S) -> VirtualNode<Dom>
where S: Into<String>,

Create a new virtual text node with the given text.

These get patched into the DOM using document.createTextNode

let _text = VirtualNode::<()>::new_text("My text node");
Source

pub fn as_elem(&self) -> Option<&VirtualElement<Dom>>

Return a VirtualElement reference, if this is an Element variant.

Source

pub fn as_elem_mut(&mut self) -> Option<&mut VirtualElement<Dom>>

Return a mutable VirtualElement reference, if this is an Element variant.

Source

pub fn as_text(&self) -> Option<&VirtualText>

Return a VirtualText reference, if this is an Text variant.

Source

pub fn as_text_mut(&mut self) -> Option<&mut VirtualText>

Return a mutable VText reference, if this is an Text variant.

Source

pub fn map_real_dom<New>( self, convert_event: &dyn Fn(EventHandler<Dom>) -> EventHandler<New>, ) -> VirtualNode<New>
where New: RealDom,

Convert this VirtualNode<DomA> into a VirtualNode<DomB>.

Source

pub fn insert_space_before_text(&mut self)

Used by html-macro to insert space before text that is inside of a block that came after an open tag.

html! {

{world}
}

So that we end up with

world
when we’re finished parsing.

Source

pub fn insert_space_after_text(&mut self)

Used by html-macro to insert space after braced text if we know that the next block is another block or a closing tag.

html! {

{Hello} {world}
} ->
Hello world
html! {
{Hello}
} ->
Hello

So that we end up with

Hello world
when we’re finished parsing.

Source§

impl VirtualNode<Window>

Source

pub fn create_dom_node( &self, events: &mut VirtualEvents<Window>, ) -> (Node, VirtualEventNode)

Create and return a web_sys::Node along with its events.

Trait Implementations§

Source§

impl<Dom> Debug for VirtualNode<Dom>
where Dom: RealDom,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<Dom> Display for VirtualNode<Dom>
where Dom: RealDom,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<V, Dom> From<&V> for VirtualNode<Dom>
where Dom: RealDom, V: View<Dom>,

Source§

fn from(v: &V) -> VirtualNode<Dom>

Converts to this type from the input type.
Source§

impl<Dom> From<&str> for VirtualNode<Dom>
where Dom: RealDom,

Source§

fn from(other: &str) -> VirtualNode<Dom>

Converts to this type from the input type.
Source§

impl<Dom> From<String> for VirtualNode<Dom>
where Dom: RealDom,

Source§

fn from(other: String) -> VirtualNode<Dom>

Converts to this type from the input type.
Source§

impl<Dom> From<VirtualElement<Dom>> for VirtualNode<Dom>
where Dom: RealDom,

Source§

fn from(other: VirtualElement<Dom>) -> VirtualNode<Dom>

Converts to this type from the input type.
Source§

impl<Handle> From<VirtualNode<Handle>> for IterableNodes<Handle>
where Handle: RealDom,

Source§

fn from(other: VirtualNode<Handle>) -> IterableNodes<Handle>

Converts to this type from the input type.
Source§

impl<Dom> From<VirtualText> for VirtualNode<Dom>
where Dom: RealDom,

Source§

fn from(other: VirtualText) -> VirtualNode<Dom>

Converts to this type from the input type.
Source§

impl<Dom> Into<IntoIter<VirtualNode<Dom>>> for VirtualNode<Dom>
where Dom: RealDom,

Source§

fn into(self) -> IntoIter<VirtualNode<Dom>>

Converts this type into the (usually inferred) input type.
Source§

impl<Dom> IntoIterator for VirtualNode<Dom>
where Dom: RealDom,

Source§

type Item = VirtualNode<Dom>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<VirtualNode<Dom>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> <VirtualNode<Dom> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<Dom> PartialEq for VirtualNode<Dom>
where Dom: RealDom,

Source§

fn eq(&self, other: &VirtualNode<Dom>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<Dom> !Freeze for VirtualNode<Dom>

§

impl<Dom> !RefUnwindSafe for VirtualNode<Dom>

§

impl<Dom> !Send for VirtualNode<Dom>

§

impl<Dom> !Sync for VirtualNode<Dom>

§

impl<Dom> !UnwindSafe for VirtualNode<Dom>

§

impl<Dom> Unpin for VirtualNode<Dom>
where <Dom as RealDom>::EventCallback: Unpin,

§

impl<Dom> UnsafeUnpin for VirtualNode<Dom>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more