Skip to content

Commit 3b8ea02

Browse files
committed
Merge pull request #15 from hudochenkov/fix-incorrect-output-with-shorthand
Fix incorrect output when using both > and >= (or similar) #12
2 parents 4e4a4e1 + 7e116f6 commit 3b8ea02

4 files changed

Lines changed: 42 additions & 5 deletions

File tree

index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,34 @@ module.exports = postcss.plugin('postcss-media-minmax', function () {
8080
})
8181

8282
/**
83-
* 转换 <mf-value> <|>= <mf-name> <|>= <mf-value>
84-
* $1 $2 $3 $4$5 $5
83+
* 转换 <mf-value> <|<= <mf-name> <|<= <mf-value>
84+
* 转换 <mf-value> >|>= <mf-name> >|>= <mf-value>
85+
* $1 $2$3 $4 $5$6 $7
8586
* (500px <= width <= 1200px) => (min-width: 500px) and (max-width: 1200px)
87+
* (500px < width <= 1200px) => (min-width: 501px) and (max-width: 1200px)
8688
* (900px >= width >= 300px) => (min-width: 300px) and (max-width: 900px)
8789
*/
8890

8991
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) {
9092

9193
if (feature_name.indexOf($4) > -1) {
9294
if ($2 === '<' && $5 === '<' || $2 === '>' && $5 === '>') {
93-
var min = ($2 === '<') ? $1 : $7;
94-
var max = ($2 === '<') ? $7 : $1;
95+
var min = ($2 === '<') ? $1 : $7;
96+
var max = ($2 === '<') ? $7 : $1;
97+
98+
// output differently depended on expression direction
99+
// <mf-value> <|<= <mf-name> <|<= <mf-value>
100+
// or
101+
// <mf-value> >|>= <mf-name> >|>= <mf-value>
102+
var equals_for_min = $3;
103+
var equals_for_max = $6;
104+
105+
if ($2 === '>') {
106+
equals_for_min = $6;
107+
equals_for_max = $3;
108+
}
95109

96-
return create_query($4, '>', $3, min) + ' and ' + create_query($4, '<', $6, max);
110+
return create_query($4, '>', equals_for_min, min) + ' and ' + create_query($4, '<', equals_for_max, max);
97111
}
98112
}
99113
//如果不是指定的属性,不做替换

test/fixtures/shorthands.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@media (1024px > width >= 768px) {}
2+
@media (768px <= width < 1024px) {}
3+
4+
@media (1024px >= width > 768px) {}
5+
@media (768px < width <= 1024px) {}
6+
7+
@media (1024px >= width >= 768px) {}
8+
@media (768px <= width <= 1024px) {}
9+
10+
@media (1024px > width > 768px) {}
11+
@media (768px < width < 1024px) {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@media (min-width: 768px) and (max-width: 1023px) {}
2+
@media (min-width: 768px) and (max-width: 1023px) {}
3+
4+
@media (min-width: 769px) and (max-width: 1024px) {}
5+
@media (min-width: 769px) and (max-width: 1024px) {}
6+
7+
@media (min-width: 768px) and (max-width: 1024px) {}
8+
@media (min-width: 768px) and (max-width: 1024px) {}
9+
10+
@media (min-width: 769px) and (max-width: 1023px) {}
11+
@media (min-width: 769px) and (max-width: 1023px) {}

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ test("@media", function(t) {
3434
compareFixtures(t, "more-units", "should transform")
3535

3636
compareFixtures(t, "min-max", "should transform")
37+
compareFixtures(t, "shorthands", "should transform shorthands")
3738

3839
t.end()
3940
})

0 commit comments

Comments
 (0)