check for protected command names
This commit is contained in:
@@ -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))
|
||||
|
||||
10
packages/tiptap-core/src/utils/getAllMethodNames.ts
Normal file
10
packages/tiptap-core/src/utils/getAllMethodNames.ts
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user