Struct yrs::types::xml::XmlElement [−][src]
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
Converts current XML node into a textual representation. This representation if flat, it doesn’t include any indentation.
A tag name of a current top-level XML node, eg. node <p></p>
has “p” as it’s tag name.
Removes an attribute recognized by an attr_name
from a current XML element.
pub 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.
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.
pub fn attributes<'a, 'b>(&'a self, txn: &'b Transaction) -> Attributes<'b>ⓘNotable traits for Attributes<'a>impl<'a> Iterator for Attributes<'a> type Item = (&'a str, String);
pub fn attributes<'a, 'b>(&'a self, txn: &'b Transaction) -> Attributes<'b>ⓘNotable traits for Attributes<'a>impl<'a> Iterator for Attributes<'a> type Item = (&'a str, String);
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.
Returns a next sibling of a current XML element, if any exists.
Returns a previous sibling of a current XML element, if any exists.
Returns a parent XML element, current node can be found within.
Returns None
, if current node is a root.
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.
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.
pub fn successors<'a, 'b>(&'a self, txn: &'b Transaction) -> TreeWalker<'b>ⓘNotable traits for TreeWalker<'a>impl<'a> Iterator for TreeWalker<'a> type Item = Xml;
pub fn successors<'a, 'b>(&'a self, txn: &'b Transaction) -> TreeWalker<'b>ⓘNotable traits for TreeWalker<'a>impl<'a> Iterator for TreeWalker<'a> type Item = Xml;
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(&txn) {
match node {
Xml::Element(elem) => println!("- {}", elem.tag()),
Xml::Text(txt) => println!("- {}", txt.to_string(&txn))
}
}
/* will print:
- UNDEFINED // (XML root element)
- p
- Hello
- b
- world
- again
*/
pub fn insert_elem<S: ToString>(
&self,
txn: &mut Transaction,
index: u32,
name: S
) -> XmlElement
pub fn insert_elem<S: ToString>(
&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.
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.
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.
Pushes a new XmlElement with a given tag name
as the last child of a current one and
returns it.
Pushes a new XmlElement with a given tag name
as the first child of a current one and
returns it.
Pushes a new XmlText field as the last child of a current XML element and returns it.
Pushes a new XmlText field as the first child of a current XML element and returns it.
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.
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.
Trait Implementations
Performs the conversion.
Performs the conversion.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
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
Mutably borrows from an owned value. Read more