fix(core): address enableContentCheck insertion bug (#5390)

This commit is contained in:
Nick Perez 2024-07-24 17:43:30 +02:00 committed by GitHub
parent 52f717b3eb
commit cc3497efd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 13 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---
Fixes a bug where if `enableContentCheck` was true, inserting content as JSON nodes would fail. This was because the node that was being created technically had a different schema than the content being inserted, so it would fail to generate the correct content value

View File

@ -58,13 +58,14 @@ export function createNodeFromContent(
}
if (isTextContent) {
let schemaToUse = schema
let hasInvalidContent = false
let invalidContent = ''
// Only ever check for invalid content if we're supposed to throw an error
// Check for invalid content
if (options.errorOnInvalidContent) {
schemaToUse = new Schema({
let hasInvalidContent = false
let invalidContent = ''
// A copy of the current schema with a catch-all node at the end
const contentCheckSchema = new Schema({
topNode: schema.spec.topNode,
marks: schema.spec.marks,
// Prosemirror's schemas are executed such that: the last to execute, matches last
@ -88,19 +89,26 @@ export function createNodeFromContent(
},
}),
})
if (options.slice) {
DOMParser.fromSchema(contentCheckSchema).parseSlice(elementFromString(content), options.parseOptions)
} else {
DOMParser.fromSchema(contentCheckSchema).parse(elementFromString(content), options.parseOptions)
}
if (options.errorOnInvalidContent && hasInvalidContent) {
throw new Error('[tiptap error]: Invalid HTML content', { cause: new Error(`Invalid element found: ${invalidContent}`) })
}
}
const parser = DOMParser.fromSchema(schemaToUse)
const parser = DOMParser.fromSchema(schema)
const response = options.slice
? parser.parseSlice(elementFromString(content), options.parseOptions).content
: parser.parse(elementFromString(content), options.parseOptions)
if (options.errorOnInvalidContent && hasInvalidContent) {
throw new Error('[tiptap error]: Invalid HTML content', { cause: new Error(`Invalid element found: ${invalidContent}`) })
if (options.slice) {
return parser.parseSlice(elementFromString(content), options.parseOptions).content
}
return response
return parser.parse(elementFromString(content), options.parseOptions)
}
return createNodeFromContent('', schema, options)