Skip to content

Commit 01239f1

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 97c9af0 commit 01239f1

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,8 +1,8 @@
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

33
}
44

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

77
}
88

@@ -18,19 +18,19 @@
1818

1919
}
2020

21-
@media screen and (min-width: 1px) and (max-width: 500.579px) {
21+
@media screen and (min-width: 0.02px) and (max-width: 500.579px) {
2222
}
2323

2424
@media screen and (width) and (min-width: 0.081px) and (max-width: 0.679px) {
2525

2626
}
2727

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

3131
}
3232

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

3535
}
3636

@@ -46,7 +46,7 @@
4646

4747
}
4848

49-
@media screen and (min-height: 1px) and (max-height: 500.579px) {
49+
@media screen and (min-height: 0.02px) and (max-height: 500.579px) {
5050
}
5151

5252
@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)