Преглед на файлове

Check for invalid widget factory name.

Ian Rose преди 5 години
родител
ревизия
9b5a715791
променени са 2 файла, в които са добавени 26 реда и са изтрити 1 реда
  1. 5 1
      packages/docregistry/src/registry.ts
  2. 21 0
      tests/test-docregistry/src/registry.spec.ts

+ 5 - 1
packages/docregistry/src/registry.ts

@@ -104,15 +104,19 @@ export class DocumentRegistry implements IDisposable {
    * @returns A disposable which will unregister the factory.
    *
    * #### Notes
-   * If a factory with the given `'displayName'` is already registered,
+   * If a factory with the given `'name'` is already registered,
    * a warning will be logged, and this will be a no-op.
    * If `'*'` is given as a default extension, the factory will be registered
    * as the global default.
    * If an extension or global default is already registered, this factory
    * will override the existing default.
+   * The factory cannot be named an empty string or the string `'default'`.
    */
   addWidgetFactory(factory: DocumentRegistry.WidgetFactory): IDisposable {
     let name = factory.name.toLowerCase();
+    if (!name || name === 'default') {
+      throw Error('Invalid factory name');
+    }
     if (this._widgetFactories[name]) {
       console.warn(`Duplicate registered factory ${name}`);
       return new DisposableDelegate(Private.noOp);

+ 21 - 0
tests/test-docregistry/src/registry.spec.ts

@@ -138,6 +138,27 @@ describe('docregistry/registry', () => {
         disposable.dispose();
         expect(registry.getWidgetFactory('test')).to.be.undefined;
       });
+
+      it('should throw for an invalid factory name', () => {
+        expect(() => {
+          registry.addWidgetFactory(
+            new WidgetFactory({
+              name: 'default',
+              fileTypes: [],
+              defaultFor: []
+            })
+          );
+        }).to.throw(/Invalid/);
+        expect(() => {
+          registry.addWidgetFactory(
+            new WidgetFactory({
+              name: '',
+              fileTypes: [],
+              defaultFor: []
+            })
+          );
+        }).to.throw(/Invalid/);
+      });
     });
 
     describe('#addModelFactory()', () => {