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.

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.

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.

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
*/

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.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.