tiptap-rusty-parser 0.3.1

Fast schema-agnostic parser and manipulator for Tiptap/ProseMirror JSONContent documents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Opt-in schema validation.
//!
//! The crate is schema-agnostic by default; nothing here runs unless you call
//! [`Node::validate`]. A [`Schema`] is an allow-list of node types, marks,
//! attributes, and child types. Validation collects *all* problems as
//! [`Violation`]s (each carrying the offending node's index path), so a single
//! pass reports everything wrong.
//!
//! A schema can be built in Rust or loaded from JSON:
//!
//! ```
//! use tiptap_rusty_parser::{Document, Schema, NodeSpec, MarkSpec};
//!
//! let schema = Schema::new()
//!     .node("doc", NodeSpec::new().content(["paragraph"]))
//!     .node("paragraph", NodeSpec::new().content(["text"]).marks(["bold"]))
//!     .node("text", NodeSpec::new())
//!     .mark("bold", MarkSpec::new());
//!
//! let doc = Document::from_json_str(
//!     r#"{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"hi"}]}]}"#,
//! ).unwrap();
//! assert!(doc.is_valid(&schema));
//! ```

use crate::content::{ContentExpr, ContentRule, ParseExprError};
use crate::node::Node;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt;

/// An allow-list schema: which node/mark types, attributes, and children are
/// permitted. Build with [`Schema::new`] or load with [`Schema::from_json_str`].
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Schema {
    /// Node type -> its spec. Types absent here are reported as unknown.
    #[serde(default)]
    pub nodes: HashMap<String, NodeSpec>,
    /// Mark type -> its spec. Marks absent here are reported as unknown.
    #[serde(default)]
    pub marks: HashMap<String, MarkSpec>,
}

/// Rules for one node type.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct NodeSpec {
    /// Allowed content: a set of child types (array form) or a content
    /// expression (string form). `None` = any child allowed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content: Option<ContentRule>,
    /// Groups this node belongs to (space-separated, ProseMirror-style), so
    /// content expressions can reference it by group name (e.g. `block`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub group: Option<String>,
    /// Allowed mark types on this node. `None` = any mark allowed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub marks: Option<HashSet<String>>,
    /// Allowed attribute keys. `None` = any attrs allowed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub attrs: Option<HashSet<String>>,
    /// Attribute keys that must be present.
    #[serde(default, skip_serializing_if = "HashSet::is_empty")]
    pub required_attrs: HashSet<String>,
}

/// Rules for one mark type.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct MarkSpec {
    /// Allowed attribute keys. `None` = any attrs allowed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub attrs: Option<HashSet<String>>,
    /// Attribute keys that must be present.
    #[serde(default, skip_serializing_if = "HashSet::is_empty")]
    pub required_attrs: HashSet<String>,
}

fn into_set<I, S>(items: I) -> HashSet<String>
where
    I: IntoIterator<Item = S>,
    S: Into<String>,
{
    items.into_iter().map(Into::into).collect()
}

impl Schema {
    /// An empty schema.
    pub fn new() -> Self {
        Self::default()
    }

    /// Register (or replace) a node type's spec.
    pub fn node(mut self, node_type: impl Into<String>, spec: NodeSpec) -> Self {
        self.nodes.insert(node_type.into(), spec);
        self
    }

    /// Register (or replace) a mark type's spec.
    pub fn mark(mut self, mark_type: impl Into<String>, spec: MarkSpec) -> Self {
        self.marks.insert(mark_type.into(), spec);
        self
    }

    /// Load a schema from its JSON definition.
    ///
    /// ```
    /// use tiptap_rusty_parser::Schema;
    /// let schema = Schema::from_json_str(r#"{
    ///   "nodes": { "doc": { "content": ["paragraph"] }, "paragraph": { "content": ["text"] }, "text": {} },
    ///   "marks": { "link": { "attrs": ["href"], "required_attrs": ["href"] } }
    /// }"#).unwrap();
    /// assert!(schema.nodes.contains_key("doc"));
    /// ```
    pub fn from_json_str(s: &str) -> crate::Result<Self> {
        Ok(serde_json::from_str(s)?)
    }
}

impl NodeSpec {
    /// An unrestricted node spec (any attrs/marks/children allowed).
    pub fn new() -> Self {
        Self::default()
    }

    /// Restrict allowed child node types (any count/order). For ordering and
    /// cardinality use [`content_match`](Self::content_match).
    pub fn content<I, S>(mut self, types: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.content = Some(ContentRule::Types(into_set(types)));
        self
    }

