mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-08 01:53:04 +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) {
|
if (isTextContent) {
|
||||||
let schemaToUse = schema
|
|
||||||
|
// Check for invalid content
|
||||||
|
if (options.errorOnInvalidContent) {
|
||||||
let hasInvalidContent = false
|
let hasInvalidContent = false
|
||||||
let invalidContent = ''
|
let invalidContent = ''
|
||||||
|
|
||||||
// Only ever check for invalid content if we're supposed to throw an error
|
// A copy of the current schema with a catch-all node at the end
|
||||||
if (options.errorOnInvalidContent) {
|
const contentCheckSchema = new Schema({
|
||||||
schemaToUse = new Schema({
|
|
||||||
topNode: schema.spec.topNode,
|
topNode: schema.spec.topNode,
|
||||||
marks: schema.spec.marks,
|
marks: schema.spec.marks,
|
||||||
// Prosemirror's schemas are executed such that: the last to execute, matches last
|
// 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
const parser = DOMParser.fromSchema(schemaToUse)
|
|
||||||
|
|
||||||
const response = options.slice
|
|
||||||
? parser.parseSlice(elementFromString(content), options.parseOptions).content
|
|
||||||
: parser.parse(elementFromString(content), options.parseOptions)
|
|
||||||
|
|
||||||
if (options.errorOnInvalidContent && hasInvalidContent) {
|
if (options.errorOnInvalidContent && hasInvalidContent) {
|
||||||
throw new Error('[tiptap error]: Invalid HTML content', { cause: new Error(`Invalid element found: ${invalidContent}`) })
|
throw new Error('[tiptap error]: Invalid HTML content', { cause: new Error(`Invalid element found: ${invalidContent}`) })
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const parser = DOMParser.fromSchema(schema)
|
||||||
|
|
||||||
|
if (options.slice) {
|
||||||
|
return parser.parseSlice(elementFromString(content), options.parseOptions).content
|
||||||
|
}
|
||||||
|
|
||||||
|
return parser.parse(elementFromString(content), options.parseOptions)
|
||||||
|
|
||||||
return response
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return createNodeFromContent('', schema, options)
|
return createNodeFromContent('', schema, options)
|
||||||
|
Loading…
Reference in New Issue
Block a user