#[repr(transparent)]pub struct XmlElement(_);
Expand description
XML element data type. It represents an XML node, which can contain key-value attributes (interpreted as strings) as well as other nested XML elements or rich text (represented by XmlText type).
In terms of conflict resolution, XmlElement uses following rules:
- Attribute updates use logical last-write-wins principle, meaning the past updates are automatically overridden and discarded by newer ones, while concurrent updates made by different peers are resolved into a single value using document id seniority to establish an order.
- Child node insertion uses sequencing rules from other Yrs collections - elements are inserted using interleave-resistant algorithm, where order of concurrent inserts at the same index is established using peer’s document id seniority.
Implementations
sourceimpl XmlElement
impl XmlElement
sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Converts current XML node into a textual representation. This representation if flat, it doesn’t include any indentation.
sourcepub fn tag(&self) -> &str
pub fn tag(&self) -> &str
A tag name of a current top-level XML node, eg. node <p></p>
has “p” as it’s tag name.
sourcepub fn remove_attribute<K: AsRef<str>>(
&self,
txn: &mut Transaction,
attr_name: &K
)
pub fn remove_attribute<K: AsRef<str>>(
&self,
txn: &mut Transaction,
attr_name: &K
)
Removes an attribute recognized by an attr_name
from a current XML element.
sourcepub fn insert_attribute<K: Into<Rc<str>>, V: AsRef<str>>(
&self,
txn: &mut Transaction,
attr_name: K,
attr_value: V
)
pub fn insert_attribute<K: Into<Rc<str>>, V: AsRef<str>>(
&self,
txn: &mut Transaction,
attr_name: K,
attr_value: V
)
Inserts an attribute entry into current XML element.
sourcepub fn get_attribute(&self, attr_name: &str) -> Option<String>
pub fn get_attribute(&self, attr_name: &str) -> Option<String>
Returns a value of an attribute given its attr_name
. Returns None
if no such attribute
can be found inside of a current XML element.
sourcepub fn attributes(&self) -> Attributes<'_>ⓘNotable traits for Attributes<'a>impl<'a> Iterator for Attributes<'a> type Item = (&'a str, String);
pub fn attributes(&self) -> Attributes<'_>ⓘNotable traits for Attributes<'a>impl<'a> Iterator for Attributes<'a> type Item = (&'a str, String);
Returns an unordered iterator over all attributes (key-value pairs), that can be found inside of a current XML element.
sourcepub fn next_sibling(&self) -> Option<Xml>
pub fn next_sibling(&self) -> Option<Xml>
Returns a next sibling of a current XML element, if any exists.
sourcepub fn prev_sibling(&self) -> Option<Xml>
pub fn prev_sibling(&self) -> Option<Xml>
Returns a previous sibling of a current XML element, if any exists.
sourcepub fn parent(&self) -> Option<XmlElement>
pub fn parent(&self) -> Option<XmlElement>
Returns a parent XML element, current node can be found within.
Returns None
, if current node is a root.
sourcepub fn first_child(&self) -> Option<Xml>
pub fn first_child(&self) -> Option<Xml>
Returns a first child XML node (either XmlElement or XmlText), that can be found in
a current XML element. Returns None
if current element is empty.
sourcepub fn len(&self) -> u32
pub fn len(&self) -> u32
Returns a number of child XML nodes, that can be found inside of a current XML element. This is a flat count - successor nodes (children of a children) are not counted.
sourcepub fn successors(&self) -> TreeWalker<'_>ⓘNotable traits for TreeWalker<'a>impl<'a> Iterator for TreeWalker<'a> type Item = Xml;
pub fn successors(&self) -> TreeWalker<'_>ⓘNotable traits for TreeWalker<'a>impl<'a> Iterator for TreeWalker<'a> type Item = Xml;
Returns an iterator that can be used to traverse over the successors of a current XML element. This includes recursive step over children of its children. The recursive iteration is depth-first.
Example:
/* construct node with a shape:
<div>
<p>Hello <b>world</b></p>
again
</div>
*/
use yrs::{XmlElement, Doc, Xml};
let doc = Doc::new();
let mut txn = doc.transact();
let mut html = txn.get_xml_element("div");
let p = html.push_elem_back(&mut txn, "p");
let txt = p.push_text_back(&mut txn);
txt.push(&mut txn, "Hello ");
let b = p.push_elem_back(&mut txn, "b");
let txt = b.push_text_back(&mut txn);
txt.push(&mut txn, "world");
let txt = html.push_text_back(&mut txn);
txt.push(&mut txn, "again");
for node in html.successors() {
match node {
Xml::Element(elem) => println!("- {}", elem.tag()),
Xml::Text(txt) => println!("- {}", txt.to_string())
}
}
/* will print:
- UNDEFINED // (XML root element)
- p
- Hello
- b
- world
- again
*/
sourcepub fn insert_elem<S: Into<Rc<str>>>(
&self,
txn: &mut Transaction,
index: u32,
name: S
) -> XmlElement
pub fn insert_elem<S: Into<Rc<str>>>(
&self,
txn: &mut Transaction,
index: u32,
name: S
) -> XmlElement
Inserts another XmlElement with a given tag name
into a current one at the given index
and returns it. If index
is equal to 0
, new element will be inserted as a first child.
If index
is equal to length of current XML element, new element will be inserted as a last
child.
This method will panic if index
is greater than the length of current XML element.
sourcepub fn insert_text(&self, txn: &mut Transaction, index: u32) -> XmlText
pub fn insert_text(&self, txn: &mut Transaction, index: u32) -> XmlText
Inserts a XmlText into a current XML element at the given index
and returns it.
If index
is equal to 0
, new text field will be inserted as a first child.
If index
is equal to length of current XML element, new text field will be inserted
as a last child.
This method will panic if index
is greater than the length of current XML element.
sourcepub fn remove_range(&self, txn: &mut Transaction, index: u32, len: u32)
pub fn remove_range(&self, txn: &mut Transaction, index: u32, len: u32)
Removes a range (defined by len
) of XML nodes from the current XML element, starting at
the given index
. Returns the result which may contain an error if a number of elements
removed is lesser than the expected one provided in len
parameter.
sourcepub fn push_elem_back<S: Into<Rc<str>>>(
&self,
txn: &mut Transaction,
name: S
) -> XmlElement
pub fn push_elem_back<S: Into<Rc<str>>>(
&self,
txn: &mut Transaction,
name: S
) -> XmlElement
Pushes a new XmlElement with a given tag name
as the last child of a current one and
returns it.
sourcepub fn push_elem_front<S: Into<Rc<str>>>(
&self,
txn: &mut Transaction,
name: S
) -> XmlElement
pub fn push_elem_front<S: Into<Rc<str>>>(
&self,
txn: &mut Transaction,
name: S
) -> XmlElement
Pushes a new XmlElement with a given tag name
as the first child of a current one and
returns it.
sourcepub fn push_text_back(&self, txn: &mut Transaction) -> XmlText
pub fn push_text_back(&self, txn: &mut Transaction) -> XmlText
Pushes a new XmlText field as the last child of a current XML element and returns it.
sourcepub fn push_text_front(&self, txn: &mut Transaction) -> XmlText
pub fn push_text_front(&self, txn: &mut Transaction) -> XmlText
Pushes a new XmlText field as the first child of a current XML element and returns it.
sourcepub fn get(&self, index: u32) -> Option<Xml>
pub fn get(&self, index: u32) -> Option<Xml>
Returns an XML node stored under a given index
of a current XML element.
Returns None
if provided index
is over the range of a current element.
sourcepub fn observe<F>(&mut self, f: F) -> Subscription<XmlEvent> where
F: Fn(&Transaction, &XmlEvent) + 'static,
pub fn observe<F>(&mut self, f: F) -> Subscription<XmlEvent> where
F: Fn(&Transaction, &XmlEvent) + 'static,
Subscribes a given callback to be triggered whenever current XML node is changed. A callback is triggered whenever a transaction gets committed. This function does not trigger if changes have been observed by nested shared collections.
Children node changes can be tracked by using [Event::delta] method. Attribute changes can be tracked by using [Event::keys] method.
Returns an [Observer] which, when dropped, will unsubscribe current callback.
sourcepub fn unobserve(&mut self, subscription_id: SubscriptionId)
pub fn unobserve(&mut self, subscription_id: SubscriptionId)
Unsubscribes a previously subscribed event callback identified by given subscription_id
.
Trait Implementations
sourceimpl AsMut<Branch> for XmlElement
impl AsMut<Branch> for XmlElement
sourceimpl AsRef<Branch> for XmlElement
impl AsRef<Branch> for XmlElement
sourceimpl Clone for XmlElement
impl Clone for XmlElement
sourcefn clone(&self) -> XmlElement
fn clone(&self) -> XmlElement
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for XmlElement
impl Debug for XmlElement
sourceimpl From<BranchPtr> for XmlElement
impl From<BranchPtr> for XmlElement
sourceimpl Into<XmlElement> for XmlFragment
impl Into<XmlElement> for XmlFragment
sourcefn into(self) -> XmlElement
fn into(self) -> XmlElement
Converts this type into the (usually inferred) input type.
sourceimpl PartialEq<XmlElement> for XmlElement
impl PartialEq<XmlElement> for XmlElement
sourcefn eq(&self, other: &XmlElement) -> bool
fn eq(&self, other: &XmlElement) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &XmlElement) -> bool
fn ne(&self, other: &XmlElement) -> bool
This method tests for !=
.
impl Eq for XmlElement
impl StructuralEq for XmlElement
impl StructuralPartialEq for XmlElement
Auto Trait Implementations
impl !RefUnwindSafe for XmlElement
impl !Send for XmlElement
impl !Sync for XmlElement
impl Unpin for XmlElement
impl !UnwindSafe for XmlElement
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> DeepObservable for T where
T: AsMut<Branch>,
impl<T> DeepObservable for T where
T: AsMut<Branch>,
sourcefn observe_deep<F>(&mut self, f: F) -> Subscription<Events> where
F: 'static + Fn(&Transaction, &Events),
fn observe_deep<F>(&mut self, f: F) -> Subscription<Events> where
F: 'static + Fn(&Transaction, &Events),
sourcefn unobserve_deep(&mut self, subscription_id: u32)
fn unobserve_deep(&mut self, subscription_id: u32)
Unobserves callback identified by subscription_id
(which can be obtained by consuming
Subscription using into
cast). Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more