UNPKG

4.38 kBJavaScriptView Raw
1"use strict";
2
3function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
4
5function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
6
7var n2f = require('num2fraction');
8
9var Prefixer = require('./prefixer');
10
11var utils = require('./utils');
12
13var REGEXP = /(min|max)-resolution\s*:\s*\d*\.?\d+(dppx|dpi|x)/gi;
14var SPLIT = /(min|max)-resolution(\s*:\s*)(\d*\.?\d+)(dppx|dpi|x)/i;
15
16var Resolution =
17/*#__PURE__*/
18function (_Prefixer) {
19 _inheritsLoose(Resolution, _Prefixer);
20
21 function Resolution() {
22 return _Prefixer.apply(this, arguments) || this;
23 }
24
25 var _proto = Resolution.prototype;
26
27 /**
28 * Return prefixed query name
29 */
30 _proto.prefixName = function prefixName(prefix, name) {
31 if (prefix === '-moz-') {
32 return name + '--moz-device-pixel-ratio';
33 } else {
34 return prefix + name + '-device-pixel-ratio';
35 }
36 }
37 /**
38 * Return prefixed query
39 */
40 ;
41
42 _proto.prefixQuery = function prefixQuery(prefix, name, colon, value, units) {
43 if (units === 'dpi') {
44 value = Number(value / 96);
45 }
46
47 if (prefix === '-o-') {
48 value = n2f(value);
49 }
50
51 return this.prefixName(prefix, name) + colon + value;
52 }
53 /**
54 * Remove prefixed queries
55 */
56 ;
57
58 _proto.clean = function clean(rule) {
59 var _this = this;
60
61 if (!this.bad) {
62 this.bad = [];
63
64 for (var _iterator = this.prefixes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
65 var _ref;
66
67 if (_isArray) {
68 if (_i >= _iterator.length) break;
69 _ref = _iterator[_i++];
70 } else {
71 _i = _iterator.next();
72 if (_i.done) break;
73 _ref = _i.value;
74 }
75
76 var prefix = _ref;
77 this.bad.push(this.prefixName(prefix, 'min'));
78 this.bad.push(this.prefixName(prefix, 'max'));
79 }
80 }
81
82 rule.params = utils.editList(rule.params, function (queries) {
83 return queries.filter(function (query) {
84 return _this.bad.every(function (i) {
85 return !query.includes(i);
86 });
87 });
88 });
89 }
90 /**
91 * Add prefixed queries
92 */
93 ;
94
95 _proto.process = function process(rule) {
96 var _this2 = this;
97
98 var parent = this.parentPrefix(rule);
99 var prefixes = parent ? [parent] : this.prefixes;
100 rule.params = utils.editList(rule.params, function (origin, prefixed) {
101 for (var _iterator2 = origin, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
102 var _ref2;
103
104 if (_isArray2) {
105 if (_i2 >= _iterator2.length) break;
106 _ref2 = _iterator2[_i2++];
107 } else {
108 _i2 = _iterator2.next();
109 if (_i2.done) break;
110 _ref2 = _i2.value;
111 }
112
113 var query = _ref2;
114
115 if (!query.includes('min-resolution') && !query.includes('max-resolution')) {
116 prefixed.push(query);
117 continue;
118 }
119
120 var _loop = function _loop() {
121 if (_isArray3) {
122 if (_i3 >= _iterator3.length) return "break";
123 _ref3 = _iterator3[_i3++];
124 } else {
125 _i3 = _iterator3.next();
126 if (_i3.done) return "break";
127 _ref3 = _i3.value;
128 }
129
130 var prefix = _ref3;
131 var processed = query.replace(REGEXP, function (str) {
132 var parts = str.match(SPLIT);
133 return _this2.prefixQuery(prefix, parts[1], parts[2], parts[3], parts[4]);
134 });
135 prefixed.push(processed);
136 };
137
138 for (var _iterator3 = prefixes, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
139 var _ref3;
140
141 var _ret = _loop();
142
143 if (_ret === "break") break;
144 }
145
146 prefixed.push(query);
147 }
148
149 return utils.uniq(prefixed);
150 });
151 };
152
153 return Resolution;
154}(Prefixer);
155
156module.exports = Resolution;
\No newline at end of file