resolution.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 _createForOfIteratorHelperLoose(o) { var i = 0; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } i = o[Symbol.iterator](); return i.next.bind(i); }
  4. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  5. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  6. function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
  7. function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  8. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  9. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  10. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  11. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
  12. var n2f = require('num2fraction');
  13. var Prefixer = require('./prefixer');
  14. var utils = require('./utils');
  15. var REGEXP = /(min|max)-resolution\s*:\s*\d*\.?\d+(dppx|dpi|x)/gi;
  16. var SPLIT = /(min|max)-resolution(\s*:\s*)(\d*\.?\d+)(dppx|dpi|x)/i;
  17. var Resolution = /*#__PURE__*/function (_Prefixer) {
  18. _inheritsLoose(Resolution, _Prefixer);
  19. var _super = _createSuper(Resolution);
  20. function Resolution() {
  21. return _Prefixer.apply(this, arguments) || this;
  22. }
  23. var _proto = Resolution.prototype;
  24. /**
  25. * Return prefixed query name
  26. */
  27. _proto.prefixName = function prefixName(prefix, name) {
  28. if (prefix === '-moz-') {
  29. return name + '--moz-device-pixel-ratio';
  30. } else {
  31. return prefix + name + '-device-pixel-ratio';
  32. }
  33. }
  34. /**
  35. * Return prefixed query
  36. */
  37. ;
  38. _proto.prefixQuery = function prefixQuery(prefix, name, colon, value, units) {
  39. if (units === 'dpi') {
  40. value = Number(value / 96);
  41. }
  42. if (prefix === '-o-') {
  43. value = n2f(value);
  44. }
  45. return this.prefixName(prefix, name) + colon + value;
  46. }
  47. /**
  48. * Remove prefixed queries
  49. */
  50. ;
  51. _proto.clean = function clean(rule) {
  52. var _this = this;
  53. if (!this.bad) {
  54. this.bad = [];
  55. for (var _iterator = _createForOfIteratorHelperLoose(this.prefixes), _step; !(_step = _iterator()).done;) {
  56. var prefix = _step.value;
  57. this.bad.push(this.prefixName(prefix, 'min'));
  58. this.bad.push(this.prefixName(prefix, 'max'));
  59. }
  60. }
  61. rule.params = utils.editList(rule.params, function (queries) {
  62. return queries.filter(function (query) {
  63. return _this.bad.every(function (i) {
  64. return !query.includes(i);
  65. });
  66. });
  67. });
  68. }
  69. /**
  70. * Add prefixed queries
  71. */
  72. ;
  73. _proto.process = function process(rule) {
  74. var _this2 = this;
  75. var parent = this.parentPrefix(rule);
  76. var prefixes = parent ? [parent] : this.prefixes;
  77. rule.params = utils.editList(rule.params, function (origin, prefixed) {
  78. for (var _iterator2 = _createForOfIteratorHelperLoose(origin), _step2; !(_step2 = _iterator2()).done;) {
  79. var query = _step2.value;
  80. if (!query.includes('min-resolution') && !query.includes('max-resolution')) {
  81. prefixed.push(query);
  82. continue;
  83. }
  84. var _loop = function _loop() {
  85. var prefix = _step3.value;
  86. var processed = query.replace(REGEXP, function (str) {
  87. var parts = str.match(SPLIT);
  88. return _this2.prefixQuery(prefix, parts[1], parts[2], parts[3], parts[4]);
  89. });
  90. prefixed.push(processed);
  91. };
  92. for (var _iterator3 = _createForOfIteratorHelperLoose(prefixes), _step3; !(_step3 = _iterator3()).done;) {
  93. _loop();
  94. }
  95. prefixed.push(query);
  96. }
  97. return utils.uniq(prefixed);
  98. });
  99. };
  100. return Resolution;
  101. }(Prefixer);
  102. module.exports = Resolution;