cssfilter
cssfilter
Sanitize untrusted CSS with a configuration specified by a Whitelist. 根据白名单过滤CSS
安装
$ npm install cssfilter --save使用方法
var cssfilter = require('cssfilter');
var css = cssfilter('position:fixed; /* this is comments */ width:100px; height:100px; background:#aaa;');
console.log(css);
// 输出:width:100px; height:100px; background:#aaa;
// 因为position不在白名单允许范围或者:
options = {
// 白名单,可选
whiteList: {
a: true, // true表示允许
b: /^fixed|relative$/, // 正则test()返回true表示允许
c: function (value) {
// 返回true表示允许
},
d: false // 除以上三个以外,所有值均表示不允许
},
// 当匹配到一个在白名单中的属性时
onAttr: function (name, value, options) {
// name为属性名
// value为属性值
// 返回字符串表示覆盖此段CSS
// 不返回任何值表示使用默认生成方法,即 name:value
},
// 当匹配到一个不在白名单中的属性时
onIgnoreAttr: function (name, value, options) {
// name为属性名
// value为属性值
// 返回字符串表示覆盖此段CSS
// 不返回任何值表示使用默认生成方法,即将此段CSS去掉
}
};
mycss = new cssfilter.FilterCSS(options);
// then apply mycss.process()
css = mycss.process('position:fixed; width:100px; height:100px; background:#aaa;');
console.log(css);License
Last updated

