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

leizongmin / js-css-filter / #2229

04 Jan 2026 02:31PM UTC coverage: 96.588% (-0.2%) from 96.781%
#2229

push

58 of 76 branches covered (76.32%)

453 of 469 relevant lines covered (96.59%)

15.82 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);
22✔
20
}
21

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

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

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

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

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

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

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

6✔
80
    var opts = {
81
      position: position,
82
      sourcePosition: sourcePosition,
22✔
83
      source: source,
22✔
84
      isWhite: isWhite
22✔
85
    };
4!
86

4!
87
    if (isWhite) {
22✔
88

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

96
    } else {
22✔
97

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

2✔
103
    }
104
  });
105

106
  return retCSS;
107
};
108

4✔
109

110
module.exports = FilterCSS;
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