mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-27 14:59:27 +08:00
fix(core): address enableContentCheck
insertion bug (#5390)
This commit is contained in:
parent
52f717b3eb
commit
cc3497efd5
5
.changeset/tiny-walls-shave.md
Normal file
5
.changeset/tiny-walls-shave.md
Normal 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
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user