    /// Restrict content with a ProseMirror content expression (e.g.
    /// `"heading paragraph+"`). Panics on an invalid expression — use
    /// [`try_content_match`](Self::try_content_match) to handle the error.
    pub fn content_match(self, expr: &str) -> Self {
        self.try_content_match(expr)
            .expect("invalid content expression")
    }

    /// Fallible [`content_match`](Self::content_match).
    pub fn try_content_match(mut self, expr: &str) -> Result<Self, ParseExprError> {
        self.content = Some(ContentRule::Expr(ContentExpr::parse(expr)?));
        Ok(self)
    }

    /// Set the groups this node belongs to (space-separated, e.g. `"block"`).
    pub fn group(mut self, group: impl Into<String>) -> Self {
        self.group = Some(group.into());
        self
    }

    /// The allowed child-type set, if `content` is the array form (not an
    /// expression). Convenience accessor for the `content` field.
    pub fn content_types(&self) -> Option<&HashSet<String>> {
        match &self.content {
            Some(ContentRule::Types(set)) => Some(set),
            _ => None,
        }
    }

    /// Restrict allowed mark types on this node.
    pub fn marks<I, S>(mut self, types: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.marks = Some(into_set(types));
        self
    }

    /// Restrict allowed attribute keys.
    pub fn attrs<I, S>(mut self, keys: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.attrs = Some(into_set(keys));
        self
    }

    /// Set attribute keys that must be present.
    pub fn required_attrs<I, S>(mut self, keys: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.required_attrs = into_set(keys);
        self
    }
}

impl MarkSpec {
    /// An unrestricted mark spec (any attrs allowed).
    pub fn new() -> Self {
        Self::default()
    }

    /// Restrict allowed attribute keys.
    pub fn attrs<I, S>(mut self, keys: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.attrs = Some(into_set(keys));
        self
    }

    /// Set attribute keys that must be present.
    pub fn required_attrs<I, S>(mut self, keys: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.required_attrs = into_set(keys);
        self
    }
}

/// A single schema violation, located by the offending node's index path.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Violation {
    /// Index path to the node (root = `[]`), as used by [`Node::node_at`].
    pub path: Vec<usize>,
    /// What's wrong.
    pub kind: ViolationKind,
}

/// The kinds of schema violation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ViolationKind {
    /// A node had no `type`.
    MissingNodeType,
    /// A node's type is not in the schema.
    UnknownNodeType(String),
    /// A child type is not allowed under its parent.
    DisallowedChild { parent: String, child: String },
    /// A node's children don't satisfy its content expression.
    InvalidContent { parent: String, expr: String },
    /// A mark type is not in the schema.
    UnknownMark(String),
    /// A mark is registered but not allowed on this node type.
    DisallowedMark { node: String, mark: String },
    /// A required attribute is missing (on a node, or a mark of the node).
    MissingAttr { key: String },
    /// An attribute key is not in the allowed set.
    UnknownAttr { key: String },
}

impl fmt::Display for ViolationKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ViolationKind::MissingNodeType => write!(f, "node has no type"),
            ViolationKind::UnknownNodeType(t) => write!(f, "unknown node type `{t}`"),
            ViolationKind::DisallowedChild { parent, child } => {
                write!(f, "node type `{child}` not allowed inside `{parent}`")
            }
            ViolationKind::InvalidContent { parent, expr } => {
                write!(
                    f,
                    "children of `{parent}` do not match content expression `{expr}`"
                )
            }
            ViolationKind::UnknownMark(m) => write!(f, "unknown mark type `{m}`"),
            ViolationKind::DisallowedMark { node, mark } => {
                write!(f, "mark `{mark}` not allowed on `{node}`")
            }
            ViolationKind::MissingAttr { key } => write!(f, "missing required attribute `{key}`"),
            ViolationKind::UnknownAttr { key } => write!(f, "unknown attribute `{key}`"),
        }
    }
}

impl fmt::Display for Violation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "at {:?}: {}", self.path, self.kind)
    }
}

impl Node {
    /// Validate against `schema`, collecting every [`Violation`]. An empty
    /// result means the document is valid.
    ///
    /// ```
    /// use tiptap_rusty_parser::{Document, Schema, NodeSpec};
    /// let schema = Schema::new()
    ///     .node("doc", NodeSpec::new().content(["paragraph"]))
    ///     .node("paragraph", NodeSpec::new())
    ///     .node("heading", NodeSpec::new());
    /// let doc = Document::from_json_str(
    ///     r#"{"type":"doc","content":[{"type":"heading"}]}"#,
    /// ).unwrap();
    /// let v = doc.validate(&schema);
    /// assert_eq!(v.len(), 1); // heading is a known type, but not allowed as a child of doc
    /// ```
    pub fn validate(&self, schema: &Schema) -> Vec<Violation> {
        let mut out = Vec::new();
        let mut path = Vec::new();
        validate_node(self, schema, &mut path, &mut out);
        out
    }

