check for protected command names

This commit is contained in:
Philipp Kühn
2020-03-29 20:36:57 +02:00
parent cd4670fb67
commit 1d978759f2
2 changed files with 15 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ import { gapCursor } from 'prosemirror-gapcursor'
import magicMethods from './utils/magicMethods'
import elementFromString from './utils/elementFromString'
import injectCSS from './utils/injectCSS'
import getAllMethodNames from './utils/getAllMethodNames'
import ExtensionManager from './ExtensionManager'
import Extension from './Extension'
import Node from './Node'
@@ -76,6 +77,10 @@ export class Editor extends EventEmitter {
if (this.commands[name]) {
throw new Error(`tiptap: command '${name}' is already defined.`)
}
if (getAllMethodNames(this).includes(name)) {
throw new Error(`tiptap: '${name}' is a protected name.`)
}
this.commands[name] = this.chainCommand((...args: any) => {
return new Promise(resolve => callback(resolve, this, ...args))

View File

@@ -0,0 +1,10 @@
export default function getAllMethodNames(obj: Object) {
let methods = new Set()
while (obj = Reflect.getPrototypeOf(obj)) {
let keys = Reflect.ownKeys(obj)
keys.forEach((k) => methods.add(k))
}
return Array.from(methods)
}