Skip to content

Commit 9f6c52e

Browse files
committed
Merge pull request #14 from hudochenkov/pixels-rounding
Fix pixels rounding errors in fractional pixels media queries
2 parents f532c0a + 2c2eff4 commit 9f6c52e

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,21 @@ module.exports = postcss.plugin('postcss-media-minmax', function () {
3232

3333
function create_query(name, gtlt, eq, value, params) {
3434
return value.replace(/([-\d\.]+)(.*)/, function (match, number, unit) {
35-
number = parseFloat(number) || eq ? eq ? number : Number(Math.round(parseFloat(number) + step * power[gtlt] + 'e6')+'e-6') : power[gtlt] + feature_unit[name];
35+
var initialNumber = parseFloat(number);
36+
37+
if (parseFloat(number) || eq) {
38+
// if eq is true, then number remains same
39+
if (!eq) {
40+
// change integer pixels value only on integer pixel
41+
if (unit === 'px' && initialNumber === parseInt(number, 10)) {
42+
number = initialNumber + power[gtlt];
43+
} else {
44+
number = Number(Math.round(parseFloat(number) + step * power[gtlt] + 'e6')+'e-6');
45+
}
46+
}
47+
} else {
48+
number = power[gtlt] + feature_unit[name];
49+
}
3650

3751
return '(' + minmax[gtlt] + '-' + name + ': ' + number + unit + ')';
3852
});

test/fixtures/min-max.output.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
@media screen and (min-width: 500.001px) and (max-width: 1199.999px) {
1+
@media screen and (min-width: 501px) and (max-width: 1199px) {
22

33
}
44

5-
@media screen and (min-width: 500.001px) and (max-width: 1199.999px) {
5+
@media screen and (min-width: 501px) and (max-width: 1199px) {
66

77
}
88

@@ -26,11 +26,11 @@
2626
}
2727

2828
/* height */
29-
@media screen and (min-height: 500.001px) and (max-height: 1199.999px) {
29+
@media screen and (min-height: 501px) and (max-height: 1199px) {
3030

3131
}
3232

33-
@media screen and (min-height: 500.001px) and (max-height: 1199.999px) {
33+
@media screen and (min-height: 501px) and (max-height: 1199px) {
3434

3535
}
3636

0 commit comments

Comments
 (0)