Skip to main content

Document

Struct Document 

Source
pub struct Document { /* private fields */ }
Expand description

An owned Tiptap document (its root node, usually type: "doc").

Derefs to the root Node, so all query/mutation methods are available directly on a Document.

Implementations§

Source§

impl Document

Source

pub fn new(root: Node) -> Self

Wrap an existing root node.

Source

pub fn from_json_str(s: &str) -> Result<Self>

Parse from a JSON string.

use tiptap_rusty_parser::Document;
let d = Document::from_json_str(r#"{"type":"doc","content":[]}"#).unwrap();
assert_eq!(d.node_type.as_deref(), Some("doc"));
Source

pub fn from_value(value: Value) -> Result<Self>

Parse from a serde_json::Value.

Source

pub fn from_reader(reader: impl Read) -> Result<Self>

Parse from any reader.

Source

pub fn to_json_str(&self) -> Result<String>

Serialize to a compact JSON string.

Source

pub fn to_string_pretty(&self) -> Result<String>

Serialize to a pretty JSON string.

Source

pub fn to_value(&self) -> Result<Value>

Serialize to a serde_json::Value.

Source

pub fn root(&self) -> &Node

Borrow the root node.

Source

pub fn root_mut(&mut self) -> &mut Node

Mutably borrow the root node.

Source

pub fn into_root(self) -> Node

Consume into the root node.

Methods from Deref<Target = Node>§

Source

pub fn has_mark(&self, mark_type: &str) -> bool

True if a mark of mark_type is present.

Source

pub fn get_mark(&self, mark_type: &str) -> Option<&Mark>

Reference to the first mark of mark_type.

Source

pub fn add_mark(&mut self, mark: Mark) -> bool

Add mark if no mark of that type exists yet. Returns true if added.

Source

pub fn remove_mark(&mut self, mark_type: &str) -> usize

Remove every mark of mark_type. Returns count removed.

Source

pub fn toggle_mark(&mut self, mark: Mark) -> bool

Toggle a mark: remove if present, else add. Returns true if now present.

Source

pub fn set_mark_attr( &mut self, mark_type: &str, key: impl Into<String>, value: impl Into<Value>, ) -> bool

Set an attr on the (first) mark of mark_type. Returns false if absent.

Source

pub fn clear_marks(&mut self)

Drop all marks.

Source

pub fn attr(&self, key: &str) -> Option<&Value>

Reference to attr key.

Source

pub fn attrs_mut(&mut self) -> &mut Map<String, Value>

Mutable access to the attrs map, creating it if absent.

Source

pub fn set_attr( &mut self, key: impl Into<String>, value: impl Into<Value>, ) -> Option<Value>

Set attr key, returning the previous value if any.

Source

pub fn remove_attr(&mut self, key: &str) -> Option<Value>

Remove attr key, returning its value if present.

Source

pub fn child_count(&self) -> usize

Number of direct children.

Source

pub fn children(&self) -> &[Node]

Direct children slice (empty if none).

Source

pub fn children_mut(&mut self) -> &mut Vec<Node>

Mutable access to children, creating the vec if absent.

Source

pub fn child(&self, i: usize) -> Option<&Node>

Direct child at i.

Source

pub fn child_mut(&mut self, i: usize) -> Option<&mut Node>

Mutable direct child at i.

Source

pub fn push_child(&mut self, node: Node)

Append a child.

Source

pub fn insert_child(&mut self, i: usize, node: Node)

Insert a child at i (clamped to len).

Source

pub fn remove_child(&mut self, i: usize) -> Option<Node>

Remove and return the child at i.

Source

pub fn replace_child(&mut self, i: usize, node: Node) -> Option<Node>

Replace the child at i, returning the old node.

Source

pub fn clear_children(&mut self)

Remove all children.

Source

pub fn retain_children(&mut self, pred: impl FnMut(&Node) -> bool)

Retain only children matching pred.

Source

pub fn get_text(&self) -> Option<&str>

Text payload, if any.

Source

pub fn set_text(&mut self, text: impl Into<String>)

Set text payload.

Source

pub fn replace_all( &mut self, pred: impl FnMut(&Node) -> bool, f: impl FnMut(&mut Node), ) -> usize

Apply f to every node (incl. self) matching pred. Returns count.

Source

pub fn walk(&self, f: &mut impl FnMut(&Node))

Visit self and every descendant, pre-order.

use tiptap_rusty_parser::Document;
let doc = Document::from_json_str(r#"{"type":"doc","content":[{"type":"paragraph"}]}"#).unwrap();
let mut n = 0;
doc.root().walk(&mut |_| n += 1);
assert_eq!(n, 2);
Source

pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Node))

Mutable pre-order visit of self and every descendant.

Source

pub fn find(&self, pred: impl FnMut(&Node) -> bool) -> Option<&Node>

First node (incl. self) matching pred, pre-order.

Source

pub fn find_mut(&mut self, pred: impl FnMut(&Node) -> bool) -> Option<&mut Node>

Mutable variant of Node::find.

Source

pub fn find_all(&self, pred: impl FnMut(&Node) -> bool) -> Vec<&Node>

All nodes (incl. self) matching pred, pre-order.

Source

pub fn find_all_mut( &mut self, pred: &mut impl FnMut(&Node) -> bool, ) -> Vec<&mut Node>

Mutable variant of Node::find_all. Collects matching &mut Node.

Source

pub fn descendants(&self) -> Descendants<'_>

Lazy pre-order iterator over self and all descendants.

Trait Implementations§

Source§

impl Clone for Document

Source§

fn clone(&self) -> Document

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Document

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for Document

Source§

type Target = Node

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Node

Dereferences the value.
Source§

impl DerefMut for Document

Source§

fn deref_mut(&mut self) -> &mut Node

Mutably dereferences the value.
Source§

impl From<Node> for Document

Source§

fn from(root: Node) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Document

Source§

fn eq(&self, other: &Document) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Document

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.