Skip to content

Commit

Permalink
Change integer pixels by .02px rather than 1px
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
andersk committed Jul 15, 2022
1 parent b585b23 commit 92c4dd3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
28 changes: 15 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ const feature_unit = {
// Supported min-/max- attributes
const feature_name = Object.keys(feature_unit);

const step = .001; // smallest even number that won’t break complex queries (1in = 96px)

const power = {
'>': 1,
'<': -1
Expand All @@ -30,18 +28,22 @@ function create_query(name, gtlt, eq, value) {
return value.replace(/([-\d\.]+)(.*)/, function (_match, number, unit) {
const initialNumber = parseFloat(number);

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

return '(' + minmax[gtlt] + '-' + name + ': ' + number + unit + ')';
Expand Down
12 changes: 6 additions & 6 deletions test/fixtures/min-max.output.css
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
@media screen and (min-width: 501px) and (max-width: 1199px) {}
@media screen and (min-width: 500.02px) and (max-width: 1199.98px) {}

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

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

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

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

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

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

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

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

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

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

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

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

@media screen and (height) and (min-height: 0.081px) and (max-height: 0.679px) {}
12 changes: 6 additions & 6 deletions test/fixtures/shorthands.output.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@media (min-width: 768px) and (max-width: 1023px) {}
@media (min-width: 768px) and (max-width: 1023px) {}
@media (min-width: 768px) and (max-width: 1023.98px) {}
@media (min-width: 768px) and (max-width: 1023.98px) {}

@media (min-width: 769px) and (max-width: 1024px) {}
@media (min-width: 769px) and (max-width: 1024px) {}
@media (min-width: 768.02px) and (max-width: 1024px) {}
@media (min-width: 768.02px) 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) {}
@media (min-width: 768.02px) and (max-width: 1023.98px) {}
@media (min-width: 768.02px) and (max-width: 1023.98px) {}

0 comments on commit 92c4dd3

Please sign in to comment.