Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
Add warning when unit cannot be used for fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvdo committed Sep 21, 2015
1 parent 9d42ccb commit a4cdea5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/pixrem.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module.exports = postcss.plugin('pixrem', function (opts) {
value = value.replace(REGEX, function ($1) {
// Round decimal pixels down to match webkit and opera behavior:
// http://tylertate.com/blog/2012/01/05/subpixel-rounding.html
return Math.floor(parseFloat($1) * toPx(options.rootValue)) + 'px';
return Math.floor(parseFloat($1) * toPx(options.rootValue, result)) + 'px';
});

if (options.replace) {
Expand Down Expand Up @@ -122,7 +122,7 @@ function detectBrowser (browsers, browserQuery) {
}

// Return a unitless pixel value from any root font-size value.
function toPx (value) {
function toPx (value, result) {
value = (typeof value === 'string' && value.indexOf('calc(') !== -1) ? calc(value) : value;
var parts = /^(\d*\.?\d+)([a-zA-Z%]*)$/.exec(value);
if (parts !== null) {
Expand All @@ -139,6 +139,7 @@ function toPx (value) {
return (parseFloat(number) / 100) * BASE_FONT_SIZE;
} else {
// other units: vw, ex, ch, etc...
result.warn('Unit cannot be used for conversion, so 16px is used.');
return BASE_FONT_SIZE;
}
} else {
Expand Down
9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ describe('pixrem', function () {
assert.equal(processed, expected);
});

it('should warn when using browser-dependent unit', function (done) {
var expected = '.rule { font-size: 32px; font-size: 2rem }';
var processed = postcss([pixrem({rootValue: '1vw'})]).process(css).then(function (result) {
var warnings = result.warnings();
assert.deepEqual(warnings, [{type: 'warning', text: 'Unit cannot be used for conversion, so 16px is used.', plugin: 'pixrem'}]);
done();
}).catch(done);
});

it('should generate fallbacks with a value starting with dot', function () {
var expected = '.rule { font-size: 16px; font-size: 2rem }';
var processed = postcss([pixrem({rootValue: '.5em'})]).process(css).css;
Expand Down

0 comments on commit a4cdea5

Please sign in to comment.