• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

leizongmin / js-css-filter / #2197

03 Jan 2026 04:00AM UTC coverage: 96.574% (+0.2%) from 96.344%
#2197

push

61 of 77 branches covered (79.22%)

451 of 467 relevant lines covered (96.57%)

12.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.0
/lib/css.js
1
/**
2
 * cssfilter
3
 *
4
 * @author 老雷<leizongmin@gmail.com>
5
 */
6

7
var DEFAULT = require('./default');
1✔
8
var parseStyle = require('./parser');
1✔
9
var _ = require('./util');
1✔
10

11

12
/**
13
 * 返回值是否为空
14
 *
15
 * @param {Object} obj
16
 * @return {Boolean}
17
 */
18
function isNull (obj) {
1✔
19
  return (obj === undefined || obj === null);
15✔
20
}
21

22
/**
23
 * 浅拷贝对象
24
 *
25
 * @param {Object} obj
26
 * @return {Object}
27
 */
28
function shallowCopyObject (obj) {
1✔
29
  var ret = {};
10✔
30
  for (var i in obj) {
10✔
31
    ret[i] = obj[i];
6✔
32
  }
33
  return ret;
10✔
34
}
35

36
/**
37
 * 创建CSS过滤器
38
 *
39
 * @param {Object} options
40
 *   - {Object} whiteList
41
 *   - {Function} onAttr
42
 *   - {Function} onIgnoreAttr
43
 *   - {Function} safeAttrValue
44
 */
1✔
45
function FilterCSS (options) {
10✔
46
  options = shallowCopyObject(options || {});
10✔
47
  options.whiteList = options.whiteList || DEFAULT.whiteList;
10✔
48
  options.onAttr = options.onAttr || DEFAULT.onAttr;
10✔
49
  options.onIgnoreAttr = options.onIgnoreAttr || DEFAULT.onIgnoreAttr;
10✔
50
  options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
51
  this.options = options;
52
}
1✔
53

54
FilterCSS.prototype.process = function (css) {
9!
55
  // 兼容各种奇葩输入
9✔
56
  css = css || '';
9!
57
  css = css.toString();
58
  if (!css) return '';
9✔
59

9✔
60
  var me = this;
9✔
61
  var options = me.options;
9✔
62
  var whiteList = options.whiteList;
9✔
63
  var onAttr = options.onAttr;
9✔
64
  var onIgnoreAttr = options.onIgnoreAttr;
65
  var safeAttrValue = options.safeAttrValue;
9✔
66

67
  var retCSS = parseStyle(css, function (sourcePosition, position, name, value, source) {
17✔
68

17✔
69
    var check = whiteList[name];
17✔
70
    var isWhite = false;
4!
71
    if (check === true) isWhite = check;
4!
72
    else if (typeof check === 'function') isWhite = check(value);
17✔
73
    else if (check instanceof RegExp) isWhite = check.test(value);
74
    if (isWhite !== true) isWhite = false;
17✔
75

76
    // 如果过滤后 value 为空则直接忽略
77
    value = safeAttrValue(name, value);
78
    if (!value) return;
79

80
    var opts = {
81
      position: position,
17✔
82
      sourcePosition: sourcePosition,
83
      source: source,
13✔
84
      isWhite: isWhite
13✔
85
    };
2✔
86

87
    if (isWhite) {
88

11✔
89
      var ret = onAttr(name, value, opts);
11✔
90
      if (isNull(ret)) {
9✔
91
        return name + ':' + value;
92
      } else {
2✔
93
        return ret;
94
      }
95

96
    } else {
97

4✔
98
      var ret = onIgnoreAttr(name, value, opts);
4!
99
      if (!isNull(ret)) {
×
100
        return ret;
101
      }
102

4✔
103
    }
4✔
104
  });
1✔
105

106
  return retCSS;
107
};
108

109

110
module.exports = FilterCSS;
9✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc