浏览代码

Make HTML sanitizer a plugin

eels 4 年之前
父节点
当前提交
8d6bc38ec5

+ 16 - 1
packages/apputils-extension/src/index.ts

@@ -22,7 +22,9 @@ import {
   IWindowResolver,
   WindowResolver,
   Printing,
-  sessionContextDialogs
+  sessionContextDialogs,
+  ISanitizer,
+  defaultSanitizer
 } from '@jupyterlab/apputils';
 
 import { URLExt, PageConfig } from '@jupyterlab/coreutils';
@@ -518,6 +520,18 @@ const utilityCommands: JupyterFrontEndPlugin<void> = {
   }
 };
 
+/**
+ * The default HTML sanitizer.
+ */
+const sanitizer: JupyterFrontEndPlugin<ISanitizer> = {
+  id: '@jupyter/apputils-extension:sanitizer',
+  autoStart: true,
+  provides: ISanitizer,
+  activate: () => {
+    return defaultSanitizer;
+  }
+};
+
 /**
  * Export the plugins as default.
  */
@@ -526,6 +540,7 @@ const plugins: JupyterFrontEndPlugin<any>[] = [
   paletteRestorer,
   print,
   resolver,
+  sanitizer,
   settingsPlugin,
   state,
   splash,

+ 12 - 1
packages/apputils/src/sanitizer.ts

@@ -5,6 +5,17 @@
 // it which acts as a polyfill for browsers.
 import sanitize from 'sanitize-html';
 
+import { Token } from '@lumino/coreutils';
+
+/* tslint:disable */
+/**
+ * The sanitizer token.
+ */
+export const ISanitizer = new Token<ISanitizer>(
+  '@jupyterlab/apputils:ISanitizer'
+);
+/* tslint:enable */
+
 export interface ISanitizer {
   /**
    * Sanitize an HTML string.
@@ -453,7 +464,7 @@ class CssProp {
 /**
  * A class to sanitize HTML strings.
  */
-class Sanitizer implements ISanitizer {
+export class Sanitizer implements ISanitizer {
   /**
    * Sanitize an HTML string.
    *

+ 1 - 0
packages/rendermime-extension/package.json

@@ -35,6 +35,7 @@
   },
   "dependencies": {
     "@jupyterlab/application": "^3.1.0-alpha.3",
+    "@jupyterlab/apputils": "^3.1.0-alpha.3",
     "@jupyterlab/docmanager": "^3.1.0-alpha.3",
     "@jupyterlab/rendermime": "^3.1.0-alpha.3",
     "@jupyterlab/translation": "^3.1.0-alpha.3"

+ 7 - 4
packages/rendermime-extension/src/index.ts

@@ -11,6 +11,7 @@ import {
   JupyterFrontEnd,
   JupyterFrontEndPlugin
 } from '@jupyterlab/application';
+import { ISanitizer } from '@jupyterlab/apputils';
 
 import { IDocumentManager } from '@jupyterlab/docmanager';
 
@@ -31,7 +32,7 @@ namespace CommandIDs {
  */
 const plugin: JupyterFrontEndPlugin<IRenderMimeRegistry> = {
   id: '@jupyterlab/rendermime-extension:plugin',
-  requires: [ITranslator],
+  requires: [ITranslator, ISanitizer],
   optional: [IDocumentManager, ILatexTypesetter],
   provides: IRenderMimeRegistry,
   activate: activate,
@@ -50,8 +51,9 @@ function activate(
   app: JupyterFrontEnd,
   translator: ITranslator,
   docManager: IDocumentManager | null,
-  latexTypesetter: ILatexTypesetter | null
-) {
+  latexTypesetter: ILatexTypesetter | null,
+  sanitizer: ISanitizer
+): RenderMimeRegistry {
   const trans = translator.load('jupyterlab');
   if (docManager) {
     app.commands.addCommand(CommandIDs.handleLink, {
@@ -99,6 +101,7 @@ function activate(
           }
         },
     latexTypesetter: latexTypesetter ?? undefined,
-    translator: translator
+    translator: translator,
+    sanitizer: sanitizer
   });
 }

+ 6 - 1
packages/rendermime-extension/tsconfig.json

@@ -4,7 +4,9 @@
     "outDir": "lib",
     "rootDir": "src"
   },
-  "include": ["src/*"],
+  "include": [
+    "src/*"
+  ],
   "references": [
     {
       "path": "../application"
@@ -17,6 +19,9 @@
     },
     {
       "path": "../translation"
+    },
+    {
+      "path": "../apputils"
     }
   ]
 }