refactored sanitizer

This commit is contained in:
Murod Khaydarov 2017-12-19 00:36:22 +03:00
commit 7f5b0a5f9f

View file

@ -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;
})({});
}