Trait perseus::GenericNode [−][src]
pub trait GenericNode: 'static + Debug + Clone + PartialEq<Self> + Eq + Hash {
Show 21 methods
fn element(tag: &str) -> Self;
fn text_node(text: &str) -> Self;
fn marker() -> Self;
fn set_attribute(&self, name: &str, value: &str);
fn remove_attribute(&self, name: &str);
fn set_class_name(&self, value: &str);
fn set_property(&self, name: &str, value: &JsValue);
fn remove_property(&self, name: &str);
fn append_child(&self, child: &Self);
fn first_child(&self) -> Option<Self>;
fn insert_child_before(
&self,
new_node: &Self,
reference_node: Option<&Self>
);
fn remove_child(&self, child: &Self);
fn replace_child(&self, old: &Self, new: &Self);
fn insert_sibling_before(&self, child: &Self);
fn parent_node(&self) -> Option<Self>;
fn next_sibling(&self) -> Option<Self>;
fn remove_self(&self);
fn event(&self, name: &str, handler: Box<dyn Fn(Event) + 'static, Global>);
fn update_inner_text(&self, text: &str);
fn dangerously_set_inner_html(&self, html: &str);
fn clone_node(&self) -> Self;
}
Expand description
Abstraction over a rendering backend.
You would probably use this trait as a trait bound when you want to accept any rendering
backend. For example, components are often generic over GenericNode
to be able to render to
different backends.
Note that components are NOT represented by GenericNode
. Instead, components are
disappearing, meaning that they are simply functions that generate GenericNode
s inside a
new reactive context. This means that using components add minimal overhead.
Sycamore ships with 2 rendering backends out of the box:
DomNode
- Rendering in the browser (to real DOM nodes).SsrNode
- Render to a static string (often on the server side for Server Side Rendering, aka. SSR).
To implement your own rendering backend, you will need to create a new struct which implements
GenericNode
.
Cloning
GenericNode
s should be cheaply cloneable (usually backed by a Rc
or other
reference counted container) and preserve reference equality.
Required methods
Create a marker (dummy) node. For DomNode
, this is implemented by creating an empty
comment node. This is used, for example, in Keyed
and
Indexed
for scenarios where you want to push a new item to the
end of the list. If the list is empty, a dummy node is needed to store the position of
the component.
fn set_attribute(&self, name: &str, value: &str)
fn set_attribute(&self, name: &str, value: &str)
Sets an attribute on a node.
fn remove_attribute(&self, name: &str)
fn remove_attribute(&self, name: &str)
Removes an attribute on a node.
fn set_class_name(&self, value: &str)
fn set_class_name(&self, value: &str)
Sets the class
attribute on a node.
This should have the same outcome as calling set_attribute("class", value)
.
For DomNode
, this sets the className
property directly which is about 2x faster (on
Chrome).
fn set_property(&self, name: &str, value: &JsValue)
fn set_property(&self, name: &str, value: &JsValue)
Sets a property on a node.
fn remove_property(&self, name: &str)
fn remove_property(&self, name: &str)
Removes a property on a node.
fn append_child(&self, child: &Self)
fn append_child(&self, child: &Self)
Appends a child to the node’s children.
fn first_child(&self) -> Option<Self>
fn first_child(&self) -> Option<Self>
Get the first child of the node.
fn insert_child_before(&self, new_node: &Self, reference_node: Option<&Self>)
fn insert_child_before(&self, new_node: &Self, reference_node: Option<&Self>)
Insert a new child node to this node’s children. If reference_node
is Some
, the child
will be inserted before the reference node. Else if None
, the child will be inserted
at the end.
fn remove_child(&self, child: &Self)
fn remove_child(&self, child: &Self)
Remove a child node from this node’s children.
fn replace_child(&self, old: &Self, new: &Self)
fn replace_child(&self, old: &Self, new: &Self)
Replace a child node from this node’s children with a new child node.
fn insert_sibling_before(&self, child: &Self)
fn insert_sibling_before(&self, child: &Self)
Insert a new node before this node.
fn parent_node(&self) -> Option<Self>
fn parent_node(&self) -> Option<Self>
Returns the parent node, or None
if detached.
fn next_sibling(&self) -> Option<Self>
fn next_sibling(&self) -> Option<Self>
Returns the next sibling, or None
if this node is the last sibling.
fn remove_self(&self)
fn remove_self(&self)
Remove this node from the tree.
Add a EventHandler
to the event name
.
fn update_inner_text(&self, text: &str)
fn update_inner_text(&self, text: &str)
Update inner text of the node. If the node has elements, all the elements are replaced with a new text node.
fn dangerously_set_inner_html(&self, html: &str)
fn dangerously_set_inner_html(&self, html: &str)
Updates the inner html of the node.
The html will not be parsed in non-browser environments. This means that accessing methods
such as first_child
will return None
.
fn clone_node(&self) -> Self
fn clone_node(&self) -> Self
Create a deep clone of the node.