@@ -2,19 +2,41 @@ var postcss = require('postcss');
22
33module . exports = postcss . plugin ( 'postcss-media-minmax' , function ( ) {
44 return function ( css ) {
5+ var feature_unit = {
6+ 'width' : 'px' ,
7+ 'height' : 'px' ,
8+ 'device-width' : 'px' ,
9+ 'device-height' : 'px' ,
10+ 'aspect-ratio' : '' ,
11+ 'device-aspect-ratio' : '' ,
12+ 'color' : '' ,
13+ 'color-index' : '' ,
14+ 'monochrome' : '' ,
15+ 'resolution' : 'dpi'
16+ } ;
17+
518 //支持 min-/max- 前缀的属性
6- var feature_name = [
7- 'width' ,
8- 'height' ,
9- 'device-width' ,
10- 'device-height' ,
11- 'aspect-ratio' ,
12- 'device-aspect-ratio' ,
13- 'color' ,
14- 'color-index' ,
15- 'monochrome' ,
16- 'resolution'
17- ]
19+ var feature_name = Object . keys ( feature_unit ) ;
20+
21+ var step = .001 ; // smallest even number that won’t break complex queries (1in = 96px)
22+
23+ var power = {
24+ '>' : 1 ,
25+ '<' : - 1
26+ } ;
27+
28+ var minmax = {
29+ '>' : 'min' ,
30+ '<' : 'max'
31+ } ;
32+
33+ function create_query ( name , gtlt , eq , value , params ) {
34+ return value . replace ( / ( [ - \d \. ] + ) ( .* ) / , function ( match , number , unit ) {
35+ number = parseFloat ( number ) || eq ? eq ? number : parseFloat ( number ) + step * power [ gtlt ] : power [ gtlt ] + feature_unit [ name ] ;
36+
37+ return '(' + minmax [ gtlt ] + '-' + name + ': ' + number + unit + ')' ;
38+ } ) ;
39+ }
1840
1941 // 读取 media-feature
2042 css . walkAtRules ( function ( rule , i ) {
@@ -23,7 +45,7 @@ module.exports = postcss.plugin('postcss-media-minmax', function () {
2345 }
2446
2547 /**
26- * 转换 <mf-name> <= |>= <mf-value>
48+ * 转换 <mf-name> <|>= <mf-value>
2749 * $1 $2 $3
2850 * (width >= 300px) => (min-width: 300px)
2951 * (width <= 900px) => (max-width: 900px)
@@ -32,37 +54,32 @@ module.exports = postcss.plugin('postcss-media-minmax', function () {
3254 //取值不支持负值
3355 //But -0 is always equivalent to 0 in CSS, and so is also accepted as a valid <mq-boolean> value.
3456
35- rule . params = rule . params . replace ( / \( \s * ( [ a - z - ] + ?) \s * ( [ < > ] = ) \s * ( (?: - ? \d * \. ? (?: \s * \/ ? \s * ) ? \d + [ a - z ] * ) ? ) \s * \) / gi, function ( $0 , $1 , $2 , $3 ) {
57+ rule . params = rule . params . replace ( / \( \s * ( [ a - z - ] + ?) \s * ( [ < > ] ) ( = ? ) \s * ( (?: - ? \d * \. ? (?: \s * \/ ? \s * ) ? \d + [ a - z ] * ) ? ) \s * \) / gi, function ( $0 , $1 , $2 , $3 , $4 ) {
3658
3759 var params = '' ;
3860
3961 if ( feature_name . indexOf ( $1 ) > - 1 ) {
40- if ( $2 === '<=' ) {
41- params = '(' + 'max-' + $1 + ': ' + $3 + ')' ;
42- }
43- if ( $2 === '>=' ) {
44- params += '(' + 'min-' + $1 + ': ' + $3 + ')' ;
45- }
46- return params ;
62+ return create_query ( $1 , $2 , $3 , $4 , rule . params ) ;
4763 }
4864 //如果不是指定的属性,不做替换
4965 return $0 ;
5066 } )
5167
5268 /**
53- * 转换 <mf-value> <= |>= <mf-name> <= |>= <mf-value>
54- * $1 $2 $3 $4 $5
69+ * 转换 <mf-value> <|>= <mf-name> <|>= <mf-value>
70+ * $1 $2 $3 $4$5 $5
5571 * (500px <= width <= 1200px) => (min-width: 500px) and (max-width: 1200px)
5672 * (900px >= width >= 300px) => (min-width: 300px) and (max-width: 900px)
5773 */
5874
59- 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 ) {
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+
77+ if ( feature_name . indexOf ( $4 ) > - 1 ) {
78+ if ( $2 === '<' && $5 === '<' || $2 === '>' && $5 === '>' ) {
79+ var min = ( $2 === '<' ) ? $1 : $7 ;
80+ var max = ( $2 === '<' ) ? $7 : $1 ;
6081
61- if ( feature_name . indexOf ( $3 ) > - 1 ) {
62- if ( $2 === '<=' && $4 === '<=' || $2 === '>=' && $4 === '>=' ) {
63- var min = ( $2 === '<=' ) ? $1 : $5 ;
64- var max = ( $2 === '<=' ) ? $5 : $1 ;
65- return '(' + 'min-' + $3 + ': ' + min + ') and (' + 'max-' + $3 + ': ' + max + ')' ;
82+ return create_query ( $4 , '>' , $3 , min ) + ' and ' + create_query ( $4 , '<' , $6 , max ) ;
6683 }
6784 }
6885 //如果不是指定的属性,不做替换
0 commit comments