resolution.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; 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."); } it = o[Symbol.iterator](); return it.next.bind(it); }
  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(o); 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 _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
  7. var n2f = require('num2fraction');
  8. var Prefixer = require('./prefixer');
  9. var utils = require('./utils');
  10. var REGEXP = /(min|max)-resolution\s*:\s*\d*\.?\d+(dppx|dpi|x)/gi;
  11. var SPLIT = /(min|max)-resolution(\s*:\s*)(\d*\.?\d+)(dppx|dpi|x)/i;
  12. var Resolution = /*#__PURE__*/function (_Prefixer) {
  13. _inheritsLoose(Resolution, _Prefixer);
  14. function Resolution() {
  15. return _Prefixer.apply(this, arguments) || this;
  16. }
  17. var _proto = Resolution.prototype;
  18. /**
  19. * Return prefixed query name
  20. */
  21. _proto.prefixName = function prefixName(prefix, name) {
  22. if (prefix === '-moz-') {
  23. return name + '--moz-device-pixel-ratio';
  24. } else {
  25. return prefix + name + '-device-pixel-ratio';
  26. }
  27. }
  28. /**
  29. * Return prefixed query
  30. */
  31. ;
  32. _proto.prefixQuery = function prefixQuery(prefix, name, colon, value, units) {
  33. if (units === 'dpi') {
  34. value = Number(value / 96);
  35. }
  36. if (prefix === '-o-') {
  37. value = n2f(value);
  38. }
  39. return this.prefixName(prefix, name) + colon + value;
  40. }
  41. /**
  42. * Remove prefixed queries
  43. */
  44. ;
  45. _proto.clean = function clean(rule) {
  46. var _this = this;
  47. if (!this.bad) {
  48. this.bad = [];
  49. for (var _iterator = _createForOfIteratorHelperLoose(this.prefixes), _step; !(_step = _iterator()).done;) {
  50. var prefix = _step.value;
  51. this.bad.push(this.prefixName(prefix, 'min'));
  52. this.bad.push(this.prefixName(prefix, 'max'));
  53. }
  54. }
  55. rule.params = utils.editList(rule.params, function (queries) {
  56. return queries.filter(function (query) {
  57. return _this.bad.every(function (i) {
  58. return !query.includes(i);
  59. });
  60. });
  61. });
  62. }
  63. /**
  64. * Add prefixed queries
  65. */
  66. ;
  67. _proto.process = function process(rule) {
  68. var _this2 = this;
  69. var parent = this.parentPrefix(rule);
  70. var prefixes = parent ? [parent] : this.prefixes;
  71. rule.params = utils.editList(rule.params, function (origin, prefixed) {
  72. for (var _iterator2 = _createForOfIteratorHelperLoose(origin), _step2; !(_step2 = _iterator2()).done;) {
  73. var query = _step2.value;
  74. if (!query.includes('min-resolution') && !query.includes('max-resolution')) {
  75. prefixed.push(query);
  76. continue;
  77. }
  78. var _loop = function _loop() {
  79. var prefix = _step3.value;
  80. var processed = query.replace(REGEXP, function (str) {
  81. var parts = str.match(SPLIT);
  82. return _this2.prefixQuery(prefix, parts[1], parts[2], parts[3], parts[4]);
  83. });
  84. prefixed.push(processed);
  85. };
  86. for (var _iterator3 = _createForOfIteratorHelperLoose(prefixes), _step3; !(_step3 = _iterator3()).done;) {
  87. _loop();
  88. }
  89. prefixed.push(query);
  90. }
  91. return utils.uniq(prefixed);
  92. });
  93. };
  94. return Resolution;
  95. }(Prefixer);
  96. module.exports = Resolution;