From 7f5b0a5f9f253141efe79b176fec5cd9bed52481 Mon Sep 17 00:00:00 2001 From: Murod Khaydarov Date: Tue, 19 Dec 2017 00:36:22 +0300 Subject: [PATCH] refactored sanitizer --- .../modules/{_sanitizer.js => sanitizer.js} | 99 +++++++------------ 1 file changed, 37 insertions(+), 62 deletions(-) rename src/components/modules/{_sanitizer.js => sanitizer.js} (51%) diff --git a/src/components/modules/_sanitizer.js b/src/components/modules/sanitizer.js similarity index 51% rename from src/components/modules/_sanitizer.js rename to src/components/modules/sanitizer.js index 070f3c3c..7f6f0bfa 100644 --- a/src/components/modules/_sanitizer.js +++ b/src/components/modules/sanitizer.js @@ -19,45 +19,31 @@ export default class Sanitizer extends Module { constructor(config) { super(config); + // default config + this._config = {}; + this._janitor = null; - this.janitor = require('html-janitor'); - + /** Custom configuration */ + this.sanitizerConfig = config.settings.sanitizer; + /** HTML Janitor library */ + this.sanitizerInstance = require('html-janitor'); } - prepare() { - - } - -} -module.exports = (function (sanitizer) { - - /** HTML Janitor library */ - let janitor = require('html-janitor'); - - /** Codex Editor */ - let editor = codex.editor; - - sanitizer.prepare = function () { - - if (editor.settings.sanitizer && !editor.core.isEmpty(editor.settings.sanitizer)) { - - Config.CUSTOM = editor.settings.sanitizer; - - } - - }; - /** - * Basic config + * @param library + * + * @description If developer uses editor's API, then he can customize sane restrictions. + * Or, sane config can be defined globally in editors initialization. That config will be used everywhere + * At least, if there is no config overrides, that API uses BASIC Default configation */ - var Config = { + set sanitizerInstance(library) { + this._janitor = new library(this._config); + } - /** User configuration */ - CUSTOM : null, - - BASIC : { + set sanitizerConfig(config) { + this._config = { tags: { p: {}, a: { @@ -66,43 +52,32 @@ module.exports = (function (sanitizer) { rel: 'nofollow' } } - } - }; + }; - sanitizer.Config = Config; - - /** - * - * @param userCustomConfig - * @returns {*} - * @private - * - * @description If developer uses editor's API, then he can customize sane restrictions. - * Or, sane config can be defined globally in editors initialization. That config will be used everywhere - * At least, if there is no config overrides, that API uses BASIC Default configation - */ - let init_ = function (userCustomConfig) { - - let configuration = userCustomConfig || Config.CUSTOM || Config.BASIC; - - return new janitor(configuration); - - }; + } /** * Cleans string from unwanted tags - * @protected + * @param dirtyString + * @return {*} + */ + clean(dirtyString) { + return this._janitor.clean(dirtyString); + } + + /** + * Cleans string from unwanted tags + * @static + * + * Method allows to use default config + * * @param {String} dirtyString - taint string * @param {Object} customConfig - allowed tags */ - sanitizer.clean = function (dirtyString, customConfig) { + static clean(dirtyString, customConfig) { + let newInstance = Sanitizer(customConfig); - let janitorInstance = init_(customConfig); + return newInstance.clean(dirtyString); + } - return janitorInstance.clean(dirtyString); - - }; - - return sanitizer; - -})({}); \ No newline at end of file +}