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

leizongmin / js-css-filter / #2176

23 Dec 2025 08:42PM UTC coverage: 96.129% (-0.2%) from 96.368%
#2176

push

60 of 80 branches covered (75.0%)

447 of 465 relevant lines covered (96.13%)

9.53 hits per line

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

95.92
/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);
12✔
20
}
21

22
/**
23
 * 浅拷贝对象
24
 *
25
 * @param {Object} obj
26
 * @return {Object}
27
 */
28
function shallowCopyObject (obj) {
1✔
29
  var ret = {};
6✔
30
  for (var i in obj) {
6✔
31
    ret[i] = obj[i];
4✔
32
  }
33
  return ret;
6✔
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) {
6✔
46
  options = shallowCopyObject(options || {});
6✔
47
  options.whiteList = options.whiteList || DEFAULT.whiteList;
6✔
48
  options.onAttr = options.onAttr || DEFAULT.onAttr;
6✔
49
  options.onIgnoreAttr = options.onIgnoreAttr || DEFAULT.onIgnoreAttr;
6✔
50
  options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
6✔
51
  this.options = options;
52
}
53

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

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

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

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

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

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

×
87
    if (isWhite) {
88

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

2✔
96
    } else {
97

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

1✔
103
    }
104
  });
105

106
  return retCSS;
107
};
108

5✔
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