improved error handling for invalid content
This commit is contained in:
@@ -32,6 +32,9 @@ module.exports = {
|
|||||||
// required semicolons
|
// required semicolons
|
||||||
'semi': ['error', 'never'],
|
'semi': ['error', 'never'],
|
||||||
|
|
||||||
|
// error handling
|
||||||
|
'no-console': ['error', { allow: ['warn', 'error'] }],
|
||||||
|
|
||||||
// indent
|
// indent
|
||||||
'no-tabs': 'off',
|
'no-tabs': 'off',
|
||||||
'indent': 'off',
|
'indent': 'off',
|
||||||
|
|||||||
@@ -40,6 +40,12 @@ export default class Editor {
|
|||||||
editable: true,
|
editable: true,
|
||||||
extensions: [],
|
extensions: [],
|
||||||
content: '',
|
content: '',
|
||||||
|
emptyDocument: {
|
||||||
|
type: 'doc',
|
||||||
|
content: [{
|
||||||
|
type: 'paragraph',
|
||||||
|
}],
|
||||||
|
},
|
||||||
onInit: () => {},
|
onInit: () => {},
|
||||||
onUpdate: () => {},
|
onUpdate: () => {},
|
||||||
onFocus: () => {},
|
onFocus: () => {},
|
||||||
@@ -125,7 +131,12 @@ export default class Editor {
|
|||||||
|
|
||||||
createDocument(content) {
|
createDocument(content) {
|
||||||
if (typeof content === 'object') {
|
if (typeof content === 'object') {
|
||||||
return this.schema.nodeFromJSON(content)
|
try {
|
||||||
|
return this.schema.nodeFromJSON(content)
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('[tiptap warn]: Invalid content.', 'Passed value:', content, 'Error:', error)
|
||||||
|
return this.schema.nodeFromJSON(this.options.emptyDocument)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof content === 'string') {
|
if (typeof content === 'string') {
|
||||||
@@ -263,12 +274,7 @@ export default class Editor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clearContent(emitUpdate = false) {
|
clearContent(emitUpdate = false) {
|
||||||
this.setContent({
|
this.setContent(this.options.emptyDocument, emitUpdate)
|
||||||
type: 'doc',
|
|
||||||
content: [{
|
|
||||||
type: 'paragraph',
|
|
||||||
}],
|
|
||||||
}, emitUpdate)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setActiveNodesAndMarks() {
|
setActiveNodesAndMarks() {
|
||||||
|
|||||||
@@ -25,6 +25,38 @@ test('create editor', () => {
|
|||||||
expect(editor).toBeDefined()
|
expect(editor).toBeDefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('check invalid content (JSON)', () => {
|
||||||
|
const editor = new Editor({
|
||||||
|
content: { thisIsNotAValidDocument: true },
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(editor.getHTML()).toEqual('<p></p>')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('check invalid content (HTML)', () => {
|
||||||
|
const editor = new Editor({
|
||||||
|
content: '</>',
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(editor.getHTML()).toEqual('<p></p>')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('check invalid content (unsupported format: Function)', () => {
|
||||||
|
const editor = new Editor({
|
||||||
|
content: () => false,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(editor.getHTML()).toEqual('<p></p>')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('check invalid content (unsupported format: Array)', () => {
|
||||||
|
const editor = new Editor({
|
||||||
|
content: [],
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(editor.getHTML()).toEqual('<p></p>')
|
||||||
|
})
|
||||||
|
|
||||||
test('set HTML, get HTML', () => {
|
test('set HTML, get HTML', () => {
|
||||||
const content = '<p>Lorem <strong>ipsum</strong> dolor sit amet.</p>'
|
const content = '<p>Lorem <strong>ipsum</strong> dolor sit amet.</p>'
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user