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

leizongmin / js-css-filter / #959

27 Nov 2025 12:13AM UTC coverage: 95.349% (-0.6%) from 95.966%
#959

push

58 of 82 branches covered (70.73%)

451 of 473 relevant lines covered (95.35%)

9.39 hits per line

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

88.14
/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

1✔
11

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

22
/**
×
23
 * 浅拷贝对象
×
24
 *
25
 * @param {Object} obj
×
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
}
35

1✔
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) {
1✔
46
  options = shallowCopyObject(options || {});
12!
47
  options.whiteList = options.whiteList || DEFAULT.whiteList;
48
  options.onAttr = options.onAttr || DEFAULT.onAttr;
12!
49
  options.onIgnoreAttr = options.onIgnoreAttr || DEFAULT.onIgnoreAttr;
×
50
  options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
51
  this.options = options;
12✔
52
}
53

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

60
  var me = this;
1✔
61
  var options = me.options;
6✔
62
  var whiteList = options.whiteList;
6✔
63
  var onAttr = options.onAttr;
4✔
64
  var onIgnoreAttr = options.onIgnoreAttr;
65
  var safeAttrValue = options.safeAttrValue;
6✔
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 为空则直接忽略
77
    value = safeAttrValue(name, value);
1✔
78
    if (!value) return;
6✔
79

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

1✔
87
    if (isWhite) {
88

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

5✔
96
    } else {
5✔
97

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

12✔
103
    }
12!
104
  });
×
105

106
  return retCSS;
107
};
12✔
108

12✔
109

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

© 2026 Coveralls, Inc