Blame view

node_modules/autoprefixer/lib/resolution.js 3.26 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  (function() {
    var Prefixer, Resolution, n2f, regexp, split, utils,
      extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
      hasProp = {}.hasOwnProperty;
  
    Prefixer = require('./prefixer');
  
    utils = require('./utils');
  
    n2f = require('num2fraction');
  
    regexp = /(min|max)-resolution\s*:\s*\d*\.?\d+(dppx|dpi)/gi;
  
    split = /(min|max)-resolution(\s*:\s*)(\d*\.?\d+)(dppx|dpi)/i;
  
    Resolution = (function(superClass) {
      extend(Resolution, superClass);
  
      function Resolution() {
        return Resolution.__super__.constructor.apply(this, arguments);
      }
  
      Resolution.prototype.prefixName = function(prefix, name) {
        return name = prefix === '-moz-' ? name + '--moz-device-pixel-ratio' : prefix + name + '-device-pixel-ratio';
      };
  
      Resolution.prototype.prefixQuery = function(prefix, name, colon, value, units) {
        if (units === 'dpi') {
          value = Number(value / 96);
        }
        if (prefix === '-o-') {
          value = n2f(value);
        }
        return this.prefixName(prefix, name) + colon + value;
      };
  
      Resolution.prototype.clean = function(rule) {
        var j, len, prefix, ref;
        if (!this.bad) {
          this.bad = [];
          ref = this.prefixes;
          for (j = 0, len = ref.length; j < len; j++) {
            prefix = ref[j];
            this.bad.push(this.prefixName(prefix, 'min'));
            this.bad.push(this.prefixName(prefix, 'max'));
          }
        }
        return rule.params = utils.editList(rule.params, (function(_this) {
          return function(queries) {
            return queries.filter(function(query) {
              return _this.bad.every(function(i) {
                return query.indexOf(i) === -1;
              });
            });
          };
        })(this));
      };
  
      Resolution.prototype.process = function(rule) {
        var parent, prefixes;
        parent = this.parentPrefix(rule);
        prefixes = parent ? [parent] : this.prefixes;
        return rule.params = utils.editList(rule.params, (function(_this) {
          return function(origin, prefixed) {
            var j, k, len, len1, prefix, processed, query;
            for (j = 0, len = origin.length; j < len; j++) {
              query = origin[j];
              if (query.indexOf('min-resolution') === -1 && query.indexOf('max-resolution') === -1) {
                prefixed.push(query);
                continue;
              }
              for (k = 0, len1 = prefixes.length; k < len1; k++) {
                prefix = prefixes[k];
                if (prefix === '-moz-' && rule.params.indexOf('dpi') !== -1) {
                  continue;
                } else {
                  processed = query.replace(regexp, function(str) {
                    var parts;
                    parts = str.match(split);
                    return _this.prefixQuery(prefix, parts[1], parts[2], parts[3], parts[4]);
                  });
                  prefixed.push(processed);
                }
              }
              prefixed.push(query);
            }
            return utils.uniq(prefixed);
          };
        })(this));
      };
  
      return Resolution;
  
    })(Prefixer);
  
    module.exports = Resolution;
  
  }).call(this);