Trait yrs::types::xml::XmlFragment

source ·
pub trait XmlFragment: AsRef<Branch> {
    // Provided methods
    fn first_child(&self) -> Option<XmlNode> { ... }
    fn len<T: ReadTxn>(&self, _txn: &T) -> u32 { ... }
    fn insert<V>(
        &self,
        txn: &mut TransactionMut<'_>,
        index: u32,
        xml_node: V
    ) -> V::Return
       where V: XmlPrelim { ... }
    fn push_back<V>(
        &self,
        txn: &mut TransactionMut<'_>,
        xml_node: V
    ) -> V::Return
       where V: XmlPrelim { ... }
    fn push_front<V>(
        &self,
        txn: &mut TransactionMut<'_>,
        xml_node: V
    ) -> V::Return
       where V: XmlPrelim { ... }
    fn remove(&self, txn: &mut TransactionMut<'_>, index: u32) { ... }
    fn remove_range(&self, txn: &mut TransactionMut<'_>, index: u32, len: u32) { ... }
    fn get<T: ReadTxn>(&self, _txn: &T, index: u32) -> Option<XmlNode> { ... }
    fn successors<'a, T: ReadTxn>(
        &'a self,
        txn: &'a T
    ) -> TreeWalker<'a, &'a T, T>  { ... }
}

Provided Methods§

source

fn first_child(&self) -> Option<XmlNode>

source

fn len<T: ReadTxn>(&self, _txn: &T) -> u32

Returns a number of elements stored in current array.

source

fn insert<V>( &self, txn: &mut TransactionMut<'_>, index: u32, xml_node: V ) -> V::Return
where V: XmlPrelim,

Inserts a value at the given index. Inserting at index 0 is equivalent to prepending current array with given value, while inserting at array length is equivalent to appending that value at the end of it.

Using index value that’s higher than current array length results in panic.

source

fn push_back<V>(&self, txn: &mut TransactionMut<'_>, xml_node: V) -> V::Return
where V: XmlPrelim,

Inserts given value at the end of the current array.

source

fn push_front<V>(&self, txn: &mut TransactionMut<'_>, xml_node: V) -> V::Return
where V: XmlPrelim,

Inserts given value at the beginning of the current array.

source

fn remove(&self, txn: &mut TransactionMut<'_>, index: u32)

Removes a single element at provided index.

source

fn remove_range(&self, txn: &mut TransactionMut<'_>, index: u32, len: u32)

Removes a range of elements from current array, starting at given index up until a particular number described by len has been deleted. This method panics in case when not all expected elements were removed (due to insufficient number of elements in an array) or index is outside of the bounds of an array.

source

fn get<T: ReadTxn>(&self, _txn: &T, index: u32) -> Option<XmlNode>

Retrieves a value stored at a given index. Returns None when provided index was out of the range of a current array.

source

fn successors<'a, T: ReadTxn>(&'a self, txn: &'a T) -> TreeWalker<'a, &'a T, T>

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::{Doc, Text, Xml, XmlNode, Transact, XmlFragment, XmlElementPrelim, XmlTextPrelim, GetString};

let doc = Doc::new();
let mut html = doc.get_or_insert_xml_fragment("div");
let mut txn = doc.transact_mut();
let p = html.push_back(&mut txn, XmlElementPrelim::empty("p"));
let txt = p.push_back(&mut txn, XmlTextPrelim::new("Hello "));
let b = p.push_back(&mut txn, XmlElementPrelim::empty("b"));
let txt = b.push_back(&mut txn, XmlTextPrelim::new("world"));
let txt = html.push_back(&mut txn, XmlTextPrelim::new("again"));

let mut result = Vec::new();
for node in html.successors(&txn) {
  let value = match node {
      XmlNode::Element(elem) => elem.tag().to_string(),
      XmlNode::Text(txt) => txt.get_string(&txn),
      _ => panic!("shouldn't be the case here")
  };
  result.push(value);
}
assert_eq!(result, vec![
  "p".to_string(),
  "Hello ".to_string(),
  "b".to_string(),
  "world".to_string(),
  "again".to_string()
]);

Object Safety§

This trait is not object safe.

Implementors§