Skip to content

Commit

Permalink
Merge branch 'stagetwo-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
cuth committed Jun 19, 2015
2 parents 17d861f + f7b0da3 commit dd7de31
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Default:
root_value: 16,
unit_precision: 5,
prop_white_list: ['font', 'font-size', 'line-height', 'letter-spacing'],
selector_black_list: [],
replace: true,
media_query: false
}
Expand All @@ -45,6 +46,7 @@ Default:
- `root_value` (Number) The root element font size.
- `unit_precision` (Number) The decimal numbers to allow the REM units to grow to.
- `prop_white_list` (Array) The properties that can change from px to rem.
- `selector_black_list` (Array) The selectors to ignore and leave as px.
- `replace` (Boolean) replaces rules containing rems instead of adding fallbacks.
- `media_query` (Boolean) Allow px to be converted in media queries.

Expand Down
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = postcss.plugin('postcss-pxtorem', function (options) {
options = options || {};
var rootValue = options.root_value || 16;
var unitPrecision = options.unit_precision || 5;
var selectorBlackList = options.selector_black_list || [];
var propWhiteList = options.prop_white_list || ['font', 'font-size', 'line-height', 'letter-spacing'];
var replace = (options.replace === false) ? false : true;
var mediaQuery = options.media_query || false;
Expand All @@ -22,6 +23,8 @@ module.exports = postcss.plugin('postcss-pxtorem', function (options) {
css.eachDecl(function (decl, i) {
if (propWhiteList.indexOf(decl.prop) === -1) return;

if (blacklistedSelector(selectorBlackList, decl.parent.selector)) return;

var rule = decl.parent;
var value = decl.value;

Expand Down Expand Up @@ -63,3 +66,9 @@ function remExists(decls, prop, value) {
return (decl.prop === prop && decl.value === value);
});
}

function blacklistedSelector(blacklist, selector) {
return blacklist.some(function (regex) {
return selector.match(regex);
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "postcss-pxtorem",
"description": "A CSS post-processor that converts px to rem.",
"version": "2.1.0",
"version": "2.2.0",
"author": "cuth",
"license": "MIT",
"repository": {
Expand Down
11 changes: 11 additions & 0 deletions spec/pxtorem-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ describe('pxtorem', function () {
expect(processed).toBe(expected);
});

it('should ignore selectors in the selector black list', function () {
var rules = '.rule { font-size: 15px } .rule2 { font-size: 15px }';
var expected = '.rule { font-size: 0.9375rem } .rule2 { font-size: 15px }';
var options = {
selector_black_list: ['.rule2']
};
var processed = postcss(pxtorem(options)).process(rules).css;

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

it('should leave fallback pixel unit with root em value', function () {
var options = {
replace: false
Expand Down

0 comments on commit dd7de31

Please sign in to comment.