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

Commit

Permalink
Throw error on node (fix #46)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvdo committed Sep 23, 2015
1 parent 826d9d4 commit dd9a27e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 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, result)) + 'px';
return Math.floor(parseFloat($1) * toPx(options.rootValue, decl, 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, result) {
function toPx (value, decl, 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 @@ -143,7 +143,7 @@ function toPx (value, result) {
return BASE_FONT_SIZE;
}
} else {
throw new Error('Root font-size is invalid');
throw decl.error('Root font-size is invalid', {plugin: 'pixrem'});
}
}

Expand Down
15 changes: 8 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,15 @@ describe('pixrem', function () {
assert.equal(processed, expected);
});

it('should throw error when root font-size is invalid', function () {
it('should throw error when root font-size is invalid', function (done) {
var css = 'html { font-size: calc(1em + 2px) } .rule { font-size: 2rem; }';
var processed = function () {
return postcss([pixrem]).process(css).css;
};
assert.throws(processed, function (err) {
return err.message === 'Root font-size is invalid';
});
postcss([pixrem]).process(css).then(function () {
done('should not run');
}).catch(function (err) {
assert.equal(err.name, 'CssSyntaxError');
assert.equal(err.reason, 'Root font-size is invalid');
done();
}).catch(done);
});

it('should reduce line-breaks when inserting new node', function () {
Expand Down

0 comments on commit dd9a27e

Please sign in to comment.