Skip to content

Commit

Permalink
Fix incorrect output when using both > and >= (or similar) #12
Browse files Browse the repository at this point in the history
  • Loading branch information
hudochenkov committed Feb 27, 2016
1 parent 4e4a4e1 commit 7e116f6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
24 changes: 19 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,34 @@ module.exports = postcss.plugin('postcss-media-minmax', function () {
})

/**
* 转换 <mf-value> <|>= <mf-name> <|>= <mf-value>
* $1 $2 $3 $4$5 $5
* 转换 <mf-value> <|<= <mf-name> <|<= <mf-value>
* 转换 <mf-value> >|>= <mf-name> >|>= <mf-value>
* $1 $2$3 $4 $5$6 $7
* (500px <= width <= 1200px) => (min-width: 500px) and (max-width: 1200px)
* (500px < width <= 1200px) => (min-width: 501px) and (max-width: 1200px)
* (900px >= width >= 300px) => (min-width: 300px) and (max-width: 900px)
*/

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

if (feature_name.indexOf($4) > -1) {
if ($2 === '<' && $5 === '<' || $2 === '>' && $5 === '>') {
var min = ($2 === '<') ? $1 : $7;
var max = ($2 === '<') ? $7 : $1;
var min = ($2 === '<') ? $1 : $7;
var max = ($2 === '<') ? $7 : $1;

// output differently depended on expression direction
// <mf-value> <|<= <mf-name> <|<= <mf-value>
// or
// <mf-value> >|>= <mf-name> >|>= <mf-value>
var equals_for_min = $3;
var equals_for_max = $6;

if ($2 === '>') {
equals_for_min = $6;
equals_for_max = $3;
}

return create_query($4, '>', $3, min) + ' and ' + create_query($4, '<', $6, max);
return create_query($4, '>', equals_for_min, min) + ' and ' + create_query($4, '<', equals_for_max, max);
}
}
//如果不是指定的属性,不做替换
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/shorthands.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@media (1024px > width >= 768px) {}
@media (768px <= width < 1024px) {}

@media (1024px >= width > 768px) {}
@media (768px < width <= 1024px) {}

@media (1024px >= width >= 768px) {}
@media (768px <= width <= 1024px) {}

@media (1024px > width > 768px) {}
@media (768px < width < 1024px) {}
11 changes: 11 additions & 0 deletions test/fixtures/shorthands.output.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@media (min-width: 768px) and (max-width: 1023px) {}
@media (min-width: 768px) and (max-width: 1023px) {}

@media (min-width: 769px) and (max-width: 1024px) {}
@media (min-width: 769px) and (max-width: 1024px) {}

@media (min-width: 768px) and (max-width: 1024px) {}
@media (min-width: 768px) and (max-width: 1024px) {}

@media (min-width: 769px) and (max-width: 1023px) {}
@media (min-width: 769px) and (max-width: 1023px) {}
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ test("@media", function(t) {
compareFixtures(t, "more-units", "should transform")

compareFixtures(t, "min-max", "should transform")
compareFixtures(t, "shorthands", "should transform shorthands")

t.end()
})

0 comments on commit 7e116f6

Please sign in to comment.