    /// True if the document has no schema violations.
    ///
    /// ```
    /// use tiptap_rusty_parser::{Document, Schema, NodeSpec};
    /// let schema = Schema::new().node("doc", NodeSpec::new());
    /// let doc = Document::from_json_str(r#"{"type":"doc"}"#).unwrap();
    /// assert!(doc.is_valid(&schema));
    /// ```
    pub fn is_valid(&self, schema: &Schema) -> bool {
        self.validate(schema).is_empty()
    }
}

fn validate_node(node: &Node, schema: &Schema, path: &mut Vec<usize>, out: &mut Vec<Violation>) {
    let push = |out: &mut Vec<Violation>, path: &[usize], kind: ViolationKind| {
        out.push(Violation {
            path: path.to_vec(),
            kind,
        });
    };

    let spec = match &node.node_type {
        None => {
            push(out, path, ViolationKind::MissingNodeType);
            None
        }
        Some(t) => match schema.nodes.get(t) {
            Some(spec) => Some(spec),
            None => {
                push(out, path, ViolationKind::UnknownNodeType(t.clone()));
                None
            }
        },
    };

    if let Some(spec) = spec {
        // node attrs
        check_attrs(
            node.attrs.as_ref(),
            spec.attrs.as_ref(),
            &spec.required_attrs,
            path,
            out,
        );

        // marks
        if let Some(marks) = &node.marks {
            let node_type = node.node_type.as_deref().unwrap_or_default();
            for mark in marks {
                match schema.marks.get(&mark.mark_type) {
                    None => push(
                        out,
                        path,
                        ViolationKind::UnknownMark(mark.mark_type.clone()),
                    ),
                    Some(mark_spec) => {
                        if let Some(allowed) = &spec.marks {
                            if !allowed.contains(&mark.mark_type) {
                                push(
                                    out,
                                    path,
                                    ViolationKind::DisallowedMark {
                                        node: node_type.to_string(),
                                        mark: mark.mark_type.clone(),
                                    },
                                );
                            }
                        }
                        check_attrs(
                            mark.attrs.as_ref(),
                            mark_spec.attrs.as_ref(),
                            &mark_spec.required_attrs,
                            path,
                            out,
                        );
                    }
                }
            }
        }

        // content rules
        if let Some(rule) = &spec.content {
            let parent = node.node_type.as_deref().unwrap_or_default();
            match rule {
                // array form: per-child type membership (unchanged behavior)
                ContentRule::Types(allowed) => {
                    if let Some(children) = &node.content {
                        for child in children {
                            if let Some(ct) = &child.node_type {
                                if !allowed.contains(ct) {
                                    push(
                                        out,
                                        path,
                                        ViolationKind::DisallowedChild {
                                            parent: parent.to_string(),
                                            child: ct.clone(),
                                        },
                                    );
                                }
                            }
                        }
                    }
                }
                // expression form: cardinality + ordering over the child sequence
                ContentRule::Expr(expr) => {
                    let children = node.content.as_deref().unwrap_or(&[]);
                    if !expr.matches(children, schema) {
                        push(
                            out,
                            path,
                            ViolationKind::InvalidContent {
                                parent: parent.to_string(),
                                expr: expr.as_str().to_string(),
                            },
                        );
                    }
                }
            }
        }
    }

    // recurse
    if let Some(children) = &node.content {
        for (i, child) in children.iter().enumerate() {
            path.push(i);
            validate_node(child, schema, path, out);
            path.pop();
        }
    }
}

fn check_attrs(
    attrs: Option<&serde_json::Map<String, serde_json::Value>>,
    allowed: Option<&HashSet<String>>,
    required: &HashSet<String>,
    path: &[usize],
    out: &mut Vec<Violation>,
) {
    for key in required {
        let present = attrs.is_some_and(|m| m.contains_key(key));
        if !present {
            out.push(Violation {
                path: path.to_vec(),
                kind: ViolationKind::MissingAttr { key: key.clone() },
            });
        }
    }
    if let (Some(allowed), Some(attrs)) = (allowed, attrs) {
        for key in attrs.keys() {
            if !allowed.contains(key) {
                out.push(Violation {
                    path: path.to_vec(),
                    kind: ViolationKind::UnknownAttr { key: key.clone() },
                });
            }
        }
    }
}