editor.js/modules/sanitizer.js

80 lines
1.7 KiB
JavaScript
Raw Normal View History

/**
* Codex Sanitizer
*/
module.exports = (function (sanitizer) {
2017-03-15 22:42:47 +01:00
/** 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
*/
var Config = {
2017-03-15 22:42:47 +01:00
/** User configuration */
CUSTOM : null,
BASIC : {
tags: {
p: {},
a: {
href: true,
target: '_blank',
rel: 'nofollow'
2017-04-25 02:47:30 +02:00
}
}
}
};
sanitizer.Config = Config;
2017-04-23 00:12:06 +02:00
/**
*
* @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
*/
2017-04-22 23:53:06 +02:00
let init_ = function (userCustomConfig) {
2017-03-15 22:42:47 +01:00
2017-04-22 23:53:06 +02:00
let configuration = userCustomConfig || Config.CUSTOM || Config.BASIC;
2017-03-15 22:42:47 +01:00
return new janitor(configuration);
};
2017-04-22 23:53:06 +02:00
/**
* Cleans string from unwanted tags
* @protected
* @param {String} dirtyString - taint string
* @param {Object} customConfig - allowed tags
*/
sanitizer.clean = function (dirtyString, customConfig) {
2017-04-22 23:53:06 +02:00
let janitorInstance = init_(customConfig);
return janitorInstance.clean(dirtyString);
};
return sanitizer;
})({});