Skip to content

Commit 8229bb0

Browse files
committed
Add test #2, Fix <ratio> unit(16/9)
1 parent 438e3bf commit 8229bb0

28 files changed

Lines changed: 500 additions & 9 deletions

README-zh.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# PostCSS Media Minmax
1+
# PostCSS Media Minmax [![Build Status](https://travis-ci.org/postcss/postcss-media-minmax.svg)](https://travis-ci.org/postcss/postcss-media-minmax)
22

33
> 写简单优雅的 Media Queries!
44
55
Media Queries 中的 `min-width``max-width` 等属性非常容易混淆,每次看到他们,我都想哭。现在[新的规范](http://dev.w3.org/csswg/mediaqueries/#mq-min-max)中,可以使用更加直观的 `>=``<=` 替代 media queries 中的 min-/max- 前缀。
66

7-
这是一个实现 CSS Media Queries Level 4 Polyfill 的插件,让你现在就可以使用这些特性,妈妈再也不用担心我记不住了,鹅妹子嘤!
7+
这是一个实现 [CSS Media Queries Level 4](http://dev.w3.org/csswg/mediaqueries/) Polyfill 的插件,让你现在就可以使用这些特性,妈妈再也不用担心我记不住了,鹅妹子嘤!
88

99

1010
[English](README.md)
@@ -67,19 +67,54 @@ input.css:
6767

6868
## CSS 语法
6969

70+
### [语法](http://dev.w3.org/csswg/mediaqueries/#mq-syntax)
71+
7072
```
7173
<mf-range> = <mf-name> [ '<' | '>' ]? '='? <mf-value>
7274
| <mf-value> [ '<' | '>' ]? '='? <mf-name>
7375
| <mf-value> '<' '='? <mf-name> '<' '='? <mf-value>
7476
| <mf-value> '>' '='? <mf-name> '>' '='? <mf-value>
7577
```
7678

79+
## [取值(Values)](http://dev.w3.org/csswg/mediaqueries/#values)
80+
81+
**The special values:**
82+
83+
* [<ratio>](http://dev.w3.org/csswg/mediaqueries/#typedef-ratio)
84+
85+
The <ratio> value type is a positive (not zero or negative) <integer> followed by optional whitespace, followed by a solidus ('/'), followed by optional whitespace, followed by a positive <integer>. <ratio>s can be ordered or compared by transforming them into the number obtained by dividing their first <integer> by their second <integer>.
86+
87+
```css
88+
@media screen and (device-aspect-ratio: 16 / 9) {
89+
/* rules */
90+
}
91+
92+
/* equivalent to */
93+
@media screen and (device-aspect-ratio: 16/9) {
94+
/* rules */
95+
}
96+
```
97+
98+
* [<mq-boolean>](http://dev.w3.org/csswg/mediaqueries/#typedef-mq-boolean)
99+
100+
The <mq-boolean> value type is an <integer> with the value 0 or 1. Any other integer value is invalid. Note that -0 is always equivalent to 0 in CSS, and so is also accepted as a valid <mq-boolean> value.
101+
102+
```css
103+
@media screen and (grid: -0) {
104+
/* rules */
105+
}
106+
107+
/* equivalent to */
108+
@media screen and (grid: 0) {
109+
/* rules */
110+
}
111+
```
77112

78113
## 如何使用
79114

80115
### 简写
81116

82-
示例 1中同一个 feature name 同时存在 `>=``<=` 时,可以简写为:
117+
示例 1中同一个 Media features name 同时存在 `>=` 和 `<=` 时,可以简写为:
83118

84119
```css
85120
@media screen and (500px <= width <= 1200px) {
@@ -104,7 +139,7 @@ input.css:
104139
}
105140
}
106141
```
107-
**注意**:当 feature name 在中间的时候,一定要保证两个 `<=``>=` 的方向一致,否则不会转换。
142+
**注意**:当 Media features name 在中间的时候,一定要保证两个 `<=``>=` 的方向一致,否则不会转换。
108143

109144
例如在下面的示例中,width 大于等于 500px 同时又大于等于 1200px,这在语法和逻辑上都是错误的。
110145

@@ -116,7 +151,7 @@ input.css:
116151
}
117152
```
118153

119-
### 支持的 feature name
154+
### 支持的 Media features name
120155

121156
规范中目前以下属性支持 min-/max 前缀,PostCSS Media Minmax 全部支持自动转换。
122157

@@ -139,7 +174,7 @@ input.css:
139174
var fs = require('fs')
140175
var chokidar = require('chokidar')
141176
var postcss = require('postcss')
142-
var minmax = require('minmax')
177+
var minmax = require('postcss-media-minmax')
143178
var customMedia = require('postcss-custom-media')
144179

145180
var src = 'input.css'

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# PostCSS Media Minmax
1+
# PostCSS Media Minmax [![Build Status](https://travis-ci.org/postcss/postcss-media-minmax.svg)](https://travis-ci.org/postcss/postcss-media-minmax)
2+
23

34
> Writing simple and graceful Media Queries!
45

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ function mediaMinmax() {
3030
* (width <= 900px) => (max-width: 900px)
3131
*/
3232

33-
rule.params = rule.params.replace(/\(\s*([a-z-]+?)\s*([<>]=)\s*((?:-?\d*\.?\d+[a-z]*)?)\s*\)/gi, function($0, $1, $2, $3) {
33+
//取值不支持负值
34+
//But -0 is always equivalent to 0 in CSS, and so is also accepted as a valid <mq-boolean> value.
35+
36+
rule.params = rule.params.replace(/\(\s*([a-z-]+?)\s*([<>]=)\s*((?:-?\d*\.?(?:\s*\/?\s*)?\d+[a-z]*)?)\s*\)/gi, function($0, $1, $2, $3) {
3437

3538
var params = '';
3639

@@ -54,7 +57,7 @@ function mediaMinmax() {
5457
* (900px >= width >= 300px) => (min-width: 300px) and (max-width: 900px)
5558
*/
5659

57-
rule.params = rule.params.replace(/\(\s*((?:-?\d*\.?\d+[a-z]*)?)\s*(<=|>=)\s*([a-z-]+)\s*(<=|>=)\s*((?:-?\d*\.?\d+[a-z]*)?)\s*\)/gi, function($0, $1, $2, $3, $4, $5) {
60+
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) {
5861

5962
if (feature_name.indexOf($3) > -1) {
6063
if ($2 === '<=' && $4 === '<=' || $2 === '>=' && $4 === '>=') {
@@ -67,6 +70,7 @@ function mediaMinmax() {
6770
return $0;
6871

6972
});
73+
7074
});
7175

7276
}

test/fixtures/aspect-ratio.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@media screen and (aspect-ratio >= 1/1000) and (aspect-ratio <= 16/9) {
2+
3+
}
4+
5+
@media screen and (1 / 1000 <= aspect-ratio <= 16 / 9) {
6+
7+
}
8+
9+
@media screen and (0/0 <= aspect-ratio <= 16/9) {
10+
}
11+
12+
@media screen and (aspect-ratio) and (1 / 1000 <= aspect-ratio <= 16 / 9) {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@media screen and (min-aspect-ratio: 1/1000) and (max-aspect-ratio: 16/9) {
2+
3+
}
4+
5+
@media screen and (min-aspect-ratio: 1 / 1000) and (max-aspect-ratio: 16 / 9) {
6+
7+
}
8+
9+
@media screen and (min-aspect-ratio: 0/0) and (max-aspect-ratio: 16/9) {
10+
}
11+
12+
@media screen and (aspect-ratio) and (min-aspect-ratio: 1 / 1000) and (max-aspect-ratio: 16 / 9) {
13+
14+
}

test/fixtures/color-index.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@media screen and (color-index >= 0) and (color-index <= 8) {
2+
3+
}
4+
5+
@media screen and (0 <= color-index <= 8) {
6+
7+
}
8+
9+
@media screen and (color-index) and (6 <= color-index <= 256) {
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@media screen and (min-color-index: 0) and (max-color-index: 8) {
2+
3+
}
4+
5+
@media screen and (min-color-index: 0) and (max-color-index: 8) {
6+
7+
}
8+
9+
@media screen and (color-index) and (min-color-index: 6) and (max-color-index: 256) {
10+
11+
}

test/fixtures/color.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@media screen and (color >= 0) and (color <= 8) {
2+
3+
}
4+
5+
@media screen and (0 <= color <= 8) {
6+
7+
}
8+
9+
@media screen and (color) and (6 <= color <= 256) {
10+
11+
}

test/fixtures/color.output.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@media screen and (min-color: 0) and (max-color: 8) {
2+
3+
}
4+
5+
@media screen and (min-color: 0) and (max-color: 8) {
6+
7+
}
8+
9+
@media screen and (color) and (min-color: 6) and (max-color: 256) {
10+
11+
}

test/fixtures/comment.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@media screen and /* comment */ (/* comment */width >= 500px) /* comment */ and (width/* comment */ <= 1200px) /* comment */{
2+
.bar {
3+
display: block;
4+
}
5+
}
6+
7+
@media screen and /* comment */(500px/* comment */ <= /* comment */width <= 1200px/* comment */) /* comment */{
8+
.bar {
9+
display: block;
10+
}
11+
}

0 commit comments

Comments
 (0)