#[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

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() {
    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
*/

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.

Unsubscribes a previously subscribed event callback identified by given subscription_id.

Trait Implementations

Converts this type into a mutable reference of the (usually inferred) input type.

Converts this type into a shared reference of the (usually inferred) input type.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Converts this type into the (usually inferred) input type.

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

Subscribe a callback f for all events emitted by this and nested collaborative types. Callback is accepting transaction which triggered that event and event itself, wrapped within an Event structure. Read more

Unobserves callback identified by subscription_id (which can be obtained by consuming Subscription using into cast). Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

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)

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.