You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.3 KiB

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