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

leizongmin / js-css-filter / #2142

18 Dec 2025 05:41PM UTC coverage: 96.368% (-0.4%) from 96.767%
#2142

push

55 of 74 branches covered (74.32%)

451 of 468 relevant lines covered (96.37%)

9.49 hits per line

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

96.3
/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
}
12✔
21

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

36
/**
37
 * 创建CSS过滤器
38
 *
39
 * @param {Object} options
40
 *   - {Object} whiteList
41
 *   - {Function} onAttr
1✔
42
 *   - {Function} onIgnoreAttr
6✔
43
 *   - {Function} safeAttrValue
6✔
44
 */
4✔
45
function FilterCSS (options) {
46
  options = shallowCopyObject(options || {});
6✔
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
  // 兼容各种奇葩输入
56
  css = css || '';
57
  css = css.toString();
1✔
58
  if (!css) return '';
6✔
59

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

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

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

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

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

12✔
87
    if (isWhite) {
88

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

8✔
96
    } else {
8!
97

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

6✔
103
    }
104
  });
2✔
105

106
  return retCSS;
107
};
108

109

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

© 2025 Coveralls, Inc