Skip to content

Commit 14c1213

Browse files
committed
Merge pull request #10 from jonathantneal/feature/ltgt
Support for < and > without =
2 parents 8717789 + 0f1495b commit 14c1213

7 files changed

Lines changed: 146 additions & 31 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.1.0 2015-09-05
2+
3+
Add: Support for `<` and `>` without `=`.
4+
15
# 2.0.0 2015-09-05
26

37
Change: Use PostCSS 5.0 API.

index.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,41 @@ var postcss = require('postcss');
22

33
module.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
//如果不是指定的属性,不做替换

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-media-minmax",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Using more intuitive `>=` or `<=` instead of media queries min/max prefix.",
55
"scripts": {
66
"test": "tape test"

test/fixtures/min-max.css

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@media screen and (width > 500px) and (width < 1200px) {
2+
3+
}
4+
5+
@media screen and (500px < width < 1200px) {
6+
7+
}
8+
9+
@media screen and (40em < width < 60em) {
10+
11+
}
12+
13+
@media screen and (6in < width < 9in) {
14+
15+
}
16+
17+
@media screen and (0 < width < 500.58px) {
18+
}
19+
20+
@media screen and (width) and (.08px < width < 0.68px) {
21+
22+
}
23+
24+
/* height */
25+
@media screen and (height > 500px) and (height < 1200px) {
26+
27+
}
28+
29+
@media screen and (500px < height < 1200px) {
30+
31+
}
32+
33+
@media screen and (40em < height < 60em) {
34+
35+
}
36+
37+
@media screen and (6in < height < 9in) {
38+
39+
}
40+
41+
@media screen and (0 < height < 500.58px) {
42+
}
43+
44+
@media screen and (height) and (.08px < height < 0.68px) {
45+
46+
}

test/fixtures/min-max.output.css

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@media screen and (min-width: 500.001px) and (max-width: 1199.999px) {
2+
3+
}
4+
5+
@media screen and (min-width: 500.001px) and (max-width: 1199.999px) {
6+
7+
}
8+
9+
@media screen and (min-width: 40.001em) and (max-width: 59.999em) {
10+
11+
}
12+
13+
@media screen and (min-width: 6.001in) and (max-width: 8.999in) {
14+
15+
}
16+
17+
@media screen and (min-width: 1px) and (max-width: 500.579px) {
18+
}
19+
20+
@media screen and (width) and (min-width: 0.081px) and (max-width: 0.679px) {
21+
22+
}
23+
24+
/* height */
25+
@media screen and (min-height: 500.001px) and (max-height: 1199.999px) {
26+
27+
}
28+
29+
@media screen and (min-height: 500.001px) and (max-height: 1199.999px) {
30+
31+
}
32+
33+
@media screen and (min-height: 40.001em) and (max-height: 59.999em) {
34+
35+
}
36+
37+
@media screen and (min-height: 6.001in) and (max-height: 8.999in) {
38+
39+
}
40+
41+
@media screen and (min-height: 1px) and (max-height: 500.579px) {
42+
}
43+
44+
@media screen and (height) and (min-height: 0.081px) and (max-height: 0.679px) {
45+
46+
}

test/fixtures/monochrome.output.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
@media screen and (monochrome) and (min-monochrome: 1) and (max-monochrome: 300) {
1010

11-
}
11+
}

test/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@ test("@media", function(t) {
3333
compareFixtures(t, "other-name", "should transform")
3434
compareFixtures(t, "more-units", "should transform")
3535

36+
compareFixtures(t, "min-max", "should transform")
37+
3638
t.end()
3739
})

0 commit comments

Comments
 (0)