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

leizongmin / js-css-filter / #2448

17 Jan 2026 05:08AM UTC coverage: 96.375% (-0.2%) from 96.624%
#2448

push

61 of 79 branches covered (77.22%)

452 of 469 relevant lines covered (96.38%)

9.52 hits per line

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

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

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

11

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

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

6✔
36
/**
6✔
37
 * 创建CSS过滤器
4✔
38
 *
39
 * @param {Object} options
6✔
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;
49
  options.onIgnoreAttr = options.onIgnoreAttr || DEFAULT.onIgnoreAttr;
50
  options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
51
  this.options = options;
52
}
53

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

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

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

69
    var check = whiteList[name];
70
    var isWhite = false;
71
    if (check === true) isWhite = check;
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

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

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

5✔
87
    if (isWhite) {
5✔
88

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

12!
96
    } else {
×
97

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

12✔
103
    }
12✔
104
  });
105

106
  return retCSS;
12✔
107
};
4!
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