resolution.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
  6. var Prefixer = require('./prefixer');
  7. var utils = require('./utils');
  8. var n2f = require('num2fraction');
  9. var regexp = /(min|max)-resolution\s*:\s*\d*\.?\d+(dppx|dpi)/gi;
  10. var split = /(min|max)-resolution(\s*:\s*)(\d*\.?\d+)(dppx|dpi)/i;
  11. var Resolution = function (_Prefixer) {
  12. _inherits(Resolution, _Prefixer);
  13. function Resolution() {
  14. _classCallCheck(this, Resolution);
  15. return _possibleConstructorReturn(this, _Prefixer.apply(this, arguments));
  16. }
  17. /**
  18. * Return prefixed query name
  19. */
  20. Resolution.prototype.prefixName = function prefixName(prefix, name) {
  21. var newName = prefix === '-moz-' ? name + '--moz-device-pixel-ratio' : prefix + name + '-device-pixel-ratio';
  22. return newName;
  23. };
  24. /**
  25. * Return prefixed query
  26. */
  27. Resolution.prototype.prefixQuery = function prefixQuery(prefix, name, colon, value, units) {
  28. if (units === 'dpi') {
  29. value = Number(value / 96);
  30. }
  31. if (prefix === '-o-') {
  32. value = n2f(value);
  33. }
  34. return this.prefixName(prefix, name) + colon + value;
  35. };
  36. /**
  37. * Remove prefixed queries
  38. */
  39. Resolution.prototype.clean = function clean(rule) {
  40. var _this2 = this;
  41. if (!this.bad) {
  42. this.bad = [];
  43. for (var _iterator = this.prefixes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  44. var _ref;
  45. if (_isArray) {
  46. if (_i >= _iterator.length) break;
  47. _ref = _iterator[_i++];
  48. } else {
  49. _i = _iterator.next();
  50. if (_i.done) break;
  51. _ref = _i.value;
  52. }
  53. var prefix = _ref;
  54. this.bad.push(this.prefixName(prefix, 'min'));
  55. this.bad.push(this.prefixName(prefix, 'max'));
  56. }
  57. }
  58. rule.params = utils.editList(rule.params, function (queries) {
  59. return queries.filter(function (query) {
  60. return _this2.bad.every(function (i) {
  61. return query.indexOf(i) === -1;
  62. });
  63. });
  64. });
  65. };
  66. /**
  67. * Add prefixed queries
  68. */
  69. Resolution.prototype.process = function process(rule) {
  70. var _this3 = this;
  71. var parent = this.parentPrefix(rule);
  72. var prefixes = parent ? [parent] : this.prefixes;
  73. rule.params = utils.editList(rule.params, function (origin, prefixed) {
  74. for (var _iterator2 = origin, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  75. var _ref2;
  76. if (_isArray2) {
  77. if (_i2 >= _iterator2.length) break;
  78. _ref2 = _iterator2[_i2++];
  79. } else {
  80. _i2 = _iterator2.next();
  81. if (_i2.done) break;
  82. _ref2 = _i2.value;
  83. }
  84. var query = _ref2;
  85. if (query.indexOf('min-resolution') === -1 && query.indexOf('max-resolution') === -1) {
  86. prefixed.push(query);
  87. continue;
  88. }
  89. var _loop = function _loop(prefix) {
  90. var processed = query.replace(regexp, function (str) {
  91. var parts = str.match(split);
  92. return _this3.prefixQuery(prefix, parts[1], parts[2], parts[3], parts[4]);
  93. });
  94. prefixed.push(processed);
  95. };
  96. for (var _iterator3 = prefixes, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
  97. var _ref3;
  98. if (_isArray3) {
  99. if (_i3 >= _iterator3.length) break;
  100. _ref3 = _iterator3[_i3++];
  101. } else {
  102. _i3 = _iterator3.next();
  103. if (_i3.done) break;
  104. _ref3 = _i3.value;
  105. }
  106. var prefix = _ref3;
  107. _loop(prefix);
  108. }
  109. prefixed.push(query);
  110. }
  111. return utils.uniq(prefixed);
  112. });
  113. };
  114. return Resolution;
  115. }(Prefixer);
  116. module.exports = Resolution;