index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var postcss = require('postcss');
  2. module.exports = postcss.plugin('postcss-media-minmax', function () {
  3. return function(css) {
  4. var feature_unit = {
  5. 'width': 'px',
  6. 'height': 'px',
  7. 'device-width': 'px',
  8. 'device-height': 'px',
  9. 'aspect-ratio': '',
  10. 'device-aspect-ratio': '',
  11. 'color': '',
  12. 'color-index': '',
  13. 'monochrome': '',
  14. 'resolution': 'dpi'
  15. };
  16. //支持 min-/max- 前缀的属性
  17. var feature_name = Object.keys(feature_unit);
  18. var step = .001; // smallest even number that won’t break complex queries (1in = 96px)
  19. var power = {
  20. '>': 1,
  21. '<': -1
  22. };
  23. var minmax = {
  24. '>': 'min',
  25. '<': 'max'
  26. };
  27. function create_query(name, gtlt, eq, value, params) {
  28. return value.replace(/([-\d\.]+)(.*)/, function (match, number, unit) {
  29. var initialNumber = parseFloat(number);
  30. if (parseFloat(number) || eq) {
  31. // if eq is true, then number remains same
  32. if (!eq) {
  33. // change integer pixels value only on integer pixel
  34. if (unit === 'px' && initialNumber === parseInt(number, 10)) {
  35. number = initialNumber + power[gtlt];
  36. } else {
  37. number = Number(Math.round(parseFloat(number) + step * power[gtlt] + 'e6')+'e-6');
  38. }
  39. }
  40. } else {
  41. number = power[gtlt] + feature_unit[name];
  42. }
  43. return '(' + minmax[gtlt] + '-' + name + ': ' + number + unit + ')';
  44. });
  45. }
  46. // 读取 media-feature
  47. css.walkAtRules(function(rule, i) {
  48. if (rule.name !== "media" && rule.name !== "custom-media") {
  49. return
  50. }
  51. /**
  52. * 转换 <mf-name> <|>= <mf-value>
  53. * $1 $2 $3
  54. * (width >= 300px) => (min-width: 300px)
  55. * (width <= 900px) => (max-width: 900px)
  56. */
  57. //取值不支持负值
  58. //But -0 is always equivalent to 0 in CSS, and so is also accepted as a valid <mq-boolean> value.
  59. rule.params = rule.params.replace(/\(\s*([a-z-]+?)\s*([<>])(=?)\s*((?:-?\d*\.?(?:\s*\/?\s*)?\d+[a-z]*)?)\s*\)/gi, function($0, $1, $2, $3, $4) {
  60. var params = '';
  61. if (feature_name.indexOf($1) > -1) {
  62. return create_query($1, $2, $3, $4, rule.params);
  63. }
  64. //如果不是指定的属性,不做替换
  65. return $0;
  66. })
  67. /**
  68. * 转换 <mf-value> <|<= <mf-name> <|<= <mf-value>
  69. * 转换 <mf-value> >|>= <mf-name> >|>= <mf-value>
  70. * $1 $2$3 $4 $5$6 $7
  71. * (500px <= width <= 1200px) => (min-width: 500px) and (max-width: 1200px)
  72. * (500px < width <= 1200px) => (min-width: 501px) and (max-width: 1200px)
  73. * (900px >= width >= 300px) => (min-width: 300px) and (max-width: 900px)
  74. */
  75. rule.params = rule.params.replace(/\(\s*((?:-?\d*\.?(?:\s*\/?\s*)?\d+[a-z]*)?)\s*(<|>)(=?)\s*([a-z-]+)\s*(<|>)(=?)\s*((?:-?\d*\.?(?:\s*\/?\s*)?\d+[a-z]*)?)\s*\)/gi, function($0, $1, $2, $3, $4, $5, $6, $7) {
  76. if (feature_name.indexOf($4) > -1) {
  77. if ($2 === '<' && $5 === '<' || $2 === '>' && $5 === '>') {
  78. var min = ($2 === '<') ? $1 : $7;
  79. var max = ($2 === '<') ? $7 : $1;
  80. // output differently depended on expression direction
  81. // <mf-value> <|<= <mf-name> <|<= <mf-value>
  82. // or
  83. // <mf-value> >|>= <mf-name> >|>= <mf-value>
  84. var equals_for_min = $3;
  85. var equals_for_max = $6;
  86. if ($2 === '>') {
  87. equals_for_min = $6;
  88. equals_for_max = $3;
  89. }
  90. return create_query($4, '>', equals_for_min, min) + ' and ' + create_query($4, '<', equals_for_max, max);
  91. }
  92. }
  93. //如果不是指定的属性,不做替换
  94. return $0;
  95. });
  96. });
  97. }
  98. });