Pārlūkot izejas kodu

Merge pull request #2260 from blink1073/notebook-api-cleanup

Move trust to notebook actions
Brian E. Granger 8 gadi atpakaļ
vecāks
revīzija
44c457055e

+ 2 - 3
packages/notebook-extension/src/index.ts

@@ -24,8 +24,7 @@ import {
 
 import {
   CellTools, ICellTools, INotebookTracker, NotebookActions,
-  NotebookModelFactory,  NotebookPanel, NotebookTracker, NotebookWidgetFactory,
-  trustNotebook
+  NotebookModelFactory,  NotebookPanel, NotebookTracker, NotebookWidgetFactory
 } from '@jupyterlab/notebook';
 
 import {
@@ -536,7 +535,7 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
       if (!current) {
         return;
       }
-      return trustNotebook(current.context.model).then(() => {
+      return NotebookActions.trust(current.notebook).then(() => {
         return current.context.save();
       });
     },

+ 50 - 2
packages/notebook/src/actions.ts

@@ -6,7 +6,7 @@ import {
 } from '@jupyterlab/services';
 
 import {
-  IClientSession, Clipboard
+  IClientSession, Clipboard, Dialog, showDialog
 } from '@jupyterlab/apputils';
 
 import {
@@ -31,6 +31,10 @@ import {
 } from './widget';
 
 
+// The message to display to the user when prompting to trust the notebook.
+const TRUST_MESSAGE = '<p>A trusted Jupyter notebook may execute hidden malicious code when you open it.<br>Selecting trust will re-render this notebook in a trusted state.<br>For more information, see the <a href="http://ipython.org/ipython-doc/2/notebook/security.html">Jupyter security documentation</a>.</p>';
+
+
 /**
  * The mimetype used for Jupyter cell data.
  */
@@ -402,7 +406,7 @@ namespace NotebookActions {
    * The cell insert can be undone.
    */
   export
-  function runAndInsert(widget: Notebook, session: IClientSession): Promise<boolean> {
+  function runAndInsert(widget: Notebook, session?: IClientSession): Promise<boolean> {
     if (!widget.model || !widget.activeCell) {
       return Promise.resolve(false);
     }
@@ -811,6 +815,50 @@ namespace NotebookActions {
     Private.changeCellType(widget, 'markdown');
     Private.handleState(widget, state);
   }
+
+  /**
+   * Trust the notebook after prompting the user.
+   *
+   * @param widget - The target notebook widget.
+   *
+   * @returns a promise that resolves when the transaction is finished.
+   *
+   * #### Notes
+   * No dialog will be presented if the notebook is already trusted.
+   */
+  export
+  function trust(widget: Notebook): Promise<void> {
+    if (!widget.model) {
+      return Promise.resolve(void 0);
+    }
+    // Do nothing if already trusted.
+    let cells = widget.model.cells;
+    let trusted = true;
+    for (let i = 0; i < cells.length; i++) {
+      let cell = cells.at(i);
+      if (!cell.trusted) {
+        trusted = false;
+      }
+    }
+    if (trusted) {
+      return showDialog({
+        body: 'Notebook is already trusted',
+        buttons: [Dialog.okButton()]
+      }).then(() => void 0);
+    }
+    return showDialog({
+      body: TRUST_MESSAGE,
+      title: 'Trust this notebook?',
+      buttons: [Dialog.cancelButton(), Dialog.warnButton()]
+    }).then(result => {
+      if (result.accept) {
+        for (let i = 0; i < cells.length; i++) {
+          let cell = cells.at(i);
+          cell.trusted = true;
+        }
+      }
+    });
+  }
 }
 
 

+ 0 - 1
packages/notebook/src/index.ts

@@ -8,6 +8,5 @@ export * from './model';
 export * from './modelfactory';
 export * from './panel';
 export * from './tracker';
-export * from './trust';
 export * from './widget';
 export * from './widgetfactory';

+ 0 - 61
packages/notebook/src/trust.ts

@@ -1,61 +0,0 @@
-// Copyright (c) Jupyter Development Team.
-// Distributed under the terms of the Modified BSD License.
-
-import {
-  Dialog, showDialog
-} from '@jupyterlab/apputils';
-
-import {
-  INotebookModel
-} from './model';
-
-
-// The message to display to the user when prompting to trust the notebook.
-const TRUST_MESSAGE = '<p>A trusted Jupyter notebook may execute hidden malicious code when you open it.<br>Selecting trust will re-render this notebook in a trusted state.<br>For more information, see the <a href="http://ipython.org/ipython-doc/2/notebook/security.html">Jupyter security documentation</a>.</p>';
-
-
-/**
- * Trust the notebook after prompting the user.
- *
- * @param model - The notebook model.
- *
- * @param host - The host node for the confirmation dialog (defaults to body).
- *
- * @returns a promise that resolves when the transaction is finished.
- *
- * #### Notes
- * No dialog will be presented if the notebook is already trusted.
- */
-export
-function trustNotebook(model: INotebookModel, host?: HTMLElement): Promise<void> {
-  if (!model) {
-    return Promise.resolve(void 0);
-  }
-  // Do nothing if already trusted.
-  let cells = model.cells;
-  let trusted = true;
-  for (let i = 0; i < cells.length; i++) {
-    let cell = cells.at(i);
-    if (!cell.trusted) {
-      trusted = false;
-    }
-  }
-  if (trusted) {
-    return showDialog({
-      body: 'Notebook is already trusted',
-      buttons: [Dialog.okButton()]
-    }).then(() => void 0);
-  }
-  return showDialog({
-    body: TRUST_MESSAGE,
-    title: 'Trust this notebook?',
-    buttons: [Dialog.cancelButton(), Dialog.warnButton()]
-  }).then(result => {
-    if (result.accept) {
-      for (let i = 0; i < cells.length; i++) {
-        let cell = cells.at(i);
-        cell.trusted = true;
-      }
-    }
-  });
-}

+ 0 - 1
test/src/index.ts

@@ -81,7 +81,6 @@ import './notebook/model.spec';
 import './notebook/modelfactory.spec';
 import './notebook/panel.spec';
 import './notebook/tracker.spec';
-import './notebook/trust.spec';
 import './notebook/widget.spec';
 import './notebook/widgetfactory.spec';
 

+ 46 - 1
test/src/notebook/actions.spec.ts

@@ -28,7 +28,7 @@ import {
 } from '@jupyterlab/notebook';
 
 import {
-  createClientSession
+  acceptDialog, createClientSession, dismissDialog
 } from '../utils';
 
 import {
@@ -1415,6 +1415,51 @@ describe('@jupyterlab/notebook', () => {
 
     });
 
+    describe('#trust()', () => {
+
+      it('should trust the notebook cells if the user accepts', (done) => {
+        let model = widget.model;
+        widget.model.fromJSON(DEFAULT_CONTENT);
+        let cell = model.cells.at(0);
+        expect(cell.trusted).to.not.be(true);
+        NotebookActions.trust(widget).then(() => {
+          expect(cell.trusted).to.be(true);
+          done();
+        });
+        acceptDialog();
+      });
+
+      it('should not trust the notebook cells if the user aborts', (done) => {
+        let model = widget.model;
+        model.fromJSON(DEFAULT_CONTENT);
+        let cell = model.cells.at(0);
+        expect(cell.trusted).to.not.be(true);
+        NotebookActions.trust(widget).then(() => {
+          expect(cell.trusted).to.not.be(true);
+          done();
+        });
+        dismissDialog();
+      });
+
+      it('should bail if the model is `null`', (done) => {
+        widget.model = null;
+        NotebookActions.trust(widget).then(() => { done(); });
+      });
+
+      it('should show a dialog if all cells are trusted', (done) => {
+        let model = widget.model;
+        widget.model.fromJSON(DEFAULT_CONTENT);
+        model.fromJSON(DEFAULT_CONTENT);
+        for (let i = 0; i < model.cells.length; i++) {
+          let cell = model.cells.at(i);
+          cell.trusted = true;
+        }
+        NotebookActions.trust(widget).then(() => { done(); });
+        acceptDialog();
+      });
+
+    });
+
   });
 
 });

+ 0 - 68
test/src/notebook/trust.spec.ts

@@ -1,68 +0,0 @@
-// Copyright (c) Jupyter Development Team.
-// Distributed under the terms of the Modified BSD License.
-
-import expect = require('expect.js');
-
-import {
- NotebookModel
-} from '@jupyterlab/notebook';
-
-import {
-  trustNotebook
-} from '@jupyterlab/notebook';
-
-import {
-  acceptDialog, dismissDialog
-} from '../utils';
-
-import {
-  DEFAULT_CONTENT
-} from './utils';
-
-
-describe('notebook/notebook/trust', () => {
-
-  describe('#trustNotebook()', () => {
-
-    it('should trust the notebook cells if the user accepts', (done) => {
-      let model = new NotebookModel();
-      model.fromJSON(DEFAULT_CONTENT);
-      let cell = model.cells.at(0);
-      expect(cell.trusted).to.not.be(true);
-      trustNotebook(model).then(() => {
-        expect(cell.trusted).to.be(true);
-        done();
-      });
-      acceptDialog();
-    });
-
-    it('should not trust the notebook cells if the user aborts', (done) => {
-      let model = new NotebookModel();
-      model.fromJSON(DEFAULT_CONTENT);
-      let cell = model.cells.at(0);
-      expect(cell.trusted).to.not.be(true);
-      trustNotebook(model).then(() => {
-        expect(cell.trusted).to.not.be(true);
-        done();
-      });
-      dismissDialog();
-    });
-
-    it('should bail if the model is `null`', (done) => {
-      trustNotebook(null).then(() => { done(); });
-    });
-
-    it('should show a dialog if all cells are trusted', (done) => {
-      let model = new NotebookModel();
-      model.fromJSON(DEFAULT_CONTENT);
-      for (let i = 0; i < model.cells.length; i++) {
-        let cell = model.cells.at(i);
-        cell.trusted = true;
-      }
-      trustNotebook(model).then(() => { done(); });
-      acceptDialog();
-    });
-
-  });
-
-});