Repository navigation
y-tiptap 3.0.10: yXmlFragmentToProseMirrorRootNode and initProseMirrorDoc throw on an empty fragment #8549
Replies: 2 comments 3 replies
|
Not a maintainer, so I can't answer 1 or 2, but here's how it got in: d9a55b5 is the suggested fix from a CodeRabbit review comment on #8475 (thread), applied as-is to both functions. The bot flagged that, because Until a maintainer weighs in, guarding on the fragment length works on both 3.0.9 and 3.0.10. I'd use it instead of try/catch, because non-empty content that doesn't fit the schema still throws: const toDoc = (fragment, schema) =>
fragment.length === 0
? schema.topNodeType.createAndFill()
: yXmlFragmentToProseMirrorRootNode(fragment, schema)For |
|
The empty-fragment guard is still needed if you want a valid headless document. I ran the example against the published 3.0.10, 3.2.0 and 3.3.0 packages using Codex. Both helpers still threw for an empty fragment and a I found one additional difference after the schema-repair change. On 3.3.0, a fragment containing only an unknown node lost that node before the converter threw on the now-empty document. The original fragment went from length 1 to 0. For a read-only preview or export, I’d run the conversion on a separate Y.Doc so it cannot edit the shared document: function previewDoc(ydoc: Y.Doc, field: string, schema: Schema) {
const snapshot = new Y.Doc();
try {
Y.applyUpdate(snapshot, Y.encodeStateAsUpdate(ydoc));
const fragment = snapshot.getXmlFragment(field);
const doc = fragment.length === 0
? schema.topNodeType.createAndFill()
: yXmlFragmentToProseMirrorRootNode(fragment, schema);
if (!doc) throw new Error('Cannot fill the schema’s top node');
doc.check();
return doc;
} finally {
snapshot.destroy();
}
}With 3.3.0, that returned a valid |
Uh oh!
There was an error while loading. Please reload this page.
Package:
@tiptap/y-tiptap3.0.10, with yjs 13.6.33 and prosemirror-model 1.25.12. 3.0.9 is not affected. Not browser-specific.Since 3.0.10,
yXmlFragmentToProseMirrorRootNodeandinitProseMirrorDocthrowRangeError: Invalid content for node doc: <>for an emptyY.XmlFragmentwhen the schema's doc node needs a child (block+, as in the basic schema and StarterKit). In 3.0.9 both returned an emptydocnode.An empty fragment is the normal state of a new
Y.Docbefore anything is written, so any headless code that converts a new document now throws. The editor itself is fine, because the sync plugin applies content withtr.replaceand keeps its placeholder document.I see this change is intentional (the 3.0.10 changelog says "Headless document creation rejects empty content when the schema requires child nodes"), so I have two questions:
schema.topNodeType.createAndFill(), the same one the editor uses) when the fragment is empty, and keeps the error for non-empty content that does not fit the schema? I am happy to open it.Reproduction (Node 24, plain
node repro.mjs):yXmlFragmentToProseMirrorRootNodedocRangeErrorinitProseMirrorDocdocRangeErroryDocToProsemirrordocdocOutput
Expected behavior
An empty fragment converts to the schema's placeholder document (
doc(paragraph)here), as the editor shows for an emptyY.Doc. Content that is not empty and does not fit the schema can still throw.I agree that the 3.0.9 result was not ideal: it was an empty
docthat failsnode.check()for ablock+schema.createAndFill()would give a valid document instead of an error.Context
Where it happens (
mainat ecde2bc, same lines onrelease/v3at 6e2e5ed):yXmlFragmentToProseMirrorRootNode.tsL12-L14initProseMirrorDoc.tsL19-L21validateDocumentContent.tsL4-L5: it skips empty content with the comment "Empty shared fragments use the editor's placeholder document", but thecreateCheckedcall right after it then rejects the same empty content.The change from
createtocreateCheckedcame in commit d9a55b5 ("fix(y-tiptap): validate converted document content") in #8475, released in 3.0.10 through #8482. Its test "rejects empty content when the schema requires a block" covers this case. The other goal of that commit, rejecting non-empty content that does not fit the schema, would stay as it is.yDocToProsemirrorandyXmlFragmentToProsemirrorgo throughNode.fromJSONand still return an emptydocin 3.0.10, so the public conversions now disagree on the same input.All reactions