Skip to content

Commit 92c4dd3

Browse files
committed
Change integer pixels by .02px rather than 1px
According to the Bootstrap developers, using .02px rather than .01px should be sufficient to work around the Safari rounding bug. Fixes #19. Signed-off-by: Anders Kaseorg <[email protected]>
1 parent b585b23 commit 92c4dd3

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

index.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ const feature_unit = {
1414
// Supported min-/max- attributes
1515
const feature_name = Object.keys(feature_unit);
1616

17-
const step = .001; // smallest even number that won’t break complex queries (1in = 96px)
18-
1917
const power = {
2018
'>': 1,
2119
'<': -1
@@ -30,18 +28,22 @@ function create_query(name, gtlt, eq, value) {
3028
return value.replace(/([-\d\.]+)(.*)/, function (_match, number, unit) {
3129
const initialNumber = parseFloat(number);
3230

33-
if (parseFloat(number) || eq) {
34-
// if eq is true, then number remains same
35-
if (!eq) {
36-
// change integer pixels value only on integer pixel
37-
if (unit === 'px' && initialNumber === parseInt(number, 10)) {
38-
number = initialNumber + power[gtlt];
39-
} else {
40-
number = Number(Math.round(parseFloat(number) + step * power[gtlt] + 'e6')+'e-6');
41-
}
31+
// if eq is true, then number remains same
32+
if (!eq) {
33+
if (!initialNumber) {
34+
unit = feature_unit[name];
35+
}
36+
let step;
37+
if (unit === 'px' && initialNumber === Math.round(initialNumber)) {
38+
// change integer pixels by .02px to work around a Safari rounding bug:
39+
// https://bugs.webkit.org/show_bug.cgi?id=178261
40+
// https://github.com/twbs/bootstrap/pull/25177
41+
step = .02;
42+
} else {
43+
// smallest even number that won’t break complex queries (1in = 96px)
44+
step = .001;
4245
}
43-
} else {
44-
number = power[gtlt] + feature_unit[name];
46+
number = Number((initialNumber + step * power[gtlt]).toFixed(6));
4547
}
4648

4749
return '(' + minmax[gtlt] + '-' + name + ': ' + number + unit + ')';

test/fixtures/min-max.output.css

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
@media screen and (min-width: 501px) and (max-width: 1199px) {}
1+
@media screen and (min-width: 500.02px) and (max-width: 1199.98px) {}
22

3-
@media screen and (min-width: 501px) and (max-width: 1199px) {}
3+
@media screen and (min-width: 500.02px) and (max-width: 1199.98px) {}
44

55
@media screen and (min-width: 40.001em) and (max-width: 59.999em) {}
66

77
@media screen and (min-width: 13.801rem) and (max-width: 51.2rem) {}
88

99
@media screen and (min-width: 6.001in) and (max-width: 8.999in) {}
1010

11-
@media screen and (min-width: 1px) and (max-width: 500.579px) {}
11+
@media screen and (min-width: 0.02px) and (max-width: 500.579px) {}
1212

1313
@media screen and (width) and (min-width: 0.081px) and (max-width: 0.679px) {}
1414

1515
/* height */
16-
@media screen and (min-height: 501px) and (max-height: 1199px) {}
16+
@media screen and (min-height: 500.02px) and (max-height: 1199.98px) {}
1717

18-
@media screen and (min-height: 501px) and (max-height: 1199px) {}
18+
@media screen and (min-height: 500.02px) and (max-height: 1199.98px) {}
1919

2020
@media screen and (min-height: 40.001em) and (max-height: 59.999em) {}
2121

2222
@media screen and (min-height: 13.8rem) and (max-height: 51.199rem) {}
2323

2424
@media screen and (min-height: 6.001in) and (max-height: 8.999in) {}
2525

26-
@media screen and (min-height: 1px) and (max-height: 500.579px) {}
26+
@media screen and (min-height: 0.02px) and (max-height: 500.579px) {}
2727

2828
@media screen and (height) and (min-height: 0.081px) and (max-height: 0.679px) {}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
@media (min-width: 768px) and (max-width: 1023px) {}
2-
@media (min-width: 768px) and (max-width: 1023px) {}
1+
@media (min-width: 768px) and (max-width: 1023.98px) {}
2+
@media (min-width: 768px) and (max-width: 1023.98px) {}
33

4-
@media (min-width: 769px) and (max-width: 1024px) {}
5-
@media (min-width: 769px) and (max-width: 1024px) {}
4+
@media (min-width: 768.02px) and (max-width: 1024px) {}
5+
@media (min-width: 768.02px) and (max-width: 1024px) {}
66

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

10-
@media (min-width: 769px) and (max-width: 1023px) {}
11-
@media (min-width: 769px) and (max-width: 1023px) {}
10+
@media (min-width: 768.02px) and (max-width: 1023.98px) {}
11+
@media (min-width: 768.02px) and (max-width: 1023.98px) {}

0 commit comments

Comments
 (0)