From 1d978759f2ce07b724c4bae25659c8b496a520c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Sun, 29 Mar 2020 20:36:57 +0200 Subject: [PATCH] check for protected command names --- packages/tiptap-core/src/Editor.ts | 5 +++++ packages/tiptap-core/src/utils/getAllMethodNames.ts | 10 ++++++++++ 2 files changed, 15 insertions(+) create mode 100644 packages/tiptap-core/src/utils/getAllMethodNames.ts diff --git a/packages/tiptap-core/src/Editor.ts b/packages/tiptap-core/src/Editor.ts index f7f6233c..77995df2 100644 --- a/packages/tiptap-core/src/Editor.ts +++ b/packages/tiptap-core/src/Editor.ts @@ -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)) diff --git a/packages/tiptap-core/src/utils/getAllMethodNames.ts b/packages/tiptap-core/src/utils/getAllMethodNames.ts new file mode 100644 index 00000000..de23f7c3 --- /dev/null +++ b/packages/tiptap-core/src/utils/getAllMethodNames.ts @@ -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) +} \ No newline at end of file