Skip to content

Commit

Permalink
Add selector black list
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedawwwg committed Jun 18, 2015
1 parent 17d861f commit dff48e5
Show file tree
Hide file tree
Showing 4 changed files with 27 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
13 changes: 13 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,18 @@ module.exports = postcss.plugin('postcss-pxtorem', function (options) {
css.eachDecl(function (decl, i) {
if (propWhiteList.indexOf(decl.prop) === -1) return;

var selector = decl.parent.selector;
var match = false;
for (var j=0; j<selectorBlackList.length; j++) {
match = selector.match(selectorBlackList[j]);
if (match) {
break;
}
}
if (match) {
return;
}

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

Expand Down
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 @@ -12,6 +12,17 @@ var css = '.rule { font-size: 15px }';

describe('pxtorem', function () {

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 replace the px unit with rem', function () {
var processed = postcss(pxtorem()).process(css).css;
var expected = '.rule { font-size: 0.9375rem }';
Expand Down

0 comments on commit dff48e5

Please sign in to comment.