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

Commit

Permalink
Failing silently to 16px font size when values can't be calculated (e…
Browse files Browse the repository at this point in the history
…g viewport relative units)

(fix #45)
  • Loading branch information
iamvdo committed Sep 17, 2015
1 parent d95ace2 commit 9ccf84f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/pixrem.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict';
var calc = require('reduce-css-calc');
var postcss = require('postcss');
var browserslist = require('browserslist');

var _remgex, _PROPS, _VALUES, _rootvalue, _options;
var calc = require('reduce-css-calc');
var BASE_FONT_SIZE = 16;


// Add pixel fallbacks for rem units to a string of CSS
// - css `String`: the contents of a CSS file.
Expand All @@ -16,7 +19,7 @@ function Pixrem (rootvalue, options) {
_remgex = /(\d*\.?\d+)rem/ig;
_PROPS = /^(background-size|border-image|border-radius|box-shadow|clip-path|column|grid|mask|object|perspective|scroll|shape|size|stroke|transform)/;
_VALUES = /(calc|gradient)\(/;
_rootvalue = typeof rootvalue !== 'undefined' ? rootvalue : 16;
_rootvalue = typeof rootvalue !== 'undefined' ? rootvalue : BASE_FONT_SIZE;
options = options || {};
_options = {};
_options.replace = (options.replace !== undefined) ? options.replace : false;
Expand Down Expand Up @@ -150,10 +153,13 @@ function toPx (value) {
return parseFloat(number);
}
else if (unit === 'em' || unit === 'rem') {
return parseFloat(number) * 16;
return parseFloat(number) * BASE_FONT_SIZE;
}
else if (unit === '%') {
return (parseFloat(number) / 100) * 16;
return (parseFloat(number) / 100) * BASE_FONT_SIZE;
} else {
// other units: vw, ex, ch, etc...
return BASE_FONT_SIZE;
}
} else {
throw new Error('Root font-size is invalid');
Expand Down
7 changes: 7 additions & 0 deletions spec/pixrem-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ describe('pixrem', function () {
expect(processed).toBe(expected);
});

it('should generate fallbacks with a vw root em value', function () {
var expected = '.rule { font-size: 32px; font-size: 2rem }';
var processed = pixrem.process(css, '.625vw');

expect(processed).toBe(expected);
});

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

0 comments on commit 9ccf84f

Please sign in to comment.