No Description

resolution.js 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. (function() {
  2. var Prefixer, Resolution, n2f, regexp, split, utils,
  3. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  4. hasProp = {}.hasOwnProperty;
  5. Prefixer = require('./prefixer');
  6. utils = require('./utils');
  7. n2f = require('num2fraction');
  8. regexp = /(min|max)-resolution\s*:\s*\d*\.?\d+(dppx|dpi)/gi;
  9. split = /(min|max)-resolution(\s*:\s*)(\d*\.?\d+)(dppx|dpi)/i;
  10. Resolution = (function(superClass) {
  11. extend(Resolution, superClass);
  12. function Resolution() {
  13. return Resolution.__super__.constructor.apply(this, arguments);
  14. }
  15. Resolution.prototype.prefixName = function(prefix, name) {
  16. return name = prefix === '-moz-' ? name + '--moz-device-pixel-ratio' : prefix + name + '-device-pixel-ratio';
  17. };
  18. Resolution.prototype.prefixQuery = function(prefix, name, colon, value, units) {
  19. if (units === 'dpi') {
  20. value = Number(value / 96);
  21. }
  22. if (prefix === '-o-') {
  23. value = n2f(value);
  24. }
  25. return this.prefixName(prefix, name) + colon + value;
  26. };
  27. Resolution.prototype.clean = function(rule) {
  28. var j, len, prefix, ref;
  29. if (!this.bad) {
  30. this.bad = [];
  31. ref = this.prefixes;
  32. for (j = 0, len = ref.length; j < len; j++) {
  33. prefix = ref[j];
  34. this.bad.push(this.prefixName(prefix, 'min'));
  35. this.bad.push(this.prefixName(prefix, 'max'));
  36. }
  37. }
  38. return rule.params = utils.editList(rule.params, (function(_this) {
  39. return function(queries) {
  40. return queries.filter(function(query) {
  41. return _this.bad.every(function(i) {
  42. return query.indexOf(i) === -1;
  43. });
  44. });
  45. };
  46. })(this));
  47. };
  48. Resolution.prototype.process = function(rule) {
  49. var parent, prefixes;
  50. parent = this.parentPrefix(rule);
  51. prefixes = parent ? [parent] : this.prefixes;
  52. return rule.params = utils.editList(rule.params, (function(_this) {
  53. return function(origin, prefixed) {
  54. var j, k, len, len1, prefix, processed, query;
  55. for (j = 0, len = origin.length; j < len; j++) {
  56. query = origin[j];
  57. if (query.indexOf('min-resolution') === -1 && query.indexOf('max-resolution') === -1) {
  58. prefixed.push(query);
  59. continue;
  60. }
  61. for (k = 0, len1 = prefixes.length; k < len1; k++) {
  62. prefix = prefixes[k];
  63. if (prefix === '-moz-' && rule.params.indexOf('dpi') !== -1) {
  64. continue;
  65. } else {
  66. processed = query.replace(regexp, function(str) {
  67. var parts;
  68. parts = str.match(split);
  69. return _this.prefixQuery(prefix, parts[1], parts[2], parts[3], parts[4]);
  70. });
  71. prefixed.push(processed);
  72. }
  73. }
  74. prefixed.push(query);
  75. }
  76. return utils.uniq(prefixed);
  77. };
  78. })(this));
  79. };
  80. return Resolution;
  81. })(Prefixer);
  82. module.exports = Resolution;
  83. }).call(this);