Skip to content

Commit cc3497e

Browse files
authoredJul 24, 2024··
fix(core): address enableContentCheck insertion bug (#5390)
1 parent 52f717b commit cc3497e

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed
 

‎.changeset/tiny-walls-shave.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tiptap/core": patch
3+
---
4+
5+
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

‎packages/core/src/helpers/createNodeFromContent.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ export function createNodeFromContent(
5858
}
5959

6060
if (isTextContent) {
61-
let schemaToUse = schema
62-
let hasInvalidContent = false
63-
let invalidContent = ''
6461

65-
// Only ever check for invalid content if we're supposed to throw an error
62+
// Check for invalid content
6663
if (options.errorOnInvalidContent) {
67-
schemaToUse = new Schema({
64+
let hasInvalidContent = false
65+
let invalidContent = ''
66+
67+
// A copy of the current schema with a catch-all node at the end
68+
const contentCheckSchema = new Schema({
6869
topNode: schema.spec.topNode,
6970
marks: schema.spec.marks,
7071
// Prosemirror's schemas are executed such that: the last to execute, matches last
@@ -88,19 +89,26 @@ export function createNodeFromContent(
8889
},
8990
}),
9091
})
91-
}
9292

93-
const parser = DOMParser.fromSchema(schemaToUse)
93+
if (options.slice) {
94+
DOMParser.fromSchema(contentCheckSchema).parseSlice(elementFromString(content), options.parseOptions)
95+
} else {
96+
DOMParser.fromSchema(contentCheckSchema).parse(elementFromString(content), options.parseOptions)
97+
}
9498

95-
const response = options.slice
96-
? parser.parseSlice(elementFromString(content), options.parseOptions).content
97-
: parser.parse(elementFromString(content), options.parseOptions)
99+
if (options.errorOnInvalidContent && hasInvalidContent) {
100+
throw new Error('[tiptap error]: Invalid HTML content', { cause: new Error(`Invalid element found: ${invalidContent}`) })
101+
}
102+
}
103+
104+
const parser = DOMParser.fromSchema(schema)
98105

99-
if (options.errorOnInvalidContent && hasInvalidContent) {
100-
throw new Error('[tiptap error]: Invalid HTML content', { cause: new Error(`Invalid element found: ${invalidContent}`) })
106+
if (options.slice) {
107+
return parser.parseSlice(elementFromString(content), options.parseOptions).content
101108
}
102109

103-
return response
110+
return parser.parse(elementFromString(content), options.parseOptions)
111+
104112
}
105113

106114
return createNodeFromContent('', schema, options)

0 commit comments

Comments
 (0)
Please sign in to comment.